Use mock.patch.dict to patch environment

This commit is contained in:
Chris Kuehl 2015-09-22 14:03:30 -07:00
parent 7825fda346
commit 4b9d6927de
4 changed files with 37 additions and 18 deletions

View file

@ -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
View file

@ -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

View file

@ -1,3 +1,4 @@
mock
pre-commit>=0.5.0
pytest
pytest-timeout

View file

@ -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