Use mock.patch.dict to patch environment
This commit is contained in:
parent
7825fda346
commit
4b9d6927de
4 changed files with 37 additions and 18 deletions
10
Makefile
10
Makefile
|
@ -12,7 +12,9 @@ DOCKER_DEB_TEST := sh -euxc ' \
|
|||
&& tmp=$$(mktemp -d) \
|
||||
&& cp -r /mnt/* "$$tmp" \
|
||||
&& cd "$$tmp" \
|
||||
&& pip install pytest \
|
||||
&& pip install --upgrade pip \
|
||||
&& /usr/local/bin/pip install --upgrade setuptools distribute \
|
||||
&& /usr/local/bin/pip install -r requirements-dev.txt \
|
||||
&& py.test tests/ \
|
||||
&& exec dumb-init /mnt/tests/test-zombies \
|
||||
'
|
||||
|
@ -26,8 +28,10 @@ DOCKER_PYTHON_TEST := sh -uexc ' \
|
|||
&& cd "$$tmp" \
|
||||
&& python setup.py clean \
|
||||
&& python setup.py sdist \
|
||||
&& pip install -vv dist/*.tar.gz \
|
||||
&& pip install pytest \
|
||||
&& pip install --upgrade pip \
|
||||
&& /usr/local/bin/pip install --upgrade setuptools distribute \
|
||||
&& /usr/local/bin/pip install -vv dist/*.tar.gz \
|
||||
&& /usr/local/bin/pip install -r requirements-dev.txt \
|
||||
&& py.test tests/ \
|
||||
&& exec dumb-init /mnt/tests/test-zombies \
|
||||
'
|
||||
|
|
3
debian/control
vendored
3
debian/control
vendored
|
@ -3,7 +3,8 @@ Section: utils
|
|||
Priority: extra
|
||||
Maintainer: Chris Kuehl <ckuehl@yelp.com>
|
||||
Uploaders: Kent Wills <rkwills@yelp.com>
|
||||
Build-Depends: debhelper (>= 7), gcc, fakeroot, python, python-pytest
|
||||
Build-Depends: debhelper (>= 7), gcc, fakeroot, python, python-pytest,
|
||||
python-mock
|
||||
Standards-Version: 3.9.6
|
||||
|
||||
Package: dumb-init
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
mock
|
||||
pre-commit>=0.5.0
|
||||
pytest
|
||||
pytest-timeout
|
||||
|
|
|
@ -1,35 +1,48 @@
|
|||
import os
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope='function')
|
||||
@pytest.yield_fixture(autouse=True, scope='function')
|
||||
def clean_environment():
|
||||
"""Ensure tests don't pollute each others' environment variables."""
|
||||
os.environ.pop('DUMB_INIT_DEBUG', None)
|
||||
os.environ.pop('DUMB_INIT_SETSID', None)
|
||||
"""Ensure all tests start with a clean environment.
|
||||
|
||||
Even if tests properly clean up after themselves, we still need this in
|
||||
case the user runs tests with an already-polluted environment.
|
||||
"""
|
||||
with mock.patch.dict(
|
||||
os.environ,
|
||||
{'DUMB_INIT_DEBUG': '', 'DUMB_INIT_SETSID': ''},
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(params=['1', '0'])
|
||||
@pytest.yield_fixture(params=['1', '0'])
|
||||
def both_debug_modes(request):
|
||||
os.environ['DUMB_INIT_DEBUG'] = request.param
|
||||
with mock.patch.dict(os.environ, {'DUMB_INIT_DEBUG': request.param}):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest.yield_fixture
|
||||
def debug_disabled():
|
||||
os.environ['DUMB_INIT_DEBUG'] = '0'
|
||||
with mock.patch.dict(os.environ, {'DUMB_INIT_DEBUG': '0'}):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(params=['1', '0'])
|
||||
@pytest.yield_fixture(params=['1', '0'])
|
||||
def both_setsid_modes(request):
|
||||
os.environ['DUMB_INIT_SETSID'] = request.param
|
||||
with mock.patch.dict(os.environ, {'DUMB_INIT_SETSID': request.param}):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest.yield_fixture
|
||||
def setsid_enabled():
|
||||
os.environ['DUMB_INIT_SETSID'] = '1'
|
||||
with mock.patch.dict(os.environ, {'DUMB_INIT_SETSID': '1'}):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest.yield_fixture
|
||||
def setsid_disabled():
|
||||
os.environ['DUMB_INIT_SETSID'] = '0'
|
||||
with mock.patch.dict(os.environ, {'DUMB_INIT_SETSID': '0'}):
|
||||
yield
|
||||
|
|
Loading…
Reference in a new issue