From 23be44ea50ee1f00672bfdc0b7b36f62072e7395 Mon Sep 17 00:00:00 2001 From: wanghui Date: Tue, 22 Nov 2016 10:55:35 +0800 Subject: [PATCH] Fix git hook checks all the staged files I think because of this commit: 78e8165b06ada2a027f4c9976889bbe863c24cbd The git hook will check all the staged files even it is not a Python source files. This commit try to fix this and issue #271. --- src/flake8/main/git.py | 4 +++- tests/unit/test_git.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/flake8/main/git.py b/src/flake8/main/git.py index 096fed6..5ff0fc7 100644 --- a/src/flake8/main/git.py +++ b/src/flake8/main/git.py @@ -5,6 +5,7 @@ """ import contextlib +import fnmatch import os import shutil import stat @@ -40,7 +41,8 @@ def hook(lazy=False, strict=False): from flake8.main import application app = application.Application() with make_temporary_directory() as tempdir: - filepaths = list(copy_indexed_files_to(tempdir, lazy)) + filepaths = [x for x in list(copy_indexed_files_to(tempdir, lazy)) + if fnmatch.fnmatch(x, '*.py')] app.initialize(['.']) app.options.exclude = update_excludes(app.options.exclude, tempdir) app.run_checks(filepaths) diff --git a/tests/unit/test_git.py b/tests/unit/test_git.py index 24d4b5b..0a063b0 100644 --- a/tests/unit/test_git.py +++ b/tests/unit/test_git.py @@ -27,3 +27,20 @@ def test_find_modified_files(lazy): git.find_modified_files(lazy) piped_process.assert_called_once_with(call) + + +@mock.patch("flake8.main.git.copy_indexed_files_to") +def test_only_py_files(mock_files): + """Confirm only run checks on Python source file.""" + mock_files.return_value = [ + "/tmp/xxx/test.py", + "/tmp/xxx/test.html", + "/tmp/xxx/test.txt", + ] + + spec = "flake8.main.application.Application.run_checks" + with mock.patch(spec) as mock_run: + git.hook(lazy=False) + mock_run.assert_called_once_with([ + "/tmp/xxx/test.py" + ])