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
10 changes: 9 additions & 1 deletion src/nskit/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
_print_tree(entry, console, prefix + (" " if is_last else "│ "))


def create_cli(

Check failure on line 120 in src/nskit/cli/app.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 209 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=djpugh_nskit&issues=AaAANrofONzRfOHPSTDx&open=AaAANrofONzRfOHPSTDx&pullRequest=114
recipe_entrypoint: str,
app_name: str = "nskit",
app_help: str = "CLI for managing nskit recipes.",
Expand Down Expand Up @@ -150,8 +150,12 @@
backend = None

@app.command(name="list", help="List available recipes.")
def list_recipes():
def list_recipes(
json_output: Annotated[bool, typer.Option("--json", help="Output as JSON array of recipe names.")] = False,
):
"""List available recipes from backend or installed entry points."""
import json as json_mod

if client:
recipes = client.list_recipes()
else:
Expand All @@ -161,6 +165,10 @@
names = get_extension_names(recipe_entrypoint)
recipes = [RecipeInfo(name=n, versions=["local"]) for n in names]

if json_output:
print(json_mod.dumps(sorted(r.name for r in recipes)))
return

if not recipes:
rich_print("[yellow]No recipes found[/yellow]")
return
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/test_cli/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,46 @@ def test_no_push_when_declined(self, tmp_path):
_commit_and_maybe_push(project, "proj", "", False, mock_vcs, console)

mock_vcs.create.assert_not_called()


class TestListJsonFlag:
"""``list --json`` outputs a machine-readable JSON array."""

def test_list_json_exits_zero(self, runner):
app = create_cli(recipe_entrypoint="nskit.recipes")
result = runner.invoke(app, ["list", "--json"])
assert result.exit_code == 0, result.output

def test_list_json_outputs_valid_json(self, runner):
import json

app = create_cli(recipe_entrypoint="nskit.recipes")
result = runner.invoke(app, ["list", "--json"])
data = json.loads(result.output)
assert isinstance(data, list)

def test_list_json_contains_registered_recipes(self, runner):
import json

app = create_cli(recipe_entrypoint="nskit.recipes")
result = runner.invoke(app, ["list", "--json"])
data = json.loads(result.output)
assert "python_package" in data

def test_list_json_is_sorted(self, runner):
import json

app = create_cli(recipe_entrypoint="nskit.recipes")
result = runner.invoke(app, ["list", "--json"])
data = json.loads(result.output)
assert data == sorted(data)

def test_list_without_json_shows_table(self, runner):
"""Without --json, output is a rich table (not JSON)."""
import json

app = create_cli(recipe_entrypoint="nskit.recipes")
result = runner.invoke(app, ["list"])
assert result.exit_code == 0
with pytest.raises(json.JSONDecodeError):
json.loads(result.output)
Loading