• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "base/test/scoped_environment_variable_override.h"
6 
7 #include "base/environment.h"
8 
9 namespace base {
10 namespace test {
11 
ScopedEnvironmentVariableOverride(const std::string & variable_name,const std::string & value)12 ScopedEnvironmentVariableOverride::ScopedEnvironmentVariableOverride(
13     const std::string& variable_name,
14     const std::string& value)
15     : environment_(Environment::Create()),
16       variable_name_(variable_name),
17       overridden_(false),
18       was_set_(false) {
19   was_set_ = environment_->GetVar(variable_name, &old_value_);
20   overridden_ = environment_->SetVar(variable_name, value);
21 }
22 
~ScopedEnvironmentVariableOverride()23 ScopedEnvironmentVariableOverride::~ScopedEnvironmentVariableOverride() {
24   if (overridden_) {
25     if (was_set_)
26       environment_->SetVar(variable_name_, old_value_);
27     else
28       environment_->UnSetVar(variable_name_);
29   }
30 }
31 
32 }  // namespace test
33 }  // namespace base
34