Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions funasr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
31 changes: 31 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from contextlib import redirect_stdout
from unittest.mock import patch

import pytest

from funasr import cli


Expand Down Expand Up @@ -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")
Expand Down
Loading