diff --git a/funasr/cli.py b/funasr/cli.py index cf9cad319..bdc59fb4a 100644 --- a/funasr/cli.py +++ b/funasr/cli.py @@ -617,6 +617,11 @@ def main(): p.add_argument("--version", action="version", version=f"%(prog)s {_get_version()}") args = p.parse_args() + for audio_path in args.audio: + if not os.path.isfile(audio_path): + print(f"Error: file not found: {audio_path}", file=sys.stderr) + sys.exit(1) + if args.verbose: print(f"Loading model: {args.model} ...", file=sys.stderr) @@ -641,10 +646,6 @@ def main(): os.makedirs(args.output_dir, exist_ok=True) for audio_path in args.audio: - if not os.path.isfile(audio_path): - print(f"Error: file not found: {audio_path}", file=sys.stderr) - sys.exit(1) - if args.verbose: print(f"Transcribing: {audio_path}", file=sys.stderr) diff --git a/tests/test_cli.py b/tests/test_cli.py index f6fe42054..8301c5f90 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,6 +4,8 @@ from contextlib import redirect_stdout from unittest.mock import patch +import pytest + from funasr import cli @@ -49,6 +51,35 @@ def generate(self, **kwargs): ] +@pytest.mark.parametrize("input_kind", ["missing", "directory", "later_missing"]) +def test_cli_rejects_invalid_audio_paths_before_loading_model(tmp_path, capsys, input_kind): + valid_path = tmp_path / "first.wav" + valid_path.write_bytes(b"not a real wav") + invalid_path = tmp_path / "missing.wav" + if input_kind == "directory": + invalid_path.mkdir() + paths = [str(invalid_path)] + if input_kind == "later_missing": + paths.insert(0, str(valid_path)) + out_dir = tmp_path / "output" + fake_torch = types.SimpleNamespace( + cuda=types.SimpleNamespace(is_available=lambda: False), + ) + + with ( + patch.object(sys, "argv", ["funasr", *paths, "-o", str(out_dir)]), + patch.dict(sys.modules, {"torch": fake_torch}), + patch("funasr.AutoModel", side_effect=AssertionError("model was loaded")) as model, + pytest.raises(SystemExit) as exc_info, + ): + cli.main() + + assert exc_info.value.code == 1 + assert capsys.readouterr().err == f"Error: file not found: {invalid_path}\n" + model.assert_not_called() + assert not out_dir.exists() + + def test_cli_passes_hub_to_auto_model(tmp_path): audio_path = tmp_path / "sample.wav" audio_path.write_bytes(b"not a real wav")