From 9d48766c02fec71b6bf7a81e7d84526ae7cd304b Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 2 Sep 2018 18:39:11 -0700 Subject: [PATCH 1/3] git mv tests/meta_hooks/{,check_}useless_excludes_test.py --- .../{useless_excludes_test.py => check_useless_excludes_test.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/meta_hooks/{useless_excludes_test.py => check_useless_excludes_test.py} (100%) diff --git a/tests/meta_hooks/useless_excludes_test.py b/tests/meta_hooks/check_useless_excludes_test.py similarity index 100% rename from tests/meta_hooks/useless_excludes_test.py rename to tests/meta_hooks/check_useless_excludes_test.py From 21c2c9df3366e8f2a7544405f9d19af0a0423857 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 2 Sep 2018 18:45:21 -0700 Subject: [PATCH 2/3] No need for OrderedDict --- tests/meta_hooks/check_hooks_apply_test.py | 129 +++++++++--------- .../meta_hooks/check_useless_excludes_test.py | 95 ++++++------- 2 files changed, 109 insertions(+), 115 deletions(-) diff --git a/tests/meta_hooks/check_hooks_apply_test.py b/tests/meta_hooks/check_hooks_apply_test.py index f0f38d69..e6a7b133 100644 --- a/tests/meta_hooks/check_hooks_apply_test.py +++ b/tests/meta_hooks/check_hooks_apply_test.py @@ -1,5 +1,3 @@ -from collections import OrderedDict - from pre_commit.meta_hooks import check_hooks_apply from testing.fixtures import add_config_to_repo from testing.fixtures import git_dir @@ -7,17 +5,19 @@ from testing.util import cwd def test_hook_excludes_everything(capsys, tempdir_factory, mock_store_dir): - config = OrderedDict(( - ('repo', 'meta'), - ( - 'hooks', ( - OrderedDict(( - ('id', 'check-useless-excludes'), - ('exclude', '.pre-commit-config.yaml'), - )), - ), - ), - )) + config = { + 'repos': [ + { + 'repo': 'meta', + 'hooks': [ + { + 'id': 'check-useless-excludes', + 'exclude': '.pre-commit-config.yaml', + }, + ], + }, + ], + } repo = git_dir(tempdir_factory) add_config_to_repo(repo, config) @@ -30,17 +30,19 @@ def test_hook_excludes_everything(capsys, tempdir_factory, mock_store_dir): def test_hook_includes_nothing(capsys, tempdir_factory, mock_store_dir): - config = OrderedDict(( - ('repo', 'meta'), - ( - 'hooks', ( - OrderedDict(( - ('id', 'check-useless-excludes'), - ('files', 'foo'), - )), - ), - ), - )) + config = { + 'repos': [ + { + 'repo': 'meta', + 'hooks': [ + { + 'id': 'check-useless-excludes', + 'files': 'foo', + }, + ], + }, + ], + } repo = git_dir(tempdir_factory) add_config_to_repo(repo, config) @@ -53,17 +55,19 @@ def test_hook_includes_nothing(capsys, tempdir_factory, mock_store_dir): def test_hook_types_not_matched(capsys, tempdir_factory, mock_store_dir): - config = OrderedDict(( - ('repo', 'meta'), - ( - 'hooks', ( - OrderedDict(( - ('id', 'check-useless-excludes'), - ('types', ['python']), - )), - ), - ), - )) + config = { + 'repos': [ + { + 'repo': 'meta', + 'hooks': [ + { + 'id': 'check-useless-excludes', + 'types': ['python'], + }, + ], + }, + ], + } repo = git_dir(tempdir_factory) add_config_to_repo(repo, config) @@ -78,17 +82,19 @@ def test_hook_types_not_matched(capsys, tempdir_factory, mock_store_dir): def test_hook_types_excludes_everything( capsys, tempdir_factory, mock_store_dir, ): - config = OrderedDict(( - ('repo', 'meta'), - ( - 'hooks', ( - OrderedDict(( - ('id', 'check-useless-excludes'), - ('exclude_types', ['yaml']), - )), - ), - ), - )) + config = { + 'repos': [ + { + 'repo': 'meta', + 'hooks': [ + { + 'id': 'check-useless-excludes', + 'exclude_types': ['yaml'], + }, + ], + }, + ], + } repo = git_dir(tempdir_factory) add_config_to_repo(repo, config) @@ -101,22 +107,21 @@ def test_hook_types_excludes_everything( def test_valid_includes(capsys, tempdir_factory, mock_store_dir): - config = OrderedDict(( - ('repo', 'meta'), - ( - 'hooks', ( - OrderedDict(( - ('id', 'check-useless-excludes'), - )), - # Should not be reported as an error due to always_run - OrderedDict(( - ('id', 'check-useless-excludes'), - ('files', '^$'), - ('always_run', True), - )), - ), - ), - )) + config = { + 'repos': [ + { + 'repo': 'meta', + 'hooks': [ + # Should not be reported as an error due to always_run + { + 'id': 'check-useless-excludes', + 'files': '^$', + 'always_run': True, + }, + ], + }, + ], + } repo = git_dir(tempdir_factory) add_config_to_repo(repo, config) diff --git a/tests/meta_hooks/check_useless_excludes_test.py b/tests/meta_hooks/check_useless_excludes_test.py index 137c357f..1a03fb08 100644 --- a/tests/meta_hooks/check_useless_excludes_test.py +++ b/tests/meta_hooks/check_useless_excludes_test.py @@ -1,5 +1,3 @@ -from collections import OrderedDict - from pre_commit.meta_hooks import check_useless_excludes from testing.fixtures import add_config_to_repo from testing.fixtures import git_dir @@ -7,23 +5,15 @@ from testing.util import cwd def test_useless_exclude_global(capsys, tempdir_factory): - config = OrderedDict(( - ('exclude', 'foo'), - ( - 'repos', [ - OrderedDict(( - ('repo', 'meta'), - ( - 'hooks', ( - OrderedDict(( - ('id', 'check-useless-excludes'), - )), - ), - ), - )), - ], - ), - )) + config = { + 'exclude': 'foo', + 'repos': [ + { + 'repo': 'meta', + 'hooks': [{'id': 'check-useless-excludes'}], + }, + ], + } repo = git_dir(tempdir_factory) add_config_to_repo(repo, config) @@ -32,21 +22,19 @@ def test_useless_exclude_global(capsys, tempdir_factory): assert check_useless_excludes.main(()) == 1 out, _ = capsys.readouterr() - assert "The global exclude pattern 'foo' does not match any files" in out + out = out.strip() + assert "The global exclude pattern 'foo' does not match any files" == out def test_useless_exclude_for_hook(capsys, tempdir_factory): - config = OrderedDict(( - ('repo', 'meta'), - ( - 'hooks', ( - OrderedDict(( - ('id', 'check-useless-excludes'), - ('exclude', 'foo'), - )), - ), - ), - )) + config = { + 'repos': [ + { + 'repo': 'meta', + 'hooks': [{'id': 'check-useless-excludes', 'exclude': 'foo'}], + }, + ], + } repo = git_dir(tempdir_factory) add_config_to_repo(repo, config) @@ -55,24 +43,23 @@ def test_useless_exclude_for_hook(capsys, tempdir_factory): assert check_useless_excludes.main(()) == 1 out, _ = capsys.readouterr() + out = out.strip() expected = ( "The exclude pattern 'foo' for check-useless-excludes " "does not match any files" ) - assert expected in out + assert expected == out def test_no_excludes(capsys, tempdir_factory): - config = OrderedDict(( - ('repo', 'meta'), - ( - 'hooks', ( - OrderedDict(( - ('id', 'check-useless-excludes'), - )), - ), - ), - )) + config = { + 'repos': [ + { + 'repo': 'meta', + 'hooks': [{'id': 'check-useless-excludes'}], + }, + ], + } repo = git_dir(tempdir_factory) add_config_to_repo(repo, config) @@ -85,17 +72,19 @@ def test_no_excludes(capsys, tempdir_factory): def test_valid_exclude(capsys, tempdir_factory): - config = OrderedDict(( - ('repo', 'meta'), - ( - 'hooks', ( - OrderedDict(( - ('id', 'check-useless-excludes'), - ('exclude', '.pre-commit-config.yaml'), - )), - ), - ), - )) + config = { + 'repos': [ + { + 'repo': 'meta', + 'hooks': [ + { + 'id': 'check-useless-excludes', + 'exclude': '.pre-commit-config.yaml', + }, + ], + }, + ], + } repo = git_dir(tempdir_factory) add_config_to_repo(repo, config) From ce25b652b91966b0dfb528299f8f1f84a3893192 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 2 Sep 2018 18:54:34 -0700 Subject: [PATCH 3/3] Exempt `language: fail` hooks from check-hooks-apply --- pre_commit/meta_hooks/check_hooks_apply.py | 2 +- tests/meta_hooks/check_hooks_apply_test.py | 31 +++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/pre_commit/meta_hooks/check_hooks_apply.py b/pre_commit/meta_hooks/check_hooks_apply.py index 23420f46..4c4719c8 100644 --- a/pre_commit/meta_hooks/check_hooks_apply.py +++ b/pre_commit/meta_hooks/check_hooks_apply.py @@ -15,7 +15,7 @@ def check_all_hooks_match_files(config_file): for repo in repositories(load_config(config_file), Store()): for hook_id, hook in repo.hooks: - if hook['always_run']: + if hook['always_run'] or hook['language'] == 'fail': continue include, exclude = hook['files'], hook['exclude'] filtered = _filter_by_include_exclude(files, include, exclude) diff --git a/tests/meta_hooks/check_hooks_apply_test.py b/tests/meta_hooks/check_hooks_apply_test.py index e6a7b133..c75b036a 100644 --- a/tests/meta_hooks/check_hooks_apply_test.py +++ b/tests/meta_hooks/check_hooks_apply_test.py @@ -106,7 +106,7 @@ def test_hook_types_excludes_everything( assert 'check-useless-excludes does not apply to this repository' in out -def test_valid_includes(capsys, tempdir_factory, mock_store_dir): +def test_valid_always_run(capsys, tempdir_factory, mock_store_dir): config = { 'repos': [ { @@ -131,3 +131,32 @@ def test_valid_includes(capsys, tempdir_factory, mock_store_dir): out, _ = capsys.readouterr() assert out == '' + + +def test_valid_language_fail(capsys, tempdir_factory, mock_store_dir): + config = { + 'repos': [ + { + 'repo': 'local', + 'hooks': [ + # Should not be reported as an error due to language: fail + { + 'id': 'changelogs-rst', + 'name': 'changelogs must be rst', + 'entry': 'changelog filenames must end in .rst', + 'language': 'fail', + 'files': r'changelog/.*(?