Fix git hook checks all the staged files

I think because of this commit:

78e8165b06

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.
This commit is contained in:
wanghui 2016-11-22 10:55:35 +08:00
parent a52aedc0a0
commit 23be44ea50
No known key found for this signature in database
GPG key ID: 9DD1E28DBFC06C26
2 changed files with 20 additions and 1 deletions

View file

@ -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)

View file

@ -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"
])