|
| 1 | +import subprocess |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture(scope="module") |
| 8 | +def poetry_path() -> str: |
| 9 | + result = subprocess.run(["which", "poetry"], capture_output=True, text=True) |
| 10 | + poetry_path = result.stdout.strip() |
| 11 | + return poetry_path |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope="module") |
| 15 | +def git_path() -> str: |
| 16 | + result = subprocess.run(["which", "git"], capture_output=True, text=True) |
| 17 | + git_path = result.stdout.strip() |
| 18 | + return git_path |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(scope="module") |
| 22 | +def run_command(poetry_path, git_path, new_project): |
| 23 | + """ |
| 24 | + Run subprocess command with captured output and a limited environment (env). |
| 25 | +
|
| 26 | + We restrict the environment as different systems & tools (i.e. PyCharm) include |
| 27 | + environment variables which may supersede the ones provided here. In such cases, |
| 28 | + this can lead to a breaking alteration in the PTB poetry environment. Thus, |
| 29 | + we provide the minimum information needed to execute the pre-commit command. |
| 30 | + """ |
| 31 | + |
| 32 | + def _run_command_fixture(command, **kwargs): |
| 33 | + defaults = { |
| 34 | + "capture_output": True, |
| 35 | + "check": True, |
| 36 | + "cwd": new_project, |
| 37 | + "env": {"PATH": f"{Path(git_path).parent}:{Path(poetry_path).parent}"}, |
| 38 | + "text": True, |
| 39 | + |
| 40 | + } |
| 41 | + config = {**defaults, **kwargs} |
| 42 | + |
| 43 | + return subprocess.run(command, **config) |
| 44 | + |
| 45 | + return _run_command_fixture |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture(scope="module") |
| 49 | +def pre_commit(run_command, new_project, poetry_path): |
| 50 | + run_command(command=["git", "init"]) |
| 51 | + run_command([poetry_path, "install"]) |
| 52 | + run_command([poetry_path, "run", "--", "pre-commit", "install"]) |
| 53 | + |
| 54 | + |
| 55 | +class TestPreCommitConfig: |
| 56 | + @staticmethod |
| 57 | + def _command(poetry_path: str, stage: str) -> list[str]: |
| 58 | + return [poetry_path, "run", "--", "pre-commit", "run", "--hook-stage", stage, |
| 59 | + "--files", |
| 60 | + "exasol/package/version.py"] |
| 61 | + |
| 62 | + def test_stage_pre_commit(self, pre_commit, poetry_path, run_command): |
| 63 | + command = self._command(poetry_path, "pre-commit") |
| 64 | + output = run_command(command=command, check=False) |
| 65 | + |
| 66 | + assert "Failed" not in output.stdout |
| 67 | + assert "Passed" in output.stdout |
| 68 | + assert output.returncode == 0 |
| 69 | + |
| 70 | + def test_stage_pre_push(self, pre_commit, poetry_path, run_command): |
| 71 | + command = self._command(poetry_path, "pre-push") |
| 72 | + output = run_command(command=command, check=False) |
| 73 | + |
| 74 | + assert "Failed" not in output.stdout |
| 75 | + assert "Passed" in output.stdout |
| 76 | + assert output.returncode == 0 |
0 commit comments