Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit e8685e9

Browse files
authored
chore: migrate to testing_util::ScopedEnvironment (#184)
1 parent f2b41e0 commit e8685e9

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

google/cloud/connection_options_test.cc

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414

1515
#include "google/cloud/connection_options.h"
1616
#include "google/cloud/internal/compiler_info.h"
17-
#include "google/cloud/internal/setenv.h"
1817
#include "google/cloud/log.h"
19-
#include "google/cloud/testing_util/environment_variable_restore.h"
18+
#include "google/cloud/testing_util/scoped_environment.h"
2019
#include <gmock/gmock.h>
2120
#include <map>
2221

@@ -25,7 +24,6 @@ namespace cloud {
2524
inline namespace GOOGLE_CLOUD_CPP_NS {
2625
namespace {
2726

28-
using ::google::cloud::testing_util::EnvironmentVariableRestore;
2927
using ::testing::ElementsAre;
3028
using ::testing::HasSubstr;
3129
using ::testing::StartsWith;
@@ -77,18 +75,14 @@ TEST(ConnectionOptionsTest, Tracing) {
7775
}
7876

7977
TEST(ConnectionOptionsTest, DefaultTracingUnset) {
80-
EnvironmentVariableRestore restore("GOOGLE_CLOUD_CPP_ENABLE_TRACING");
81-
82-
google::cloud::internal::UnsetEnv("GOOGLE_CLOUD_CPP_ENABLE_TRACING");
78+
testing_util::ScopedEnvironment env("GOOGLE_CLOUD_CPP_ENABLE_TRACING", {});
8379
TestConnectionOptions options(grpc::InsecureChannelCredentials());
8480
EXPECT_FALSE(options.tracing_enabled("rpc"));
8581
}
8682

8783
TEST(ConnectionOptionsTest, DefaultTracingSet) {
88-
EnvironmentVariableRestore restore("GOOGLE_CLOUD_CPP_ENABLE_TRACING");
89-
90-
google::cloud::internal::SetEnv("GOOGLE_CLOUD_CPP_ENABLE_TRACING",
91-
"foo,bar,baz");
84+
testing_util::ScopedEnvironment env("GOOGLE_CLOUD_CPP_ENABLE_TRACING",
85+
"foo,bar,baz");
9286
TestConnectionOptions options(grpc::InsecureChannelCredentials());
9387
EXPECT_FALSE(options.tracing_enabled("rpc"));
9488
EXPECT_TRUE(options.tracing_enabled("foo"));
@@ -97,12 +91,10 @@ TEST(ConnectionOptionsTest, DefaultTracingSet) {
9791
}
9892

9993
TEST(ConnectionOptionsTest, TracingOptions) {
100-
EnvironmentVariableRestore restore("GOOGLE_CLOUD_CPP_TRACING_OPTIONS");
101-
102-
google::cloud::internal::SetEnv("GOOGLE_CLOUD_CPP_TRACING_OPTIONS",
103-
",single_line_mode=off"
104-
",use_short_repeated_primitives=off"
105-
",truncate_string_field_longer_than=32");
94+
testing_util::ScopedEnvironment env("GOOGLE_CLOUD_CPP_TRACING_OPTIONS",
95+
",single_line_mode=off"
96+
",use_short_repeated_primitives=off"
97+
",truncate_string_field_longer_than=32");
10698
TestConnectionOptions options(grpc::InsecureChannelCredentials());
10799
TracingOptions tracing_options = options.tracing_options();
108100
EXPECT_FALSE(tracing_options.single_line_mode());
@@ -196,22 +188,20 @@ TEST(ConnectionOptionsTest, CustomBackgroundThreads) {
196188
}
197189

198190
TEST(ConnectionOptionsTest, DefaultTracingComponentsNoEnvironment) {
199-
EnvironmentVariableRestore restore("GOOGLE_CLOUD_CPP_ENABLE_TRACING");
200-
internal::UnsetEnv("GOOGLE_CLOUD_CPP_ENABLE_TRACING");
191+
testing_util::ScopedEnvironment env("GOOGLE_CLOUD_CPP_ENABLE_TRACING", {});
201192
auto const actual = internal::DefaultTracingComponents();
202193
EXPECT_THAT(actual, ElementsAre());
203194
}
204195

205196
TEST(ConnectionOptionsTest, DefaultTracingComponentsWithValue) {
206-
EnvironmentVariableRestore restore("GOOGLE_CLOUD_CPP_ENABLE_TRACING");
207-
internal::SetEnv("GOOGLE_CLOUD_CPP_ENABLE_TRACING", "a,b,c");
197+
testing_util::ScopedEnvironment env("GOOGLE_CLOUD_CPP_ENABLE_TRACING",
198+
"a,b,c");
208199
auto const actual = internal::DefaultTracingComponents();
209200
EXPECT_THAT(actual, ElementsAre("a", "b", "c"));
210201
}
211202

212203
TEST(ConnectionOptionsTest, DefaultTracingOptionsNoEnvironment) {
213-
EnvironmentVariableRestore restore("GOOGLE_CLOUD_CPP_TRACING_OPTIONS");
214-
internal::UnsetEnv("GOOGLE_CLOUD_CPP_TRACING_OPTIONS");
204+
testing_util::ScopedEnvironment env("GOOGLE_CLOUD_CPP_TRACING_OPTIONS", {});
215205
auto const actual = internal::DefaultTracingOptions();
216206
auto const expected = TracingOptions{};
217207
EXPECT_EQ(expected.single_line_mode(), actual.single_line_mode());
@@ -222,11 +212,10 @@ TEST(ConnectionOptionsTest, DefaultTracingOptionsNoEnvironment) {
222212
}
223213

224214
TEST(ConnectionOptionsTest, DefaultTracingOptionsWithValue) {
225-
EnvironmentVariableRestore restore("GOOGLE_CLOUD_CPP_TRACING_OPTIONS");
226-
internal::SetEnv("GOOGLE_CLOUD_CPP_TRACING_OPTIONS",
227-
"single_line_mode=on"
228-
",use_short_repeated_primitives=ON"
229-
",truncate_string_field_longer_than=42");
215+
testing_util::ScopedEnvironment env("GOOGLE_CLOUD_CPP_TRACING_OPTIONS",
216+
"single_line_mode=on"
217+
",use_short_repeated_primitives=ON"
218+
",truncate_string_field_longer_than=42");
230219
auto const actual = internal::DefaultTracingOptions();
231220
EXPECT_TRUE(actual.single_line_mode());
232221
EXPECT_TRUE(actual.use_short_repeated_primitives());

google/cloud/log_test.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "google/cloud/log.h"
16-
#include "google/cloud/internal/setenv.h"
17-
#include "google/cloud/testing_util/environment_variable_restore.h"
16+
#include "google/cloud/testing_util/scoped_environment.h"
1817
#include <gmock/gmock.h>
1918

2019
namespace google {
@@ -155,9 +154,8 @@ TEST(LogSinkTest, ClogEnvironment) {
155154
auto old_style = testing::FLAGS_gtest_death_test_style;
156155
testing::FLAGS_gtest_death_test_style = "threadsafe";
157156

158-
testing_util::EnvironmentVariableRestore restore(
159-
"GOOGLE_CLOUD_CPP_ENABLE_CLOG");
160-
internal::SetEnv("GOOGLE_CLOUD_CPP_ENABLE_CLOG", "anyvalue");
157+
testing_util::ScopedEnvironment env("GOOGLE_CLOUD_CPP_ENABLE_CLOG",
158+
"anyvalue");
161159

162160
auto f = [] {
163161
GCP_LOG(INFO) << "testing clog";

0 commit comments

Comments
 (0)