import pathlib import subprocess import sys import tempfile import threading import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from failure import Failure from wait_for import main TOOL = pathlib.Path(__file__).resolve().parents[1] / "wait-for" class Clock: """A fake clock whose sleep advances time and runs the next of the steps given, one per sleep.""" def __init__(self, *steps): self.now = 0.0 self.steps = list(steps) def monotonic(self) -> float: return self.now def sleep(self, seconds: float): self.now += seconds if self.steps: self.steps.pop(0)() class WaitFor(unittest.TestCase): def setUp(self): self.file = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory()), "report.md") def test_prints_the_first_line_matching_the_pattern_once_the_file_holds_it(self): clock = Clock(lambda: self.file.write_text("running\n"), lambda: self.file.write_text("running\n== 3 passed in 1s ==\nFull log: x\n")) self.assertEqual(main.waited(str(self.file), r"passed|failed", 60, clock), "== 3 passed in 1s ==") self.assertEqual(clock.now, 2 * main.POLL_SECONDS) def test_without_a_pattern_prints_the_last_line_once_the_file_holds_anything(self): clock = Clock(lambda: self.file.write_text(""), lambda: self.file.write_text("# Report\nNo findings.\n")) self.assertEqual(main.waited(str(self.file), None, 60, clock), "No findings.") def test_a_file_already_holding_the_line_returns_at_once(self): self.file.write_text("done\n") clock = Clock() self.assertEqual(main.waited(str(self.file), "done", 60, clock), "done") self.assertEqual(clock.now, 0) def test_gives_up_at_the_timeout_naming_the_file_and_its_last_line(self): self.file.write_text("still running\n") clock = Clock() self.assertEqual(main.waited(str(self.file), "passed", 30, clock), Failure(f"{self.file} held no line matching `passed` after 30 s; its last line: still running")) self.assertLessEqual(clock.now, 30) def test_gives_up_on_a_file_that_never_appears(self): self.assertEqual(main.waited(str(self.file), None, 5, Clock()), Failure(f"{self.file} did not appear in 5 s")) def test_the_command_waits_for_a_file_another_process_writes(self): writer = threading.Timer(0.5, lambda: self.file.write_text("== 1 passed in 0.1s ==\n")) writer.start() self.addCleanup(writer.cancel) result = subprocess.run([sys.executable, str(TOOL), str(self.file), "passed", "--timeout", "20"], capture_output=True, text=True) self.assertEqual((result.returncode, result.stdout, result.stderr), (0, "== 1 passed in 0.1s ==\n", "")) def test_refuses_a_bad_pattern_timeout_or_argument_count_waiting_for_nothing(self): for argv in ([], [str(self.file), "("], [str(self.file), "--timeout", "0"], [str(self.file), "--timeout", "x"], [str(self.file), "a", "b"]): with self.subTest(argv=argv): result = subprocess.run([sys.executable, str(TOOL), *argv], capture_output=True, text=True, timeout=10) self.assertEqual((result.returncode, result.stdout), (1, "")) self.assertIn("wait-for: ", result.stderr) if __name__ == "__main__": unittest.main()