From bea33af31024b086b9abee2a5dc7dcdc626f9fda Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 6 Jan 2019 11:52:22 -0800 Subject: [PATCH] small cleanups in tests --- testing/resources/manifest_without_foo.yaml | 5 -- .../valid_yaml_but_invalid_config.yaml | 5 -- .../valid_yaml_but_invalid_manifest.yaml | 1 - tests/clientlib_test.py | 83 ++++++------------- tests/commands/autoupdate_test.py | 12 +-- tests/commands/migrate_config_test.py | 2 - 6 files changed, 30 insertions(+), 78 deletions(-) delete mode 100644 testing/resources/manifest_without_foo.yaml delete mode 100644 testing/resources/valid_yaml_but_invalid_config.yaml delete mode 100644 testing/resources/valid_yaml_but_invalid_manifest.yaml diff --git a/testing/resources/manifest_without_foo.yaml b/testing/resources/manifest_without_foo.yaml deleted file mode 100644 index 0220233a..00000000 --- a/testing/resources/manifest_without_foo.yaml +++ /dev/null @@ -1,5 +0,0 @@ -- id: bar - name: Bar - entry: bar - language: python - files: \.py$ diff --git a/testing/resources/valid_yaml_but_invalid_config.yaml b/testing/resources/valid_yaml_but_invalid_config.yaml deleted file mode 100644 index 2ed187b2..00000000 --- a/testing/resources/valid_yaml_but_invalid_config.yaml +++ /dev/null @@ -1,5 +0,0 @@ -- repo: git@github.com:pre-commit/pre-commit-hooks - hooks: - - id: pyflakes - - id: jslint - - id: trim_trailing_whitespace diff --git a/testing/resources/valid_yaml_but_invalid_manifest.yaml b/testing/resources/valid_yaml_but_invalid_manifest.yaml deleted file mode 100644 index 20e9ff3f..00000000 --- a/testing/resources/valid_yaml_but_invalid_manifest.yaml +++ /dev/null @@ -1 +0,0 @@ -foo: bar diff --git a/tests/clientlib_test.py b/tests/clientlib_test.py index fd7f051a..839bcaf9 100644 --- a/tests/clientlib_test.py +++ b/tests/clientlib_test.py @@ -13,7 +13,6 @@ from pre_commit.clientlib import MigrateShaToRev from pre_commit.clientlib import validate_config_main from pre_commit.clientlib import validate_manifest_main from testing.fixtures import sample_local_config -from testing.util import get_resource_path def is_valid_according_to_schema(obj, obj_schema): @@ -30,19 +29,6 @@ def test_check_type_tag_failures(value): check_type_tag(value) -@pytest.mark.parametrize( - ('args', 'expected_output'), - ( - (['.pre-commit-config.yaml'], 0), - (['non_existent_file.yaml'], 1), - ([get_resource_path('valid_yaml_but_invalid_config.yaml')], 1), - ([get_resource_path('non_parseable_yaml_file.notyaml')], 1), - ), -) -def test_validate_config_main(args, expected_output): - assert validate_config_main(args) == expected_output - - @pytest.mark.parametrize( ('config_obj', 'expected'), ( ( @@ -91,39 +77,13 @@ def test_config_valid(config_obj, expected): def test_local_hooks_with_rev_fails(): - config_obj = {'repos': [sample_local_config()]} - config_obj['repos'][0]['rev'] = 'foo' + config_obj = {'repos': [dict(sample_local_config(), rev='foo')]} with pytest.raises(cfgv.ValidationError): cfgv.validate(config_obj, CONFIG_SCHEMA) -@pytest.mark.parametrize( - 'config_obj', ( - {'repos': [{ - 'repo': 'local', - 'hooks': [{ - 'id': 'arg-per-line', - 'name': 'Args per line hook', - 'entry': 'bin/hook.sh', - 'language': 'script', - 'files': '', - 'args': ['hello', 'world'], - }], - }]}, - {'repos': [{ - 'repo': 'local', - 'hooks': [{ - 'id': 'arg-per-line', - 'name': 'Args per line hook', - 'entry': 'bin/hook.sh', - 'language': 'script', - 'files': '', - 'args': ['hello', 'world'], - }], - }]}, - ), -) -def test_config_with_local_hooks_definition_passes(config_obj): +def test_config_with_local_hooks_definition_passes(): + config_obj = {'repos': [sample_local_config()]} cfgv.validate(config_obj, CONFIG_SCHEMA) @@ -135,17 +95,30 @@ def test_config_schema_does_not_contain_defaults(): assert not isinstance(item, cfgv.Optional) -@pytest.mark.parametrize( - ('args', 'expected_output'), - ( - (['.pre-commit-hooks.yaml'], 0), - (['non_existent_file.yaml'], 1), - ([get_resource_path('valid_yaml_but_invalid_manifest.yaml')], 1), - ([get_resource_path('non_parseable_yaml_file.notyaml')], 1), - ), -) -def test_validate_manifest_main(args, expected_output): - assert validate_manifest_main(args) == expected_output +def test_validate_manifest_main_ok(): + assert not validate_manifest_main(('.pre-commit-hooks.yaml',)) + + +def test_validate_config_main_ok(): + assert not validate_config_main(('.pre-commit-config.yaml',)) + + +def test_validate_config_old_list_format_ok(tmpdir): + f = tmpdir.join('cfg.yaml') + f.write('- {repo: meta, hooks: [{id: identity}]}') + assert not validate_config_main((f.strpath,)) + + +@pytest.mark.parametrize('fn', (validate_config_main, validate_manifest_main)) +def test_mains_not_ok(tmpdir, fn): + not_yaml = tmpdir.join('f.notyaml') + not_yaml.write('{') + not_schema = tmpdir.join('notconfig.yaml') + not_schema.write('{}') + + assert fn(('does-not-exist',)) + assert fn((not_yaml.strpath,)) + assert fn((not_schema.strpath,)) @pytest.mark.parametrize( @@ -174,8 +147,6 @@ def test_validate_manifest_main(args, expected_output): ), ( # A regression in 0.13.5: always_run and files are permissible - # together (but meaningless). In a future version upgrade this to - # an error [{ 'id': 'a', 'name': 'b', diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py index df7cb085..c1fceb42 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -1,8 +1,6 @@ from __future__ import unicode_literals -import os.path import pipes -import shutil import pytest @@ -16,10 +14,10 @@ from testing.auto_namedtuple import auto_namedtuple from testing.fixtures import add_config_to_repo from testing.fixtures import make_config_from_repo from testing.fixtures import make_repo +from testing.fixtures import modify_manifest from testing.fixtures import read_config from testing.fixtures import sample_local_config from testing.fixtures import write_config -from testing.util import get_resource_path from testing.util import git_commit @@ -275,12 +273,8 @@ def hook_disappearing_repo(tempdir_factory): path = make_repo(tempdir_factory, 'python_hooks_repo') original_rev = git.head_rev(path) - shutil.copy( - get_resource_path('manifest_without_foo.yaml'), - os.path.join(path, C.MANIFEST_FILE), - ) - cmd_output('git', 'add', '.', cwd=path) - git_commit(cwd=path) + with modify_manifest(path) as manifest: + manifest[0]['id'] = 'bar' yield auto_namedtuple(path=path, original_rev=original_rev) diff --git a/tests/commands/migrate_config_test.py b/tests/commands/migrate_config_test.py index da599f10..8f9153fd 100644 --- a/tests/commands/migrate_config_test.py +++ b/tests/commands/migrate_config_test.py @@ -129,7 +129,6 @@ def test_migrate_config_sha_to_rev(tmpdir): '- repo: https://github.com/pre-commit/pre-commit-hooks\n' ' sha: v1.2.0\n' ' hooks: []\n' - 'repos:\n' '- repo: https://github.com/pre-commit/pre-commit-hooks\n' ' sha: v1.2.0\n' ' hooks: []\n' @@ -144,7 +143,6 @@ def test_migrate_config_sha_to_rev(tmpdir): '- repo: https://github.com/pre-commit/pre-commit-hooks\n' ' rev: v1.2.0\n' ' hooks: []\n' - 'repos:\n' '- repo: https://github.com/pre-commit/pre-commit-hooks\n' ' rev: v1.2.0\n' ' hooks: []\n'