From 8b4bbd23bf37fb946b664f5932e4903f802c6e0d Mon Sep 17 00:00:00 2001 From: Joe Gordon Date: Mon, 13 Oct 2014 21:42:32 -0700 Subject: [PATCH] Add first pass at integration style tests In order to better prevent regressions (such as related to concurrency), Add a integration test framework to simulate running flake8 with arguments. --- flake8/tests/test_integration.py | 71 ++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 flake8/tests/test_integration.py diff --git a/flake8/tests/test_integration.py b/flake8/tests/test_integration.py new file mode 100644 index 0000000..55ace48 --- /dev/null +++ b/flake8/tests/test_integration.py @@ -0,0 +1,71 @@ +from __future__ import with_statement + +import os +import unittest +try: + from unittest import mock +except ImportError: + import mock # < PY33 + +from flake8 import engine + + +class IntegrationTestCase(unittest.TestCase): + """Integration style tests to exercise different command line options.""" + + def this_file(self): + """Return the real path of this file.""" + this_file = os.path.realpath(__file__) + if this_file.endswith("pyc"): + this_file = this_file[:-1] + return this_file + + def check_files(self, arglist=[], explicit_stdin=False, count=0): + """Call check_files.""" + if explicit_stdin: + target_file = "-" + else: + target_file = self.this_file() + argv = ['flake8'] + arglist + [target_file] + with mock.patch("sys.argv", argv): + style_guide = engine.get_style_guide(parse_argv=True) + report = style_guide.check_files() + self.assertEqual(report.total_errors, count) + return style_guide, report + + def test_no_args(self): + # assert there are no reported errors + self.check_files() + + def _job_tester(self, jobs): + # mock stdout.flush so we can count the number of jobs created + with mock.patch('sys.stdout.flush') as mocked: + guide, report = self.check_files(arglist=['--jobs=%s' % jobs]) + self.assertEqual(guide.options.jobs, jobs) + self.assertEqual(mocked.call_count, jobs) + + def test_jobs(self): + self._job_tester(2) + self._job_tester(10) + + def test_stdin(self): + self.count = 0 + + def fake_stdin(): + self.count += 1 + with open(self.this_file(), "r") as f: + return f.read() + + with mock.patch("pep8.stdin_get_value", fake_stdin): + guide, report = self.check_files(arglist=['--jobs=4'], + explicit_stdin=True) + self.assertEqual(self.count, 1) + + def test_stdin_fail(self): + def fake_stdin(): + return "notathing\n" + with mock.patch("pep8.stdin_get_value", fake_stdin): + # only assert needed is in check_files + guide, report = self.check_files(arglist=['--jobs=4'], + explicit_stdin=True, + count=1)