Skip to content

Commit

Permalink
Remove the side effects between tests in vlog_is_on_test.cc
Browse files Browse the repository at this point in the history
The original version of the tests did not reset the VLOG levels between tests, which could lead to false positives. This CL fixes the issue by resetting the VLOG levels to their default values before each test.

PiperOrigin-RevId: 675711787
Change-Id: Id311ba0c00a9e929afcddc7346882487fd64ea16
  • Loading branch information
Alex Titov authored and copybara-github committed Sep 17, 2024
1 parent abc9b91 commit 3aa6061
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions absl/log/vlog_is_on_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,23 @@ absl::optional<int> MinLogLevel() {
#endif
}

TEST(VLogIsOn, GlobalWorksWithoutMaxVerbosityAndMinLogLevel) {
// This fixture is used to reset the VLOG levels to their default values before
// each test.
class VLogIsOnTest : public ::testing::Test {
protected:
void SetUp() override { ResetVLogLevels(); }

private:
// Resets the VLOG levels to their default values.
// It is supposed to be called in the SetUp() method of the test fixture to
// eliminate any side effects from other tests.
static void ResetVLogLevels() {
absl::log_internal::UpdateVModule("");
absl::SetGlobalVLogLevel(0);
}
};

TEST_F(VLogIsOnTest, GlobalWorksWithoutMaxVerbosityAndMinLogLevel) {
if (MaxLogVerbosity().has_value() || MinLogLevel().has_value()) {
GTEST_SKIP();
}
Expand All @@ -59,7 +75,7 @@ TEST(VLogIsOn, GlobalWorksWithoutMaxVerbosityAndMinLogLevel) {
VLOG(4) << "spam";
}

TEST(VLogIsOn, FileWorksWithoutMaxVerbosityAndMinLogLevel) {
TEST_F(VLogIsOnTest, FileWorksWithoutMaxVerbosityAndMinLogLevel) {
if (MaxLogVerbosity().has_value() || MinLogLevel().has_value()) {
GTEST_SKIP();
}
Expand All @@ -74,7 +90,7 @@ TEST(VLogIsOn, FileWorksWithoutMaxVerbosityAndMinLogLevel) {
VLOG(4) << "spam";
}

TEST(VLogIsOn, PatternWorksWithoutMaxVerbosityAndMinLogLevel) {
TEST_F(VLogIsOnTest, PatternWorksWithoutMaxVerbosityAndMinLogLevel) {
if (MaxLogVerbosity().has_value() || MinLogLevel().has_value()) {
GTEST_SKIP();
}
Expand All @@ -89,7 +105,7 @@ TEST(VLogIsOn, PatternWorksWithoutMaxVerbosityAndMinLogLevel) {
VLOG(4) << "spam";
}

TEST(VLogIsOn, GlobalDoesNotFilterBelowMaxVerbosity) {
TEST_F(VLogIsOnTest, GlobalDoesNotFilterBelowMaxVerbosity) {
if (!MaxLogVerbosity().has_value() || *MaxLogVerbosity() < 2) {
GTEST_SKIP();
}
Expand All @@ -104,7 +120,7 @@ TEST(VLogIsOn, GlobalDoesNotFilterBelowMaxVerbosity) {
VLOG(2) << "asdf";
}

TEST(VLogIsOn, FileDoesNotFilterBelowMaxVerbosity) {
TEST_F(VLogIsOnTest, FileDoesNotFilterBelowMaxVerbosity) {
if (!MaxLogVerbosity().has_value() || *MaxLogVerbosity() < 2) {
GTEST_SKIP();
}
Expand All @@ -119,7 +135,7 @@ TEST(VLogIsOn, FileDoesNotFilterBelowMaxVerbosity) {
VLOG(2) << "asdf";
}

TEST(VLogIsOn, PatternDoesNotFilterBelowMaxVerbosity) {
TEST_F(VLogIsOnTest, PatternDoesNotFilterBelowMaxVerbosity) {
if (!MaxLogVerbosity().has_value() || *MaxLogVerbosity() < 2) {
GTEST_SKIP();
}
Expand All @@ -134,7 +150,7 @@ TEST(VLogIsOn, PatternDoesNotFilterBelowMaxVerbosity) {
VLOG(2) << "asdf";
}

TEST(VLogIsOn, GlobalFiltersAboveMaxVerbosity) {
TEST_F(VLogIsOnTest, GlobalFiltersAboveMaxVerbosity) {
if (!MaxLogVerbosity().has_value() || *MaxLogVerbosity() >= 4) {
GTEST_SKIP();
}
Expand All @@ -147,7 +163,7 @@ TEST(VLogIsOn, GlobalFiltersAboveMaxVerbosity) {
VLOG(4) << "dfgh";
}

TEST(VLogIsOn, FileFiltersAboveMaxVerbosity) {
TEST_F(VLogIsOnTest, FileFiltersAboveMaxVerbosity) {
if (!MaxLogVerbosity().has_value() || *MaxLogVerbosity() >= 4) {
GTEST_SKIP();
}
Expand All @@ -160,7 +176,7 @@ TEST(VLogIsOn, FileFiltersAboveMaxVerbosity) {
VLOG(4) << "dfgh";
}

TEST(VLogIsOn, PatternFiltersAboveMaxVerbosity) {
TEST_F(VLogIsOnTest, PatternFiltersAboveMaxVerbosity) {
if (!MaxLogVerbosity().has_value() || *MaxLogVerbosity() >= 4) {
GTEST_SKIP();
}
Expand Down

0 comments on commit 3aa6061

Please sign in to comment.