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

@ -6,13 +6,12 @@ import pytest
from pre_commit import envcontext
from pre_commit.color import format_color
from pre_commit.color import GREEN
from pre_commit.color import InvalidColorSetting
from pre_commit.color import use_color
@pytest.mark.parametrize(
('in_text', 'in_color', 'in_use_color', 'expected'), (
('foo', GREEN, True, f'{GREEN}foo\033[0m'),
('foo', GREEN, True, f'{GREEN}foo\033[m'),
('foo', GREEN, False, 'foo'),
),
)
@ -56,5 +55,5 @@ def test_use_color_dumb_term():
def test_use_color_raises_if_given_shenanigans():
with pytest.raises(InvalidColorSetting):
with pytest.raises(ValueError):
use_color('herpaderp')

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

View file

@ -140,7 +140,6 @@ def test_error_handler_no_tty(tempdir_factory):
ret, out, _ = cmd_output_mocked_pre_commit_home(
sys.executable,
'-c',
'from __future__ import unicode_literals\n'
'from pre_commit.error_handler import error_handler\n'
'with error_handler():\n'
' raise ValueError("\\u2603")\n',

View file

@ -16,7 +16,7 @@ def test_norm_version_expanduser():
expected_path = fr'{home}\python343'
else: # pragma: windows no cover
path = '~/.pyenv/versions/3.4.3/bin/python'
expected_path = home + '/.pyenv/versions/3.4.3/bin/python'
expected_path = f'{home}/.pyenv/versions/3.4.3/bin/python'
result = python.norm_version(path)
assert result == expected_path

View file

@ -12,7 +12,7 @@ def test_logging_handler_color(cap_out):
handler = LoggingHandler(True)
handler.emit(_log_record('hi', logging.WARNING))
ret = cap_out.get()
assert ret == color.YELLOW + '[WARNING]' + color.NORMAL + ' hi\n'
assert ret == f'{color.YELLOW}[WARNING]{color.NORMAL} hi\n'
def test_logging_handler_no_color(cap_out):

View file

@ -1,85 +1,9 @@
from unittest import mock
import io
import pytest
from pre_commit import color
from pre_commit import output
@pytest.mark.parametrize(
'kwargs',
(
# both end_msg and end_len
{'end_msg': 'end', 'end_len': 1, 'end_color': '', 'use_color': True},
# Neither end_msg nor end_len
{},
# Neither color option for end_msg
{'end_msg': 'end'},
# No use_color for end_msg
{'end_msg': 'end', 'end_color': ''},
# No end_color for end_msg
{'end_msg': 'end', 'use_color': ''},
),
)
def test_get_hook_message_raises(kwargs):
with pytest.raises(AssertionError):
output.get_hook_message('start', **kwargs)
def test_case_with_end_len():
ret = output.get_hook_message('start', end_len=5, cols=15)
assert ret == 'start' + '.' * 4
def test_case_with_end_msg():
ret = output.get_hook_message(
'start',
end_msg='end',
end_color='',
use_color=False,
cols=15,
)
assert ret == 'start' + '.' * 6 + 'end' + '\n'
def test_case_with_end_msg_using_color():
ret = output.get_hook_message(
'start',
end_msg='end',
end_color=color.RED,
use_color=True,
cols=15,
)
assert ret == 'start' + '.' * 6 + color.RED + 'end' + color.NORMAL + '\n'
def test_case_with_postfix_message():
ret = output.get_hook_message(
'start',
postfix='post ',
end_msg='end',
end_color='',
use_color=False,
cols=20,
)
assert ret == 'start' + '.' * 6 + 'post ' + 'end' + '\n'
def test_make_sure_postfix_is_not_colored():
ret = output.get_hook_message(
'start',
postfix='post ',
end_msg='end',
end_color=color.RED,
use_color=True,
cols=20,
)
assert ret == (
'start' + '.' * 6 + 'post ' + color.RED + 'end' + color.NORMAL + '\n'
)
def test_output_write_writes():
fake_stream = mock.Mock()
output.write('hello world', fake_stream)
assert fake_stream.write.call_count == 1
stream = io.BytesIO()
output.write('hello world', stream)
assert stream.getvalue() == b'hello world'

View file

@ -94,9 +94,9 @@ def test_foo_something_unstaged_diff_color_always(foo_staged, patch_dir):
def test_foo_both_modify_non_conflicting(foo_staged, patch_dir):
with open(foo_staged.foo_filename, 'w') as foo_file:
foo_file.write(FOO_CONTENTS + '9\n')
foo_file.write(f'{FOO_CONTENTS}9\n')
_test_foo_state(foo_staged, FOO_CONTENTS + '9\n', 'AM')
_test_foo_state(foo_staged, f'{FOO_CONTENTS}9\n', 'AM')
with staged_files_only(patch_dir):
_test_foo_state(foo_staged)
@ -107,7 +107,7 @@ def test_foo_both_modify_non_conflicting(foo_staged, patch_dir):
_test_foo_state(foo_staged, FOO_CONTENTS.replace('1', 'a'), 'AM')
_test_foo_state(foo_staged, FOO_CONTENTS.replace('1', 'a') + '9\n', 'AM')
_test_foo_state(foo_staged, f'{FOO_CONTENTS.replace("1", "a")}9\n', 'AM')
def test_foo_both_modify_conflicting(foo_staged, patch_dir):