Skip to content

Commit 66a7c00

Browse files
committed
test: move updater script fixtures into fixtures module
1 parent a9c72cb commit 66a7c00

2 files changed

Lines changed: 192 additions & 172 deletions

File tree

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
"""Fixtures and helpers for country package updater script tests."""
2+
3+
from __future__ import annotations
4+
5+
import importlib.util
6+
import os
7+
import subprocess
8+
from pathlib import Path
9+
from types import ModuleType
10+
11+
import pytest
12+
13+
from fixtures.test_modal_scripts import REPO_ROOT, SCRIPTS_DIR
14+
15+
16+
SCRIPT = SCRIPTS_DIR / "update-country-package.sh"
17+
CHANGELOG_SCRIPT = SCRIPTS_DIR / "check-country-package-updates.py"
18+
19+
20+
@pytest.fixture
21+
def changelog_module() -> ModuleType:
22+
spec = importlib.util.spec_from_file_location(
23+
"check_country_package_updates", CHANGELOG_SCRIPT
24+
)
25+
assert spec is not None
26+
assert spec.loader is not None
27+
module = importlib.util.module_from_spec(spec)
28+
spec.loader.exec_module(module)
29+
return module
30+
31+
32+
@pytest.fixture
33+
def fake_repo(tmp_path: Path) -> Path:
34+
project = tmp_path / "simulation"
35+
modal_dir = project / "src" / "modal"
36+
modal_dir.mkdir(parents=True)
37+
38+
(project / "pyproject.toml").write_text(
39+
"\n".join(
40+
[
41+
"[project]",
42+
'dependencies = ["policyengine-us==1.0.0", "policyengine-uk==2.0.0"]',
43+
]
44+
),
45+
encoding="utf-8",
46+
)
47+
(project / "uv.lock").write_text(
48+
"\n".join(
49+
[
50+
"[[package]]",
51+
'name = "policyengine-us"',
52+
'version = "1.0.0"',
53+
"",
54+
"[[package]]",
55+
'name = "policyengine-uk"',
56+
'version = "2.0.0"',
57+
]
58+
),
59+
encoding="utf-8",
60+
)
61+
(modal_dir / "app.py").write_text(
62+
"\n".join(
63+
[
64+
"import os",
65+
'US_VERSION = os.environ.get("POLICYENGINE_US_VERSION", "1.0.0")',
66+
'UK_VERSION = os.environ.get("POLICYENGINE_UK_VERSION", "2.0.0")',
67+
]
68+
),
69+
encoding="utf-8",
70+
)
71+
72+
helper_dir = tmp_path / ".github" / "scripts"
73+
helper_dir.mkdir(parents=True)
74+
(helper_dir / "check-country-package-updates.py").write_text(
75+
'#!/usr/bin/env python3\nprint("### Added\\n- Example upstream change")\n',
76+
encoding="utf-8",
77+
)
78+
79+
return tmp_path
80+
81+
82+
@pytest.fixture
83+
def fake_bin(tmp_path: Path) -> Path:
84+
path = tmp_path / "bin"
85+
path.mkdir()
86+
return path
87+
88+
89+
def write_executable(path: Path, content: str) -> None:
90+
path.write_text(content, encoding="utf-8")
91+
path.chmod(0o755)
92+
93+
94+
def install_fake_git(
95+
fake_bin: Path,
96+
*,
97+
root: Path,
98+
log: Path,
99+
remote_branch_exists: bool = False,
100+
diff_has_changes: bool = False,
101+
) -> None:
102+
write_executable(
103+
fake_bin / "git",
104+
f"""#!/usr/bin/env bash
105+
set -euo pipefail
106+
printf 'git %s\\n' "$*" >> "{log}"
107+
108+
if [[ "$1" == "rev-parse" && "$2" == "--show-toplevel" ]]; then
109+
echo "{root}"
110+
exit 0
111+
fi
112+
113+
if [[ "$1" == "ls-remote" ]]; then
114+
if [[ "{int(remote_branch_exists)}" == "1" ]]; then
115+
exit 0
116+
fi
117+
exit 2
118+
fi
119+
120+
if [[ "$1" == "diff" ]]; then
121+
if [[ "{int(diff_has_changes)}" == "1" ]]; then
122+
exit 1
123+
fi
124+
exit 0
125+
fi
126+
127+
exit 0
128+
""",
129+
)
130+
131+
132+
def install_fake_gh(fake_bin: Path, *, log: Path, open_pr: str = "") -> None:
133+
write_executable(
134+
fake_bin / "gh",
135+
f"""#!/usr/bin/env bash
136+
set -euo pipefail
137+
printf 'gh %s\\n' "$*" >> "{log}"
138+
139+
if [[ "$1" == "pr" && "$2" == "list" ]]; then
140+
printf '%s\\n' "{open_pr}"
141+
exit 0
142+
fi
143+
144+
if [[ "$1" == "pr" && "$2" == "create" ]]; then
145+
exit 0
146+
fi
147+
148+
exit 0
149+
""",
150+
)
151+
152+
153+
def install_fake_uv(fake_bin: Path, *, log: Path) -> None:
154+
write_executable(
155+
fake_bin / "uv",
156+
f"""#!/usr/bin/env bash
157+
set -euo pipefail
158+
printf 'uv %s\\n' "$*" >> "{log}"
159+
exit 0
160+
""",
161+
)
162+
163+
164+
def updater_env(fake_bin: Path, fake_repo: Path, **extra: str) -> dict[str, str]:
165+
env = os.environ.copy()
166+
env.update(
167+
{
168+
"PATH": f"{fake_bin}{os.pathsep}{env['PATH']}",
169+
"PROJECT_DIR": "simulation",
170+
}
171+
)
172+
env.update(extra)
173+
return env
174+
175+
176+
def run_updater(*args: str, env: dict[str, str]) -> subprocess.CompletedProcess[str]:
177+
return subprocess.run(
178+
["bash", str(SCRIPT), *args],
179+
cwd=REPO_ROOT,
180+
env=env,
181+
capture_output=True,
182+
text=True,
183+
)

projects/policyengine-api-simulation/tests/test_country_package_update_scripts.py

Lines changed: 9 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -2,185 +2,22 @@
22

33
from __future__ import annotations
44

5-
import importlib.util
6-
import os
75
import subprocess
86
from pathlib import Path
97
from types import ModuleType
108

119
import pytest
1210

13-
from fixtures.test_modal_scripts import REPO_ROOT, SCRIPTS_DIR
11+
from fixtures.test_country_package_update_scripts import (
12+
SCRIPT,
13+
install_fake_gh,
14+
install_fake_git,
15+
install_fake_uv,
16+
run_updater,
17+
updater_env,
18+
)
1419

15-
16-
SCRIPT = SCRIPTS_DIR / "update-country-package.sh"
17-
CHANGELOG_SCRIPT = SCRIPTS_DIR / "check-country-package-updates.py"
18-
19-
20-
@pytest.fixture
21-
def changelog_module() -> ModuleType:
22-
spec = importlib.util.spec_from_file_location(
23-
"check_country_package_updates", CHANGELOG_SCRIPT
24-
)
25-
assert spec is not None
26-
assert spec.loader is not None
27-
module = importlib.util.module_from_spec(spec)
28-
spec.loader.exec_module(module)
29-
return module
30-
31-
32-
@pytest.fixture
33-
def fake_repo(tmp_path: Path) -> Path:
34-
project = tmp_path / "simulation"
35-
modal_dir = project / "src" / "modal"
36-
modal_dir.mkdir(parents=True)
37-
38-
(project / "pyproject.toml").write_text(
39-
"\n".join(
40-
[
41-
"[project]",
42-
'dependencies = ["policyengine-us==1.0.0", "policyengine-uk==2.0.0"]',
43-
]
44-
),
45-
encoding="utf-8",
46-
)
47-
(project / "uv.lock").write_text(
48-
"\n".join(
49-
[
50-
"[[package]]",
51-
'name = "policyengine-us"',
52-
'version = "1.0.0"',
53-
"",
54-
"[[package]]",
55-
'name = "policyengine-uk"',
56-
'version = "2.0.0"',
57-
]
58-
),
59-
encoding="utf-8",
60-
)
61-
(modal_dir / "app.py").write_text(
62-
"\n".join(
63-
[
64-
"import os",
65-
'US_VERSION = os.environ.get("POLICYENGINE_US_VERSION", "1.0.0")',
66-
'UK_VERSION = os.environ.get("POLICYENGINE_UK_VERSION", "2.0.0")',
67-
]
68-
),
69-
encoding="utf-8",
70-
)
71-
72-
helper_dir = tmp_path / ".github" / "scripts"
73-
helper_dir.mkdir(parents=True)
74-
(helper_dir / "check-country-package-updates.py").write_text(
75-
'#!/usr/bin/env python3\nprint("### Added\\n- Example upstream change")\n',
76-
encoding="utf-8",
77-
)
78-
79-
return tmp_path
80-
81-
82-
@pytest.fixture
83-
def fake_bin(tmp_path: Path) -> Path:
84-
path = tmp_path / "bin"
85-
path.mkdir()
86-
return path
87-
88-
89-
def write_executable(path: Path, content: str) -> None:
90-
path.write_text(content, encoding="utf-8")
91-
path.chmod(0o755)
92-
93-
94-
def install_fake_git(
95-
fake_bin: Path,
96-
*,
97-
root: Path,
98-
log: Path,
99-
remote_branch_exists: bool = False,
100-
diff_has_changes: bool = False,
101-
) -> None:
102-
write_executable(
103-
fake_bin / "git",
104-
f"""#!/usr/bin/env bash
105-
set -euo pipefail
106-
printf 'git %s\\n' "$*" >> "{log}"
107-
108-
if [[ "$1" == "rev-parse" && "$2" == "--show-toplevel" ]]; then
109-
echo "{root}"
110-
exit 0
111-
fi
112-
113-
if [[ "$1" == "ls-remote" ]]; then
114-
if [[ "{int(remote_branch_exists)}" == "1" ]]; then
115-
exit 0
116-
fi
117-
exit 2
118-
fi
119-
120-
if [[ "$1" == "diff" ]]; then
121-
if [[ "{int(diff_has_changes)}" == "1" ]]; then
122-
exit 1
123-
fi
124-
exit 0
125-
fi
126-
127-
exit 0
128-
""",
129-
)
130-
131-
132-
def install_fake_gh(fake_bin: Path, *, log: Path, open_pr: str = "") -> None:
133-
write_executable(
134-
fake_bin / "gh",
135-
f"""#!/usr/bin/env bash
136-
set -euo pipefail
137-
printf 'gh %s\\n' "$*" >> "{log}"
138-
139-
if [[ "$1" == "pr" && "$2" == "list" ]]; then
140-
printf '%s\\n' "{open_pr}"
141-
exit 0
142-
fi
143-
144-
if [[ "$1" == "pr" && "$2" == "create" ]]; then
145-
exit 0
146-
fi
147-
148-
exit 0
149-
""",
150-
)
151-
152-
153-
def install_fake_uv(fake_bin: Path, *, log: Path) -> None:
154-
write_executable(
155-
fake_bin / "uv",
156-
f"""#!/usr/bin/env bash
157-
set -euo pipefail
158-
printf 'uv %s\\n' "$*" >> "{log}"
159-
exit 0
160-
""",
161-
)
162-
163-
164-
def updater_env(fake_bin: Path, fake_repo: Path, **extra: str) -> dict[str, str]:
165-
env = os.environ.copy()
166-
env.update(
167-
{
168-
"PATH": f"{fake_bin}{os.pathsep}{env['PATH']}",
169-
"PROJECT_DIR": "simulation",
170-
}
171-
)
172-
env.update(extra)
173-
return env
174-
175-
176-
def run_updater(*args: str, env: dict[str, str]) -> subprocess.CompletedProcess[str]:
177-
return subprocess.run(
178-
["bash", str(SCRIPT), *args],
179-
cwd=REPO_ROOT,
180-
env=env,
181-
capture_output=True,
182-
text=True,
183-
)
20+
pytest_plugins = ("fixtures.test_country_package_update_scripts",)
18421

18522

18623
def test_update_country_package_script_has_valid_bash_syntax() -> None:

0 commit comments

Comments
 (0)