Skip to content

Commit 715d8b8

Browse files
committed
Add isolated test to run pre-commit hooks to ensure they work
1 parent 1100ed3 commit 715d8b8

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import subprocess
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
7+
# def run_with_check(command_list: list[str], cwd: Path):
8+
9+
10+
@pytest.fixture(scope="module")
11+
def poetry_path() -> str:
12+
result = subprocess.run(["which", "poetry"], capture_output=True, text=True)
13+
poetry_path = result.stdout.strip()
14+
return poetry_path
15+
16+
@pytest.fixture(scope="module")
17+
def git_path() -> str:
18+
result = subprocess.run(["which", "git"], capture_output=True, text=True)
19+
git_path = result.stdout.strip()
20+
return git_path
21+
22+
23+
@pytest.fixture(scope="module")
24+
def run_command(poetry_path, git_path, new_project):
25+
def _run_command_fixture(command, **kwargs):
26+
defaults = {
27+
"capture_output": True,
28+
"check": True,
29+
"cwd": new_project,
30+
"env": {"PATH": f"{Path(git_path).parent}:{Path(poetry_path).parent}"},
31+
"text": True,
32+
33+
}
34+
config = {**defaults, **kwargs}
35+
36+
return subprocess.run(command, **config)
37+
38+
return _run_command_fixture
39+
40+
41+
@pytest.fixture(scope="module")
42+
def pre_commit(run_command, new_project, poetry_path):
43+
run_command(command=["git", "init"])
44+
run_command([poetry_path, "add", "pre-commit"])
45+
run_command([poetry_path, "install"])
46+
run_command([poetry_path, "run", "--", "pre-commit", "install"])
47+
48+
49+
class TestPreCommitConfig:
50+
@staticmethod
51+
def _command(poetry_path: str, stage: str) -> list[str]:
52+
return [poetry_path, "run", "--", "pre-commit", "run", "--hook-stage", stage,
53+
"--files",
54+
"exasol/package/version.py"]
55+
56+
def test_stage_pre_commit(self, pre_commit, poetry_path, run_command):
57+
command = self._command(poetry_path, "pre-commit")
58+
output = run_command(command=command, check=False)
59+
60+
assert "Failed" not in output.stdout
61+
assert "Passed" in output.stdout
62+
assert output.returncode == 0
63+
64+
def test_stage_pre_push(self, pre_commit, poetry_path, run_command):
65+
command = self._command(poetry_path, "pre-push")
66+
output = run_command(command=command, check=False)
67+
68+
assert "Failed" not in output.stdout
69+
assert "Passed" in output.stdout
70+
assert output.returncode == 0

0 commit comments

Comments
 (0)