Skip to content

test: add table-driven CLI command tests with shared executor helper - #4

Merged
kusutori merged 1 commit into
mainfrom
codex/add-tests-for-cobra-subcommands
May 6, 2026
Merged

kusutori merged 1 commit into
mainfrom
codex/add-tests-for-cobra-subcommands

Conversation

@kusutori

@kusutori kusutori commented May 6, 2026

Copy link
Copy Markdown
Owner

Motivation

  • Improve CLI command test coverage by adding table-driven tests for key Cobra subcommands and a shared executor to standardize command invocation and output/error assertions.

Description

  • Add cmd/command_exec_test.go which provides runCommandTest helper to set RootCmd args, capture stdout/stderr, and assert error/exit semantics.
  • Add table-driven tests for completion (cmd/completion_cmd_test.go), add (cmd/add_cmd_test.go) and remove (cmd/remove_cmd_test.go) that cover missing required args, invalid input formats, flag combinations, --help output, and success paths.
  • Add setupTempConfig in cmd/add_cmd_test.go to inject a temporary deca.toml by overriding configPath and restore it after the test.
  • Tests use key-fragment assertions via the wantContains field to avoid brittle exact-string matches.

Testing

  • Attempted to run the new tests with go test ./cmd -run 'Test(AddCommand|RemoveCommand|CompletionCommand)' and go test ./cmd -run TestCompletionCommand -v in the execution environment but the test runs did not complete within the session (hung/timeouts).
  • All new test files compile as part of the changes and were added to the repository for CI execution.

Codex Task

@kusutori
kusutori merged commit 8c702a4 into main May 6, 2026
0 of 2 checks passed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 22d41d5623

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread cmd/command_exec_test.go
Comment on lines +23 to +26
out := &bytes.Buffer{}
errOut := &bytes.Buffer{}
RootCmd.SetOut(out)
RootCmd.SetErr(errOut)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Capture process stdout/stderr in command test helper

runCommandTest only captures RootCmd's writers, but several tested code paths write directly to process stdio (e.g. coloredHelpFunc uses fmt.Println and UI logging uses color printers). As a result, wantContains checks run against an empty buffer even when commands produce output, which makes help/success cases fail consistently and turns these table tests into false negatives.

Useful? React with 👍 / 👎.

Comment thread cmd/command_exec_test.go
Comment on lines +27 to +28
RootCmd.SilenceUsage = true
RootCmd.SilenceErrors = true

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Stop silencing Cobra errors when asserting error output

The helper sets RootCmd.SilenceUsage and RootCmd.SilenceErrors to true but error-path test cases assert for Cobra-generated error text like "requires at least 1 arg". With both silence flags enabled, that text is intentionally suppressed from output, so those assertions fail regardless of command behavior.

Useful? React with 👍 / 👎.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant