Skip to content

Commit f5e283f

Browse files
committed
Speed up pre-commit hooks, add timeouts and error handling
- Move pytest from pre-commit to pre-push only (biggest time saver) - Scope pyright to changed files instead of all of src/ - Add --ff (failed-first) to pytest-cov for faster feedback - Add timeout=90 to _run_systemctl to prevent hung CLI - Wrap secret_exists in try/except for when podman is unavailable
1 parent 0fca868 commit f5e283f

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,12 @@ repos:
8282
- "--template=[#{}]"
8383

8484
# Pyright - static type checking (matches IDE)
85+
# Only checks changed files rather than all of src/ for speed
8586
- repo: https://github.com/RobertCraigie/pyright-python
8687
rev: v1.1.390
8788
hooks:
8889
- id: pyright
89-
args: [src/]
90+
files: ^src/
9091
additional_dependencies:
9192
- cyclopts>=3.9
9293
- pytest>=8.0
@@ -99,23 +100,13 @@ repos:
99100
args: [--fix]
100101
- id: ruff-format
101102

102-
# pytest - run tests before commit (fast, no coverage)
103-
# Only runs when Python files in src/ or tests/ are modified
103+
# pytest - run before push with coverage and last-failed optimization
104+
# Skipped on commit for speed; full suite gates push instead
104105
- repo: local
105106
hooks:
106-
- id: pytest
107-
name: pytest
108-
entry: .venv/bin/pytest tests/ -x -q
109-
language: system
110-
pass_filenames: false
111-
files: ^(src/|tests/).*\.py$
112-
stages: [pre-commit]
113-
114-
# pytest with coverage - run before push
115-
# Only runs when Python files in src/ or tests/ are modified
116107
- id: pytest-cov
117108
name: pytest-cov
118-
entry: .venv/bin/pytest tests/ -x -q --cov=ots_containers --cov-report=term-missing --cov-fail-under=70
109+
entry: .venv/bin/pytest tests/ -x -q --ff --cov=ots_containers --cov-report=term-missing --cov-fail-under=70
119110
language: system
120111
pass_filenames: false
121112
files: ^(src/|tests/).*\.py$

src/ots_containers/environment_file.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,16 @@ def ensure_podman_secret(secret_name: str, value: str) -> str:
405405

406406

407407
def secret_exists(secret_name: str) -> bool:
408-
"""Check if a podman secret exists."""
409-
result = subprocess.run(
410-
["podman", "secret", "exists", secret_name],
411-
capture_output=True,
412-
)
413-
return result.returncode == 0
408+
"""Check if a podman secret exists. Returns False if podman is unavailable."""
409+
try:
410+
result = subprocess.run(
411+
["podman", "secret", "exists", secret_name],
412+
capture_output=True,
413+
timeout=10,
414+
)
415+
return result.returncode == 0
416+
except (subprocess.SubprocessError, OSError):
417+
return False
414418

415419

416420
def generate_quadlet_secret_lines(secrets: list[SecretSpec]) -> str:

src/ots_containers/systemd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _run_systemctl(action: str, unit: str) -> None:
4040
"""Run a systemctl command with diagnostic output on failure."""
4141
cmd = ["sudo", "systemctl", action, unit]
4242
print(f" $ {' '.join(cmd)}")
43-
result = subprocess.run(cmd, capture_output=True, text=True)
43+
result = subprocess.run(cmd, capture_output=True, text=True, timeout=90)
4444
if result.returncode != 0:
4545
journal = _fetch_journal(unit)
4646
raise SystemctlError(unit, action, journal)

tests/test_systemd.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ def test_start_calls_systemctl_start(self, mocker):
194194
["sudo", "systemctl", "start", "onetime-web@7043"],
195195
capture_output=True,
196196
text=True,
197+
timeout=90,
197198
)
198199

199200
def test_start_raises_systemctl_error_on_failure(self, mocker):
@@ -227,6 +228,7 @@ def test_stop_calls_systemctl_stop(self, mocker):
227228
["sudo", "systemctl", "stop", "onetime-web@7043"],
228229
capture_output=True,
229230
text=True,
231+
timeout=90,
230232
)
231233

232234
def test_stop_raises_systemctl_error_on_failure(self, mocker):
@@ -260,6 +262,7 @@ def test_restart_calls_systemctl_restart(self, mocker):
260262
["sudo", "systemctl", "restart", "onetime-web@7043"],
261263
capture_output=True,
262264
text=True,
265+
timeout=90,
263266
)
264267

265268
def test_restart_raises_systemctl_error_on_failure(self, mocker):

0 commit comments

Comments
 (0)