From e774c09fac2410c4ec2855a0cf8d9df83fabb776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Berti=20Sassi?= Date: Fri, 12 May 2017 23:24:04 -0300 Subject: [PATCH 1/2] Add pass_filenames hook option This option controls whether filenames are passed along as arguments to the hook program. --- pre_commit/clientlib.py | 1 + pre_commit/commands/run.py | 5 ++++- tests/commands/run_test.py | 26 ++++++++++++++++++++++++++ tests/manifest_test.py | 2 ++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/pre_commit/clientlib.py b/pre_commit/clientlib.py index d386dcd4..bceecaa6 100644 --- a/pre_commit/clientlib.py +++ b/pre_commit/clientlib.py @@ -46,6 +46,7 @@ MANIFEST_HOOK_DICT = schema.Map( ), schema.Optional('args', schema.check_array(schema.check_string), []), schema.Optional('always_run', schema.check_bool, False), + schema.Optional('pass_filenames', schema.check_bool, True), schema.Optional('description', schema.check_string, ''), schema.Optional( 'exclude', diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index 40917e1e..3039b662 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -86,7 +86,10 @@ def _run_single_hook(hook, repo, args, skips, cols): sys.stdout.flush() diff_before = cmd_output('git', 'diff', retcode=None, encoding=None) - retcode, stdout, stderr = repo.run_hook(hook, tuple(filenames)) + retcode, stdout, stderr = repo.run_hook( + hook, + tuple(filenames) if hook['pass_filenames'] else (), + ) diff_after = cmd_output('git', 'diff', retcode=None, encoding=None) file_modifications = diff_before != diff_after diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index 984ac6bd..b0d677d0 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -731,3 +731,29 @@ def test_files_running_subdir( tempdir_factory=tempdir_factory, ) assert 'subdir/foo.py'.replace('/', os.sep) in stdout + + +@pytest.mark.parametrize( + ('pass_filenames', 'hook_args', 'expected_out'), + ( + (True, [], b'foo.py'), + (False, [], b''), + (True, ['some', 'args'], b'some args foo.py'), + (False, ['some', 'args'], b'some args'), + ) +) +def test_pass_filenames( + cap_out, repo_with_passing_hook, mock_out_store_directory, + pass_filenames, + hook_args, + expected_out, +): + with modify_config() as config: + config[0]['hooks'][0]['pass_filenames'] = pass_filenames + config[0]['hooks'][0]['args'] = hook_args + stage_a_file() + ret, printed = _do_run( + cap_out, repo_with_passing_hook, _get_opts(verbose=True), + ) + assert expected_out + b'\nHello World' in printed + assert ('foo.py' in printed) == pass_filenames diff --git a/tests/manifest_test.py b/tests/manifest_test.py index 1296b219..47e7fa32 100644 --- a/tests/manifest_test.py +++ b/tests/manifest_test.py @@ -32,6 +32,7 @@ def test_manifest_contents(manifest): 'log_file': '', 'minimum_pre_commit_version': '0', 'name': 'Bash hook', + 'pass_filenames': True, 'stages': [], }] @@ -51,6 +52,7 @@ def test_hooks(manifest): 'log_file': '', 'minimum_pre_commit_version': '0', 'name': 'Bash hook', + 'pass_filenames': True, 'stages': [], } From 7259135d19cd5fe7bb1d98dda99da1b5749c7460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Berti=20Sassi?= Date: Sat, 13 May 2017 20:12:16 -0300 Subject: [PATCH 2/2] Fix string literal type for Python 3 --- tests/commands/run_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index b0d677d0..d8522da4 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -756,4 +756,4 @@ def test_pass_filenames( cap_out, repo_with_passing_hook, _get_opts(verbose=True), ) assert expected_out + b'\nHello World' in printed - assert ('foo.py' in printed) == pass_filenames + assert (b'foo.py' in printed) == pass_filenames