summaryrefslogtreecommitdiffstats
path: root/tests/commands
diff options
context:
space:
mode:
Diffstat (limited to 'tests/commands')
-rw-r--r--tests/commands/autoupdate_test.py5
-rw-r--r--tests/commands/install_uninstall_test.py151
-rw-r--r--tests/commands/migrate_config_test.py13
-rw-r--r--tests/commands/validate_config_test.py64
-rw-r--r--tests/commands/validate_manifest_test.py18
5 files changed, 177 insertions, 74 deletions
diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py
index 3806b0e..4bcb5d8 100644
--- a/tests/commands/autoupdate_test.py
+++ b/tests/commands/autoupdate_test.py
@@ -4,12 +4,11 @@ import shlex
from unittest import mock
import pytest
-import yaml
import pre_commit.constants as C
from pre_commit import envcontext
from pre_commit import git
-from pre_commit import util
+from pre_commit import yaml
from pre_commit.commands.autoupdate import _check_hooks_still_exist_at_rev
from pre_commit.commands.autoupdate import autoupdate
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
@@ -206,7 +205,7 @@ def test_autoupdate_with_core_useBuiltinFSMonitor(out_of_date, tmpdir, store):
def test_autoupdate_pure_yaml(out_of_date, tmpdir, store):
- with mock.patch.object(util, 'Dumper', yaml.SafeDumper):
+ with mock.patch.object(yaml, 'Dumper', yaml.yaml.SafeDumper):
test_autoupdate_out_of_date_repo(out_of_date, tmpdir, store)
diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py
index 379c03a..a1ecda8 100644
--- a/tests/commands/install_uninstall_test.py
+++ b/tests/commands/install_uninstall_test.py
@@ -248,7 +248,7 @@ def test_install_idempotent(tempdir_factory, store):
def _path_without_us():
# Choose a path which *probably* doesn't include us
env = dict(os.environ)
- exe = find_executable('pre-commit', _environ=env)
+ exe = find_executable('pre-commit', env=env)
while exe:
parts = env['PATH'].split(os.pathsep)
after = [
@@ -258,7 +258,7 @@ def _path_without_us():
if parts == after:
raise AssertionError(exe, parts)
env['PATH'] = os.pathsep.join(after)
- exe = find_executable('pre-commit', _environ=env)
+ exe = find_executable('pre-commit', env=env)
return env['PATH']
@@ -276,18 +276,19 @@ def test_environment_not_sourced(tempdir_factory, store):
# Use a specific homedir to ignore --user installs
homedir = tempdir_factory.get()
- ret, out = git_commit(
- env={
- 'HOME': homedir,
- 'PATH': _path_without_us(),
- # Git needs this to make a commit
- 'GIT_AUTHOR_NAME': os.environ['GIT_AUTHOR_NAME'],
- 'GIT_COMMITTER_NAME': os.environ['GIT_COMMITTER_NAME'],
- 'GIT_AUTHOR_EMAIL': os.environ['GIT_AUTHOR_EMAIL'],
- 'GIT_COMMITTER_EMAIL': os.environ['GIT_COMMITTER_EMAIL'],
- },
- check=False,
- )
+ env = {
+ 'HOME': homedir,
+ 'PATH': _path_without_us(),
+ # Git needs this to make a commit
+ 'GIT_AUTHOR_NAME': os.environ['GIT_AUTHOR_NAME'],
+ 'GIT_COMMITTER_NAME': os.environ['GIT_COMMITTER_NAME'],
+ 'GIT_AUTHOR_EMAIL': os.environ['GIT_AUTHOR_EMAIL'],
+ 'GIT_COMMITTER_EMAIL': os.environ['GIT_COMMITTER_EMAIL'],
+ }
+ if os.name == 'nt' and 'PATHEXT' in os.environ: # pragma: no cover
+ env['PATHEXT'] = os.environ['PATHEXT']
+
+ ret, out = git_commit(env=env, check=False)
assert ret == 1
assert out == (
'`pre-commit` not found. '
@@ -739,20 +740,22 @@ def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
def test_post_commit_integration(tempdir_factory, store):
path = git_dir(tempdir_factory)
- config = [
- {
- 'repo': 'local',
- 'hooks': [{
- 'id': 'post-commit',
- 'name': 'Post commit',
- 'entry': 'touch post-commit.tmp',
- 'language': 'system',
- 'always_run': True,
- 'verbose': True,
- 'stages': ['post-commit'],
- }],
- },
- ]
+ config = {
+ 'repos': [
+ {
+ 'repo': 'local',
+ 'hooks': [{
+ 'id': 'post-commit',
+ 'name': 'Post commit',
+ 'entry': 'touch post-commit.tmp',
+ 'language': 'system',
+ 'always_run': True,
+ 'verbose': True,
+ 'stages': ['post-commit'],
+ }],
+ },
+ ],
+ }
write_config(path, config)
with cwd(path):
_get_commit_output(tempdir_factory)
@@ -765,20 +768,22 @@ def test_post_commit_integration(tempdir_factory, store):
def test_post_merge_integration(tempdir_factory, store):
path = git_dir(tempdir_factory)
- config = [
- {
- 'repo': 'local',
- 'hooks': [{
- 'id': 'post-merge',
- 'name': 'Post merge',
- 'entry': 'touch post-merge.tmp',
- 'language': 'system',
- 'always_run': True,
- 'verbose': True,
- 'stages': ['post-merge'],
- }],
- },
- ]
+ config = {
+ 'repos': [
+ {
+ 'repo': 'local',
+ 'hooks': [{
+ 'id': 'post-merge',
+ 'name': 'Post merge',
+ 'entry': 'touch post-merge.tmp',
+ 'language': 'system',
+ 'always_run': True,
+ 'verbose': True,
+ 'stages': ['post-merge'],
+ }],
+ },
+ ],
+ }
write_config(path, config)
with cwd(path):
# create a simple diamond of commits for a non-trivial merge
@@ -807,20 +812,22 @@ def test_post_merge_integration(tempdir_factory, store):
def test_post_rewrite_integration(tempdir_factory, store):
path = git_dir(tempdir_factory)
- config = [
- {
- 'repo': 'local',
- 'hooks': [{
- 'id': 'post-rewrite',
- 'name': 'Post rewrite',
- 'entry': 'touch post-rewrite.tmp',
- 'language': 'system',
- 'always_run': True,
- 'verbose': True,
- 'stages': ['post-rewrite'],
- }],
- },
- ]
+ config = {
+ 'repos': [
+ {
+ 'repo': 'local',
+ 'hooks': [{
+ 'id': 'post-rewrite',
+ 'name': 'Post rewrite',
+ 'entry': 'touch post-rewrite.tmp',
+ 'language': 'system',
+ 'always_run': True,
+ 'verbose': True,
+ 'stages': ['post-rewrite'],
+ }],
+ },
+ ],
+ }
write_config(path, config)
with cwd(path):
open('init', 'a').close()
@@ -836,21 +843,23 @@ def test_post_rewrite_integration(tempdir_factory, store):
def test_post_checkout_integration(tempdir_factory, store):
path = git_dir(tempdir_factory)
- config = [
- {
- 'repo': 'local',
- 'hooks': [{
- 'id': 'post-checkout',
- 'name': 'Post checkout',
- 'entry': 'bash -c "echo ${PRE_COMMIT_TO_REF}"',
- 'language': 'system',
- 'always_run': True,
- 'verbose': True,
- 'stages': ['post-checkout'],
- }],
- },
- {'repo': 'meta', 'hooks': [{'id': 'identity'}]},
- ]
+ config = {
+ 'repos': [
+ {
+ 'repo': 'local',
+ 'hooks': [{
+ 'id': 'post-checkout',
+ 'name': 'Post checkout',
+ 'entry': 'bash -c "echo ${PRE_COMMIT_TO_REF}"',
+ 'language': 'system',
+ 'always_run': True,
+ 'verbose': True,
+ 'stages': ['post-checkout'],
+ }],
+ },
+ {'repo': 'meta', 'hooks': [{'id': 'identity'}]},
+ ],
+ }
write_config(path, config)
with cwd(path):
cmd_output('git', 'add', '.')
diff --git a/tests/commands/migrate_config_test.py b/tests/commands/migrate_config_test.py
index b80244e..fca1ad9 100644
--- a/tests/commands/migrate_config_test.py
+++ b/tests/commands/migrate_config_test.py
@@ -1,6 +1,9 @@
from __future__ import annotations
+import pytest
+
import pre_commit.constants as C
+from pre_commit.clientlib import InvalidConfigError
from pre_commit.commands.migrate_config import migrate_config
@@ -129,3 +132,13 @@ def test_migrate_config_sha_to_rev(tmpdir):
' rev: v1.2.0\n'
' hooks: []\n'
)
+
+
+def test_migrate_config_invalid_yaml(tmpdir):
+ contents = '['
+ cfg = tmpdir.join(C.CONFIG_FILE)
+ cfg.write(contents)
+ with tmpdir.as_cwd(), pytest.raises(InvalidConfigError) as excinfo:
+ migrate_config(C.CONFIG_FILE)
+ expected = '\n==> File .pre-commit-config.yaml\n=====> '
+ assert str(excinfo.value).startswith(expected)
diff --git a/tests/commands/validate_config_test.py b/tests/commands/validate_config_test.py
new file mode 100644
index 0000000..a475cd8
--- /dev/null
+++ b/tests/commands/validate_config_test.py
@@ -0,0 +1,64 @@
+from __future__ import annotations
+
+import logging
+
+from pre_commit.commands.validate_config import validate_config
+
+
+def test_validate_config_ok():
+ assert not validate_config(('.pre-commit-config.yaml',))
+
+
+def test_validate_warn_on_unknown_keys_at_repo_level(tmpdir, caplog):
+ f = tmpdir.join('cfg.yaml')
+ f.write(
+ 'repos:\n'
+ '- repo: https://gitlab.com/pycqa/flake8\n'
+ ' rev: 3.7.7\n'
+ ' hooks:\n'
+ ' - id: flake8\n'
+ ' args: [--some-args]\n',
+ )
+ ret_val = validate_config((f.strpath,))
+ assert not ret_val
+ assert caplog.record_tuples == [
+ (
+ 'pre_commit',
+ logging.WARNING,
+ 'Unexpected key(s) present on https://gitlab.com/pycqa/flake8: '
+ 'args',
+ ),
+ ]
+
+
+def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog):
+ f = tmpdir.join('cfg.yaml')
+ f.write(
+ 'repos:\n'
+ '- repo: https://gitlab.com/pycqa/flake8\n'
+ ' rev: 3.7.7\n'
+ ' hooks:\n'
+ ' - id: flake8\n'
+ 'foo:\n'
+ ' id: 1.0.0\n',
+ )
+ ret_val = validate_config((f.strpath,))
+ assert not ret_val
+ assert caplog.record_tuples == [
+ (
+ 'pre_commit',
+ logging.WARNING,
+ 'Unexpected key(s) present at root: foo',
+ ),
+ ]
+
+
+def test_mains_not_ok(tmpdir):
+ not_yaml = tmpdir.join('f.notyaml')
+ not_yaml.write('{')
+ not_schema = tmpdir.join('notconfig.yaml')
+ not_schema.write('{}')
+
+ assert validate_config(('does-not-exist',))
+ assert validate_config((not_yaml.strpath,))
+ assert validate_config((not_schema.strpath,))
diff --git a/tests/commands/validate_manifest_test.py b/tests/commands/validate_manifest_test.py
new file mode 100644
index 0000000..a4bc8ac
--- /dev/null
+++ b/tests/commands/validate_manifest_test.py
@@ -0,0 +1,18 @@
+from __future__ import annotations
+
+from pre_commit.commands.validate_manifest import validate_manifest
+
+
+def test_validate_manifest_ok():
+ assert not validate_manifest(('.pre-commit-hooks.yaml',))
+
+
+def test_not_ok(tmpdir):
+ not_yaml = tmpdir.join('f.notyaml')
+ not_yaml.write('{')
+ not_schema = tmpdir.join('notconfig.yaml')
+ not_schema.write('{}')
+
+ assert validate_manifest(('does-not-exist',))
+ assert validate_manifest((not_yaml.strpath,))
+ assert validate_manifest((not_schema.strpath,))