From 0bb570958e5dc91b38321064ef4dd132ddd6df72 Mon Sep 17 00:00:00 2001 From: Shadman PC Date: Sun, 22 Nov 2026 14:05:30 +0600 Subject: [PATCH] audio_wav.py: validate CLI --jobs and --out arguments in main() audio_wav.py previously clamped --jobs below 1 silently to 1 instead of raising a command-line error, allowed main() only without arguments, and did not validate --out when pointing to an existing file before attempting to create directories. - main(argv=None) now accepts programmatic arguments. - --jobs < 1 is rejected with ap.error('--jobs must be at least 1'). - --out pointing to an existing file is rejected with ap.error. - Extracted FakeFfmpegDecodeTest in tests/test_audio_wav.py so fake decode checks run without requiring ffmpeg on PATH. - Added AudioWavCliTest covering all CLI validation error paths. --- tests/test_audio_wav.py | 77 +++++++++++++++++++++++++++++++++++++++++ tools/audio_wav.py | 11 ++++-- 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/tests/test_audio_wav.py b/tests/test_audio_wav.py index cd92aebf4..bc230ad31 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 @@ -427,6 +429,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") @@ -464,5 +468,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 208f4bdb2..db228384a 100644 --- a/tools/audio_wav.py +++ b/tools/audio_wav.py @@ -413,14 +413,16 @@ 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") src = os.path.abspath(a.src) if os.path.isfile(src): @@ -440,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: @@ -462,7 +467,7 @@ def main() -> int: if repaired: print(f"{repaired} file(s) needed distinct names on the output filesystem or beside an unverified WAV.") - jobs = max(1, a.jobs) + jobs = a.jobs t0 = time.time() written = skipped = 0 todo = []