Kill the surviving mutants of run-tests and wait-for, and name a file wait-for cannot read
CI / tests (pull_request) Successful in 2m38s
CI / tests (pull_request) Successful in 2m38s
This commit is contained in:
@@ -10,6 +10,7 @@ import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from machine_load.slots import CPUS_PER_SLOT
|
||||
from run_tests import main
|
||||
|
||||
BIN = pathlib.Path(__file__).resolve().parents[1]
|
||||
@@ -31,11 +32,73 @@ class PytestArgs(unittest.TestCase):
|
||||
self.assertEqual(main.pytest_args([str(test_file)]), [str(test_file)])
|
||||
self.assertEqual(main.pytest_args(["-x", f"{test_file}::T::test_a"]), ["-x", f"{test_file}::T::test_a"])
|
||||
|
||||
def test_the_pinned_command_is_readme_tests_incantation(self):
|
||||
self.assertEqual(main.PYTEST[1:], ["--python", main.PYTEST[2], "--with", "pytest-xdist==3.8.0", "pytest==9.2.0", "-n", "4"])
|
||||
def test_the_command_pins_pytest_and_fills_one_heavy_slot(self):
|
||||
self.assertEqual(main.PYTEST[1:], ["--python", main.PYTEST[2], "--with", "pytest-xdist==3.8.0", "pytest==9.2.0", "-n", str(CPUS_PER_SLOT)])
|
||||
self.assertEqual(main.PYTEST[0], "uvx")
|
||||
|
||||
|
||||
def errors(count: int) -> str:
|
||||
return "".join(f"E line {n}\n" for n in range(count))
|
||||
|
||||
|
||||
class Report(unittest.TestCase):
|
||||
def test_prints_each_failure_and_error_with_its_errors_then_the_summary(self):
|
||||
log = (
|
||||
"===== test session starts =====\nE not in a section\n"
|
||||
f"===== FAILURES =====\n_____ test_a _____\n\n def test_a():\n{errors(main.ERROR_LINES)}a.py:2: AssertionError\n"
|
||||
f"===== ERRORS =====\n_____ ERROR at setup of test_b _____\n{errors(main.ERROR_LINES + 3)}b.py:4: ValueError\n"
|
||||
"===== short test summary info =====\nFAILED a.py::test_a\nERROR b.py::test_b\n===== 1 failed, 1 error in 0.10s =====\n"
|
||||
)
|
||||
self.assertEqual(main.report(log), [
|
||||
"_____ test_a _____", *errors(main.ERROR_LINES).splitlines(), "a.py:2: AssertionError",
|
||||
"_____ ERROR at setup of test_b _____", *errors(main.ERROR_LINES).splitlines(), "E … 3 more lines in the log", "b.py:4: ValueError",
|
||||
"FAILED a.py::test_a", "ERROR b.py::test_b", "===== 1 failed, 1 error in 0.10s =====",
|
||||
])
|
||||
|
||||
def test_prints_nothing_where_pytest_printed_no_summary_line(self):
|
||||
self.assertEqual(main.report("usage: pytest\nerror: unrecognized arguments\n"), [])
|
||||
|
||||
|
||||
class RunWithAStandIn(unittest.TestCase):
|
||||
"""run with a stand-in for pytest: a Python script given the arguments run passes on."""
|
||||
|
||||
def run_script(self, script: str, cwd: str | None = None) -> tuple[int, str, str]:
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||
code = main.run(["-x"], [sys.executable, "-c", script], cwd)
|
||||
lines = out.getvalue().splitlines()
|
||||
if lines and lines[-1].startswith("Full log: "):
|
||||
self.addCleanup(os.unlink, lines[-1].removeprefix("Full log: "))
|
||||
return code, out.getvalue(), err.getvalue()
|
||||
|
||||
def test_saves_the_log_as_run_tests_star_log_in_the_temp_folder_which_ci_sh_prints(self):
|
||||
code, out, _ = self.run_script("print('== 1 passed in 0.1s ==')")
|
||||
self.assertEqual(code, 0)
|
||||
log = pathlib.Path(out.splitlines()[-1].removeprefix("Full log: "))
|
||||
self.assertEqual(log.parent, pathlib.Path(tempfile.gettempdir()))
|
||||
self.assertRegex(log.name, r"^run-tests-.+\.log$")
|
||||
|
||||
def test_runs_in_the_folder_given_and_shows_the_last_lines_of_a_log_without_a_summary(self):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
script = "import os, sys; sys.stdout.buffer.write(b'bad \\xff byte\\n'); print('\\n'.join(map(str, range(30)))); print(os.getcwd())"
|
||||
code, out, _ = self.run_script(script, folder)
|
||||
self.assertEqual(code, 0)
|
||||
tail = [str(n) for n in range(30 - main.LOG_TAIL + 1, 30)] + [os.path.realpath(folder)]
|
||||
self.assertEqual(out.splitlines()[:-1], ["run-tests: pytest printed no summary line; the log ends:", *tail])
|
||||
|
||||
def test_reads_a_log_that_is_not_utf_8(self):
|
||||
code, out, _ = self.run_script("import sys; sys.stdout.buffer.write(b'\\xff\\n== 1 failed in 0.1s ==\\n'); sys.exit(1)")
|
||||
self.assertEqual(code, 1)
|
||||
self.assertIn("== 1 failed in 0.1s ==", out)
|
||||
|
||||
def test_says_on_stderr_that_a_command_cannot_start(self):
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||
code = main.run([], ["/no/such/uvx"])
|
||||
self.assertEqual((code, out.getvalue()), (1, ""))
|
||||
self.assertEqual(err.getvalue(), "run-tests: cannot start /no/such/uvx: No such file or directory\n")
|
||||
|
||||
|
||||
@unittest.skipUnless(HAS_PYTEST, "needs pytest importable")
|
||||
class Run(unittest.TestCase):
|
||||
def run_in(self, folder: str, pytest: list[str], *args: str) -> tuple[int, str]:
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import contextlib
|
||||
import io
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -57,6 +59,51 @@ class WaitFor(unittest.TestCase):
|
||||
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_reads_a_line_that_is_not_utf_8(self):
|
||||
self.file.write_bytes(b"caf\xe9 passed\n")
|
||||
self.assertEqual(main.waited(str(self.file), "passed", 60, Clock()), "caf\ufffd passed")
|
||||
|
||||
def test_an_empty_file_gives_up_saying_it_held_nothing_and_no_last_line(self):
|
||||
self.file.write_text("")
|
||||
clock = Clock()
|
||||
self.assertEqual(main.waited(str(self.file), None, 3, clock), Failure(f"{self.file} held nothing after 3 s"))
|
||||
self.assertEqual(clock.now, 3)
|
||||
|
||||
def test_sleeps_only_the_time_left_before_the_timeout(self):
|
||||
clock = Clock()
|
||||
main.waited(str(self.file), None, 3, clock)
|
||||
self.assertEqual(clock.now, 3)
|
||||
clock = Clock()
|
||||
main.waited(str(self.file), None, 2.5, clock)
|
||||
self.assertEqual(clock.now, 2.5)
|
||||
|
||||
def test_a_folder_fails_at_once_naming_it(self):
|
||||
self.assertEqual(main.waited(str(self.file.parent), None, 60, Clock()), Failure(f"cannot read {self.file.parent}: Is a directory"))
|
||||
|
||||
def test_main_prints_the_line_or_the_reason_and_gives_the_exit_code(self):
|
||||
self.file.write_text("== 1 passed ==\n")
|
||||
for argv, expected in (
|
||||
([str(self.file), "passed", "--timeout", "5"], (0, "== 1 passed ==\n", "")),
|
||||
(["--help"], (0, main.__doc__.strip() + "\n", "")),
|
||||
([str(self.file), "("], (1, "", "wait-for: cannot read the pattern `(`: missing ), unterminated subpattern at position 0\n")),
|
||||
):
|
||||
with self.subTest(argv=argv):
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||
code = main.main(argv)
|
||||
self.assertEqual((code, out.getvalue(), err.getvalue()), expected)
|
||||
|
||||
def test_arguments_take_the_timeout_anywhere_and_refuse_a_bad_one(self):
|
||||
self.assertEqual(main.arguments(["--timeout", "1.5", "f", "p"]), ("f", "p", 1.5))
|
||||
self.assertEqual(main.arguments(["f"]), ("f", None, 540.0))
|
||||
for timeout in ("0", "-1", "x", "1.", None):
|
||||
with self.subTest(timeout=timeout):
|
||||
argv = ["f", "--timeout"] + ([timeout] if timeout else [])
|
||||
self.assertEqual(main.arguments(argv), Failure(f"give --timeout a number of seconds above 0; {main.USAGE}"))
|
||||
for argv in ([], ["f", "a", "b"]):
|
||||
with self.subTest(argv=argv):
|
||||
self.assertEqual(main.arguments(argv), Failure(main.USAGE))
|
||||
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user