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 5 #ifndef BASE_TEST_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_ 6 #define BASE_TEST_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_ 7 8 #include <memory> 9 #include <string> 10 11 namespace base { 12 13 class Environment; 14 15 namespace test { 16 17 // Helper class to override |variable_name| environment variable to |value| for 18 // the lifetime of this class. Upon destruction, the previous value is restored. 19 class ScopedEnvironmentVariableOverride final { 20 public: 21 ScopedEnvironmentVariableOverride(const std::string& variable_name, 22 const std::string& value); 23 ~ScopedEnvironmentVariableOverride(); 24 GetEnv()25 base::Environment* GetEnv() { return environment_.get(); } IsOverridden()26 bool IsOverridden() { return overridden_; } WasSet()27 bool WasSet() { return was_set_; } 28 29 private: 30 std::unique_ptr<Environment> environment_; 31 std::string variable_name_; 32 bool overridden_; 33 bool was_set_; 34 std::string old_value_; 35 }; 36 37 } // namespace test 38 } // namespace base 39 40 #endif // BASE_TEST_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_ 41