From b64421c5e857abac64f529f4e1a71522c2c24afa Mon Sep 17 00:00:00 2001 From: Tomer Keren Date: Sun, 13 Jan 2019 21:24:44 +0200 Subject: [PATCH] Write a test checking plugin failure exception handling --- tests/unit/conftest.py | 17 +++++++++++++++++ tests/unit/test_file_checker.py | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/unit/conftest.py diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py new file mode 100644 index 0000000..5b8daa3 --- /dev/null +++ b/tests/unit/conftest.py @@ -0,0 +1,17 @@ +"""Shared fixtures between unit tests""" +import pytest +import optparse + +def options_from(**kwargs): + """Generate a Values instances with our kwargs.""" + kwargs.setdefault('hang_closing', True) + kwargs.setdefault('max_line_length', 79) + kwargs.setdefault('verbose', False) + kwargs.setdefault('stdin_display_name', 'stdin') + return optparse.Values(kwargs) + +@pytest.fixture +def default_options(): + """Fixture returning the default options of flake8""" + return options_from() + \ No newline at end of file diff --git a/tests/unit/test_file_checker.py b/tests/unit/test_file_checker.py index 4ca6154..9e9bd03 100644 --- a/tests/unit/test_file_checker.py +++ b/tests/unit/test_file_checker.py @@ -1,5 +1,7 @@ """Unit tests for the FileChecker class.""" import mock +import pytest +import flake8 from flake8 import checker @@ -43,3 +45,19 @@ def test_nonexistent_file(): assert len(c.results) == 1 error = c.results[0] assert error[0] == "E902" + + +def test_raises_proper_exception_on_failed_plugin_run(tmp_path, default_options): + foobar = tmp_path / 'foobar.py' + foobar.write_text("I exist") + plugin = { + "name": "failure", + "plugin_name": "failure", # Both are necessary + "parameters": dict(), + "plugin": mock.MagicMock(side_effect=ValueError), + } + """Verify a failing plugin results in an plugin error""" + fchecker = checker.FileChecker( + str(foobar), checks=[], options=default_options) + with pytest.raises(flake8.exceptions.PluginExecutionFailed): + fchecker.run_check(plugin)