Skip to content

test(cli): add direct unit tests for CLI layout renderers#1015

Open
7vignesh wants to merge 2 commits intoTracer-Cloud:mainfrom
7vignesh:issue/875-direct-cli-layout-tests
Open

test(cli): add direct unit tests for CLI layout renderers#1015
7vignesh wants to merge 2 commits intoTracer-Cloud:mainfrom
7vignesh:issue/875-direct-cli-layout-tests

Conversation

@7vignesh
Copy link
Copy Markdown
Contributor

Fixes #875

Added direct tests for the CLI layout in app/cli/layout.py:

  • cover render_help()
  • cover render_landing()
  • cover RichGroup.format_help()
  • add a sync check for root command drift
  • keep help/landing metadata aligned with registered commands

Demo/Screenshot for feature changes and bug fixes -

Validation run:

  • python -m pytest tests/cli/test_layout.py -q
  • python -m pytest tests/cli -q
  • python -m ruff check app/ tests/
  • python -m ruff format --check app/ tests/
  • python -m mypy app/

Code Understanding and AI Usage

Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?

  • No, I wrote all the code myself
  • Yes, I used AI assistance (continue below)

If you used AI assistance:

  • I have reviewed every single line of the AI-generated code
  • I can explain the purpose and logic of each function/component I added
  • I have tested edge cases and understand how the code handles them
  • I have modified the AI output to follow this project's coding standards and conventions

Explain your implementation approach:
I added focused tests for the CLI layout renderers and command sync, using content checks instead of full output snapshots so the tests stay stable.


Checklist before requesting a review

  • I have added proper PR title and linked to the issue
  • I have performed a self-review of my code
  • I can explain the purpose of every function, class, and logic block I added
  • I understand why my changes work and have tested them thoroughly
  • I have considered potential edge cases and how my code handles them
  • If it is a core feature, I have added thorough tests
  • My code follows the project's style guidelines and conventions

Copilot AI review requested due to automatic review settings April 27, 2026 20:06
Comment thread tests/cli/test_layout.py Fixed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds direct unit tests for the CLI’s custom Rich-based help/landing renderers and introduces a command-metadata sync guard to catch root command drift (Fixes #875).

Changes:

  • Add tests/cli/test_layout.py to exercise render_help(), render_landing(), and RichGroup.format_help().
  • Add a test ensuring _HELP_COMMANDS stays aligned with the actual registered root commands.
  • Update CLI layout metadata to include the guardrails command in help and landing “Quick start” content.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
tests/cli/test_layout.py Adds focused unit tests for Rich CLI layout rendering and command drift detection.
app/cli/layout.py Updates help/landing command lists to include guardrails.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/cli/test_layout.py
Comment on lines +30 to +33
for label, description in _HELP_COMMANDS:
assert label in output
assert description in output

Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output is not normalized here, so Rich line-wrapping can split long descriptions (e.g., the update help text) across newlines, making assert description in output fail depending on console width. Consider normalizing whitespace (like _normalized_output) before asserting, or otherwise making the assertions robust to wrapped output.

Copilot uses AI. Check for mistakes.
Comment thread tests/cli/test_layout.py Outdated
Comment on lines +3 to +4
from unittest.mock import MagicMock

Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import: MagicMock is not referenced in this test module. Even though tests ignore F401, removing it will keep the file cleaner and avoid confusion.

Suggested change
from unittest.mock import MagicMock

Copilot uses AI. Check for mistakes.
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 27, 2026

Greptile Summary

This PR adds direct unit tests for app/cli/layout.py (covering render_help, render_landing, RichGroup.format_help, and a command-drift sync check) and registers the guardrails command in both _HELP_COMMANDS and _LANDING_COMMANDS to keep metadata aligned with the already-registered CLI command. The changes are clean and well-scoped; only two minor P2 style points were found.

Confidence Score: 5/5

Safe to merge — all findings are P2 style/cleanup with no correctness or reliability impact.

Both findings are P2: one unused import silently ignored by the per-file ruff config, and one inconsistency in output normalization between two test helpers. Neither affects test correctness or production behavior.

No files require special attention.

Important Files Changed

Filename Overview
tests/cli/test_layout.py New test file covering render_help, render_landing, RichGroup.format_help, and a command-sync drift check; one unused MagicMock import and minor inconsistency in output normalization, both P2 style only.
app/cli/layout.py Adds guardrails entry to both _HELP_COMMANDS and _LANDING_COMMANDS, matching the already-registered guardrails command in app/cli/commands/init.py — straightforward metadata update with no issues.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[test_render_help_shows_root_commands] -->|calls| B[render_help]
    B -->|reads| C[_HELP_COMMANDS]
    B -->|reads| D[_SHORT_OPTIONS]

    E[test_render_landing_shows_root_commands_and_header] -->|calls| F[render_landing]
    F -->|reads| G[_LANDING_COMMANDS]
    F -->|reads| D

    H[test_help_command_names_match_layout_metadata] -->|reads| I[cli.commands.keys]
    H -->|compares against| C

    J[test_rich_group_format_help_delegates_to_render_help] -->|monkeypatches| B
    J -->|invokes| K[RichGroup.format_help]
    K -->|delegates to| B
Loading

Reviews (1): Last reviewed commit: "test(cli): cover layout renderers" | Re-trigger Greptile

Comment thread tests/cli/test_layout.py Outdated
@@ -0,0 +1,75 @@
from __future__ import annotations

from unittest.mock import MagicMock
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Unused import MagicMock

MagicMock is imported but never referenced anywhere in the file. The ruff.toml per-file rule "tests/**/*.py" = ["F401", ...] silently suppresses the warning, but it's still dead code that could mislead future readers into thinking a mock is in use.

Comment thread tests/cli/test_layout.py
Comment on lines +22 to +36
def test_render_help_shows_root_commands(capsys) -> None:
render_help()
output = capsys.readouterr().out

assert "Usage: opensre [OPTIONS] COMMAND [ARGS]..." in output
assert "Commands:" in output
assert "Options:" in output

for label, description in _HELP_COMMANDS:
assert label in output
assert description in output

for label, description in _SHORT_OPTIONS:
assert label in output
assert description in output
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Inconsistent output normalization across render tests

test_render_landing_shows_root_commands_and_header passes the captured output through _normalized_output() (collapsing all whitespace) before asserting, but test_render_help_shows_root_commands checks the raw output directly. With Rich's default 80-column non-TTY width, the help output currently fits without wrapping — but any future widening of labels or descriptions could silently break only this test while the landing test stays green. Consider applying _normalized_output consistently to both, or adding a comment noting why the raw form is safe here.

@7vignesh
Copy link
Copy Markdown
Contributor Author

@VaibhavUpreti the pr is ready for review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add direct unit tests for app/cli/layout.py

3 participants