Add --verbose flag, and tests for it.

The new warnings associated with the --jobs argument should only appear when
the --verbose flag is passed.
This commit is contained in:
Christian Long 2015-02-26 16:25:27 -06:00
parent c3f5d144bc
commit 59ae3dfec7
3 changed files with 55 additions and 36 deletions

View file

@ -124,7 +124,10 @@ def get_style_guide(**kwargs):
for options_hook in options_hooks: for options_hook in options_hooks:
options_hook(options) options_hook(options)
if options.jobs and options.jobs.isdigit() and int(options.jobs) > 1: if (options.verbose
and options.jobs
and options.jobs.isdigit()
and int(options.jobs) > 1):
if not multiprocessing: if not multiprocessing:
warnings.warn("The multiprocessing module is not available. " warnings.warn("The multiprocessing module is not available. "
"Ignoring --jobs arguments.") "Ignoring --jobs arguments.")

View file

@ -1,9 +1,9 @@
""" """
toast_warnings.py _test_warnings.py
Tests for the warnings that are emitted by flake8. Tests for the warnings that are emitted by flake8.
This module is named toast_warnings instead of test_warnings so that a This module is named _test_warnings instead of test_warnings so that a
normal nosetests run does not collect it. The tests in this module pass normal nosetests run does not collect it. The tests in this module pass
when they are run alone, but they fail when they are run along with other when they are run alone, but they fail when they are run along with other
tests (nosetests --with-isolation doesn't help). tests (nosetests --with-isolation doesn't help).
@ -29,6 +29,12 @@ class IntegrationTestCaseWarnings(unittest.TestCase):
"""Integration style tests to check that warnings are issued properly for """Integration style tests to check that warnings are issued properly for
different command line options.""" different command line options."""
windows_warning_text = ("The --jobs option is not available on Windows."
" Ignoring --jobs arguments.")
stdin_warning_text = ("The --jobs option is not compatible with"
" supplying input using - . Ignoring --jobs"
" arguments.")
def this_file(self): def this_file(self):
"""Return the real path of this file.""" """Return the real path of this file."""
this_file = os.path.realpath(__file__) this_file = os.path.realpath(__file__)
@ -66,7 +72,7 @@ class IntegrationTestCaseWarnings(unittest.TestCase):
# warnings. # warnings.
return (style_guide, collected_warnings) return (style_guide, collected_warnings)
def verify_warnings(self, collected_warnings, expected_warnings=None): def verify_warnings(self, collected_warnings, expected_warnings):
""" """
Verifies that collected_warnings is a sequence that contains user Verifies that collected_warnings is a sequence that contains user
warnings that match the sequence of string values passed in as warnings that match the sequence of string values passed in as
@ -90,8 +96,11 @@ class IntegrationTestCaseWarnings(unittest.TestCase):
def check_files_collect_warnings(self, def check_files_collect_warnings(self,
arglist=[], arglist=[],
explicit_stdin=False, explicit_stdin=False,
count=0): count=0,
verbose=False):
"""Call check_files and collect any warnings that are issued.""" """Call check_files and collect any warnings that are issued."""
if verbose:
arglist.append('--verbose')
if explicit_stdin: if explicit_stdin:
target_file = "-" target_file = "-"
else: else:
@ -109,36 +118,30 @@ class IntegrationTestCaseWarnings(unittest.TestCase):
def check_files_no_warnings_allowed(self, def check_files_no_warnings_allowed(self,
arglist=[], arglist=[],
explicit_stdin=False, explicit_stdin=False,
count=0): count=0,
verbose=False):
"""Call check_files, and assert that there were no warnings issued.""" """Call check_files, and assert that there were no warnings issued."""
(style_guide, (style_guide,
report, report,
collected_warnings, collected_warnings,
) = self.check_files_collect_warnings(arglist=arglist, ) = self.check_files_collect_warnings(arglist=arglist,
explicit_stdin=explicit_stdin, explicit_stdin=explicit_stdin,
count=count) count=count,
self.verify_warnings(collected_warnings) verbose=verbose)
self.verify_warnings(collected_warnings, expected_warnings=None)
return style_guide, report return style_guide, report
def test_no_args_no_warnings(self): def _job_tester(self, jobs, verbose=False):
# assert there are no reported errors or warnings
self.check_files_no_warnings_allowed()
def _job_tester(self, jobs):
# mock stdout.flush so we can count the number of jobs created # mock stdout.flush so we can count the number of jobs created
with mock.patch('sys.stdout.flush') as mocked: with mock.patch('sys.stdout.flush') as mocked:
(guide, (guide,
report, report,
collected_warnings, collected_warnings,
) = self.check_files_collect_warnings(arglist=['--jobs=%s' ) = self.check_files_collect_warnings(
% jobs]) arglist=['--jobs=%s' % jobs],
verbose=verbose)
expected_warings = []
if is_windows(): if is_windows():
expected_warings.append("The --jobs option is not "
"available on Windows. Ignoring "
"--jobs arguments.")
# The code path where guide.options.jobs gets converted to an # The code path where guide.options.jobs gets converted to an
# int is not run on windows. So, do the int conversion here. # int is not run on windows. So, do the int conversion here.
self.assertEqual(int(guide.options.jobs), jobs) self.assertEqual(int(guide.options.jobs), jobs)
@ -147,13 +150,20 @@ class IntegrationTestCaseWarnings(unittest.TestCase):
else: else:
self.assertEqual(guide.options.jobs, jobs) self.assertEqual(guide.options.jobs, jobs)
self.assertEqual(mocked.call_count, jobs) self.assertEqual(mocked.call_count, jobs)
expected_warings = []
if verbose and is_windows():
expected_warings.append(self.windows_warning_text)
self.verify_warnings(collected_warnings, expected_warings) self.verify_warnings(collected_warnings, expected_warings)
def test_jobs(self): def test_jobs(self, verbose=False):
self._job_tester(2) self._job_tester(2, verbose=verbose)
self._job_tester(10) self._job_tester(10, verbose=verbose)
def test_stdin_jobs_warning(self): def test_no_args_no_warnings(self, verbose=False):
self.check_files_no_warnings_allowed(verbose=verbose)
def test_stdin_jobs_warning(self, verbose=False):
self.count = 0 self.count = 0
def fake_stdin(): def fake_stdin():
@ -165,20 +175,26 @@ class IntegrationTestCaseWarnings(unittest.TestCase):
(style_guide, (style_guide,
report, report,
collected_warnings, collected_warnings,
) = self.check_files_collect_warnings( ) = self.check_files_collect_warnings(arglist=['--jobs=4'],
arglist=['--jobs=4'], explicit_stdin=True,
explicit_stdin=True) verbose=verbose)
expected_warings = []
expected_warings = ["The --jobs option is not compatible with " if verbose:
"supplying input using - . Ignoring --jobs " expected_warings.append(self.stdin_warning_text)
"arguments."]
if is_windows(): if is_windows():
expected_warings.append("The --jobs option is not " expected_warings.append(self.windows_warning_text)
"available on Windows. Ignoring "
"--jobs arguments.")
self.verify_warnings(collected_warnings, expected_warings) self.verify_warnings(collected_warnings, expected_warings)
self.assertEqual(self.count, 1) self.assertEqual(self.count, 1)
def test_jobs_verbose(self):
self.test_jobs(verbose=True)
def test_no_args_no_warnings_verbose(self):
self.test_no_args_no_warnings(verbose=True)
def test_stdin_jobs_warning_verbose(self):
self.test_stdin_jobs_warning(verbose=True)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -11,7 +11,7 @@ deps =
commands = commands =
python setup.py test -q python setup.py test -q
python setup.py flake8 python setup.py flake8
nosetests flake8.tests.toast_warnings nosetests flake8.tests._test_warnings
[testenv:py27-flake8] [testenv:py27-flake8]
basepython = python2.7 basepython = python2.7