Skip to content

Commit

Permalink
Fix environment variables being passed to subprocess while packaging
Browse files Browse the repository at this point in the history
Fixes aws#501
  • Loading branch information
stealthycoin committed Aug 31, 2017
1 parent 3c75891 commit e005df9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
19 changes: 14 additions & 5 deletions chalice/deploy/packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def __init__(self, osutils, pip_runner=None):
# type: (OSUtils, Optional[PipRunner]) -> None
self._osutils = osutils
if pip_runner is None:
pip_runner = PipRunner(SubprocessPip())
pip_runner = PipRunner(SubprocessPip(osutils))
self._pip = pip_runner

def _is_compatible_wheel_filename(self, filename):
Expand Down Expand Up @@ -576,10 +576,16 @@ def get_package_name_and_version(self, sdist_path):

class SubprocessPip(object):
"""Wrapper around calling pip through a subprocess."""
def __init__(self, osutils=None):
# type: (Optional[OSUtils]) -> None
if osutils is None:
osutils = OSUtils()
self._osutils = osutils

def main(self, args, env_vars=None, shim=None):
# type: (List[str], OptStrMap, OptStr) -> Tuple[int, Optional[bytes]]
if env_vars is None:
env_vars = {}
env_vars = self._osutils.environ()
if shim is None:
shim = ''
env_vars.update(subprocess_python_base_environ)
Expand All @@ -597,9 +603,12 @@ def main(self, args, env_vars=None, shim=None):

class PipRunner(object):
"""Wrapper around pip calls used by chalice."""
def __init__(self, pip):
# type: (SubprocessPip) -> None
def __init__(self, pip, osutils=None):
# type: (SubprocessPip, Optional[OSUtils]) -> None
if osutils is None:
osutils = OSUtils()
self._wrapped_pip = pip
self._osutils = osutils

def _execute(self, command, args, env_vars=None, shim=None):
# type: (str, List[str], OptStrMap, OptStr) -> Tuple[int, OptBytes]
Expand All @@ -613,7 +622,7 @@ def build_wheel(self, wheel, directory, compile_c=True):
# type: (str, str, bool) -> None
"""Build an sdist into a wheel file."""
arguments = ['--no-deps', '--wheel-dir', directory, wheel]
env_vars = {} # type: StrMap
env_vars = self._osutils.environ()
shim = ''
if not compile_c:
env_vars.update(pip_no_compile_c_env_vars)
Expand Down
4 changes: 4 additions & 0 deletions chalice/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def create_zip_file(source_dir, outfile):
class OSUtils(object):
ZIP_DEFLATED = zipfile.ZIP_DEFLATED

def environ(self):
# type: () -> Dict[str,str]
return os.environ.copy()

def open(self, filename, mode):
# type: (str, str) -> IO
return open(filename, mode)
Expand Down
13 changes: 11 additions & 2 deletions tests/functional/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self):
self._side_effects = defaultdict(lambda: [])

def main(self, args, env_vars=None, shim=None):

cmd, args = args[0], args[1:]
self._calls[cmd].append((args, env_vars, shim))
try:
Expand Down Expand Up @@ -162,9 +163,17 @@ def osutils():


@pytest.fixture
def pip_runner():
def empty_env_osutils():
class EmptyEnv(object):
def environ(self):
return {}
return EmptyEnv()


@pytest.fixture
def pip_runner(empty_env_osutils):
pip = FakePip()
pip_runner = PipRunner(pip)
pip_runner = PipRunner(pip, osutils=empty_env_osutils)
return pip, pip_runner


Expand Down
37 changes: 35 additions & 2 deletions tests/unit/deploy/test_packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ def pip_runner():
return pip, pip_runner


@pytest.fixture
def custom_env_osutils():
class CustomEnv(object):
def __init__(self, env):
self._env = env

def environ(self):
return self._env
return CustomEnv


@pytest.fixture
def pip_runner_custom_env(custom_env_osutils):
def create_pip_runner(env):
pip = FakePip()
pip_runner = PipRunner(pip, osutils=custom_env_osutils(env))
return pip, pip_runner
return create_pip_runner


@pytest.fixture
def osutils():
return OSUtils()
Expand Down Expand Up @@ -120,6 +140,17 @@ def test_can_invoke_pip(self):


class TestPipRunner(object):
def test_does_propagate_env_vars(self, pip_runner_custom_env):
pip, runner = pip_runner_custom_env(
env={'foo': 'bar'})
wheel = 'foobar-1.2-py3-none-any.whl'
directory = 'directory'
runner.build_wheel(wheel, directory)
call = pip.calls[0]

assert 'foo' in call.env_vars
assert call.env_vars['foo'] == 'bar'

def test_build_wheel(self, pip_runner):
# Test that `pip wheel` is called with the correct params
pip, runner = pip_runner
Expand All @@ -131,7 +162,8 @@ def test_build_wheel(self, pip_runner):
call = pip.calls[0]
assert call.args == ['wheel', '--no-deps', '--wheel-dir',
directory, wheel]
assert call.env_vars == {}
for compile_env_var in pip_no_compile_c_env_vars:
assert compile_env_var not in call.env_vars
assert call.shim == ''

def test_build_wheel_without_c_extensions(self, pip_runner):
Expand All @@ -146,7 +178,8 @@ def test_build_wheel_without_c_extensions(self, pip_runner):
call = pip.calls[0]
assert call.args == ['wheel', '--no-deps', '--wheel-dir',
directory, wheel]
assert call.env_vars == pip_no_compile_c_env_vars
for compile_env_var in pip_no_compile_c_env_vars:
assert compile_env_var in call.env_vars
assert call.shim == pip_no_compile_c_shim

def test_download_all_deps(self, pip_runner):
Expand Down

0 comments on commit e005df9

Please sign in to comment.