scoped_environment_variable_override.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TEST_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_
  5. #define BASE_TEST_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_
  6. #include <memory>
  7. #include <string>
  8. namespace base {
  9. class Environment;
  10. namespace test {
  11. // Helper class to override |variable_name| environment variable to |value| for
  12. // the lifetime of this class. Upon destruction, the previous value is restored.
  13. class ScopedEnvironmentVariableOverride final {
  14. public:
  15. ScopedEnvironmentVariableOverride(const std::string& variable_name,
  16. const std::string& value);
  17. ~ScopedEnvironmentVariableOverride();
  18. base::Environment* GetEnv() { return environment_.get(); }
  19. bool IsOverridden() { return overridden_; }
  20. bool WasSet() { return was_set_; }
  21. private:
  22. std::unique_ptr<Environment> environment_;
  23. std::string variable_name_;
  24. bool overridden_;
  25. bool was_set_;
  26. std::string old_value_;
  27. };
  28. } // namespace test
  29. } // namespace base
  30. #endif // BASE_TEST_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_