diff --git a/packages/skilllint/cli_docs.py b/packages/skilllint/cli_docs.py index fdbca585..1baa509d 100644 --- a/packages/skilllint/cli_docs.py +++ b/packages/skilllint/cli_docs.py @@ -257,11 +257,11 @@ def sections( Output is written to stdout. """ + if file_path.exists() and not file_path.is_file(): + typer.echo(f"Expected a file path: {file_path}", err=True) + raise typer.Exit(code=2) from None if json_output: file_exists = file_path.exists() - if file_exists and not file_path.is_file(): - typer.echo(f"Expected a file path: {file_path}", err=True) - raise typer.Exit(code=2) from None emit_and_exit(build_sections_response(file_path, list_sections(file_path), file_exists=file_exists)) table = format_section_index(file_path) typer.echo(table) @@ -285,11 +285,11 @@ def section( Exit status: 1 when the heading is not found. """ + if file_path.exists() and not file_path.is_file(): + typer.echo(f"Expected a file path: {file_path}", err=True) + raise typer.Exit(code=2) from None if json_output: file_exists = file_path.exists() - if file_exists and not file_path.is_file(): - typer.echo(f"Expected a file path: {file_path}", err=True) - raise typer.Exit(code=2) from None found = find_section(file_path, heading) emit_and_exit( build_section_response(file_path, heading, found, file_exists=file_exists), @@ -322,11 +322,11 @@ def verify( Exit status: 1 when MODIFIED or UNVERIFIABLE. """ + if file_path.exists() and not file_path.is_file(): + typer.echo(f"Expected a file path: {file_path}", err=True) + raise typer.Exit(code=2) from None if json_output: file_exists = file_path.exists() - if file_exists and not file_path.is_file(): - typer.echo(f"Expected a file path: {file_path}", err=True) - raise typer.Exit(code=2) from None outcome = verify_integrity(file_path) emit_and_exit( build_verify_response(outcome, file_exists=file_exists), diff --git a/packages/skilllint/tests/test_cli_docs.py b/packages/skilllint/tests/test_cli_docs.py index 947d1543..3d45ec19 100644 --- a/packages/skilllint/tests/test_cli_docs.py +++ b/packages/skilllint/tests/test_cli_docs.py @@ -13,6 +13,7 @@ from __future__ import annotations +import operator import re import subprocess import sys @@ -761,3 +762,17 @@ def test_file_path_forwarded_to_verify_integrity(self, cli_runner: CliRunner, mo # Assert mock_verify.assert_called_once_with(Path(target)) + + +@pytest.mark.parametrize("args", [("sections",), ("section", "Usage"), ("verify",)], ids=operator.itemgetter(0)) +def test_directory_argument_is_a_usage_error_without_json( + args: tuple[str, ...], cli_runner: CliRunner, tmp_path: Path +) -> None: + """A directory gets the same usage error as under ``--json``, not an ``IsADirectoryError`` traceback (#315).""" + # Act + result = cli_runner.invoke(plugin_validator.app, ["docs", args[0], str(tmp_path), *args[1:]]) + + # Assert + assert result.exit_code == 2 + assert f"Expected a file path: {tmp_path}" in result.stderr + assert result.exception is None or isinstance(result.exception, SystemExit)