fix: move pytest.importorskip below pytest import in skip-guarded tests

The original PR placed 'pwd = pytest.importorskip("pwd")' on line 4
but 'import pytest' on line 9 — NameError on module load. Same for
test_file_sync_back.py. Plus, the in-function 'pwd = pytest.importorskip'
calls in test_auto_detected_root_is_rejected confused Python's scope
analysis (later 'import pytest' made pytest local everywhere in the
function) and caused UnboundLocalError. Drop the now-redundant
in-function importorskip calls and rely on the module-level guard.
This commit is contained in:
Teknium 2026-05-09 09:08:17 -07:00
parent 4e8b8573ca
commit b959cfa056
2 changed files with 4 additions and 6 deletions

View file

@ -1,13 +1,14 @@
"""Tests for gateway service management helpers."""
import os
pwd = pytest.importorskip("pwd")
import subprocess
from pathlib import Path
from types import SimpleNamespace
import pytest
pwd = pytest.importorskip("pwd")
import hermes_cli.gateway as gateway_cli
from gateway import status
from gateway.restart import (
@ -1284,20 +1285,17 @@ class TestSystemServiceIdentityRootHandling:
def test_auto_detected_root_is_rejected(self, monkeypatch):
"""When root is auto-detected (not explicitly requested), raise."""
pwd = pytest.importorskip("pwd")
import grp
monkeypatch.delenv("SUDO_USER", raising=False)
monkeypatch.setenv("USER", "root")
monkeypatch.setenv("LOGNAME", "root")
import pytest
with pytest.raises(ValueError, match="pass --run-as-user root to override"):
gateway_cli._system_service_identity(run_as_user=None)
def test_explicit_root_is_allowed(self, monkeypatch):
"""When root is explicitly passed via --run-as-user root, allow it."""
pwd = pytest.importorskip("pwd")
import grp
root_info = pwd.getpwnam("root")
@ -1309,7 +1307,6 @@ class TestSystemServiceIdentityRootHandling:
def test_non_root_user_passes_through(self, monkeypatch):
"""Normal non-root user works as before."""
pwd = pytest.importorskip("pwd")
import grp
monkeypatch.delenv("SUDO_USER", raising=False)

View file

@ -1,6 +1,5 @@
"""Tests for FileSyncManager.sync_back() — pull remote changes to host."""
fcntl = pytest.importorskip("fcntl")
import io
import logging
import os
@ -12,6 +11,8 @@ from unittest.mock import MagicMock, call, patch
import pytest
fcntl = pytest.importorskip("fcntl")
from tools.environments.file_sync import (
FileSyncManager,
_sha256_file,