Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Commit d41d2e3

Browse files
committed
Test for OverrideLogCleanerFilePathPrefix
1 parent 5a2cb2a commit d41d2e3

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,11 @@ if (BUILD_TESTING)
728728
729729
target_link_libraries (cleanup_with_relative_prefix_unittest PRIVATE glog_test)
730730
731+
add_executable (cleanup_with_file_path_override_unittest
732+
src/cleanup_with_file_path_override_unittest.cc)
733+
734+
target_link_libraries (cleanup_with_file_path_override_unittest PRIVATE glog_test)
735+
731736
set (CLEANUP_LOG_DIR ${glog_BINARY_DIR}/cleanup_tests)
732737
733738
add_test (NAME cleanup_init COMMAND
@@ -754,6 +759,13 @@ if (BUILD_TESTING)
754759
-DTEST_SUBDIR=test_subdir/
755760
-P ${glog_SOURCE_DIR}/cmake/RunCleanerTest3.cmake
756761
WORKING_DIRECTORY ${glog_BINARY_DIR})
762+
add_test (NAME cleanup_with_file_path_override COMMAND
763+
${CMAKE_COMMAND}
764+
-DLOGCLEANUP=$<TARGET_FILE:cleanup_with_file_path_override_unittest>
765+
-DTEST_DIR=${glog_BINARY_DIR}/
766+
-DTEST_SUBDIR=test_subdir/
767+
-P ${glog_SOURCE_DIR}/cmake/RunCleanerTestFilePathOverride.cmake
768+
WORKING_DIRECTORY ${glog_BINARY_DIR})
757769
758770
# Fixtures setup
759771
set_tests_properties (cleanup_init PROPERTIES FIXTURES_SETUP logcleanuptest)
@@ -763,6 +775,7 @@ if (BUILD_TESTING)
763775
set_tests_properties (cleanup_immediately PROPERTIES FIXTURES_REQUIRED logcleanuptest)
764776
set_tests_properties (cleanup_with_absolute_prefix PROPERTIES FIXTURES_REQUIRED logcleanuptest)
765777
set_tests_properties (cleanup_with_relative_prefix PROPERTIES FIXTURES_REQUIRED logcleanuptest)
778+
set_tests_properties (cleanup_with_file_path_override PROPERTIES FIXTURES_REQUIRED logcleanuptest)
766779
767780
add_executable (striplog0_unittest
768781
src/striplog_unittest.cc

bazel/glog.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ def glog_library(with_gflags = 1, **kwargs):
231231
"cleanup_immediately",
232232
"cleanup_with_absolute_prefix",
233233
"cleanup_with_relative_prefix",
234+
"cleanup_with_file_path_override",
234235
# "demangle", # Broken
235236
# "logging", # Broken
236237
# "mock-log", # Broken
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
file (TOUCH test_cleanup_SHOULD_BE_CLEANED.logfileoverride)
2+
file (TOUCH test_cleanup_SHOULD_BE_CLEANED.logfileoverride.notalog)
3+
execute_process (COMMAND ${LOGCLEANUP} RESULT_VARIABLE _RESULT)
4+
5+
if (NOT _RESULT EQUAL 0)
6+
message (FATAL_ERROR "Failed to run logcleanup_unittest (error: ${_RESULT})")
7+
endif (NOT _RESULT EQUAL 0)
8+
9+
file (GLOB LOG_FILES ${TEST_DIR}/test_cleanup_*.logfileoverride)
10+
list (LENGTH LOG_FILES NUM_LOG_FILES)
11+
12+
if (WIN32)
13+
# On Windows open files cannot be removed and will result in a permission
14+
# denied error while unlinking such file. Therefore, the last file will be
15+
# retained.
16+
set (_expected 1)
17+
else (WIN32)
18+
set (_expected 0)
19+
endif (WIN32)
20+
21+
if (NOT NUM_LOG_FILES EQUAL _expected)
22+
message (SEND_ERROR "Expected ${_expected} log file in log directory but found ${NUM_LOG_FILES}")
23+
endif (NOT NUM_LOG_FILES EQUAL _expected)
24+
25+
file (GLOB NON_LOG_FILES ${TEST_DIR}/test_cleanup_*.notalog)
26+
list (LENGTH NON_LOG_FILES NUM_NON_LOG_FILES)
27+
28+
if (NOT NUM_NON_LOG_FILES EQUAL 1)
29+
message (SEND_ERROR "Expected 1 non-log file in log directory but found ${NUM_NON_LOG_FILES}")
30+
endif (NOT NUM_NON_LOG_FILES EQUAL 1)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) 2024, Google Inc.
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without
5+
// modification, are permitted provided that the following conditions are
6+
// met:
7+
//
8+
// * Redistributions of source code must retain the above copyright
9+
// notice, this list of conditions and the following disclaimer.
10+
// * Redistributions in binary form must reproduce the above
11+
// copyright notice, this list of conditions and the following disclaimer
12+
// in the documentation and/or other materials provided with the
13+
// distribution.
14+
// * Neither the name of Google Inc. nor the names of its
15+
// contributors may be used to endorse or promote products derived from
16+
// this software without specific prior written permission.
17+
//
18+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
30+
#include "base/commandlineflags.h"
31+
#include "glog/logging.h"
32+
#include "glog/raw_logging.h"
33+
#include "googletest.h"
34+
35+
#ifdef GLOG_USE_GFLAGS
36+
# include <gflags/gflags.h>
37+
using namespace GFLAGS_NAMESPACE;
38+
#endif
39+
40+
#ifdef HAVE_LIB_GMOCK
41+
# include <gmock/gmock.h>
42+
43+
# include "mock-log.h"
44+
// Introduce several symbols from gmock.
45+
using google::glog_testing::ScopedMockLog;
46+
using testing::_;
47+
using testing::AllOf;
48+
using testing::AnyNumber;
49+
using testing::HasSubstr;
50+
using testing::InitGoogleMock;
51+
using testing::StrictMock;
52+
using testing::StrNe;
53+
#endif
54+
55+
using namespace google;
56+
57+
TEST(CleanImmediatelyWithFilePathOverride, logging) {
58+
using namespace std::chrono_literals;
59+
google::EnableLogCleaner(0h);
60+
google::SetLogFilenameExtension(".logfileoverride");
61+
google::SetLogDestination(GLOG_ERROR, "test_cleanup_error_");
62+
google::OverrideLogCleanerFilePathPrefix("test_cleanup_");
63+
64+
LOG(ERROR) << "cleanup test";
65+
66+
google::DisableLogCleaner();
67+
}
68+
69+
int main(int argc, char** argv) {
70+
FLAGS_colorlogtostderr = false;
71+
FLAGS_timestamp_in_logfile_name = true;
72+
#ifdef GLOG_USE_GFLAGS
73+
ParseCommandLineFlags(&argc, &argv, true);
74+
#endif
75+
// Make sure stderr is not buffered as stderr seems to be buffered
76+
// on recent windows.
77+
setbuf(stderr, nullptr);
78+
79+
// Test some basics before InitGoogleLogging:
80+
CaptureTestStderr();
81+
const string early_stderr = GetCapturedTestStderr();
82+
83+
EXPECT_FALSE(IsGoogleLoggingInitialized());
84+
85+
InitGoogleLogging(argv[0]);
86+
87+
EXPECT_TRUE(IsGoogleLoggingInitialized());
88+
89+
InitGoogleTest(&argc, argv);
90+
#ifdef HAVE_LIB_GMOCK
91+
InitGoogleMock(&argc, argv);
92+
#endif
93+
94+
// so that death tests run before we use threads
95+
CHECK_EQ(RUN_ALL_TESTS(), 0);
96+
}

0 commit comments

Comments
 (0)