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

Use mock.patch.dict to patch environment #29

Merged
merged 1 commit into from
Sep 22, 2015
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
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only way I can get mock to install on an ubuntu:lucid Docker (ancient pip, setuptools, etc.)

I know it looks really hacky, but I tried everything else :\

&& py.test tests/ \
&& exec dumb-init /mnt/tests/test-zombies \
'
Expand All @@ -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 \
'
Expand Down
3 changes: 2 additions & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mock
pre-commit>=0.5.0
pytest
pytest-timeout
41 changes: 27 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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