diff --git a/tests/test_world_interior_names.py b/tests/test_world_interior_names.py index 9766b35e5..6a3afe59c 100644 --- a/tests/test_world_interior_names.py +++ b/tests/test_world_interior_names.py @@ -362,6 +362,28 @@ class CliTest(unittest.TestCase): self.assertEqual(f.read(), data, p) self.assertFalse(os.path.exists(os.path.join(folder, "r.json"))) + def test_out_inside_another_case_of_the_carve_folder_is_refused(self): + # normcase(realpath()) keeps the typed case on macOS, so CARVED/r.json used to be outside carved. + folder = os.path.join(self.tmp.name, "carved") + os.makedirs(folder) + probe = os.path.join(folder, "Probe.tmp") + with open(probe, "wb"): + pass + insensitive = os.path.exists(os.path.join(folder, "probe.tmp")) + os.remove(probe) + if not insensitive: + self.skipTest("volume keeps case") + out = os.path.join(self.tmp.name, "CARVED", "r.json") + err = io.StringIO() + sys.stderr, saved = err, sys.stderr + try: + with redirect_stdout(io.StringIO()): + self.assertEqual(T.main([folder, "--names", self.names, "--out", out]), 2) + finally: + sys.stderr = saved + self.assertIn("would overwrite an input", err.getvalue()) + self.assertFalse(os.path.exists(os.path.join(folder, "r.json"))) + def fails_with_one_line(self, argv): """main(argv) returns 2 with exactly one stderr line, prints nothing and writes no report.""" out = os.path.join(self.tmp.name, "fail", "r.json") diff --git a/tools/world_interior_names.py b/tools/world_interior_names.py index 9481678d9..10fa685eb 100644 --- a/tools/world_interior_names.py +++ b/tools/world_interior_names.py @@ -45,7 +45,7 @@ if _TOOLS_DIR not in sys.path: import world_archetypes as WA # noqa: E402 import world_index as WI # noqa: E402 from pool_workers import process_workers # noqa: E402 -from report_paths import same_report_path # noqa: E402 +from report_paths import path_key, same_report_path # noqa: E402 from world_names import hash64, joaat # noqa: E402 J = WA.joaat_cs @@ -364,11 +364,16 @@ def _inside(folder, path): """True when ``folder`` is a folder and ``path`` lies in it (a report there would be read as a carved file).""" if not os.path.isdir(folder): return False - root = os.path.normcase(os.path.realpath(folder)) + # path_key folds case on a volume that ignores it. normcase(realpath()) keeps the typed case on macOS. try: - return os.path.commonpath([root, os.path.normcase(os.path.realpath(path))]) == root - except ValueError: # different drives on Windows + root = path_key(folder) + target = path_key(path) + except OSError: return False + if target == root: + return True + prefix = root if root.endswith(os.sep) else root + os.sep + return target.startswith(prefix) def main(argv=None):