More miscellaneous cleanup

This commit is contained in:
Anthony Sottile 2020-01-12 21:17:59 -08:00
parent 489d9f9926
commit df40e862f4
33 changed files with 209 additions and 296 deletions

View file

@ -34,7 +34,7 @@ def test_is_script():
def test_is_previous_pre_commit(tmpdir):
f = tmpdir.join('foo')
f.write(PRIOR_HASHES[0] + '\n')
f.write(f'{PRIOR_HASHES[0]}\n')
assert is_our_script(f.strpath)
@ -129,11 +129,11 @@ FILES_CHANGED = (
NORMAL_PRE_COMMIT_RUN = re.compile(
r'^\[INFO\] Initializing environment for .+\.\n'
r'Bash hook\.+Passed\n'
r'\[master [a-f0-9]{7}\] commit!\n' +
FILES_CHANGED +
r' create mode 100644 foo\n$',
fr'^\[INFO\] Initializing environment for .+\.\n'
fr'Bash hook\.+Passed\n'
fr'\[master [a-f0-9]{{7}}\] commit!\n'
fr'{FILES_CHANGED}'
fr' create mode 100644 foo\n$',
)
@ -296,10 +296,10 @@ def test_failing_hooks_returns_nonzero(tempdir_factory, store):
EXISTING_COMMIT_RUN = re.compile(
r'^legacy hook\n'
r'\[master [a-f0-9]{7}\] commit!\n' +
FILES_CHANGED +
r' create mode 100644 baz\n$',
fr'^legacy hook\n'
fr'\[master [a-f0-9]{{7}}\] commit!\n'
fr'{FILES_CHANGED}'
fr' create mode 100644 baz\n$',
)
@ -453,10 +453,10 @@ def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
PRE_INSTALLED = re.compile(
r'Bash hook\.+Passed\n'
r'\[master [a-f0-9]{7}\] commit!\n' +
FILES_CHANGED +
r' create mode 100644 foo\n$',
fr'Bash hook\.+Passed\n'
fr'\[master [a-f0-9]{{7}}\] commit!\n'
fr'{FILES_CHANGED}'
fr' create mode 100644 foo\n$',
)

View file

@ -7,10 +7,13 @@ from unittest import mock
import pytest
import pre_commit.constants as C
from pre_commit import color
from pre_commit.commands.install_uninstall import install
from pre_commit.commands.run import _compute_cols
from pre_commit.commands.run import _full_msg
from pre_commit.commands.run import _get_skips
from pre_commit.commands.run import _has_unmerged_paths
from pre_commit.commands.run import _start_msg
from pre_commit.commands.run import Classifier
from pre_commit.commands.run import filter_by_include_exclude
from pre_commit.commands.run import run
@ -29,6 +32,62 @@ from testing.util import git_commit
from testing.util import run_opts
def test_start_msg():
ret = _start_msg(start='start', end_len=5, cols=15)
# 4 dots: 15 - 5 - 5 - 1
assert ret == 'start....'
def test_full_msg():
ret = _full_msg(
start='start',
end_msg='end',
end_color='',
use_color=False,
cols=15,
)
# 6 dots: 15 - 5 - 3 - 1
assert ret == 'start......end\n'
def test_full_msg_with_color():
ret = _full_msg(
start='start',
end_msg='end',
end_color=color.RED,
use_color=True,
cols=15,
)
# 6 dots: 15 - 5 - 3 - 1
assert ret == f'start......{color.RED}end{color.NORMAL}\n'
def test_full_msg_with_postfix():
ret = _full_msg(
start='start',
postfix='post ',
end_msg='end',
end_color='',
use_color=False,
cols=20,
)
# 6 dots: 20 - 5 - 5 - 3 - 1
assert ret == 'start......post end\n'
def test_full_msg_postfix_not_colored():
ret = _full_msg(
start='start',
postfix='post ',
end_msg='end',
end_color=color.RED,
use_color=True,
cols=20,
)
# 6 dots: 20 - 5 - 5 - 3 - 1
assert ret == f'start......post {color.RED}end{color.NORMAL}\n'
@pytest.fixture
def repo_with_passing_hook(tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
@ -173,7 +232,7 @@ def test_global_exclude(cap_out, store, in_git_dir):
ret, printed = _do_run(cap_out, store, str(in_git_dir), opts)
assert ret == 0
# Does not contain foo.py since it was excluded
assert printed.startswith(b'identity' + b'.' * 65 + b'Passed\n')
assert printed.startswith(f'identity{"." * 65}Passed\n'.encode())
assert printed.endswith(b'\n\n.pre-commit-config.yaml\nbar.py\n\n')
@ -190,7 +249,7 @@ def test_global_files(cap_out, store, in_git_dir):
ret, printed = _do_run(cap_out, store, str(in_git_dir), opts)
assert ret == 0
# Does not contain foo.py since it was excluded
assert printed.startswith(b'identity' + b'.' * 65 + b'Passed\n')
assert printed.startswith(f'identity{"." * 65}Passed\n'.encode())
assert printed.endswith(b'\n\nbar.py\n\n')

View file

@ -21,7 +21,7 @@ def try_repo_opts(repo, ref=None, **kwargs):
def _get_out(cap_out):
out = re.sub(r'\[INFO\].+\n', '', cap_out.get())
start, using_config, config, rest = out.split('=' * 79 + '\n')
start, using_config, config, rest = out.split(f'{"=" * 79}\n')
assert using_config == 'Using config:\n'
return start, config, rest