Skip to content

Commit 2fe1231

Browse files
committed
PEP8 line length limits applied to stderr messages
Messages formatting using .format() methods Tests over error messages for invalid --spool arguments Related to: joeyespo#79 (review)
1 parent 39277a3 commit 2fe1231

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

pytest_watch/command.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,14 @@ def main(argv=None):
105105
try:
106106
spool = int(spool)
107107
except ValueError:
108-
sys.stderr.write('Error: Spool (--spool %s) must be an integer.\n' % spool)
108+
sys.stderr.write('Error: Spool (--spool {}) must be an integer.\n'
109+
.format(spool))
109110
return 2
110111

111112
if spool < 0:
112-
sys.stderr.write("Error: Spool value(--spool %s) must be positive integer" % spool)
113+
sys.stderr.write('Error: Spool value(--spool {}) must be positive'\
114+
' integer\n'
115+
.format(spool))
113116
return 2
114117

115118
# Run pytest and watch for changes

pytest_watch/tests/test_command.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import shutil
44
import tempfile
55

6+
if sys.version_info[0] < 3:
7+
from io import BytesIO as io_mock
8+
else:
9+
from io import StringIO as io_mock
10+
611
try:
712
from unittest.mock import patch
813
except ImportError:
@@ -97,7 +102,8 @@ def test_pdb_off_and_wait_on_arguments(self, watch_callee):
97102
watch_callee.call_args[1]["pytest_args"])
98103

99104

100-
@patch("pytest_watch.command.merge_config", side_effect=lambda *args, **kwargs: True)
105+
@patch("pytest_watch.command.merge_config",
106+
side_effect=lambda *args, **kwargs: True)
101107
@patch("pytest_watch.command.watch", side_effect=lambda *args, **kwargs: 0)
102108
class TestConfigArgument(unittest.TestCase):
103109
def test_default_config(self, watch_callee, merge_config_callee):
@@ -226,20 +232,32 @@ def test_default_spool_value(self, watch_callee):
226232
self.assertEqual(200, watch_callee.call_args[1]["spool"])
227233
watch_callee.assert_called_once()
228234

229-
def test_cause_error_for_negative_spool_values(self, watch_callee):
230-
self.assertEqual(2, main("--spool -1".split()))
235+
def _assert_spool_error(self, watch_callee, value, err):
236+
with patch("pytest_watch.command.sys.stderr", new=io_mock()) as out:
237+
self.assertEqual(2, main(["--spool", value]))
238+
assert err == out.getvalue(), \
239+
"Status code for invalid 'spool' argument should be 2"
231240
watch_callee.assert_not_called()
232241

242+
def test_cause_error_for_negative_spool_values(self, watch_callee):
243+
err = "Error: Spool value(--spool -1) must be positive integer\n"
244+
self._assert_spool_error(watch_callee, value="-1", err=err)
245+
233246
def test_cause_error_for_invalid_spool_values(self, watch_callee):
234-
self.assertEquals(2, main("--spool abc".split()),
235-
"Status code for not integer 'spool' " \
236-
"argument should be 2")
237-
self.assertEquals(2, main("--spool @".split()),
238-
"Status code for not integer 'spool' " \
239-
"argument should be 2")
240-
self.assertEquals(2, main("--spool []".split()),
241-
"Status code for not integer 'spool' " \
242-
"argument should be 2")
247+
value = "abc"
248+
self._assert_spool_error(watch_callee, value=value,
249+
err="Error: Spool (--spool {}) must be" \
250+
" an integer.\n".format(value))
251+
252+
value = "@"
253+
self._assert_spool_error(watch_callee, value=value,
254+
err="Error: Spool (--spool {}) must be" \
255+
" an integer.\n".format(value))
256+
257+
value = "[]"
258+
self._assert_spool_error(watch_callee, value=value,
259+
err="Error: Spool (--spool {}) must be" \
260+
" an integer.\n".format(value))
243261

244262

245263
@patch("pytest_watch.command.watch", side_effect=lambda *args, **kwargs: 0)

pytest_watch/tests/test_main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
class TestRunCLI(unittest.TestCase):
1515

16-
@patch("pytest_watch.command.main", side_effect=lambda argv: 0)
16+
@patch("pytest_watch.command.main", side_effect=lambda argv=None: 0)
1717
def test_add_pytest_watch_folder_to_path(self, main):
1818
run_cli()
19-
self.assertIn(os.path.dirname(__main__.__file__), sys.path)
20-
19+
assert os.path.dirname(__main__.__file__) in sys.path

0 commit comments

Comments
 (0)