diff --git a/tests/test_swizzle_search.py b/tests/test_swizzle_search.py index ad41b6e149..f797f7b5dd 100644 --- a/tests/test_swizzle_search.py +++ b/tests/test_swizzle_search.py @@ -27,6 +27,12 @@ except ImportError: HAVE_NUMPY = False +# The CLI tests launch the tool with `python -I`, which does not see a --user install of numpy; ask the way it runs. +# HAVE_NUMPY (this process) stays for the tests that call numpy directly. +NUMPY_VISIBLE_TO_TOOL = subprocess.run([sys.executable, "-I", "-c", "import numpy"], + capture_output=True, check=False).returncode == 0 +PIL_VISIBLE_TO_TOOL = subprocess.run([sys.executable, "-I", "-c", "from PIL import Image"], + capture_output=True, check=False).returncode == 0 try: from PIL import Image HAVE_PIL = True @@ -368,7 +374,7 @@ def test_search_empty_when_buffer_too_small(self): class TestSwizzleSearchCliAndRender(unittest.TestCase): - @unittest.skipUnless(HAVE_NUMPY and HAVE_PIL, "numpy and PIL required") + @unittest.skipUnless(NUMPY_VISIBLE_TO_TOOL and PIL_VISIBLE_TO_TOOL and HAVE_PIL, "isolated numpy and PIL required") def test_default_tile_target_previews_a_short_complete_row(self) -> None: with tempfile.TemporaryDirectory() as td: blob = Path(td) / "two_tiles.bin" @@ -388,7 +394,6 @@ def test_default_tile_target_previews_a_short_complete_row(self) -> None: expected_row = bytes((255, 0, 0)) * 32 + bytes((0, 255, 0)) * 32 self.assertEqual(image.tobytes(), expected_row * 32) - @unittest.skipUnless(HAVE_NUMPY, "numpy required") # the tool imports numpy before it parses arguments def test_cli_bad_numeric_arguments_are_usage_errors(self): """Bad --bpe/--tile/--widths/--tiles must be usage errors, not arithmetic tracebacks.""" cases = [ @@ -415,7 +420,6 @@ def test_cli_bad_numeric_arguments_are_usage_errors(self): self.assertIn(expected, result.stderr) self.assertNotIn("Traceback", result.stderr) - @unittest.skipUnless(HAVE_NUMPY, "numpy required") def test_cli_missing_input_is_an_error_not_a_traceback(self): with tempfile.TemporaryDirectory() as tmp: missing = os.path.join(tmp, "definitely_missing_path") @@ -427,7 +431,7 @@ def test_cli_missing_input_is_an_error_not_a_traceback(self): self.assertIn("definitely_missing_path", result.stderr) self.assertNotIn("Traceback", result.stderr) - @unittest.skipUnless(HAVE_NUMPY, "numpy required") # the tool imports numpy before it parses arguments + @unittest.skipUnless(NUMPY_VISIBLE_TO_TOOL, "numpy required") # the tool imports numpy before it parses arguments def test_preview_with_no_fitting_layout_reports_the_problem(self): """CLI with tiny blob and --out must exit 1, report 'no layout', and not create file.""" with tempfile.TemporaryDirectory() as td: @@ -444,7 +448,7 @@ def test_preview_with_no_fitting_layout_reports_the_problem(self): self.assertNotIn("Traceback", result.stderr) self.assertFalse(preview.exists()) - @unittest.skipUnless(HAVE_NUMPY, "numpy required") + @unittest.skipUnless(NUMPY_VISIBLE_TO_TOOL, "numpy required") def test_cli_successful_search_output(self): """CLI on valid synthetic buffer must exit 0 and print formatted scores and bit sequences.""" with tempfile.TemporaryDirectory() as td: @@ -473,7 +477,6 @@ def test_cli_successful_search_output(self): self.assertIn("bits(low->high)", result.stdout) self.assertNotIn("Traceback", result.stderr) - @unittest.skipUnless(HAVE_NUMPY and HAVE_PIL, "numpy and PIL required") def test_preview_rejects_input_and_hardlink_alias(self): """A preview must not overwrite the input or a hard link to it.""" for hardlink in (False, True): @@ -508,7 +511,7 @@ def test_preview_rejects_input_and_hardlink_alias(self): self.assertEqual(blob.read_bytes(), original) self.assertEqual(output.read_bytes(), original) - @unittest.skipUnless(HAVE_NUMPY and HAVE_PIL, "numpy and PIL required") + @unittest.skipUnless(NUMPY_VISIBLE_TO_TOOL and PIL_VISIBLE_TO_TOOL and HAVE_PIL, "isolated numpy and PIL required") def test_preview_ranks_thin_candidates_by_their_existing_edges(self) -> None: from PIL import Image @@ -666,6 +669,7 @@ def test_render_valueerror_unknown_extension_returns_exit_2(self): self.assertNotIn("Traceback", err.getvalue()) +@unittest.skipUnless(NUMPY_VISIBLE_TO_TOOL, "the tool runs with -I, which cannot see this numpy") def test_preview_with_no_fitting_layout_reports_the_problem(tmp_path=None): """Module-level compatibility wrapper for pytest with tmp_path fixture.""" if not HAVE_NUMPY: diff --git a/tests/test_tile_probe.py b/tests/test_tile_probe.py index ef40bb5db2..6f3dfd828b 100644 --- a/tests/test_tile_probe.py +++ b/tests/test_tile_probe.py @@ -15,6 +15,11 @@ ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOL = os.path.join(ROOT, "tools", "tile_probe.py") HAVE_NUMPY = importlib.util.find_spec("numpy") is not None + +# The probe tests launch the tool with `python -I`, which does not see a --user install of numpy; ask the way it runs, +# as HAVE_PIL_ISOLATED below already does. Without this the gate opens and the child dies on a missing module. +NUMPY_VISIBLE_TO_TOOL = subprocess.run([sys.executable, "-I", "-c", "import numpy"], + capture_output=True, check=False).returncode == 0 # the tool is documented as `python -I`, which does not see a --user install of Pillow HAVE_PIL_ISOLATED = subprocess.run([sys.executable, "-I", "-c", "import PIL"], capture_output=True, check=False).returncode == 0 @@ -59,6 +64,7 @@ def test_offset_at_or_past_the_end_is_a_usage_error(self): self.assertNotIn("Traceback", p.stderr) self.assertEqual(p.stdout, "") + @unittest.skipUnless(NUMPY_VISIBLE_TO_TOOL, "the tool runs with -I, which cannot see this numpy") def test_empty_file_is_a_short_sample_not_a_bad_offset(self): empty = os.path.join(self.tmp.name, "empty.bin") open(empty, "wb").close() @@ -72,6 +78,7 @@ def test_empty_file_is_a_short_sample_not_a_bad_offset(self): self.assertIn("is past the end of blob", p.stderr) self.assertIn("(0 bytes)", p.stderr) + @unittest.skipUnless(NUMPY_VISIBLE_TO_TOOL, "the tool runs with -I, which cannot see this numpy") def test_a_sample_with_no_candidate_layout_is_an_error(self): # Before: an 8-byte or 4 KiB blob printed nothing, wrote no preview and exited 0. tiny = os.path.join(self.tmp.name, "tiny.bin") @@ -120,6 +127,7 @@ def test_out_creates_a_missing_folder(self): self.assertTrue(os.path.isdir(os.path.dirname(preview)), "the preview's folder is created") @unittest.skipIf(HAVE_PIL_ISOLATED, "this interpreter's -I can import Pillow") + @unittest.skipUnless(NUMPY_VISIBLE_TO_TOOL, "the tool runs with -I, which cannot see this numpy") def test_preview_without_pillow_is_a_message_not_a_traceback(self): # Before: the probe ran, then render()'s lazy PIL import raised after all the work was done. p = self.run_tool(self.blob, "--out", os.path.join(self.tmp.name, "preview.png")) @@ -127,6 +135,7 @@ def test_preview_without_pillow_is_a_message_not_a_traceback(self): self.assertIn("Pillow", p.stderr) self.assertNotIn("Traceback", p.stderr) + @unittest.skipUnless(NUMPY_VISIBLE_TO_TOOL, "the tool runs with -I, which cannot see this numpy") def test_offset_inside_the_file_still_probes(self): p = self.run_tool(self.blob, "--offset", "0") self.assertEqual(p.returncode, 0, p.stderr) diff --git a/tests/test_tool_startup.py b/tests/test_tool_startup.py index eb1eb65099..341f4cf33d 100644 --- a/tests/test_tool_startup.py +++ b/tests/test_tool_startup.py @@ -53,7 +53,10 @@ class ToolStartupTests(unittest.TestCase): def test_each_sibling_importing_tool_starts(self): paths = sibling_importers() self.assertGreater(len(paths), 20, "the sibling-import scan found almost nothing") - have_numpy = importlib.util.find_spec("numpy") is not None + # Ask the way the tools are launched: `-I` does not see a --user install, so find_spec in this process can + # say yes while the child gets ModuleNotFoundError (tests/test_tile_probe.py carries the same correction). + have_numpy = subprocess.run([sys.executable, "-I", "-c", "import numpy"], + capture_output=True, check=False).returncode == 0 for path in paths: name = os.path.splitext(os.path.basename(path))[0] if name in NUMERIC and not have_numpy: @@ -61,6 +64,10 @@ def test_each_sibling_importing_tool_starts(self): with self.subTest(tool=name): proc = subprocess.run([sys.executable, "-I", path, "--help"], capture_output=True, text=True, timeout=TIMEOUT, errors="replace") + # A sibling that is not numeric itself can still import a numeric one: a list of names cannot cover + # those, and a missing third-party module is not a startup failure. CI has numpy, so it never fires. + if not have_numpy and "No module named 'numpy'" in proc.stderr: + continue self.assertEqual(proc.returncode, 0, proc.stderr[-2000:]) self.assertNotIn("Traceback", proc.stderr)