|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | #include "google/cloud/log.h" |
| 16 | +#include "google/cloud/internal/setenv.h" |
| 17 | +#include "google/cloud/testing_util/environment_variable_restore.h" |
16 | 18 | #include <gmock/gmock.h> |
17 | 19 |
|
18 | 20 | namespace google { |
19 | 21 | namespace cloud { |
20 | 22 | inline namespace GOOGLE_CLOUD_CPP_NS { |
21 | 23 | namespace { |
22 | 24 |
|
23 | | -using namespace ::testing; |
| 25 | +using ::testing::_; |
| 26 | +using ::testing::ExitedWithCode; |
| 27 | +using ::testing::HasSubstr; |
| 28 | +using ::testing::Invoke; |
24 | 29 |
|
25 | 30 | TEST(LogSeverityTest, Streaming) { |
26 | 31 | std::ostringstream os; |
@@ -140,6 +145,29 @@ TEST(LogSinkTest, ClogMultiple) { |
140 | 145 | EXPECT_EQ(0, LogSink::Instance().BackendCount()); |
141 | 146 | } |
142 | 147 |
|
| 148 | +TEST(LogSinkTest, ClogEnvironment) { |
| 149 | + // We set the death test style to "threadsafe", which causes the |
| 150 | + // ASSERT_EXIT() call to re-exec the test binary before executing the given |
| 151 | + // statement. This makes the death test thread-safe and it also ensures that |
| 152 | + // the LogSink singleton instance will be reconstructed in the child and will |
| 153 | + // see the environment variable. See also: |
| 154 | + // https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#death-test-styles |
| 155 | + auto old_style = testing::FLAGS_gtest_death_test_style; |
| 156 | + testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 157 | + |
| 158 | + testing_util::EnvironmentVariableRestore restore( |
| 159 | + "GOOGLE_CLOUD_CPP_ENABLE_CLOG"); |
| 160 | + internal::SetEnv("GOOGLE_CLOUD_CPP_ENABLE_CLOG", "anyvalue"); |
| 161 | + |
| 162 | + auto f = [] { |
| 163 | + GCP_LOG(INFO) << "testing clog"; |
| 164 | + std::exit(42); |
| 165 | + }; |
| 166 | + ASSERT_EXIT(f(), ExitedWithCode(42), HasSubstr("testing clog")); |
| 167 | + |
| 168 | + testing::FLAGS_gtest_death_test_style = old_style; |
| 169 | +} |
| 170 | + |
143 | 171 | namespace { |
144 | 172 | /// A class to count calls to IOStream operator. |
145 | 173 | struct IOStreamCounter { |
|
0 commit comments