import os, subprocess, sys, tempfile, unittest, wave HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.dirname(HERE)) import suite_song as s SCRIPT = os.path.join(os.path.dirname(HERE), "suite_song.py") class Classify(unittest.TestCase): def test_formats(self): cases = { "12:00 smoke rc=0": True, "negctl rc=1 (expected 1)": True, "diag rc=1": False, '{"verdict": "FAIL", "scenario": "6"}': False, '{"verdict": "PASS"}': True, "ok 3 - parses": True, "not ok 4 - breaks": False, "test_x ... FAILED": False, "build ready": None, "": None, } for line, want in cases.items(): self.assertEqual(s.classify(line), want, line) class EndToEnd(unittest.TestCase): def run_cli(self, text, *args): return subprocess.run([sys.executable, SCRIPT, *args], input=text, capture_output=True, text=True) def test_stdin_to_valid_wav(self): with tempfile.TemporaryDirectory() as d: out = os.path.join(d, "x.wav") r = self.run_cli("a rc=0\nb rc=1\nc rc=0\n", "-", "-o", out) self.assertEqual(r.returncode, 0, r.stderr) self.assertIn("1 unexpected failure", r.stdout) with wave.open(out) as w: self.assertEqual((w.getnchannels(), w.getsampwidth(), w.getframerate()), (1, 2, s.RATE)) self.assertGreater(w.getnframes(), s.RATE) def test_nothing_recognised_writes_nothing(self): with tempfile.TemporaryDirectory() as d: out = os.path.join(d, "x.wav") r = self.run_cli("hello\nworld\n", "-", "-o", out) self.assertEqual(r.returncode, 1) self.assertFalse(os.path.exists(out)) self.assertEqual(os.listdir(d), []) def test_missing_file_and_help(self): self.assertEqual(self.run_cli("", "/nonexistent.log", "-o", "/tmp/x.wav").returncode, 2) r = self.run_cli("", "--help") self.assertEqual(r.returncode, 0) self.assertIn("Example", r.stdout) def test_note_cap(self): self.assertLess(len(s.render([True] * 5000)), s.RATE * 60) if __name__ == "__main__": unittest.main()