mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-31 18:46:53 +00:00
Merge remote-tracking branch 'origin/uk/no_ticket/forbid_artcles' into uk/no_ticket/forbid_artcles
This commit is contained in:
commit
2dff30a2db
21 changed files with 142 additions and 472 deletions
|
|
@ -38,11 +38,6 @@ t1 = ()
|
|||
'''
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def visitor():
|
||||
return Visitor()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('expression', 'calls'),
|
||||
[
|
||||
|
|
@ -85,7 +80,8 @@ def visitor():
|
|||
('builtins.tuple()', []),
|
||||
],
|
||||
)
|
||||
def test_non_dict_exprs(visitor, expression, calls):
|
||||
def test_non_dict_exprs(expression, calls):
|
||||
visitor = Visitor(ignore=set())
|
||||
visitor.visit(ast.parse(expression))
|
||||
assert visitor.builtin_type_calls == calls
|
||||
|
||||
|
|
@ -102,7 +98,8 @@ def test_non_dict_exprs(visitor, expression, calls):
|
|||
('builtins.dict()', []),
|
||||
],
|
||||
)
|
||||
def test_dict_allow_kwargs_exprs(visitor, expression, calls):
|
||||
def test_dict_allow_kwargs_exprs(expression, calls):
|
||||
visitor = Visitor(ignore=set())
|
||||
visitor.visit(ast.parse(expression))
|
||||
assert visitor.builtin_type_calls == calls
|
||||
|
||||
|
|
@ -114,17 +111,18 @@ def test_dict_allow_kwargs_exprs(visitor, expression, calls):
|
|||
('dict(a=1, b=2, c=3)', [Call('dict', 1, 0)]),
|
||||
("dict(**{'a': 1, 'b': 2, 'c': 3})", [Call('dict', 1, 0)]),
|
||||
('builtins.dict()', []),
|
||||
pytest.param('f(dict())', [Call('dict', 1, 2)], id='nested'),
|
||||
],
|
||||
)
|
||||
def test_dict_no_allow_kwargs_exprs(expression, calls):
|
||||
visitor = Visitor(allow_dict_kwargs=False)
|
||||
visitor = Visitor(ignore=set(), allow_dict_kwargs=False)
|
||||
visitor.visit(ast.parse(expression))
|
||||
assert visitor.builtin_type_calls == calls
|
||||
|
||||
|
||||
def test_ignore_constructors():
|
||||
visitor = Visitor(
|
||||
ignore=('complex', 'dict', 'float', 'int', 'list', 'str', 'tuple'),
|
||||
ignore={'complex', 'dict', 'float', 'int', 'list', 'str', 'tuple'},
|
||||
)
|
||||
visitor.visit(ast.parse(BUILTIN_CONSTRUCTORS))
|
||||
assert visitor.builtin_type_calls == []
|
||||
|
|
|
|||
|
|
@ -1,15 +0,0 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from pre_commit_hooks import check_byte_order_marker
|
||||
|
||||
|
||||
def test_failure(tmpdir):
|
||||
f = tmpdir.join('f.txt')
|
||||
f.write_text('ohai', encoding='utf-8-sig')
|
||||
assert check_byte_order_marker.main((str(f),)) == 1
|
||||
|
||||
|
||||
def test_success(tmpdir):
|
||||
f = tmpdir.join('f.txt')
|
||||
f.write_text('ohai', encoding='utf-8')
|
||||
assert check_byte_order_marker.main((str(f),)) == 0
|
||||
|
|
@ -27,10 +27,10 @@ def f1_is_a_conflict_file(tmpdir):
|
|||
|
||||
cmd_output('git', 'clone', str(repo1), str(repo2))
|
||||
|
||||
# Commit in master
|
||||
# Commit in mainline
|
||||
with repo1.as_cwd():
|
||||
repo1_f1.write('parent\n')
|
||||
git_commit('-am', 'master commit2')
|
||||
git_commit('-am', 'mainline commit2')
|
||||
|
||||
# Commit in clone and pull
|
||||
with repo2.as_cwd():
|
||||
|
|
@ -82,10 +82,10 @@ def repository_pending_merge(tmpdir):
|
|||
|
||||
cmd_output('git', 'clone', str(repo1), str(repo2))
|
||||
|
||||
# Commit in master
|
||||
# Commit in mainline
|
||||
with repo1.as_cwd():
|
||||
repo1_f1.write('parent\n')
|
||||
git_commit('-am', 'master commit2')
|
||||
git_commit('-am', 'mainline commit2')
|
||||
|
||||
# Commit in clone and pull without committing
|
||||
with repo2.as_cwd():
|
||||
|
|
@ -112,7 +112,7 @@ def test_merge_conflicts_git(capsys):
|
|||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'contents', (b'<<<<<<< HEAD\n', b'=======\n', b'>>>>>>> master\n'),
|
||||
'contents', (b'<<<<<<< HEAD\n', b'=======\n', b'>>>>>>> main\n'),
|
||||
)
|
||||
def test_merge_conflicts_failing(contents, repository_pending_merge):
|
||||
repository_pending_merge.join('f2').write_binary(contents)
|
||||
|
|
@ -150,7 +150,7 @@ def test_worktree_merge_conflicts(f1_is_a_conflict_file, tmpdir, capsys):
|
|||
cmd_output('git', 'worktree', 'add', str(worktree))
|
||||
with worktree.as_cwd():
|
||||
cmd_output(
|
||||
'git', 'pull', '--no-rebase', 'origin', 'master', retcode=None,
|
||||
'git', 'pull', '--no-rebase', 'origin', 'HEAD', retcode=None,
|
||||
)
|
||||
msg = f1_is_a_conflict_file.join('.git/worktrees/worktree/MERGE_MSG')
|
||||
assert msg.exists()
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@ def test_passing(tmpdir):
|
|||
# tags are ok
|
||||
b'https://github.com/asottile/test/blob/1.0.0/foo%20bar#L1\n'
|
||||
# links to files but not line numbers are ok
|
||||
b'https://github.com/asottile/test/blob/master/foo%20bar\n'
|
||||
b'https://github.com/asottile/test/blob/main/foo%20bar\n'
|
||||
# regression test for overly-greedy regex
|
||||
b'https://github.com/ yes / no ? /blob/master/foo#L1\n',
|
||||
b'https://github.com/ yes / no ? /blob/main/foo#L1\n',
|
||||
)
|
||||
assert not main((str(f),))
|
||||
|
||||
|
|
@ -26,17 +26,15 @@ def test_passing(tmpdir):
|
|||
def test_failing(tmpdir, capsys):
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('f.txt').write_binary(
|
||||
b'https://github.com/asottile/test/blob/master/foo#L1\n'
|
||||
b'https://example.com/asottile/test/blob/master/foo#L1\n'
|
||||
b'https://github.com/asottile/test/blob/main/foo#L1\n'
|
||||
b'https://example.com/asottile/test/blob/main/foo#L1\n',
|
||||
)
|
||||
|
||||
assert main(('f.txt', '--additional-github-domain', 'example.com'))
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == (
|
||||
'f.txt:1:https://github.com/asottile/test/blob/master/foo#L1\n'
|
||||
'f.txt:2:https://example.com/asottile/test/blob/master/foo#L1\n'
|
||||
'f.txt:3:https://example.com/asottile/test/blob/main/foo#L1\n'
|
||||
'f.txt:1:https://github.com/asottile/test/blob/main/foo#L1\n'
|
||||
'f.txt:2:https://example.com/asottile/test/blob/main/foo#L1\n'
|
||||
'\n'
|
||||
'Non-permanent github link detected.\n'
|
||||
'On any page on github press [y] to load a permalink.\n'
|
||||
|
|
|
|||
|
|
@ -67,18 +67,6 @@ from pre_commit_hooks.file_contents_sorter import PASS
|
|||
FAIL,
|
||||
b'Fie\nFoe\nfee\nfum\n',
|
||||
),
|
||||
(
|
||||
b'fee\nFie\nFoe\nfum\n',
|
||||
['--unique', '--ignore-case'],
|
||||
PASS,
|
||||
b'fee\nFie\nFoe\nfum\n',
|
||||
),
|
||||
(
|
||||
b'fee\nfee\nFie\nFoe\nfum\n',
|
||||
['--unique', '--ignore-case'],
|
||||
FAIL,
|
||||
b'fee\nFie\nFoe\nfum\n',
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_integration(input_s, argv, expected_retval, output, tmpdir):
|
||||
|
|
@ -89,3 +77,24 @@ def test_integration(input_s, argv, expected_retval, output, tmpdir):
|
|||
|
||||
assert path.read_binary() == output
|
||||
assert output_retval == expected_retval
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('input_s', 'argv'),
|
||||
(
|
||||
(
|
||||
b'fee\nFie\nFoe\nfum\n',
|
||||
['--unique', '--ignore-case'],
|
||||
),
|
||||
(
|
||||
b'fee\nfee\nFie\nFoe\nfum\n',
|
||||
['--unique', '--ignore-case'],
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_integration_invalid_args(input_s, argv, tmpdir):
|
||||
path = tmpdir.join('file.txt')
|
||||
path.write_binary(input_s)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
main([str(path)] + argv)
|
||||
|
|
|
|||
|
|
@ -1,161 +0,0 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.fix_encoding_pragma import _normalize_pragma
|
||||
from pre_commit_hooks.fix_encoding_pragma import fix_encoding_pragma
|
||||
from pre_commit_hooks.fix_encoding_pragma import main
|
||||
|
||||
|
||||
def test_integration_inserting_pragma(tmpdir):
|
||||
path = tmpdir.join('foo.py')
|
||||
path.write_binary(b'import httplib\n')
|
||||
|
||||
assert main((str(path),)) == 1
|
||||
|
||||
assert path.read_binary() == (
|
||||
b'# -*- coding: utf-8 -*-\n'
|
||||
b'import httplib\n'
|
||||
)
|
||||
|
||||
|
||||
def test_integration_ok(tmpdir):
|
||||
path = tmpdir.join('foo.py')
|
||||
path.write_binary(b'# -*- coding: utf-8 -*-\nx = 1\n')
|
||||
assert main((str(path),)) == 0
|
||||
|
||||
|
||||
def test_integration_remove(tmpdir):
|
||||
path = tmpdir.join('foo.py')
|
||||
path.write_binary(b'# -*- coding: utf-8 -*-\nx = 1\n')
|
||||
|
||||
assert main((str(path), '--remove')) == 1
|
||||
|
||||
assert path.read_binary() == b'x = 1\n'
|
||||
|
||||
|
||||
def test_integration_remove_ok(tmpdir):
|
||||
path = tmpdir.join('foo.py')
|
||||
path.write_binary(b'x = 1\n')
|
||||
assert main((str(path), '--remove')) == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'input_str',
|
||||
(
|
||||
b'',
|
||||
(
|
||||
b'# -*- coding: utf-8 -*-\n'
|
||||
b'x = 1\n'
|
||||
),
|
||||
(
|
||||
b'#!/usr/bin/env python\n'
|
||||
b'# -*- coding: utf-8 -*-\n'
|
||||
b'foo = "bar"\n'
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_ok_inputs(input_str):
|
||||
bytesio = io.BytesIO(input_str)
|
||||
assert fix_encoding_pragma(bytesio) == 0
|
||||
bytesio.seek(0)
|
||||
assert bytesio.read() == input_str
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('input_str', 'output'),
|
||||
(
|
||||
(
|
||||
b'import httplib\n',
|
||||
b'# -*- coding: utf-8 -*-\n'
|
||||
b'import httplib\n',
|
||||
),
|
||||
(
|
||||
b'#!/usr/bin/env python\n'
|
||||
b'x = 1\n',
|
||||
b'#!/usr/bin/env python\n'
|
||||
b'# -*- coding: utf-8 -*-\n'
|
||||
b'x = 1\n',
|
||||
),
|
||||
(
|
||||
b'#coding=utf-8\n'
|
||||
b'x = 1\n',
|
||||
b'# -*- coding: utf-8 -*-\n'
|
||||
b'x = 1\n',
|
||||
),
|
||||
(
|
||||
b'#!/usr/bin/env python\n'
|
||||
b'#coding=utf8\n'
|
||||
b'x = 1\n',
|
||||
b'#!/usr/bin/env python\n'
|
||||
b'# -*- coding: utf-8 -*-\n'
|
||||
b'x = 1\n',
|
||||
),
|
||||
# These should each get truncated
|
||||
(b'#coding: utf-8\n', b''),
|
||||
(b'# -*- coding: utf-8 -*-\n', b''),
|
||||
(b'#!/usr/bin/env python\n', b''),
|
||||
(b'#!/usr/bin/env python\n#coding: utf8\n', b''),
|
||||
(b'#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n', b''),
|
||||
),
|
||||
)
|
||||
def test_not_ok_inputs(input_str, output):
|
||||
bytesio = io.BytesIO(input_str)
|
||||
assert fix_encoding_pragma(bytesio) == 1
|
||||
bytesio.seek(0)
|
||||
assert bytesio.read() == output
|
||||
|
||||
|
||||
def test_ok_input_alternate_pragma():
|
||||
input_s = b'# coding: utf-8\nx = 1\n'
|
||||
bytesio = io.BytesIO(input_s)
|
||||
ret = fix_encoding_pragma(bytesio, expected_pragma=b'# coding: utf-8')
|
||||
assert ret == 0
|
||||
bytesio.seek(0)
|
||||
assert bytesio.read() == input_s
|
||||
|
||||
|
||||
def test_not_ok_input_alternate_pragma():
|
||||
bytesio = io.BytesIO(b'x = 1\n')
|
||||
ret = fix_encoding_pragma(bytesio, expected_pragma=b'# coding: utf-8')
|
||||
assert ret == 1
|
||||
bytesio.seek(0)
|
||||
assert bytesio.read() == b'# coding: utf-8\nx = 1\n'
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('input_s', 'expected'),
|
||||
(
|
||||
('# coding: utf-8', b'# coding: utf-8'),
|
||||
# trailing whitespace
|
||||
('# coding: utf-8\n', b'# coding: utf-8'),
|
||||
),
|
||||
)
|
||||
def test_normalize_pragma(input_s, expected):
|
||||
assert _normalize_pragma(input_s) == expected
|
||||
|
||||
|
||||
def test_integration_alternate_pragma(tmpdir, capsys):
|
||||
f = tmpdir.join('f.py')
|
||||
f.write('x = 1\n')
|
||||
|
||||
pragma = '# coding: utf-8'
|
||||
assert main((str(f), '--pragma', pragma)) == 1
|
||||
assert f.read() == '# coding: utf-8\nx = 1\n'
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == f'Added `# coding: utf-8` to {str(f)}\n'
|
||||
|
||||
|
||||
def test_crlf_ok(tmpdir):
|
||||
f = tmpdir.join('f.py')
|
||||
f.write_binary(b'# -*- coding: utf-8 -*-\r\nx = 1\r\n')
|
||||
assert not main((str(f),))
|
||||
|
||||
|
||||
def test_crfl_adds(tmpdir):
|
||||
f = tmpdir.join('f.py')
|
||||
f.write_binary(b'x = 1\r\n')
|
||||
assert main((str(f),))
|
||||
assert f.read_binary() == b'# -*- coding: utf-8 -*-\r\nx = 1\r\n'
|
||||
|
|
@ -1,9 +1,11 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
HOOK = Path(__file__).parents[1] / "pre_commit_hooks" / "forbid_articles_in_test_filenames.py"
|
||||
HOOK = Path(__file__).parents[1] / 'pre_commit_hooks' / 'forbid_articles_in_test_filenames.py'
|
||||
|
||||
|
||||
def run_hook(repo_path: Path):
|
||||
|
|
@ -18,39 +20,39 @@ def run_hook(repo_path: Path):
|
|||
|
||||
|
||||
def init_git_repo(tmp_path: Path):
|
||||
subprocess.run(["git", "init"], cwd=tmp_path, check=True)
|
||||
subprocess.run(["git", "config", "user.email", "test@example.com"], cwd=tmp_path, check=True)
|
||||
subprocess.run(["git", "config", "user.name", "Test User"], cwd=tmp_path, check=True)
|
||||
subprocess.run(['git', 'init'], cwd=tmp_path, check=True)
|
||||
subprocess.run(['git', 'config', 'user.email', 'test@example.com'], cwd=tmp_path, check=True)
|
||||
subprocess.run(['git', 'config', 'user.name', 'Test User'], cwd=tmp_path, check=True)
|
||||
|
||||
|
||||
def git_add_all(tmp_path: Path):
|
||||
subprocess.run(["git", "add", "."], cwd=tmp_path, check=True)
|
||||
subprocess.run(['git', 'add', '.'], cwd=tmp_path, check=True)
|
||||
|
||||
|
||||
def test_fails_on_forbidden_article_in_test_filename(tmp_path: Path):
|
||||
init_git_repo(tmp_path)
|
||||
|
||||
bad_test = tmp_path / "tests_create_an_address.py"
|
||||
bad_test.write_text("def test_something(): pass\n")
|
||||
bad_test = tmp_path / 'tests_create_an_address.py'
|
||||
bad_test.write_text('def test_something(): pass\n')
|
||||
|
||||
git_add_all(tmp_path)
|
||||
|
||||
code, output = run_hook(tmp_path)
|
||||
|
||||
assert code == 1
|
||||
assert "ERROR: Forbidden article in test filename:" in output
|
||||
assert "tests_create_an_address.py" in output
|
||||
assert 'ERROR: Forbidden article in test filename:' in output
|
||||
assert 'tests_create_an_address.py' in output
|
||||
|
||||
|
||||
def test_passes_on_valid_test_filename(tmp_path: Path):
|
||||
init_git_repo(tmp_path)
|
||||
|
||||
good_test = tmp_path / "tests_create_address.py"
|
||||
good_test.write_text("def test_something(): pass\n")
|
||||
good_test = tmp_path / 'tests_create_address.py'
|
||||
good_test.write_text('def test_something(): pass\n')
|
||||
|
||||
git_add_all(tmp_path)
|
||||
|
||||
code, output = run_hook(tmp_path)
|
||||
|
||||
assert code == 0
|
||||
assert output == ""
|
||||
assert output == ''
|
||||
|
|
|
|||
|
|
@ -11,13 +11,13 @@ from testing.util import git_commit
|
|||
def test_other_branch(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
cmd_output('git', 'checkout', '-b', 'anotherbranch')
|
||||
assert is_on_branch({'master'}) is False
|
||||
assert is_on_branch({'placeholder'}) is False
|
||||
|
||||
|
||||
def test_multi_branch(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
cmd_output('git', 'checkout', '-b', 'another/branch')
|
||||
assert is_on_branch({'master'}) is False
|
||||
assert is_on_branch({'placeholder'}) is False
|
||||
|
||||
|
||||
def test_multi_branch_fail(temp_git_dir):
|
||||
|
|
@ -26,9 +26,10 @@ def test_multi_branch_fail(temp_git_dir):
|
|||
assert is_on_branch({'another/branch'}) is True
|
||||
|
||||
|
||||
def test_master_branch(temp_git_dir):
|
||||
def test_exact_branch(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
assert is_on_branch({'master'}) is True
|
||||
cmd_output('git', 'checkout', '-b', 'branchname')
|
||||
assert is_on_branch({'branchname'}) is True
|
||||
|
||||
|
||||
def test_main_branch_call(temp_git_dir):
|
||||
|
|
@ -50,11 +51,11 @@ def test_branch_pattern_fail(temp_git_dir):
|
|||
assert is_on_branch(set(), {'another/.*'}) is True
|
||||
|
||||
|
||||
@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
|
||||
@pytest.mark.parametrize('branch_name', ('somebranch', 'another/branch'))
|
||||
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
|
||||
with temp_git_dir.as_cwd():
|
||||
cmd_output('git', 'checkout', '-b', branch_name)
|
||||
assert main(('--branch', 'master', '--pattern', 'another/.*'))
|
||||
assert main(('--branch', 'somebranch', '--pattern', 'another/.*'))
|
||||
|
||||
|
||||
def test_main_default_call(temp_git_dir):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue