forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new support for testing with environment variables (googlea…
…pis/google-cloud-cpp-common#180) `ScopedEnvironment` is an RAII object that will, upon destruction, restore the previous state of the environment variable it modified. For example: ``` // ${VAR} has some initial state. { ScopedEnvironment env("VAR", "value"); // ${VAR} now holds "value". } // The initial state of ${VAR} has been restored. ```
- Loading branch information
Showing
6 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ | |
|
||
google_cloud_cpp_testing_unit_tests = [ | ||
"assert_ok_test.cc", | ||
"scoped_environment_test.cc", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "google/cloud/testing_util/scoped_environment.h" | ||
#include "google/cloud/internal/getenv.h" | ||
#include "google/cloud/internal/setenv.h" | ||
|
||
namespace google { | ||
namespace cloud { | ||
inline namespace GOOGLE_CLOUD_CPP_NS { | ||
namespace testing_util { | ||
|
||
ScopedEnvironment::ScopedEnvironment(std::string variable, | ||
optional<std::string> const& value) | ||
: variable_(std::move(variable)), | ||
prev_value_(internal::GetEnv(variable_.c_str())) { | ||
internal::SetEnv(variable_.c_str(), value); | ||
} | ||
|
||
ScopedEnvironment::~ScopedEnvironment() { | ||
internal::SetEnv(variable_.c_str(), std::move(prev_value_)); | ||
} | ||
|
||
} // namespace testing_util | ||
} // namespace GOOGLE_CLOUD_CPP_NS | ||
} // namespace cloud | ||
} // namespace google |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_TESTING_UTIL_SCOPED_ENVIRONMENT_H | ||
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_TESTING_UTIL_SCOPED_ENVIRONMENT_H | ||
|
||
#include "google/cloud/optional.h" | ||
#include "google/cloud/version.h" | ||
#include <string> | ||
|
||
namespace google { | ||
namespace cloud { | ||
inline namespace GOOGLE_CLOUD_CPP_NS { | ||
namespace testing_util { | ||
|
||
/** | ||
* Helper class to (un)set and restore the value of an environment variable. | ||
*/ | ||
class ScopedEnvironment { | ||
public: | ||
/** | ||
* Set the @p variable environment variable to @p value. If @value is | ||
* an unset optional then the variable is unset. The previous value of | ||
* the variable will be restored upon destruction. | ||
*/ | ||
ScopedEnvironment(std::string variable, optional<std::string> const& value); | ||
|
||
~ScopedEnvironment(); | ||
|
||
// Movable, but not copyable. | ||
ScopedEnvironment(ScopedEnvironment const&) = delete; | ||
ScopedEnvironment(ScopedEnvironment&&) = default; | ||
ScopedEnvironment& operator=(ScopedEnvironment const&) = delete; | ||
ScopedEnvironment& operator=(ScopedEnvironment&&) = default; | ||
|
||
private: | ||
std::string variable_; | ||
optional<std::string> prev_value_; | ||
}; | ||
|
||
} // namespace testing_util | ||
} // namespace GOOGLE_CLOUD_CPP_NS | ||
} // namespace cloud | ||
} // namespace google | ||
|
||
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_TESTING_UTIL_SCOPED_ENVIRONMENT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "google/cloud/testing_util/scoped_environment.h" | ||
#include "google/cloud/internal/getenv.h" | ||
#include "google/cloud/internal/setenv.h" | ||
#include <gtest/gtest.h> | ||
|
||
namespace google { | ||
namespace cloud { | ||
inline namespace GOOGLE_CLOUD_CPP_NS { | ||
namespace testing_util { | ||
namespace { | ||
|
||
constexpr auto kVarName = "SCOPED_ENVIRONMENT_TEST"; | ||
|
||
TEST(ScopedEnvironment, SetOverSet) { | ||
ScopedEnvironment env_outer(kVarName, "foo"); | ||
EXPECT_EQ(*internal::GetEnv(kVarName), "foo"); | ||
{ | ||
ScopedEnvironment env_inner(kVarName, "bar"); | ||
EXPECT_EQ(*internal::GetEnv(kVarName), "bar"); | ||
} | ||
EXPECT_EQ(*internal::GetEnv(kVarName), "foo"); | ||
} | ||
|
||
TEST(ScopedEnvironment, SetOverUnset) { | ||
ScopedEnvironment env_outer(kVarName, {}); | ||
EXPECT_FALSE(internal::GetEnv(kVarName).has_value()); | ||
{ | ||
ScopedEnvironment env_inner(kVarName, "bar"); | ||
EXPECT_EQ(*internal::GetEnv(kVarName), "bar"); | ||
} | ||
EXPECT_FALSE(internal::GetEnv(kVarName).has_value()); | ||
} | ||
|
||
TEST(ScopedEnvironment, UnsetOverSet) { | ||
ScopedEnvironment env_outer(kVarName, "foo"); | ||
EXPECT_EQ(*internal::GetEnv(kVarName), "foo"); | ||
{ | ||
ScopedEnvironment env_inner(kVarName, {}); | ||
EXPECT_FALSE(internal::GetEnv(kVarName).has_value()); | ||
} | ||
EXPECT_EQ(*internal::GetEnv(kVarName), "foo"); | ||
} | ||
|
||
} // namespace | ||
} // namespace testing_util | ||
} // namespace GOOGLE_CLOUD_CPP_NS | ||
} // namespace cloud | ||
} // namespace google |