mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-12 14:04:17 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
5e4fe2c164
commit
46646b779e
3 changed files with 26 additions and 20 deletions
|
|
@ -1,15 +1,16 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
FORBIDDEN = {"a", "an", "the"}
|
FORBIDDEN = {'a', 'an', 'the'}
|
||||||
|
|
||||||
|
|
||||||
def git_ls_python_files():
|
def git_ls_python_files():
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
["git", "ls-files", "*.py"],
|
['git', 'ls-files', '*.py'],
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
check=True,
|
check=True,
|
||||||
|
|
@ -20,14 +21,14 @@ def git_ls_python_files():
|
||||||
def is_test_file(path: Path) -> bool:
|
def is_test_file(path: Path) -> bool:
|
||||||
name = path.name
|
name = path.name
|
||||||
return (
|
return (
|
||||||
name.startswith("test_")
|
name.startswith('test_') or
|
||||||
or name.startswith("tests_")
|
name.startswith('tests_') or
|
||||||
or name.endswith("_test.py")
|
name.endswith('_test.py')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def has_forbidden_article(path: Path) -> bool:
|
def has_forbidden_article(path: Path) -> bool:
|
||||||
parts = path.stem.split("_")
|
parts = path.stem.split('_')
|
||||||
return any(part in FORBIDDEN for part in parts)
|
return any(part in FORBIDDEN for part in parts)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -37,12 +38,12 @@ def main() -> int:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if has_forbidden_article(path):
|
if has_forbidden_article(path):
|
||||||
print("ERROR: Forbidden article in test filename:")
|
print('ERROR: Forbidden article in test filename:')
|
||||||
print(path)
|
print(path)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os.path
|
import os.path
|
||||||
import re
|
import re
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Sequence[str] | None = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*')
|
parser.add_argument('filenames', nargs='*')
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
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):
|
def run_hook(repo_path: Path):
|
||||||
|
|
@ -18,39 +20,39 @@ def run_hook(repo_path: Path):
|
||||||
|
|
||||||
|
|
||||||
def init_git_repo(tmp_path: Path):
|
def init_git_repo(tmp_path: Path):
|
||||||
subprocess.run(["git", "init"], 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.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', 'config', 'user.name', 'Test User'], cwd=tmp_path, check=True)
|
||||||
|
|
||||||
|
|
||||||
def git_add_all(tmp_path: Path):
|
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):
|
def test_fails_on_forbidden_article_in_test_filename(tmp_path: Path):
|
||||||
init_git_repo(tmp_path)
|
init_git_repo(tmp_path)
|
||||||
|
|
||||||
bad_test = tmp_path / "tests_create_an_address.py"
|
bad_test = tmp_path / 'tests_create_an_address.py'
|
||||||
bad_test.write_text("def test_something(): pass\n")
|
bad_test.write_text('def test_something(): pass\n')
|
||||||
|
|
||||||
git_add_all(tmp_path)
|
git_add_all(tmp_path)
|
||||||
|
|
||||||
code, output = run_hook(tmp_path)
|
code, output = run_hook(tmp_path)
|
||||||
|
|
||||||
assert code == 1
|
assert code == 1
|
||||||
assert "ERROR: Forbidden article in test filename:" in output
|
assert 'ERROR: Forbidden article in test filename:' in output
|
||||||
assert "tests_create_an_address.py" in output
|
assert 'tests_create_an_address.py' in output
|
||||||
|
|
||||||
|
|
||||||
def test_passes_on_valid_test_filename(tmp_path: Path):
|
def test_passes_on_valid_test_filename(tmp_path: Path):
|
||||||
init_git_repo(tmp_path)
|
init_git_repo(tmp_path)
|
||||||
|
|
||||||
good_test = tmp_path / "tests_create_address.py"
|
good_test = tmp_path / 'tests_create_address.py'
|
||||||
good_test.write_text("def test_something(): pass\n")
|
good_test.write_text('def test_something(): pass\n')
|
||||||
|
|
||||||
git_add_all(tmp_path)
|
git_add_all(tmp_path)
|
||||||
|
|
||||||
code, output = run_hook(tmp_path)
|
code, output = run_hook(tmp_path)
|
||||||
|
|
||||||
assert code == 0
|
assert code == 0
|
||||||
assert output == ""
|
assert output == ''
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue