Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure PexInfo is isolated from os.environ. #711

Merged
merged 1 commit into from
Apr 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pex/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _get_kv(cls, variable):

def __init__(self, environ=None, rc=None, use_defaults=True):
self._use_defaults = use_defaults
self._environ = environ.copy() if environ else os.environ
self._environ = (environ if environ is not None else os.environ).copy()
if not self.PEX_IGNORE_RCFILES:
rc_values = self.from_rc(rc).copy()
rc_values.update(self._environ)
Expand Down
42 changes: 42 additions & 0 deletions tests/test_variables.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
import os
from contextlib import contextmanager

import pytest

Expand Down Expand Up @@ -67,12 +69,52 @@ def test_pex_get_int():
assert Variables(environ={'HELLO': 'welp'})._get_int('HELLO')


@contextmanager
def environment_as(**kwargs):
jsirois marked this conversation as resolved.
Show resolved Hide resolved
existing = {key: os.environ.get(key) for key in kwargs}

def adjust_environment(mapping):
for key, value in mapping.items():
if value is not None:
os.environ[key] = value
else:
del os.environ[key]

adjust_environment(kwargs)
try:
yield
finally:
adjust_environment(existing)


def assert_pex_vars_hermetic():
v = Variables()
assert os.environ == v.copy()

existing = os.environ.get('TEST')
expected = (existing or '') + 'different'
assert expected != existing

with environment_as(TEST=expected):
assert expected != v.copy().get('TEST')


def test_pex_vars_hermetic_no_pexrc():
assert_pex_vars_hermetic()


def test_pex_vars_hermetic():
with environment_as(PEX_IGNORE_RCFILES='True'):
assert_pex_vars_hermetic()


def test_pex_vars_set():
v = Variables(environ={})
v.set('HELLO', '42')
assert v._get_int('HELLO') == 42
v.delete('HELLO')
assert v._get_int('HELLO') is None
assert {} == v.copy()


def test_pex_get_kv():
Expand Down