Fix: Add --version support to membraned CLI#36
Fix: Add --version support to membraned CLI#36gabrnavarro wants to merge 1 commit intoBennettSchwartz:masterfrom
Conversation
Signed-off-by: gabrnavarro <gabrnavarro@users.noreply.github.com>
📝 WalkthroughWalkthroughA test file is added to validate membraned CLI version functionality. Two tests verify that the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@cmd/membraned/version_test.go`:
- Around line 39-41: The test currently only checks a prefix; change it to
assert exact equality by trimming output (output :=
strings.TrimSpace(out.String())) and comparing it to the exact expected string
built from the package's version constant (e.g., expected := "membraned
"+Version or "membraned "+version if the symbol is lowercase), then use t.Errorf
when output != expected to show both values; update the assertion around the
output variable and remove the HasPrefix check so the test fails on any
formatting or extra output regressions.
- Around line 15-26: The subprocess block duplicates the --version flag logic
instead of exercising the real CLI; replace the inline flag parsing with a reset
of flag.CommandLine and os.Args and then invoke main() so the actual CLI path is
tested: when TEST_VERSION_SUBPROCESS == "1", set flag.CommandLine =
flag.NewFlagSet(os.Args[0], flag.ExitOnError), set os.Args =
[]string{"membraned", "--version"}, then call main() and exit (e.g., os.Exit(0))
after main() returns so the subprocess runs the real version handling code in
main().
🪄 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: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: fd832600-f415-43fd-8898-c59ec768a343
📒 Files selected for processing (1)
cmd/membraned/version_test.go
| if os.Getenv("TEST_VERSION_SUBPROCESS") == "1" { | ||
| // We are the subprocess: run the version logic directly. | ||
| flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) | ||
| showVersion := flag.Bool("version", false, "print version and exit") | ||
| os.Args = []string{"membraned", "--version"} | ||
| flag.Parse() | ||
| if *showVersion { | ||
| fmt.Printf("membraned %s\n", version) | ||
| os.Exit(0) | ||
| } | ||
| os.Exit(1) | ||
| } |
There was a problem hiding this comment.
Subprocess test is validating duplicated logic, not the real CLI path.
Line 17-Line 25 re-implements --version behavior inline, so this test can still pass even if main() regresses. Execute main() in the subprocess after resetting args/flags.
Proposed fix
if os.Getenv("TEST_VERSION_SUBPROCESS") == "1" {
- // We are the subprocess: run the version logic directly.
+ // We are the subprocess: execute the real CLI entrypoint.
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
- showVersion := flag.Bool("version", false, "print version and exit")
os.Args = []string{"membraned", "--version"}
- flag.Parse()
- if *showVersion {
- fmt.Printf("membraned %s\n", version)
- os.Exit(0)
- }
- os.Exit(1)
+ main()
+ os.Exit(0)
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if os.Getenv("TEST_VERSION_SUBPROCESS") == "1" { | |
| // We are the subprocess: run the version logic directly. | |
| flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) | |
| showVersion := flag.Bool("version", false, "print version and exit") | |
| os.Args = []string{"membraned", "--version"} | |
| flag.Parse() | |
| if *showVersion { | |
| fmt.Printf("membraned %s\n", version) | |
| os.Exit(0) | |
| } | |
| os.Exit(1) | |
| } | |
| if os.Getenv("TEST_VERSION_SUBPROCESS") == "1" { | |
| // We are the subprocess: execute the real CLI entrypoint. | |
| flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) | |
| os.Args = []string{"membraned", "--version"} | |
| main() | |
| os.Exit(0) | |
| } |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@cmd/membraned/version_test.go` around lines 15 - 26, The subprocess block
duplicates the --version flag logic instead of exercising the real CLI; replace
the inline flag parsing with a reset of flag.CommandLine and os.Args and then
invoke main() so the actual CLI path is tested: when TEST_VERSION_SUBPROCESS ==
"1", set flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError), set
os.Args = []string{"membraned", "--version"}, then call main() and exit (e.g.,
os.Exit(0)) after main() returns so the subprocess runs the real version
handling code in main().
| output := strings.TrimSpace(out.String()) | ||
| if !strings.HasPrefix(output, "membraned ") { | ||
| t.Errorf("expected output to start with 'membraned ', got: %q", output) |
There was a problem hiding this comment.
Assertion is too weak; verify exact output.
Line 40 only checks a prefix, which can hide formatting/extra-output regressions. Compare against the exact expected string.
Proposed fix
output := strings.TrimSpace(out.String())
-if !strings.HasPrefix(output, "membraned ") {
- t.Errorf("expected output to start with 'membraned ', got: %q", output)
+expected := fmt.Sprintf("membraned %s", version)
+if output != expected {
+ t.Errorf("expected %q, got: %q", expected, output)
}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@cmd/membraned/version_test.go` around lines 39 - 41, The test currently only
checks a prefix; change it to assert exact equality by trimming output (output
:= strings.TrimSpace(out.String())) and comparing it to the exact expected
string built from the package's version constant (e.g., expected := "membraned
"+Version or "membraned "+version if the symbol is lowercase), then use t.Errorf
when output != expected to show both values; update the assertion around the
output variable and remove the HasPrefix check so the test fails on any
formatting or extra output regressions.
What changed
Added
--versionflag to themembranedCLI. Introduced a package-levelversionvariable defaulting to"dev". When--versionis passed, the daemon printsmembraned <version>and exits 0.Why
Release builds already inject version via
-ldflags="-X main.version=${VERSION}"but there was no way for users to inspect the running binary's version.Fixes #31
Testing
TestVersionFlag: subprocess test confirms--versionprints the expected format and exits 0TestVersionDefault: confirms the fallback value is non-empty ("dev")go test ./cmd/membraned/... -vpasses both testsgo vet ./...clean