diff --git a/tests/test_audio_wav.py b/tests/test_audio_wav.py index 4b024c069..5769eff95 100644 --- a/tests/test_audio_wav.py +++ b/tests/test_audio_wav.py @@ -4,6 +4,8 @@ The decode tests use synthetic audio and skip when ffmpeg is not on PATH. """ +import contextlib +import io import os from pathlib import Path import shutil @@ -483,6 +485,8 @@ class SameStemTest(unittest.TestCase): c = chr(n) self.assertEqual(audio_wav.fold_name(c), audio_wav.fold_name(os.path.normcase(c)), hex(n)) + +class FakeFfmpegDecodeTest(unittest.TestCase): def test_every_input_gets_its_own_wav(self): with tempfile.TemporaryDirectory() as tmp: src = os.path.join(tmp, "in") @@ -520,5 +524,78 @@ class SameStemTest(unittest.TestCase): self.assertIn("0 WAV written, 3 already present", p.stdout) +class AudioWavCliTest(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.dir = self.tmp.name + self.src_dir = os.path.join(self.dir, "in") + os.makedirs(self.src_dir) + + def tearDown(self): + self.tmp.cleanup() + + def test_cli_rejects_jobs_below_one(self): + for val in ("0", "-1", "-5"): + with self.subTest(val=val): + err = io.StringIO() + with contextlib.redirect_stderr(err), self.assertRaises(SystemExit) as cm: + audio_wav.main([self.src_dir, "--jobs", val]) + self.assertEqual(cm.exception.code, 2) + self.assertIn("--jobs must be at least 1", err.getvalue()) + + def test_cli_subprocess_rejects_jobs_below_one(self): + p = subprocess.run([sys.executable, "-I", TOOL, self.src_dir, "--jobs", "0"], + capture_output=True, text=True) + self.assertEqual(p.returncode, 2) + self.assertIn("--jobs must be at least 1", p.stderr) + + def test_cli_rejects_out_file(self): + out_file = os.path.join(self.dir, "out_file") + with open(out_file, "wb") as f: + f.write(b"content") + err = io.StringIO() + with contextlib.redirect_stderr(err), self.assertRaises(SystemExit) as cm: + audio_wav.main([self.src_dir, "--out", out_file]) + self.assertEqual(cm.exception.code, 2) + self.assertIn("is a file, not a folder", err.getvalue()) + + def test_cli_subprocess_rejects_out_file(self): + out_file = os.path.join(self.dir, "out_file") + with open(out_file, "wb") as f: + f.write(b"content") + p = subprocess.run([sys.executable, "-I", TOOL, self.src_dir, "--out", out_file], + capture_output=True, text=True) + self.assertEqual(p.returncode, 2) + self.assertIn("is a file, not a folder", p.stderr) + + def test_cli_rejects_missing_source(self): + missing = os.path.join(self.dir, "nonexistent") + err = io.StringIO() + with contextlib.redirect_stderr(err): + code = audio_wav.main([missing]) + self.assertEqual(code, 2) + self.assertIn(f"source not found: {missing}", err.getvalue()) + + def test_cli_rejects_non_opus_file_extension(self): + txt_file = os.path.join(self.dir, "test.txt") + with open(txt_file, "wb") as f: + f.write(b"hello") + err = io.StringIO() + with contextlib.redirect_stderr(err): + code = audio_wav.main([txt_file]) + self.assertEqual(code, 2) + self.assertIn("is not an .ogg/.opus file", err.getvalue()) + + def test_cli_rejects_file_not_opus(self): + bogus_ogg = os.path.join(self.dir, "test.ogg") + with open(bogus_ogg, "wb") as f: + f.write(b"NotOpusData" * 10) + err = io.StringIO() + with contextlib.redirect_stderr(err): + code = audio_wav.main([bogus_ogg]) + self.assertEqual(code, 2) + self.assertIn("is not Ogg Opus", err.getvalue()) + + if __name__ == "__main__": unittest.main() diff --git a/tools/audio_wav.py b/tools/audio_wav.py index 3a6fe3582..d7647cb31 100644 --- a/tools/audio_wav.py +++ b/tools/audio_wav.py @@ -413,14 +413,14 @@ def decode(ffmpeg: str, src: str, dst: str): return False, str(e) -def main() -> int: +def main(argv=None) -> int: ap = argparse.ArgumentParser(description="Decode Ogg Opus audio to 16-bit PCM WAV with ffmpeg.") ap.add_argument("src", help="a directory to search recursively, or one .ogg/.opus file") ap.add_argument("--out", help="output directory (default: /wav for a directory, next to a single file)") ap.add_argument("--jobs", type=int, default=max(1, (os.cpu_count() or 2) // 2), help="ffmpeg processes to run at once (default: half the CPU count)") ap.add_argument("--ffmpeg", help="ffmpeg executable (default: ffmpeg on PATH)") - a = ap.parse_args() + a = ap.parse_args(argv) if a.jobs < 1: ap.error("--jobs must be at least 1") @@ -442,6 +442,9 @@ def main() -> int: error(f"source not found: {a.src}") return 2 + if a.out and os.path.isfile(a.out): + ap.error(f"{a.out} is a file, not a folder") + out = os.path.abspath(a.out or default_out) print(f"{len(items)} Ogg Opus file(s) under {src} -> {out}") if not items: