"""The copy of a tree that a mutation tool runs in.""" import os import pathlib import socket import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) from failure import Failure from task_bench.bugs.scratch_copy import scratch_copy class ScratchCopyTest(unittest.TestCase): def setUp(self): folder = tempfile.TemporaryDirectory() self.addCleanup(folder.cleanup) self.top = pathlib.Path(folder.name) (self.top / "src").mkdir() (self.top / "src/pick.ts").write_text("code", encoding="utf-8") (self.top / ".hidden").write_text("kept", encoding="utf-8") def test_copies_every_entry_of_the_tree_into_a_scratch_folder_in_it(self): scratch, copy = scratch_copy(str(self.top)) self.assertRegex(scratch, rf"^{self.top}/\.mutate-changes-[^/]+$") self.assertEqual(copy, f"{scratch}/copy") self.assertEqual(sorted(str(path.relative_to(copy)) for path in pathlib.Path(copy).rglob("*")), [".hidden", "src", "src/pick.ts"]) self.assertEqual(pathlib.Path(copy, "src/pick.ts").read_text(encoding="utf-8"), "code") def test_keeps_a_link_as_a_link(self): (self.top / "linked").symlink_to("src/missing.ts") _, copy = scratch_copy(str(self.top)) self.assertEqual(os.readlink(os.path.join(copy, "linked")), "src/missing.ts") def test_fails_without_a_scratch_folder_left_where_the_tree_cannot_be_copied(self): # A socket is an entry that no copy can read. held = socket.socket(socket.AF_UNIX) self.addCleanup(held.close) held.bind(str(self.top / "held")) found = scratch_copy(str(self.top)) self.assertIsInstance(found, Failure) self.assertRegex(found.reason, rf"^could not copy {self.top} into {self.top}/\.mutate-changes-[^/]+: ") self.assertEqual(sorted(path.name for path in self.top.iterdir()), [".hidden", "held", "src"]) def test_passes_on_that_no_scratch_folder_could_be_made(self): found = scratch_copy(str(self.top / "missing")) self.assertIsInstance(found, Failure) self.assertIn("could not make a scratch folder in ", found.reason)