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
18 changes: 9 additions & 9 deletions packages/skilllint/cli_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
15 changes: 15 additions & 0 deletions packages/skilllint/tests/test_cli_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from __future__ import annotations

import operator
import re
import subprocess
import sys
Expand Down Expand Up @@ -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)
Loading