-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
De-duplication, although I'll probably change this later.
It uses PatchMixin in unbound form.
- Loading branch information
Showing
3 changed files
with
43 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters