Skip to content

Commit 3326baa

Browse files
committed
Refactor in preparation of additional testing
1 parent fb33481 commit 3326baa

File tree

2 files changed

+51
-50
lines changed

2 files changed

+51
-50
lines changed

test/integration/project-template/conftest.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import subprocess
2+
from pathlib import Path
23

34
import pytest
45

56
from noxconfig import Config
67

78

8-
@pytest.fixture(scope="session")
9+
@pytest.fixture(scope="session", autouse=True)
910
def cwd(tmp_path_factory):
1011
return tmp_path_factory.mktemp("project_template_test")
1112

1213

13-
@pytest.fixture(scope="session")
14+
@pytest.fixture(scope="session", autouse=True)
1415
def new_project(cwd):
1516
project_name = "project"
1617
repo_name = "repo"
@@ -23,3 +24,48 @@ def new_project(cwd):
2324
], capture_output=True, check=True)
2425

2526
return cwd / repo_name
27+
28+
@pytest.fixture(scope="session", autouse=True)
29+
def poetry_install(run_command, poetry_path):
30+
run_command([poetry_path, "install"])
31+
32+
33+
@pytest.fixture(scope="session")
34+
def git_path() -> str:
35+
result = subprocess.run(["which", "git"], capture_output=True, text=True)
36+
git_path = result.stdout.strip()
37+
return git_path
38+
39+
40+
@pytest.fixture(scope="session")
41+
def poetry_path() -> str:
42+
result = subprocess.run(["which", "poetry"], capture_output=True, text=True)
43+
poetry_path = result.stdout.strip()
44+
return poetry_path
45+
46+
47+
@pytest.fixture(scope="session")
48+
def run_command(poetry_path, git_path, new_project):
49+
"""
50+
Run subprocess command with captured output and a limited environment (env).
51+
52+
We restrict the environment as different systems & tools (i.e. PyCharm) include
53+
environment variables which may supersede the ones provided here. In such cases,
54+
this can lead to a breaking alteration in the PTB poetry environment. Thus,
55+
we provide the minimum information needed to execute the pre-commit command.
56+
"""
57+
58+
def _run_command_fixture(command, **kwargs):
59+
defaults = {
60+
"capture_output": True,
61+
"check": True,
62+
"cwd": new_project,
63+
"env": {"PATH": f"{Path(git_path).parent}:{Path(poetry_path).parent}"},
64+
"text": True,
65+
66+
}
67+
config = {**defaults, **kwargs}
68+
69+
return subprocess.run(command, **config)
70+
71+
return _run_command_fixture

test/integration/project-template/pre_commit_test.py

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,9 @@
1-
import subprocess
2-
from pathlib import Path
3-
41
import pytest
52

63

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-
484
@pytest.fixture(scope="module")
495
def pre_commit(run_command, new_project, poetry_path):
50-
run_command(command=["git", "init"])
51-
run_command([poetry_path, "install"])
6+
run_command(["git", "init"])
527
run_command([poetry_path, "run", "--", "pre-commit", "install"])
538

549

@@ -61,15 +16,15 @@ def _command(poetry_path: str, stage: str) -> list[str]:
6116

6217
def test_stage_pre_commit(self, pre_commit, poetry_path, run_command):
6318
command = self._command(poetry_path, "pre-commit")
64-
output = run_command(command=command, check=False)
19+
output = run_command(command, check=False)
6520

6621
assert "Failed" not in output.stdout
6722
assert "Passed" in output.stdout
6823
assert output.returncode == 0
6924

7025
def test_stage_pre_push(self, pre_commit, poetry_path, run_command):
7126
command = self._command(poetry_path, "pre-push")
72-
output = run_command(command=command, check=False)
27+
output = run_command(command, check=False)
7328

7429
assert "Failed" not in output.stdout
7530
assert "Passed" in output.stdout

0 commit comments

Comments
 (0)