Skip to content

Commit

Permalink
De-duplication, although I'll probably change this later.
Browse files Browse the repository at this point in the history
It uses PatchMixin in unbound form.
  • Loading branch information
Julian committed Sep 19, 2012
1 parent 02d69eb commit 10745d4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
42 changes: 21 additions & 21 deletions ivoire/spec/unit/run_spec.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
from ivoire import describe

from ivoire import run
from ivoire.tests.util import mock
from ivoire.spec.util import mock, patch, patchObject


with describe(run.should_color) as it:
@it.before
def before(test):
test.stderr = patchObject(test, run.sys, "stderr")

with it("colors whenever stderr is a tty") as test:
with mock.patch.object(run.sys, "stderr"):
run.sys.stderr.isatty.return_value = True
test.assertTrue(run.should_color("auto"))
test.stderr.isatty.return_value = True
test.assertTrue(run.should_color("auto"))

with it("doesn't color otherwise") as test:
with mock.patch.object(run.sys, "stderr"):
run.sys.stderr.isatty.return_value = False
test.assertFalse(run.should_color("auto"))
test.stderr.isatty.return_value = False
test.assertFalse(run.should_color("auto"))


with describe(run.run) as it:
with it("succeeds with status code 0") as test:
config = mock.Mock(specs=[])
@it.before
def before(test):
test.config = mock.Mock(specs=[])
test.result = patch(test, "ivoire.current_result")
test.exit = patchObject(test, run.sys, "exit")

with mock.patch("ivoire.current_result") as result:
with mock.patch.object(run.sys, "exit") as exit:
result.wasSuccessful.return_value = True
run.run(config)
exit.assert_called_once_with(0)
with it("succeeds with status code 0") as test:
test.result.wasSuccessful.return_value = True
run.run(test.config)
test.exit.assert_called_once_with(0)

with it("fails with status code 1") as test:
config = mock.Mock(specs=[])

with mock.patch("ivoire.current_result") as result:
with mock.patch.object(run.sys, "exit") as exit:
result.wasSuccessful.return_value = False
run.run(config)
exit.assert_called_once_with(1)
test.result.wasSuccessful.return_value = False
run.run(test.config)
test.exit.assert_called_once_with(1)
20 changes: 20 additions & 0 deletions ivoire/spec/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from functools import wraps

try:
from unittest import mock
except ImportError:
import mock


def _cleanUpPatch(fn):
@wraps(fn)
def cleaned(test, *args, **kwargs):
patch = fn(*args, **kwargs)
test.addCleanup(patch.stop)
return patch.start()
return cleaned


patch = _cleanUpPatch(mock.patch)
patchDict = _cleanUpPatch(mock.patch.dict)
patchObject = _cleanUpPatch(mock.patch.object)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from setuptools import setup
from setuptools import find_packages, setup
import os.path

from ivoire import __version__
Expand Down Expand Up @@ -30,7 +30,7 @@
name="ivoire",
version=__version__,
entry_points={"console_scripts" : ["ivoire = ivoire.run:main"]},
packages=["ivoire", "ivoire.tests"],
packages=find_packages(),
author="Julian Berman",
author_email="Julian@GrayVines.com",
classifiers=classifiers,
Expand Down

0 comments on commit 10745d4

Please sign in to comment.