Coverage for tests / integration / tools / astro_check / conftest.py: 50%
48 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Pytest configuration for astro-check integration tests."""
3from __future__ import annotations
5import shutil
6import subprocess
7from pathlib import Path
9import pytest
12def astro_check_is_available() -> bool:
13 """Check if astro is installed and actually works.
15 This checks both that the command exists AND that it executes successfully,
16 which handles cases where a wrapper script exists but the underlying
17 tool isn't installed. Also checks bunx/npx fallbacks.
19 Returns:
20 True if astro is installed and working, False otherwise.
21 """
22 # Try direct astro command first
23 if shutil.which("astro") is not None:
24 try:
25 result = subprocess.run(
26 ["astro", "--version"],
27 capture_output=True,
28 timeout=10,
29 check=False,
30 )
31 if result.returncode == 0:
32 return True
33 except (subprocess.TimeoutExpired, OSError):
34 pass
36 # Try bunx fallback
37 if shutil.which("bunx") is not None:
38 try:
39 result = subprocess.run(
40 ["bunx", "astro", "--version"],
41 capture_output=True,
42 timeout=30,
43 check=False,
44 )
45 if result.returncode == 0:
46 return True
47 except (subprocess.TimeoutExpired, OSError):
48 pass
50 # Try npx fallback
51 if shutil.which("npx") is not None:
52 try:
53 result = subprocess.run(
54 ["npx", "astro", "--version"],
55 capture_output=True,
56 timeout=30,
57 check=False,
58 )
59 if result.returncode == 0:
60 return True
61 except (subprocess.TimeoutExpired, OSError):
62 pass
64 return False
67def _find_project_root() -> Path:
68 """Find project root by looking for pyproject.toml.
70 Returns:
71 Path to the project root directory.
73 Raises:
74 RuntimeError: If pyproject.toml is not found in any parent directory.
75 """
76 path = Path(__file__).resolve()
77 for parent in path.parents:
78 if (parent / "pyproject.toml").exists():
79 return parent
80 raise RuntimeError("pyproject.toml not found in parent directories")
83# Paths to test samples
84SAMPLE_DIR = _find_project_root() / "test_samples"
85ASTRO_CHECK_SAMPLES = SAMPLE_DIR / "tools" / "web" / "astro_check"
86CLEAN_SAMPLE = ASTRO_CHECK_SAMPLES / "astro_check_clean.astro"
87VIOLATION_SAMPLE = ASTRO_CHECK_SAMPLES / "astro_check_violations.astro"
90@pytest.fixture
91def astro_check_violation_file(tmp_path: Path) -> str:
92 """Copy the astro check violation sample to a temp directory.
94 Args:
95 tmp_path: Pytest fixture providing a temporary directory.
97 Returns:
98 Path to the copied file as a string.
99 """
100 dst = tmp_path / "astro_check_violations.astro"
101 shutil.copy(VIOLATION_SAMPLE, dst)
102 return str(dst)
105@pytest.fixture
106def astro_check_clean_file(tmp_path: Path) -> str:
107 """Copy the astro check clean sample to a temp directory.
109 Args:
110 tmp_path: Pytest fixture providing a temporary directory.
112 Returns:
113 Path to the copied file as a string.
114 """
115 dst = tmp_path / "astro_check_clean.astro"
116 shutil.copy(CLEAN_SAMPLE, dst)
117 return str(dst)