Skip to content

feat(cli): harden test inventory discovery and UX (#172)#1066

Merged
muddlebee merged 6 commits intoTracer-Cloud:mainfrom
Davidson3556:feat/harden-cli-test-inventory-172
Apr 29, 2026
Merged

feat(cli): harden test inventory discovery and UX (#172)#1066
muddlebee merged 6 commits intoTracer-Cloud:mainfrom
Davidson3556:feat/harden-cli-test-inventory-172

Conversation

@Davidson3556
Copy link
Copy Markdown
Contributor

@Davidson3556 Davidson3556 commented Apr 29, 2026

Fixes #172

Describe the changes you have made in this PR -

Hardened the opensre tests CLI command to be more reliable and give clearer errors in every environment.

discover.py — replaced the fragile "\n{target}:" string search with re.search(..., re.MULTILINE) so Makefile target detection works correctly regardless of position in the file.

commands/tests.py — three improvements:

  • Added synthetic to the valid --category choices so opensre tests list --category synthetic no longer exits with "invalid choice"
  • Wrapped run_interactive_picker so a RuntimeError (raised when questionary is missing or there is no tty) becomes a structured OpenSREError with an actionable suggestion instead of a raw traceback
  • Added an is_runnable check in tests run so passing a suite ID gives a clear error ("is a suite and cannot be run directly") with a suggestion to use opensre tests list

runner.pyrun_catalog_items now prints "Skipping '<id>' — no runnable command defined." instead of silently dropping non-runnable items.

test_cli_inventory.py — expanded from 2 tests to 16, covering: always-on list, stable IDs, category filtering, search filtering, empty-search result, JSON output structure, JSON + category combined, unknown-ID helpful error, no-subcommand non-interactive fallback, missing-TUI graceful degradation, format_command rendering for make targets and opensre subcommands, and non-runnable skip message.

Demo/Screenshot for feature changes and bug fixes -

https://www.loom.com/share/530c7020bbcc46ea8ef14cb1dc00b6ac

Test results: 18/18 new tests pass, 3806/3806 suite tests pass, lint and typecheck clean.


Code Understanding and AI Usage

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

  • 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:

The core problem was that the CLI had three silent failure modes: (1) discovery used a brittle string search that could miss targets at the start of the Makefile, (2) missing TUI dependencies produced a raw Python traceback instead of a helpful message, and (3) passing an unknown or non-runnable ID gave no guidance on what to do next.

For discovery I switched to re.search with re.MULTILINE — the simplest fix that matches how Makefile targets are actually defined. I considered parsing the Makefile more deeply but that would be over-engineering for what is a simple presence check.

For error handling I chose to catch RuntimeError at the command layer (commands/tests.py) and re-raise as OpenSREError, which already has the project's standard formatting and suggestion field. This keeps error-handling logic out of the interactive module and in the place that owns CLI output.

The test expansion follows the existing pattern in the file (CliRunner invocations) and adds one direct unit test for run_catalog_items using a manually constructed TestCatalogItem with no command, which is the simplest way to exercise the skip path without needing a real subprocess.


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

- Use re.MULTILINE regex in discover.py for reliable Makefile target detection
- Add 'synthetic' to valid --category choices in tests list
- Convert RuntimeError from interactive picker to structured OpenSREError with actionable suggestion
- Guard non-runnable suite IDs in tests run with a clear error message
- Print skip message in run_catalog_items instead of silently dropping items
- Expand test_cli_inventory.py from 2 to 16 tests covering filtering, stable IDs, JSON output, unknown-ID errors, missing-TUI graceful degradation, and command rendering
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 29, 2026

Greptile Summary

This PR hardens the opensre tests CLI command across four files: it fixes Makefile target detection using re.MULTILINE, adds synthetic to valid --category choices, wraps the interactive picker so a missing TUI degrades to a structured OpenSREError, guards tests run against non-runnable suite IDs, and logs a skip notice for non-runnable items in run_catalog_items. The test file grows from 2 to 16 tests that cover the new behaviours comprehensively. Both remaining comments are P2 style suggestions (analytics placement and stdout vs. stderr for the skip message) and do not block merge.

Confidence Score: 5/5

Safe to merge — all remaining findings are P2 style suggestions with no correctness impact

All four bug-fix targets (MULTILINE regex, synthetic category, TUI degradation, suite guard) are correctly implemented and well-tested. The only open comments are a misplaced analytics call and a stdout-vs-stderr placement, neither of which affects correctness or data integrity.

No files require special attention

Important Files Changed

Filename Overview
app/cli/commands/tests.py Added synthetic to category choices, wrapped interactive picker in RuntimeError handler, and added is_runnable guard before running a test — all changes are correct
app/cli/tests/discover.py Switched Makefile target detection from "\n{target}:" string search to re.search(…, re.MULTILINE) — correctly handles targets at the start of the file
app/cli/tests/runner.py Added skip message for non-runnable items; message goes to stdout (minor: could pollute piped output) — otherwise correct
tests/cli/test_cli_inventory.py Expanded from 2 to 16 tests covering list, category filtering, JSON output, helpful errors, TUI degradation, and skip message — good coverage of the new behaviours

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["opensre tests (no subcommand)"] --> B{is_yes or is_json_output?}
    B -- Yes --> C[raise OpenSREError\n'No subcommand provided']
    B -- No --> D[run_interactive_picker]
    D -- RuntimeError\nmissing TUI / no tty --> E[catch RuntimeError\nraise OpenSREError\nwith suggestion]
    D -- returns int --> F[raise SystemExit]

    G["opensre tests list"] --> H[load_test_catalog]
    H --> I{--category filter}
    I -- synthetic ✅ --> J[filter catalog]
    I -- rca / ci-safe / etc --> J
    J --> K[echo or JSON output]

    L["opensre tests run <id>"] --> M[find_test_item]
    M -- None --> N[OpenSREError: Unknown id]
    M -- found --> O{item.is_runnable?}
    O -- No --> P[OpenSREError: is a suite]
    O -- Yes --> Q[run_catalog_item]

    R[run_catalog_items] --> S{item.is_runnable?}
    S -- No --> T["print: Skipping '<id>'"]
    S -- Yes --> U[run_catalog_item]
Loading

Reviews (1): Last reviewed commit: "feat(cli): harden test inventory discove..." | Re-trigger Greptile

Comment thread app/cli/tests/runner.py Outdated
worst = 0
for item in items:
if not item.is_runnable:
print(f"Skipping '{item.id}' — no runnable command defined.")
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 Skip message goes to stdout instead of stderr

print() writes to stdout, so the skip notice will appear in the output stream alongside any real test output. If a caller pipes or captures stdout (e.g., opensre tests run … | jq), the diagnostic text will pollute the data stream. Writing to stderr keeps the message visible to the user without contaminating captured stdout.

Suggested change
print(f"Skipping '{item.id}' — no runnable command defined.")
import sys
print(f"Skipping '{item.id}' — no runnable command defined.", file=sys.stderr)

Comment thread app/cli/commands/tests.py Outdated
- Write skip notice to stderr instead of stdout in run_catalog_items so it
  doesn't pollute captured output when callers pipe or capture stdout
- Move capture_tests_picker_opened() inside the try block so the analytics
  event only fires when the interactive picker actually opens, not on
  degraded/non-tty paths that immediately raise RuntimeError
- Update test assertion to check stderr for the skip message
@Davidson3556 Davidson3556 force-pushed the feat/harden-cli-test-inventory-172 branch from ce30fed to e0d1cae Compare April 29, 2026 04:59
@Davidson3556
Copy link
Copy Markdown
Contributor Author

@muddlebee kindly review

Comment thread app/cli/commands/tests.py Outdated
Comment thread app/cli/commands/tests.py Outdated
Comment thread tests/cli/test_cli_inventory.py Outdated
Comment thread tests/cli/test_cli_inventory.py Outdated
- Move capture_tests_picker_opened() after picker returns so analytics
  only fires on successful open, not on RuntimeError degraded paths
- Replace 'select a specific child' suggestion with clearer wording
  that matches what the CLI actually exposes
- Drop make:test-grafana assertion from stable-IDs test since that
  target is Makefile-dependent and not universally present in CI
- Replace output-length comparison in search filter test with explicit
  ID presence/absence assertions
@Davidson3556
Copy link
Copy Markdown
Contributor Author

@muddlebee kindly review

Comment thread tests/cli/test_cli_inventory.py Outdated
@Davidson3556
Copy link
Copy Markdown
Contributor Author

@muddlebee please review

@muddlebee
Copy link
Copy Markdown
Collaborator

@Davidson3556 thank you. I will just review a bit more so edge cases aren't missed. thank you for patience

@muddlebee
Copy link
Copy Markdown
Collaborator

@Davidson3556 can you add a video demo using opensre tests and showcase it runs as the goal in issue

Make opensre tests discovery more reliable, improve missing-dependency UX, and add focused test coverage for catalog stability, filtering, and execution.

the screenshots doesnt tell much

Comment thread app/cli/tests/discover.py
Comment thread tests/cli/test_cli_inventory.py
- Add regression test for re.MULTILINE fix in discover_make_targets:
  mocks MAKEFILE_PATH with a target at line 1 (no preceding newline)
  and asserts it is discovered — catches any revert to the old string check
- Strengthen synthetic category test to assert a known stable ID
  (synthetic:001-replication-lag) is present, not just exit_code == 0
Comment thread tests/cli/test_discover.py Fixed
@Davidson3556
Copy link
Copy Markdown
Contributor Author

Davidson3556 commented Apr 29, 2026

Screenshot 2026-04-28 at 23 51 00 i made the video with loom but it looks like loom is down. Screenshot 2026-04-28 at 23 52 09 Screenshot 2026-04-28 at 23 53 14 Screenshot 2026-04-29 at 00 06 53

@muddlebee kindly review

@Davidson3556
Copy link
Copy Markdown
Contributor Author

i drop all the screenshot instead @muddlebee

@Davidson3556
Copy link
Copy Markdown
Contributor Author

@muddlebee
Copy link
Copy Markdown
Collaborator

@Davidson3556 thank you for sharing demo. you can just record in your system and push the video to github or dropdown here. that also works

@muddlebee
Copy link
Copy Markdown
Collaborator

and the demo looks smooth 👍 great, nice work

Copy link
Copy Markdown
Collaborator

@muddlebee muddlebee left a comment

Choose a reason for hiding this comment

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

LGTM. I will wait for additional reviews from maintainers before merge..

@Davidson3556
Copy link
Copy Markdown
Contributor Author

@davincios @rrajan94 please kindly review

@muddlebee muddlebee merged commit 5e0d007 into Tracer-Cloud:main Apr 29, 2026
7 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

🎉 MERGED! @Davidson3556 just shipped something. The diff gods are pleased. 🙌


👋 Join us on Discord - OpenSRE : hang out, contribute, or hunt for features and issues. Everyone's welcome.

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.

Harden OpenSRE CLI Test Inventory and User Experience

4 participants