Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions cmd/membraned/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"strings"
"testing"
)

// TestVersionFlag verifies that --version prints the version string and exits 0.
func TestVersionFlag(t *testing.T) {
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)
}
Comment on lines +15 to +26
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

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.

Suggested change
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().


cmd := exec.Command(os.Args[0], "-test.run=TestVersionFlag")
cmd.Env = append(os.Environ(), "TEST_VERSION_SUBPROCESS=1")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out

err := cmd.Run()
if err != nil {
t.Fatalf("--version subprocess exited with error: %v\noutput: %s", err, out.String())
}

output := strings.TrimSpace(out.String())
if !strings.HasPrefix(output, "membraned ") {
t.Errorf("expected output to start with 'membraned ', got: %q", output)
Comment on lines +39 to +41
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

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.

}
}

// TestVersionDefault checks that the version variable has a sensible fallback.
func TestVersionDefault(t *testing.T) {
if version == "" {
t.Error("version variable must not be empty; expected 'dev' or an injected value")
}
}