[verified] Bind managed-lock test imports to HARNESS_ROOT
This commit is contained in:
@@ -241,7 +241,61 @@ class ManagedLockTests(unittest.TestCase):
|
||||
after = path.stat()
|
||||
self.assertEqual((before.st_ino, before.st_size, before.st_mode), (after.st_ino, after.st_size, after.st_mode))
|
||||
|
||||
def test_lock_contention_from_external_cwd_without_pythonpath(self):
|
||||
root = Path(__file__).resolve().parents[3]
|
||||
script = """
|
||||
import sys
|
||||
import unittest
|
||||
sys.path.insert(0, sys.argv[1])
|
||||
suite = unittest.defaultTestLoader.loadTestsFromName(
|
||||
'harness.tests.common.test_managed_lock.ManagedLockTests.'
|
||||
'test_other_process_cannot_acquire_while_context_alive')
|
||||
result = unittest.TextTestRunner().run(suite)
|
||||
raise SystemExit(not result.wasSuccessful())
|
||||
"""
|
||||
env = os.environ.copy()
|
||||
env.pop("PYTHONPATH", None)
|
||||
env["HARNESS_ROOT"] = str(root)
|
||||
with tempfile.TemporaryDirectory() as cwd:
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-I", "-B", "-c", script, str(root)],
|
||||
cwd=cwd, env=env, text=True, capture_output=True, timeout=30)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
def test_invalid_harness_root_stops_before_lock_and_subprocess(self):
|
||||
with tempfile.TemporaryDirectory() as other:
|
||||
file_path = Path(other) / "not-a-directory"
|
||||
file_path.write_text("fixture")
|
||||
for value in (None, "", "relative", other,
|
||||
str(Path(other) / "missing"), str(file_path)):
|
||||
with self.subTest(value=value), patch.dict(os.environ), \
|
||||
patch(__name__ + ".ManagedLock") as lock, \
|
||||
patch(__name__ + ".subprocess.run") as run:
|
||||
if value is None:
|
||||
os.environ.pop("HARNESS_ROOT", None)
|
||||
else:
|
||||
os.environ["HARNESS_ROOT"] = value
|
||||
run.return_value.stdout = "blocked"
|
||||
error = None
|
||||
try:
|
||||
self.test_other_process_cannot_acquire_while_context_alive()
|
||||
except Exception as exc:
|
||||
error = exc
|
||||
self.assertIsInstance(error, RuntimeError)
|
||||
self.assertIn("HARNESS_ROOT", str(error))
|
||||
lock.assert_not_called()
|
||||
run.assert_not_called()
|
||||
|
||||
def test_other_process_cannot_acquire_while_context_alive(self):
|
||||
value = os.environ.get("HARNESS_ROOT")
|
||||
if not value:
|
||||
raise RuntimeError("HARNESS_ROOT must be configured")
|
||||
root = Path(value)
|
||||
if not root.is_absolute() or not root.is_dir():
|
||||
raise RuntimeError("HARNESS_ROOT must be an existing absolute directory")
|
||||
root = root.resolve()
|
||||
if root != Path(__file__).resolve().parents[3]:
|
||||
raise RuntimeError("HARNESS_ROOT does not match this test installation")
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
path = Path(d) / "process.lock"
|
||||
script = """
|
||||
@@ -255,7 +309,7 @@ except LockTimeoutError:
|
||||
print('blocked')
|
||||
"""
|
||||
with ManagedLock(path):
|
||||
result = subprocess.run([sys.executable, "-c", script, str(path), str(Path.cwd())], text=True, capture_output=True, check=True)
|
||||
result = subprocess.run([sys.executable, "-c", script, str(path), str(root)], text=True, capture_output=True, check=True, timeout=30)
|
||||
self.assertEqual(result.stdout.strip(), "blocked")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user