From 6e3107001f32aaea67e9e72fb0dd8b0beb80c54b Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Sat, 16 Jul 2016 20:27:07 -0500 Subject: [PATCH 1/3] Begin adding tests for the Legacy API --- tests/unit/test_legacy_api.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/unit/test_legacy_api.py diff --git a/tests/unit/test_legacy_api.py b/tests/unit/test_legacy_api.py new file mode 100644 index 0000000..60deea0 --- /dev/null +++ b/tests/unit/test_legacy_api.py @@ -0,0 +1,22 @@ +"""Tests for Flake8's legacy API.""" +import mock + +from flake8.api import legacy as api + + +def test_get_style_guide(): + """Verify the methods called on our internal Application.""" + mockedapp = mock.Mock() + with mock.patch('flake8.main.application.Application') as Application: + Application.return_value = mockedapp + style_guide = api.get_style_guide() + + Application.assert_called_once_with() + mockedapp.find_plugins.assert_called_once_with() + mockedapp.register_plugin_options.assert_called_once_with() + mockedapp.parse_configuration_and_cli.assert_called_once_with([]) + mockedapp.make_formatter.assert_called_once_with() + mockedapp.make_notifier.assert_called_once_with() + mockedapp.make_guide.assert_called_once_with() + mockedapp.make_file_checker_manager.assert_called_once_with() + assert isinstance(style_guide, api.StyleGuide) From 8b4f12872e871c287864ee4563c18b4499b8b652 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Sat, 16 Jul 2016 21:06:27 -0500 Subject: [PATCH 2/3] Add more simple tests for the legacy api --- tests/unit/test_legacy_api.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/unit/test_legacy_api.py b/tests/unit/test_legacy_api.py index 60deea0..0cb3ce9 100644 --- a/tests/unit/test_legacy_api.py +++ b/tests/unit/test_legacy_api.py @@ -20,3 +20,31 @@ def test_get_style_guide(): mockedapp.make_guide.assert_called_once_with() mockedapp.make_file_checker_manager.assert_called_once_with() assert isinstance(style_guide, api.StyleGuide) + + +def test_styleguide_options(): + """Show tha we proxy the StyleGuide.options attribute.""" + app = mock.Mock() + app.options = 'options' + style_guide = api.StyleGuide(app) + assert style_guide.options == 'options' + + +def test_styleguide_paths(): + """Show tha we proxy the StyleGuide.paths attribute.""" + app = mock.Mock() + app.paths = 'paths' + style_guide = api.StyleGuide(app) + assert style_guide.paths == 'paths' + + +def test_styleguide_check_files(): + """Verify we call the right application methods.""" + paths = ['foo', 'bar'] + app = mock.Mock() + style_guide = api.StyleGuide(app) + report = style_guide.check_files(paths) + + app.run_checks.assert_called_once_with(paths) + app.report_errors.assert_called_once_with() + assert isinstance(report, api.Report) From 21a6df725b865596e31362daf121e32c63b96a36 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Tue, 19 Jul 2016 11:12:51 -0500 Subject: [PATCH 3/3] Add a bunch more legacy API tests --- src/flake8/api/legacy.py | 5 +- tests/unit/test_legacy_api.py | 95 +++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/src/flake8/api/legacy.py b/src/flake8/api/legacy.py index 0aec7d9..8c7d337 100644 --- a/src/flake8/api/legacy.py +++ b/src/flake8/api/legacy.py @@ -115,8 +115,9 @@ class StyleGuide(object): def init_report(self, reporter=None): """Set up a formatter for this run of Flake8.""" - if (reporter is not None and - not issubclass(reporter, formatter.BaseFormatter)): + if reporter is None: + return + if not issubclass(reporter, formatter.BaseFormatter): raise ValueError("Report should be subclass of " "flake8.formatter.BaseFormatter.") self._application.make_formatter(reporter) diff --git a/tests/unit/test_legacy_api.py b/tests/unit/test_legacy_api.py index 0cb3ce9..0f1d1f3 100644 --- a/tests/unit/test_legacy_api.py +++ b/tests/unit/test_legacy_api.py @@ -1,7 +1,9 @@ """Tests for Flake8's legacy API.""" import mock +import pytest from flake8.api import legacy as api +from flake8.formatting import base as formatter def test_get_style_guide(): @@ -48,3 +50,96 @@ def test_styleguide_check_files(): app.run_checks.assert_called_once_with(paths) app.report_errors.assert_called_once_with() assert isinstance(report, api.Report) + + +def test_styleguide_excluded(): + """Verify we delegate to our file checker manager. + + We also want to ensure that if we don't specify a parent, is_path_excluded + is called exactly once. + """ + app = mock.Mock() + file_checker_manager = app.file_checker_manager = mock.Mock() + style_guide = api.StyleGuide(app) + + style_guide.excluded('file.py') + file_checker_manager.is_path_excluded.assert_called_once_with('file.py') + + +def test_styleguide_excluded_with_parent(): + """Verify we delegate to our file checker manager. + + When we add the parent argument, we don't check that is_path_excluded was + called only once. + """ + app = mock.Mock() + file_checker_manager = app.file_checker_manager = mock.Mock() + style_guide = api.StyleGuide(app) + + style_guide.excluded('file.py', 'parent') + file_checker_manager.is_path_excluded.call_args == [ + ('file.py',), + ('parent/file.py',), + ] + + +def test_styleguide_init_report_does_nothing(): + """Verify if we use None that we don't call anything.""" + app = mock.Mock() + style_guide = api.StyleGuide(app) + style_guide.init_report() + assert app.make_formatter.called is False + assert app.make_guide.called is False + + +def test_styleguide_init_report_with_non_subclass(): + """Verify we raise a ValueError with non BaseFormatter subclasses.""" + app = mock.Mock() + style_guide = api.StyleGuide(app) + with pytest.raises(ValueError): + style_guide.init_report(object) + assert app.make_formatter.called is False + assert app.make_guide.called is False + + +def test_styleguide_init_report(): + """Verify we do the right incantation for the Application.""" + app = mock.Mock(guide='fake') + style_guide = api.StyleGuide(app) + + class FakeFormatter(formatter.BaseFormatter): + def format(self, *args): + pass + + style_guide.init_report(FakeFormatter) + app.make_formatter.assert_called_once_with(FakeFormatter) + assert app.guide is None + app.make_guide.assert_called_once_with() + + +def test_styleguide_input_file(): + """Verify we call StyleGuide.check_files with the filename.""" + app = mock.Mock() + style_guide = api.StyleGuide(app) + with mock.patch.object(style_guide, 'check_files') as check_files: + style_guide.input_file('file.py') + check_files.assert_called_once_with(['file.py']) + + +def test_report_total_errors(): + """Verify total errors is just a proxy attribute.""" + app = mock.Mock(result_count='Fake count') + report = api.Report(app) + assert report.total_errors == 'Fake count' + + +def test_report_get_statistics(): + """Verify that we use the statistics object.""" + stats = mock.Mock() + stats.statistics_for.return_value = [] + style_guide = mock.Mock(stats=stats) + app = mock.Mock(guide=style_guide) + + report = api.Report(app) + assert report.get_statistics('E') == [] + stats.statistics_for.assert_called_once_with('E')