Skip to content

test: cover root command wiring#17

Open
guoqqqi wants to merge 2 commits intoapi7:mainfrom
guoqqqi:test/root-command-coverage
Open

test: cover root command wiring#17
guoqqqi wants to merge 2 commits intoapi7:mainfrom
guoqqqi:test/root-command-coverage

Conversation

@guoqqqi
Copy link
Copy Markdown
Contributor

@guoqqqi guoqqqi commented Apr 10, 2026

Summary

  • add root command tests for global flags and core subcommand registration
  • verify extension commands are discovered from the configured extensions directory
  • execute a loaded extension command to cover the root command handoff path

Validation

  • GOCACHE=/tmp/go-build-a6 GOMODCACHE=/tmp/gomod-a6 go test ./pkg/cmd/root

Refs #14

Summary by CodeRabbit

  • Tests
    • Added comprehensive tests for the root CLI that validate command metadata, required persistent flags, and discovery of core subcommands.
    • Added tests that verify extension discovery and execution via an external extension binary, confirming arguments are forwarded and extension metadata is surfaced (extension execution test is skipped on Windows).

Copilot AI review requested due to automatic review settings April 10, 2026 03:07
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 10, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a8098a0d-ef20-46cc-862a-eaf9f010eb53

📥 Commits

Reviewing files that changed from the base of the PR and between 0ab2b48 and ca548f7.

📒 Files selected for processing (1)
  • pkg/cmd/root/root_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/cmd/root/root_test.go

📝 Walkthrough

Walkthrough

Adds a new test file that verifies the root CLI registers core commands and persistent flags, and that extension commands described by manifest files can be discovered and executed.

Changes

Cohort / File(s) Summary
Root Command Tests
pkg/cmd/root/root_test.go
New tests: TestNewCmdRoot_RegistersCoreCommandsAndFlags checks root metadata, required persistent flags (output, context, server, api-key, verbose, force) and discovery of core subcommands; TestNewCmdRoot_LoadsAndExecutesExtensionCommands creates a filesystem-based extension (binary script + manifest.yaml via writeManifest) and verifies execution and command metadata. Includes helper writeManifest to write an extension.Manifest as YAML.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
E2e Test Quality Review ⚠️ Warning Tests cover happy path E2E flow but lack critical failure scenario coverage including malformed manifests, missing binaries, execution errors, and error handling verification. Add test cases for malformed/missing manifest.yaml, missing extension binary, execution failures, and verify graceful error handling in loadExtensionCommands.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'test: cover root command wiring' directly and concisely describes the main change: adding comprehensive tests for the root command's wiring of flags, subcommands, and extensions.
Security Check ✅ Passed Test file demonstrates secure practices: temporary directories isolate test data, no credentials are hardcoded or exposed, extension manifest contains only non-sensitive metadata, and API key flags are tested for existence without actual values.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

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 unit tests for the a6 root command to increase coverage around root-level wiring, including persistent flags, core subcommand registration, and extension command discovery/execution.

Changes:

  • Introduce tests asserting root command metadata and presence of global persistent flags.
  • Add coverage for core subcommand wiring.
  • Add an integration-style test that discovers an extension from the configured extensions directory and executes it through the root command.

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

Comment thread pkg/cmd/root/root_test.go Outdated
} {
found, _, err := rootCmd.Find([]string{name})
require.NoError(t, err)
assert.NotNil(t, found)
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

The core-subcommand loop doesn’t actually assert that the resolved command matches the expected subcommand name. As written, the check can succeed even if the subcommand isn’t registered (e.g., if the lookup falls back to the root command). Please assert the found command’s name/use (and that it isn’t the root command) for each entry to make this test meaningful.

Suggested change
assert.NotNil(t, found)
require.NotNil(t, found)
assert.NotSame(t, rootCmd, found)
assert.Equal(t, name, found.Name())
assert.True(t, strings.HasPrefix(found.Use, name))

Copilot uses AI. Check for mistakes.
Comment thread pkg/cmd/root/root_test.go Outdated
Comment on lines +90 to +93
cmd, _, err := rootCmd.Find([]string{"hello"})
require.NoError(t, err)
assert.Equal(t, "extension", cmd.GroupID)
assert.Equal(t, "test extension", cmd.Short)
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

Local variable cmd shadows the imported github.com/api7/a6/pkg/cmd package name (used earlier in the function). Renaming the variable (e.g., foundCmd/extCmd) will avoid confusion and prevent accidental misuse in future edits.

Suggested change
cmd, _, err := rootCmd.Find([]string{"hello"})
require.NoError(t, err)
assert.Equal(t, "extension", cmd.GroupID)
assert.Equal(t, "test extension", cmd.Short)
foundCmd, _, err := rootCmd.Find([]string{"hello"})
require.NoError(t, err)
assert.Equal(t, "extension", foundCmd.GroupID)
assert.Equal(t, "test extension", foundCmd.Short)

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
pkg/cmd/root/root_test.go (1)

19-23: Make the core wiring test hermetic by pinning extension dir

NewCmdRoot loads extensions during construction. Without isolating A6_CONFIG_DIR in this test, local/CI extension state can influence outcomes.

Suggested patch
 func TestNewCmdRoot_RegistersCoreCommandsAndFlags(t *testing.T) {
+	t.Setenv("A6_CONFIG_DIR", t.TempDir())
 	ios, _, _, _ := iostreams.Test()
 
 	rootCmd := NewCmdRoot(&cmd.Factory{IOStreams: ios})
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/cmd/root/root_test.go` around lines 19 - 23,
TestNewCmdRoot_RegistersCoreCommandsAndFlags is non-hermetic because NewCmdRoot
reads extensions from A6_CONFIG_DIR; pin A6_CONFIG_DIR to a temporary empty
directory before calling NewCmdRoot to isolate extension state. In the test
(within TestNewCmdRoot_RegistersCoreCommandsAndFlags) use os.MkdirTemp to create
an empty dir, set the env var A6_CONFIG_DIR to that path via os.Setenv, defer
cleanup (os.RemoveAll) and restore the original env value after the test, then
construct rootCmd with NewCmdRoot so the test no longer depends on local/CI
extension files.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@pkg/cmd/root/root_test.go`:
- Around line 48-50: The test currently calls rootCmd.Find([]string{name}) and
only checks err and that found is non-nil; add an explicit assertion that the
matched command's name equals the requested name to ensure the correct command
was returned. After the existing require.NoError(t, err) and assert.NotNil(t,
found) lines, assert (or require) that found.Name() (or found.Name) equals the
name variable so the test fails if a different command was matched; reference
the rootCmd.Find call and the found variable when making this assertion.

---

Nitpick comments:
In `@pkg/cmd/root/root_test.go`:
- Around line 19-23: TestNewCmdRoot_RegistersCoreCommandsAndFlags is
non-hermetic because NewCmdRoot reads extensions from A6_CONFIG_DIR; pin
A6_CONFIG_DIR to a temporary empty directory before calling NewCmdRoot to
isolate extension state. In the test (within
TestNewCmdRoot_RegistersCoreCommandsAndFlags) use os.MkdirTemp to create an
empty dir, set the env var A6_CONFIG_DIR to that path via os.Setenv, defer
cleanup (os.RemoveAll) and restore the original env value after the test, then
construct rootCmd with NewCmdRoot so the test no longer depends on local/CI
extension files.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: da81c874-ceac-4789-af47-4b4dcf639b9d

📥 Commits

Reviewing files that changed from the base of the PR and between 85d58d7 and 0ab2b48.

📒 Files selected for processing (1)
  • pkg/cmd/root/root_test.go

Comment thread pkg/cmd/root/root_test.go Outdated
@guoqqqi
Copy link
Copy Markdown
Contributor Author

guoqqqi commented Apr 10, 2026

Addressed the hermeticity and command-matching feedback in a follow-up commit. I am not adding separate E2E coverage to this PR: this branch is intentionally limited to root-command wiring, while scenario-level E2E coverage is handled separately in PR #18 so the review scope stays clean.

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.

2 participants