Skip to content
Merged
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions cmd/cbox-init/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/cboxdk/init/internal/process"
"github.com/cboxdk/init/internal/scaffold"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// captureOutput captures stdout and stderr during function execution
Expand Down Expand Up @@ -540,8 +541,36 @@ func TestGlobalConfigFlag(t *testing.T) {
// Helper functions for test execution

// executeCommandCapture executes a cobra command and captures real stdout/stderr
// resetCommandFlags restores every flag in the command tree to its default.
//
// These tests drive the SHARED global rootCmd, and cobra flag values are sticky:
// once "version --short" has run, the --short flag stays set on that command, so
// a later plain "version" printed only the number. Within a single run the
// subtests happened to be ordered such that this never showed; under -count=2
// (or any reordering) it fails, which made -count unusable for this package and
// could equally hide a real regression.
func resetCommandFlags(cmd *cobra.Command) {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if f.Changed {
_ = f.Value.Set(f.DefValue)
f.Changed = false
}
})
cmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
if f.Changed {
_ = f.Value.Set(f.DefValue)
f.Changed = false
}
})

for _, sub := range cmd.Commands() {
resetCommandFlags(sub)
}
}

func executeCommandCapture(t *testing.T, cmd *cobra.Command, args ...string) string {
t.Helper()
resetCommandFlags(cmd)
var output string
stdout, stderr := captureOutput(func() {
cmd.SetArgs(args)
Expand All @@ -554,6 +583,7 @@ func executeCommandCapture(t *testing.T, cmd *cobra.Command, args ...string) str

// executeCommandCaptureWithError executes a cobra command and returns captured output and error
func executeCommandCaptureWithError(cmd *cobra.Command, args ...string) (string, error) {
resetCommandFlags(cmd)
var cmdErr error
stdout, stderr := captureOutput(func() {
cmd.SetArgs(args)
Expand All @@ -575,6 +605,8 @@ func executeCommand(t *testing.T, cmd *cobra.Command, args ...string) string {

// executeCommandWithError executes a cobra command and returns stdout and error
func executeCommandWithError(cmd *cobra.Command, args ...string) (string, error) {
resetCommandFlags(cmd)

// Create buffers for output
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/robfig/cron/v3 v3.0.1
github.com/shirou/gopsutil/v4 v4.26.7
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.9
go.opentelemetry.io/otel v1.45.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.45.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.45.0
Expand Down Expand Up @@ -62,7 +63,6 @@ require (
github.com/prometheus/common v0.70.1 // indirect
github.com/prometheus/procfs v0.21.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/tklauser/go-sysconf v0.3.16 // indirect
github.com/tklauser/numcpus v0.11.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
Expand Down
1 change: 1 addition & 0 deletions sbom.json
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@
"pkg:golang/github.com/robfig/cron/v3@v3.0.1?type=module",
"pkg:golang/github.com/shirou/gopsutil/v4@v4.26.7?type=module",
"pkg:golang/github.com/spf13/cobra@v1.10.2?type=module",
"pkg:golang/github.com/spf13/pflag@v1.0.9?type=module",
"pkg:golang/go.opentelemetry.io/otel@v1.45.0?type=module",
"pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc@v1.45.0?type=module",
"pkg:golang/go.opentelemetry.io/otel/exporters/stdout/stdouttrace@v1.45.0?type=module",
Expand Down