Spaces:
Running
Running
File size: 8,674 Bytes
7cdac45 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | import os
import shutil
import subprocess
from pathlib import Path
import pytest
def _repo_root() -> Path:
return Path(__file__).resolve().parents[2]
def _script_text(name: str) -> str:
return (_repo_root() / "scripts" / name).read_text(encoding="utf-8")
def _braced_body(text: str, declaration: str) -> str:
start = text.index(declaration)
brace_start = text.index("{", start)
depth = 0
for index, char in enumerate(text[brace_start:], start=brace_start):
if char == "{":
depth += 1
elif char == "}":
depth -= 1
if depth == 0:
return text[brace_start + 1 : index]
raise AssertionError(f"Unclosed function body for {declaration}")
def _path_without_uv() -> str:
uv_names = ("uv", "uv.exe", "uv.cmd", "uv.bat")
entries = []
for raw_entry in os.environ.get("PATH", "").split(os.pathsep):
if not raw_entry:
continue
entry = Path(raw_entry)
if any((entry / name).exists() for name in uv_names):
continue
entries.append(raw_entry)
return os.pathsep.join(entries)
def _shell_interpreter() -> str:
sh = shutil.which("sh")
if sh is None:
pytest.skip("sh is not available on this platform")
return sh
def _powershell_interpreter() -> str:
pwsh = shutil.which("pwsh") or shutil.which("powershell")
if pwsh is None:
pytest.skip("PowerShell is not available on this platform")
return pwsh
def test_ci_sh_runs_ci_checks_in_order() -> None:
text = _script_text("ci.sh")
assert 'CHECK_ORDER="suppressions ruff-format ruff-check ty pytest"' in text
assert "grep -rE" in text
assert "Fix the underlying type errors instead" in text
assert "--exclude-dir=.venv" in text
assert "--exclude-dir=.git" in text
assert "uv run ruff format" in text
assert "uv run ruff format --check" not in text
assert "uv run ruff check --fix" in text
assert "uv run ty check" in text
assert "uv run pytest -v --tb=short" in text
assert "--only" in text
assert "--skip" in text
assert "--dry-run" in text
assert "uv is required but was not found on PATH" in text
assert "npm" not in text
assert "smoke/" not in text
assert "uv self update" not in text
def test_ci_sh_dry_run_does_not_require_uv() -> None:
result = subprocess.run(
[
_shell_interpreter(),
str(_repo_root() / "scripts" / "ci.sh"),
"--only",
"pytest",
"--dry-run",
],
cwd=_repo_root(),
env={**os.environ, "PATH": _path_without_uv()},
text=True,
capture_output=True,
check=False,
)
assert result.returncode == 0
assert "+ uv run pytest -v --tb=short" in result.stdout
assert "uv is required" not in result.stderr
@pytest.mark.parametrize(
("check_id", "command"),
[
("ruff-format", "+ uv run ruff format"),
("ruff-check", "+ uv run ruff check --fix"),
],
)
def test_ci_sh_dry_run_prints_local_ruff_repair_commands(
check_id: str, command: str
) -> None:
result = subprocess.run(
[
_shell_interpreter(),
str(_repo_root() / "scripts" / "ci.sh"),
"--only",
check_id,
"--dry-run",
],
cwd=_repo_root(),
env={**os.environ, "PATH": _path_without_uv()},
text=True,
capture_output=True,
check=False,
)
assert result.returncode == 0
assert command in result.stdout
assert "uv is required" not in result.stderr
def test_ci_sh_suppression_only_does_not_require_uv() -> None:
result = subprocess.run(
[
_shell_interpreter(),
str(_repo_root() / "scripts" / "ci.sh"),
"--only",
"suppressions",
],
cwd=_repo_root(),
env={**os.environ, "PATH": _path_without_uv()},
text=True,
capture_output=True,
check=False,
)
assert result.returncode == 0
assert "Ban type ignore suppressions" in result.stdout
assert "uv is required" not in result.stderr
def test_ci_sh_is_tracked_executable() -> None:
result = subprocess.run(
["git", "ls-files", "--stage", "scripts/ci.sh"],
cwd=_repo_root(),
text=True,
capture_output=True,
check=True,
)
assert result.stdout.startswith("100755 ")
def test_ci_sh_fail_fast_runs_checks_sequentially() -> None:
text = _script_text("ci.sh")
main = text[text.index('parse_args "$@"') :]
suppress_index = text.index("run_suppressions()")
ruff_format_index = text.index("run_ruff_format()")
ruff_check_index = text.index("run_ruff_check()")
ty_index = text.index("run_ty()")
pytest_index = text.index("run_pytest()")
assert (
suppress_index < ruff_format_index < ruff_check_index < ty_index < pytest_index
)
assert "for check_id in $CHECK_ORDER" in main
def test_ci_ps1_runs_ci_checks_in_order() -> None:
text = _script_text("ci.ps1")
assert '"suppressions"' in text
assert '"ruff-format"' in text
assert '"ruff-check"' in text
assert '"ty"' in text
assert '"pytest"' in text
assert "Select-String -Pattern" in text
assert "Fix the underlying type errors instead" in text
assert ".venv" in text
assert ".git" in text
assert '"run", "ruff", "format"' in text
assert '"format", "--check"' not in text
assert '"run", "ruff", "check", "--fix"' in text
assert '"-v", "--tb=short"' in text
assert "-Only" in text
assert "-Skip" in text
assert "-DryRun" in text
assert "uv is required but was not found on PATH" in text
assert "npm" not in text
assert "smoke/" not in text
assert "uv self update" not in text
def test_ci_ps1_dry_run_does_not_require_uv() -> None:
result = subprocess.run(
[
_powershell_interpreter(),
"-NoProfile",
"-File",
str(_repo_root() / "scripts" / "ci.ps1"),
"-Only",
"pytest",
"-DryRun",
],
cwd=_repo_root(),
env={**os.environ, "PATH": _path_without_uv()},
text=True,
capture_output=True,
check=False,
)
assert result.returncode == 0
assert "+ uv run pytest -v --tb=short" in result.stdout
assert "uv is required" not in result.stderr
@pytest.mark.parametrize(
("check_id", "command"),
[
("ruff-format", "+ uv run ruff format"),
("ruff-check", "+ uv run ruff check --fix"),
],
)
def test_ci_ps1_dry_run_prints_local_ruff_repair_commands(
check_id: str, command: str
) -> None:
result = subprocess.run(
[
_powershell_interpreter(),
"-NoProfile",
"-File",
str(_repo_root() / "scripts" / "ci.ps1"),
"-Only",
check_id,
"-DryRun",
],
cwd=_repo_root(),
env={**os.environ, "PATH": _path_without_uv()},
text=True,
capture_output=True,
check=False,
)
assert result.returncode == 0
assert command in result.stdout
assert "uv is required" not in result.stderr
def test_ci_ps1_suppression_only_does_not_require_uv() -> None:
result = subprocess.run(
[
_powershell_interpreter(),
"-NoProfile",
"-File",
str(_repo_root() / "scripts" / "ci.ps1"),
"-Only",
"suppressions",
],
cwd=_repo_root(),
env={**os.environ, "PATH": _path_without_uv()},
text=True,
capture_output=True,
check=False,
)
assert result.returncode == 0
assert "Ban type ignore suppressions" in result.stdout
assert "uv is required" not in result.stderr
def test_ci_ps1_fail_fast_runs_checks_sequentially() -> None:
text = _script_text("ci.ps1")
assert "foreach ($checkId in $CheckOrder)" in text
assert "Invoke-SuppressionsCheck" in text
assert "Invoke-RuffFormatCheck" in text
assert "Invoke-RuffLintCheck" in text
assert "Invoke-TyCheck" in text
assert "Invoke-PytestCheck" in text
suppress_index = text.index("function Invoke-SuppressionsCheck")
ruff_format_index = text.index("function Invoke-RuffFormatCheck")
ruff_check_index = text.index("function Invoke-RuffLintCheck")
ty_index = text.index("function Invoke-TyCheck")
pytest_index = text.index("function Invoke-PytestCheck")
assert (
suppress_index < ruff_format_index < ruff_check_index < ty_index < pytest_index
)
|