Exemple #1
0
def test_handle_file_plugins(plugin_target):
    """Test the FileChecker class handling different file plugin types."""
    # Mock an entry point returning the plugin target
    entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
    entry_point.name = plugin_target.name
    entry_point.resolve.return_value = plugin_target

    # Load the checker plugins using the entry point mock
    with mock.patch('pkg_resources.iter_entry_points',
                    return_value=[entry_point]):
        checks = manager.Checkers()

    # Prevent it from reading lines from stdin or somewhere else
    with mock.patch('flake8.processor.FileProcessor.read_lines',
                    return_value=['Line 1']):
        file_checker = checker.FileChecker('-', checks.to_dictionary(),
                                           mock.MagicMock())

    # Do not actually build an AST
    file_checker.processor.build_ast = lambda: True

    # Forward reports to this mock
    report = mock.Mock()
    file_checker.report = report
    file_checker.run_ast_checks()
    report.assert_called_once_with(error_code=None,
                                   line_number=EXPECTED_REPORT[0],
                                   column=EXPECTED_REPORT[1],
                                   text=EXPECTED_REPORT[2])
Exemple #2
0
def mock_file_checker_with_plugin(plugin_target):
    """Get a mock FileChecker class with plugin_target registered.

    Useful as a starting point for mocking reports/results.
    """
    # Mock an entry point returning the plugin target
    entry_point = mock.Mock(spec=['load'])
    entry_point.name = plugin_target.name
    entry_point.load.return_value = plugin_target
    entry_point.value = 'mocked:value'

    # Load the checker plugins using the entry point mock
    with mock.patch.object(
            importlib_metadata,
            'entry_points',
            return_value={'flake8.extension': [entry_point]},
    ):
        checks = manager.Checkers()

    # Prevent it from reading lines from stdin or somewhere else
    with mock.patch('flake8.processor.FileProcessor.read_lines',
                    return_value=['Line 1']):
        file_checker = checker.FileChecker(
            '-',
            checks.to_dictionary(),
            mock.MagicMock()
        )
    return file_checker
Exemple #3
0
def test_repr(*args):
    """Verify we generate a correct repr."""
    file_checker = checker.FileChecker(
        'example.py',
        checks={},
        options=object(),
    )
    assert repr(file_checker) == 'FileChecker for example.py'
Exemple #4
0
def test_nonexistent_file():
    """Verify that checking non-existent file results in an error."""
    c = checker.FileChecker("foobar.py", checks={}, options=object())

    assert c.processor is None
    assert not c.should_process
    assert len(c.results) == 1
    error = c.results[0]
    assert error[0] == "E902"
Exemple #5
0
def test_raises_exception_on_failed_plugin(tmp_path, default_options):
    """Checks that a failing plugin results in PluginExecutionFailed."""
    foobar = tmp_path / 'foobar.py'
    foobar.write_text(u"I exist!")  # Create temp file
    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)
Exemple #6
0
def test_run_ast_checks_handles_SyntaxErrors(FileProcessor):
    """Stress our SyntaxError handling.

    Related to: https://gitlab.com/pycqa/flake8/issues/237
    """
    processor = mock.Mock(lines=[])
    FileProcessor.return_value = processor
    processor.build_ast.side_effect = SyntaxError('Failed to build ast',
                                                  ('', 1, 5, 'foo(\n'))
    file_checker = checker.FileChecker(__file__, checks={}, options=object())

    with mock.patch.object(file_checker, 'report') as report:
        file_checker.run_ast_checks()

        report.assert_called_once_with(
            'E999', 1, 3,
            'SyntaxError: Failed to build ast',
        )