Skip to content

Commit

Permalink
feature/enabling environment variables unset action (#4224)
Browse files Browse the repository at this point in the history
* Enabling the possibility of unsetting variables by means of appending such variable with value equal to None. Tests covering tools.env.environment_append have been included

* Fixing PEP8 line width requirement

* Fixing PEP8 line width requirement

* Fixing path separator used in the unit test considering its os-dependant value

* Fixing how the env_vars dictionary is being modified. Instead of removing values on-the-fly, It is previourly checked which should be removed by iterating on the keys.

* Fixing how the env_vars dictionary is being modified again. Iterating on keys didn't work enough and now it is decoupled the iteration by using the keys that should be removed and were previously stored

* Fix. Setting all the variables to None (unsetting all of them) effectively removes them from environment.

- Applying changes needed.
- Adding regression test.
- Adding additional test covering the case where no variables are asked to be removed (empty dict).
- Improve tests naming.
  • Loading branch information
alacasta authored and lasote committed Jan 10, 2019
1 parent 23683d1 commit 8e4affa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
13 changes: 11 additions & 2 deletions conans/client/tools/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,29 @@ def run_environment(conanfile):
@contextmanager
def environment_append(env_vars):
"""
:param env_vars: List of simple environment vars. {name: value, name2: value2} => e.g.: MYVAR=1
:param env_vars: List (dict) of simple environment vars. {name: value, name2: value2} => e.g.: MYVAR=1
The values can also be lists of appendable environment vars. {name: [value, value2]}
=> e.g. PATH=/path/1:/path/2
If the value is set to None, then that environment variable is unset.
:return: None
"""
unset_vars = []
for key in env_vars.keys():
if env_vars[key] is None:
unset_vars.append(key)
for var in unset_vars:
env_vars.pop(var, None)
for name, value in env_vars.items():
if isinstance(value, list):
env_vars[name] = os.pathsep.join(value)
old = os.environ.get(name)
if old:
env_vars[name] += os.pathsep + old
if env_vars:
if env_vars or unset_vars:
old_env = dict(os.environ)
os.environ.update(env_vars)
for var in unset_vars:
os.environ.pop(var)
try:
yield
finally:
Expand Down
48 changes: 48 additions & 0 deletions conans/test/unittests/client/tools/test_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# coding=utf-8

import os
import unittest
import mock

from conans.client.tools import env


class ToolsEnvTest(unittest.TestCase):
def test_environment_append_variables(self):
with mock.patch.dict('os.environ', {}),\
env.environment_append({'env_var1': 'value',
'env_var2': 'value2'}):
self.assertEqual(os.environ['env_var1'], 'value')
self.assertEqual(os.environ['env_var2'], 'value2')

def test_environment_append_variables_without_values(self):
with mock.patch.dict('os.environ',
{'env_var1': 'value',
'env_var2': 'value2'}),\
env.environment_append({}):
self.assertEqual(os.environ['env_var1'], 'value')
self.assertEqual(os.environ['env_var2'], 'value2')

def test_environment_append_overwriting(self):
with mock.patch.dict('os.environ', {'env_var1': 'value'}),\
env.environment_append({'env_var1': 'new_value'}):
self.assertEqual(os.environ['env_var1'], 'new_value')

def test_environment_append_list(self):
with mock.patch.dict('os.environ', {}),\
env.environment_append({'env_var1': ['value1', 'value2']}):
self.assertEqual(os.environ['env_var1'], 'value1' +
os.pathsep + 'value2')

def test_environment_append_unsetting_some_variables(self):
with mock.patch.dict('os.environ', {'env_var1': 'value'}),\
env.environment_append({'env_var1': None, 'env_var2': 'value2'}):
self.assertNotIn('env_var1', os.environ)
self.assertEqual(os.environ['env_var2'], 'value2')

def test_environment_append_unsetting_all_variables(self):
with mock.patch.dict('os.environ',
{'env_var1': 'value',
'env_var2': 'value2'}),\
env.environment_append({'env_var1': None}):
self.assertNotIn('env_var1', os.environ)

0 comments on commit 8e4affa

Please sign in to comment.