summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2021-08-12 12:03:27 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2021-08-12 12:03:27 +0000
commitd46c288cfdeadb070fa71c744b9970b3f1c7a623 (patch)
tree729e2a3fd230cfaca2e921e06fbefa9f586a8f98 /tests
parentReleasing debian version 2.13.0-1. (diff)
downloadpre-commit-d46c288cfdeadb070fa71c744b9970b3f1c7a623.tar.xz
pre-commit-d46c288cfdeadb070fa71c744b9970b3f1c7a623.zip
Merging upstream version 2.14.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--tests/clientlib_test.py4
-rw-r--r--tests/commands/install_uninstall_test.py17
-rw-r--r--tests/commands/run_test.py1
-rw-r--r--tests/languages/docker_test.py92
-rw-r--r--tests/repository_test.py2
5 files changed, 78 insertions, 38 deletions
diff --git a/tests/clientlib_test.py b/tests/clientlib_test.py
index ff3cce3..09bdb3e 100644
--- a/tests/clientlib_test.py
+++ b/tests/clientlib_test.py
@@ -427,7 +427,7 @@ def test_minimum_pre_commit_version_passing():
@pytest.mark.parametrize('schema', (CONFIG_SCHEMA, CONFIG_REPO_DICT))
def test_warn_additional(schema):
allowed_keys = {item.key for item in schema.items if hasattr(item, 'key')}
- warn_additional, = [
+ warn_additional, = (
x for x in schema.items if isinstance(x, cfgv.WarnAdditionalKeys)
- ]
+ )
assert allowed_keys == set(warn_additional.keys)
diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py
index bd28654..314b8b9 100644
--- a/tests/commands/install_uninstall_test.py
+++ b/tests/commands/install_uninstall_test.py
@@ -39,7 +39,7 @@ def test_is_script():
def test_is_previous_pre_commit(tmpdir):
f = tmpdir.join('foo')
- f.write(f'{PRIOR_HASHES[0]}\n')
+ f.write(f'{PRIOR_HASHES[0].decode()}\n')
assert is_our_script(f.strpath)
@@ -390,6 +390,19 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
NORMAL_PRE_COMMIT_RUN.assert_matches(output[len('legacy hook\n'):])
+def test_install_with_existing_non_utf8_script(tmpdir, store):
+ cmd_output('git', 'init', str(tmpdir))
+ tmpdir.join('.git/hooks').ensure_dir()
+ tmpdir.join('.git/hooks/pre-commit').write_binary(
+ b'#!/usr/bin/env bash\n'
+ b'# garbage: \xa0\xef\x12\xf2\n'
+ b'echo legacy hook\n',
+ )
+
+ with tmpdir.as_cwd():
+ assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
+
+
FAIL_OLD_HOOK = re_assert.Matches(
r'fail!\n'
r'\[INFO\] Initializing environment for .+\.\n'
@@ -460,7 +473,7 @@ def test_replace_old_commit_script(tempdir_factory, store):
# Install a script that looks like our old script
pre_commit_contents = resource_text('hook-tmpl')
new_contents = pre_commit_contents.replace(
- CURRENT_HASH, PRIOR_HASHES[-1],
+ CURRENT_HASH.decode(), PRIOR_HASHES[-1].decode(),
)
os.makedirs(os.path.join(path, '.git/hooks'), exist_ok=True)
diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py
index e184340..da7569e 100644
--- a/tests/commands/run_test.py
+++ b/tests/commands/run_test.py
@@ -487,6 +487,7 @@ def test_all_push_options_ok(cap_out, store, repo_with_passing_hook):
args = run_opts(
from_ref='master', to_ref='master',
remote_branch='master',
+ local_branch='master',
remote_name='origin', remote_url='https://example.com/repo',
)
ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args)
diff --git a/tests/languages/docker_test.py b/tests/languages/docker_test.py
index 01b5e27..ec6bb83 100644
--- a/tests/languages/docker_test.py
+++ b/tests/languages/docker_test.py
@@ -8,6 +8,42 @@ from unittest import mock
import pytest
from pre_commit.languages import docker
+from pre_commit.util import CalledProcessError
+
+DOCKER_CGROUP_EXAMPLE = b'''\
+12:hugetlb:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
+11:blkio:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
+10:freezer:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
+9:cpu,cpuacct:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
+8:pids:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
+7:rdma:/
+6:net_cls,net_prio:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
+5:cpuset:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
+4:devices:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
+3:memory:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
+2:perf_event:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
+1:name=systemd:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
+0::/system.slice/containerd.service
+''' # noqa: E501
+
+# The ID should match the above cgroup example.
+CONTAINER_ID = 'c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7' # noqa: E501
+
+NON_DOCKER_CGROUP_EXAMPLE = b'''\
+12:perf_event:/
+11:hugetlb:/
+10:devices:/
+9:blkio:/
+8:rdma:/
+7:cpuset:/
+6:cpu,cpuacct:/
+5:freezer:/
+4:memory:/
+3:pids:/
+2:net_cls,net_prio:/
+1:name=systemd:/init.scope
+0::/init.scope
+'''
def test_docker_fallback_user():
@@ -37,45 +73,25 @@ def _mock_open(data):
def test_in_docker_docker_in_file():
- docker_cgroup_example = b'''\
-12:hugetlb:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
-11:blkio:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
-10:freezer:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
-9:cpu,cpuacct:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
-8:pids:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
-7:rdma:/
-6:net_cls,net_prio:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
-5:cpuset:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
-4:devices:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
-3:memory:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
-2:perf_event:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
-1:name=systemd:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
-0::/system.slice/containerd.service
-''' # noqa: E501
- with _mock_open(docker_cgroup_example):
+ with _mock_open(DOCKER_CGROUP_EXAMPLE):
assert docker._is_in_docker() is True
def test_in_docker_docker_not_in_file():
- non_docker_cgroup_example = b'''\
-12:perf_event:/
-11:hugetlb:/
-10:devices:/
-9:blkio:/
-8:rdma:/
-7:cpuset:/
-6:cpu,cpuacct:/
-5:freezer:/
-4:memory:/
-3:pids:/
-2:net_cls,net_prio:/
-1:name=systemd:/init.scope
-0::/init.scope
-'''
- with _mock_open(non_docker_cgroup_example):
+ with _mock_open(NON_DOCKER_CGROUP_EXAMPLE):
assert docker._is_in_docker() is False
+def test_get_container_id():
+ with _mock_open(DOCKER_CGROUP_EXAMPLE):
+ assert docker._get_container_id() == CONTAINER_ID
+
+
+def test_get_container_id_failure():
+ with _mock_open(b''), pytest.raises(RuntimeError):
+ docker._get_container_id()
+
+
def test_get_docker_path_not_in_docker_returns_same():
with mock.patch.object(docker, '_is_in_docker', return_value=False):
assert docker._get_docker_path('abc') == 'abc'
@@ -84,7 +100,10 @@ def test_get_docker_path_not_in_docker_returns_same():
@pytest.fixture
def in_docker():
with mock.patch.object(docker, '_is_in_docker', return_value=True):
- yield
+ with mock.patch.object(
+ docker, '_get_container_id', return_value=CONTAINER_ID,
+ ):
+ yield
def _linux_commonpath():
@@ -153,3 +172,10 @@ def test_get_docker_path_in_docker_windows(in_docker):
path = r'c:\folder\test\something'
expected = r'c:\users\user\test\something'
assert docker._get_docker_path(path) == expected
+
+
+def test_get_docker_path_in_docker_docker_in_docker(in_docker):
+ # won't be able to discover "self" container in true docker-in-docker
+ err = CalledProcessError(1, (), 0, b'', b'')
+ with mock.patch.object(docker, 'cmd_output_b', side_effect=err):
+ assert docker._get_docker_path('/project') == '/project'
diff --git a/tests/repository_test.py b/tests/repository_test.py
index b6f7fb2..af829c2 100644
--- a/tests/repository_test.py
+++ b/tests/repository_test.py
@@ -51,7 +51,7 @@ def _get_hook_no_install(repo_config, store, hook_id):
config = cfgv.validate(config, CONFIG_SCHEMA)
config = cfgv.apply_defaults(config, CONFIG_SCHEMA)
hooks = all_hooks(config, store)
- hook, = [hook for hook in hooks if hook.id == hook_id]
+ hook, = (hook for hook in hooks if hook.id == hook_id)
return hook