From 2eec5060e9ddbb2cb18add3ae88723279a2f2ea9 Mon Sep 17 00:00:00 2001 From: Sergey Date: Sun, 14 Aug 2016 22:19:03 +0000 Subject: [PATCH 1/2] Consider `lazy` parameter in git.find_modified_files function Refer to https://gitlab.com/pycqa/flake8/blob/867727f30408d53d743b1b6bb3cba4b7d33039c6/old/flake8/hooks.py#L34 --- src/flake8/main/git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flake8/main/git.py b/src/flake8/main/git.py index 8603f67..642ff38 100644 --- a/src/flake8/main/git.py +++ b/src/flake8/main/git.py @@ -141,7 +141,7 @@ def make_temporary_directory_from(destination, directory): def find_modified_files(lazy): diff_index = piped_process( - ['git', 'diff-index', '--cached', '--name-only', + ['git', 'diff-index', '' if lazy else '--cached', '--name-only', '--diff-filter=ACMRTUXB', 'HEAD'], ) From 941896218d477c3ec117d21dc9da45d4265269b8 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Mon, 24 Oct 2016 18:29:45 -0500 Subject: [PATCH 2/2] Change how we apply lazy to the git hook --- src/flake8/main/git.py | 11 +++++++---- tests/unit/test_git.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 tests/unit/test_git.py diff --git a/src/flake8/main/git.py b/src/flake8/main/git.py index 642ff38..d0c87d3 100644 --- a/src/flake8/main/git.py +++ b/src/flake8/main/git.py @@ -140,11 +140,14 @@ def make_temporary_directory_from(destination, directory): def find_modified_files(lazy): - diff_index = piped_process( - ['git', 'diff-index', '' if lazy else '--cached', '--name-only', - '--diff-filter=ACMRTUXB', 'HEAD'], - ) + diff_index_cmd = [ + 'git', 'diff-index', '--cached', '--name-only', + '--diff-filter=ACMRTUXB', 'HEAD' + ] + if lazy: + diff_index_cmd.remove('--cached') + diff_index = piped_process(diff_index_cmd) (stdout, _) = diff_index.communicate() stdout = to_text(stdout) return stdout.splitlines() diff --git a/tests/unit/test_git.py b/tests/unit/test_git.py new file mode 100644 index 0000000..24d4b5b --- /dev/null +++ b/tests/unit/test_git.py @@ -0,0 +1,29 @@ +"""Tests around functionality in the git integration.""" +import mock +import pytest + +from flake8.main import git + + +@pytest.mark.parametrize('lazy', [True, False]) +def test_find_modified_files(lazy): + """Confirm our logic for listing modified files.""" + if lazy: + # Here --cached is missing + call = [ + 'git', 'diff-index', '--name-only', '--diff-filter=ACMRTUXB', + 'HEAD' + ] + else: + call = [ + 'git', 'diff-index', '--cached', '--name-only', + '--diff-filter=ACMRTUXB', 'HEAD' + ] + mocked_popen = mock.Mock() + mocked_popen.communicate.return_value = ('', '') + + with mock.patch('flake8.main.git.piped_process') as piped_process: + piped_process.return_value = mocked_popen + git.find_modified_files(lazy) + + piped_process.assert_called_once_with(call)