Skip to content

Commit 3b95d98

Browse files
committed
chore: improve helm test helpers
1 parent ee26882 commit 3b95d98

3 files changed

Lines changed: 1200 additions & 3 deletions

File tree

test/colors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ const (
88
colorYellow = "\033[93m"
99
colorBlue = "\033[94m"
1010
colorBold = "\033[1m"
11-
)
11+
colorGreen = "\033[92m"
12+
)

test/command.go

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"bytes"
66
"fmt"
77
"io"
8+
"os"
89
"os/exec"
910
"strings"
1011
"sync"
12+
"testing"
1113
)
1214

1315
// CommandResult holds the result of a command execution
@@ -29,6 +31,23 @@ type CommandRunner struct {
2931
ColorOutput bool
3032
}
3133

34+
// isVerboseMode checks if we're running in verbose test mode
35+
func isVerboseMode() bool {
36+
// Check if testing.Verbose() is available (when running in test context)
37+
if testing.Testing() {
38+
return testing.Verbose()
39+
}
40+
41+
// Fallback: check for -v flag in command line args
42+
for _, arg := range os.Args {
43+
if arg == "-v" || arg == "-test.v" || arg == "-test.v=true" {
44+
return true
45+
}
46+
}
47+
48+
return false
49+
}
50+
3251
// NewCommandRunner creates a new CommandRunner
3352
func NewCommandRunner(colorOutput bool) *CommandRunner {
3453
return &CommandRunner{ColorOutput: colorOutput}
@@ -131,20 +150,69 @@ func (c *CommandRunner) RunCommandQuiet(name string, args ...string) CommandResu
131150
func (c *CommandRunner) streamOutput(reader io.Reader, prefix string, color string, buffer *bytes.Buffer, wg *sync.WaitGroup) {
132151
defer wg.Done()
133152
scanner := bufio.NewScanner(reader)
153+
isVerbose := isVerboseMode()
154+
lineCount := 0
155+
134156
for scanner.Scan() {
135157
line := scanner.Text()
136158
buffer.WriteString(line + "\n")
159+
lineCount++
160+
137161
if c.ColorOutput {
138-
fmt.Printf("%s%s%s: %s%s\n", color, prefix, colorReset, color, line+colorReset)
162+
// Always print command outputs (from stdout), but limit log statements unless verbose
163+
shouldPrint := prefix == "stdout" || isVerbose || lineCount == 1
164+
165+
if shouldPrint {
166+
fmt.Printf("%s%s%s: %s%s\n", color, prefix, colorReset, color, line+colorReset)
167+
} else if lineCount == 2 && prefix == "stderr" {
168+
// Show truncation indicator for stderr when not verbose
169+
fmt.Printf("%s%s%s: %s... (use -v for full output)%s\n", color, prefix, colorReset, color, colorReset)
170+
}
139171
}
140172
}
141173
}
142174

175+
func (c *CommandRunner) Successf(format string, args ...interface{}) CommandResult {
176+
if c.ColorOutput {
177+
fmt.Printf("%s%s%s\n", colorGreen, colorBold, fmt.Sprintf(format, args...))
178+
} else {
179+
fmt.Printf(format+"\n", args...)
180+
}
181+
return CommandResult{ExitCode: 0}
182+
}
183+
184+
func (c *CommandRunner) Errorf(format string, args ...interface{}) CommandResult {
185+
if c.ColorOutput {
186+
fmt.Printf("%s%s%s\n", colorRed, colorBold, fmt.Sprintf(format, args...))
187+
} else {
188+
fmt.Printf(format+"\n", args...)
189+
}
190+
return CommandResult{ExitCode: 1, Err: fmt.Errorf(fmt.Sprintf(format, args...))}
191+
}
192+
193+
func (c *CommandRunner) Statusf(format string, args ...interface{}) CommandResult {
194+
if c.ColorOutput {
195+
fmt.Printf("%s%s%s\n", colorYellow, colorBold, fmt.Sprintf(format, args...))
196+
} else {
197+
fmt.Printf(format+"\n", args...)
198+
}
199+
return CommandResult{ExitCode: 0}
200+
}
201+
202+
func (c *CommandRunner) Infof(format string, args ...interface{}) CommandResult {
203+
if c.ColorOutput {
204+
fmt.Printf("%s%s%s\n", colorYellow, colorBold, fmt.Sprintf(format, args...))
205+
} else {
206+
fmt.Printf(format+"\n", args...)
207+
}
208+
return CommandResult{ExitCode: 0}
209+
}
210+
143211
// Printf prints a formatted colored message
144212
func (c *CommandRunner) Printf(color, style, format string, args ...interface{}) {
145213
if c.ColorOutput {
146214
fmt.Printf("%s%s%s%s\n", color, style, fmt.Sprintf(format, args...), colorReset)
147215
} else {
148216
fmt.Printf(format+"\n", args...)
149217
}
150-
}
218+
}

0 commit comments

Comments
 (0)