Add support for bd --version flag

- Add -v, --version flag to root command
- Both bd --version and bd -v now print version info
- bd version subcommand continues to work as before
- Add tests for both flag variants

Amp-Thread-ID: https://ampcode.com/threads/T-583f9ef8-c358-48c5-91cf-c23a91bab9d7
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-11-05 17:37:48 -08:00
parent 07d2c0ccc6
commit 34ba064dcc
2 changed files with 78 additions and 0 deletions

View File

@@ -76,3 +76,69 @@ func TestVersionCommand(t *testing.T) {
// Restore default
jsonOutput = false
}
func TestVersionFlag(t *testing.T) {
// Save original stdout
oldStdout := os.Stdout
defer func() { os.Stdout = oldStdout }()
t.Run("--version flag", func(t *testing.T) {
// Create a pipe to capture output
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe: %v", err)
}
os.Stdout = w
// Set version flag and run root command
rootCmd.SetArgs([]string{"--version"})
rootCmd.Execute()
// Close writer and read output
w.Close()
var buf bytes.Buffer
buf.ReadFrom(r)
output := buf.String()
// Verify output contains version info
if !strings.Contains(output, "bd version") {
t.Errorf("Expected output to contain 'bd version', got: %s", output)
}
if !strings.Contains(output, Version) {
t.Errorf("Expected output to contain version %s, got: %s", Version, output)
}
// Reset args
rootCmd.SetArgs(nil)
})
t.Run("-v shorthand", func(t *testing.T) {
// Create a pipe to capture output
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe: %v", err)
}
os.Stdout = w
// Set version flag and run root command
rootCmd.SetArgs([]string{"-v"})
rootCmd.Execute()
// Close writer and read output
w.Close()
var buf bytes.Buffer
buf.ReadFrom(r)
output := buf.String()
// Verify output contains version info
if !strings.Contains(output, "bd version") {
t.Errorf("Expected output to contain 'bd version', got: %s", output)
}
if !strings.Contains(output, Version) {
t.Errorf("Expected output to contain version %s, got: %s", Version, output)
}
// Reset args
rootCmd.SetArgs(nil)
})
}