From 71a740d65dfa18df80643ca07082d2f0f4848847 Mon Sep 17 00:00:00 2001 From: Ben Norquist Date: Tue, 26 Mar 2019 22:31:44 -0700 Subject: [PATCH] add helpful message and test --- pre_commit/commands/run.py | 9 ++++++++- tests/commands/run_test.py | 31 ++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index 2f909522..ed5a0184 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -215,7 +215,14 @@ def _run_hooks(config, hooks, args, environ): if retval and config['fail_fast']: break if retval and args.show_diff_on_failure and git.has_diff(): - output.write_line('All changes made by hooks:') + if args.all_files: + output.write_line( + 'Pre-commit hook(s) made changes. ' + 'If you are seeing this message on CI,' + ' reproduce locally with: pre-commit run --all-files', + ) + else: + output.write_line('All changes made by hooks:') subprocess.call(('git', '--no-pager', 'diff', '--no-ext-diff')) return retval diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index f6efe244..11a8eea1 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -178,16 +178,41 @@ def test_global_exclude(cap_out, store, tempdir_factory): assert printed.endswith(expected) -def test_show_diff_on_failure(capfd, cap_out, store, tempdir_factory): +@pytest.mark.parametrize( + ('args', 'expected_out'), + [ + ( + { + 'show_diff_on_failure': True, + }, + b'All changes made by hooks:', + ), + ( + { + 'show_diff_on_failure': True, + 'all_files': True, + }, + b'reproduce locally with: pre-commit run --all-files', + ), + ], +) +def test_show_diff_on_failure( + args, + expected_out, + capfd, + cap_out, + store, + tempdir_factory, +): git_path = make_consuming_repo( tempdir_factory, 'modified_file_returns_zero_repo', ) with cwd(git_path): stage_a_file('bar.py') _test_run( - cap_out, store, git_path, {'show_diff_on_failure': True}, + cap_out, store, git_path, args, # we're only testing the output after running - (), 1, True, + expected_out, 1, True, ) out, _ = capfd.readouterr() assert 'diff --git' in out