@@ -65,7 +65,7 @@ type CustomizableCommand interface {
6565 read (key ConfigKey ) ConfigValue
6666}
6767
68- // GenericCommand is a concrete Command implementation
68+ // GenericCommand is a concrete Command implementation.
6969type GenericCommand struct {
7070 Config Config
7171 TempDir string
@@ -122,15 +122,20 @@ func (gc *GenericCommand) Run(expect *Expected) {
122122 gc .t .Helper ()
123123 }
124124
125- var result * icmd.Result
126- var env []string
125+ var (
126+ result * icmd.Result
127+ env []string
128+ tty * os.File
129+ psty * os.File
130+ )
131+
127132 output := & bytes.Buffer {}
128133 stdout := ""
129134 copyGroup := & errgroup.Group {}
130- var tty * os.File
131- var psty * os.File
135+
132136 if ! gc .async {
133137 iCmdCmd := gc .boot ()
138+
134139 if gc .pty {
135140 psty , tty , _ = pty .Open ()
136141 _ , _ = term .MakeRaw (int (tty .Fd ()))
@@ -141,6 +146,7 @@ func (gc *GenericCommand) Run(expect *Expected) {
141146 // Copy from the master
142147 copyGroup .Go (func () error {
143148 _ , _ = io .Copy (output , psty )
149+
144150 return nil
145151 })
146152
@@ -195,25 +201,29 @@ func (gc *GenericCommand) Run(expect *Expected) {
195201 if expect != nil {
196202 // Build the debug string - additionally attach the env (which iCmd does not do)
197203 debug := result .String () + "Env:\n " + strings .Join (env , "\n " )
204+
198205 // ExitCode goes first
199- if expect .ExitCode == internal .ExitCodeNoCheck { //nolint:revive
206+ switch expect .ExitCode {
207+ case internal .ExitCodeNoCheck :
200208 // ExitCodeNoCheck means we do not care at all about exit code. It can be a failure, a success, or a timeout.
201- } else if expect . ExitCode == internal .ExitCodeGenericFail {
209+ case internal .ExitCodeGenericFail :
202210 // ExitCodeGenericFail means we expect an error (excluding timeout).
203211 assert .Assert (gc .t , result .ExitCode != 0 ,
204- "Command succeeded while we were expecting an error \n " + debug )
205- } else if result . Timeout {
212+ "Expected exit code to be different than 0 \n " + debug )
213+ case internal . ExitCodeTimeout :
206214 assert .Assert (gc .t , expect .ExitCode == internal .ExitCodeTimeout ,
207215 "Command unexpectedly timed-out\n " + debug )
208- } else {
216+ default :
209217 assert .Assert (gc .t , expect .ExitCode == result .ExitCode ,
210218 fmt .Sprintf ("Expected exit code: %d\n " , expect .ExitCode )+ debug )
211219 }
220+
212221 // Range through the expected errors and confirm they are seen on stderr
213222 for _ , expectErr := range expect .Errors {
214223 assert .Assert (gc .t , strings .Contains (gc .rawStdErr , expectErr .Error ()),
215224 fmt .Sprintf ("Expected error: %q to be found in stderr\n " , expectErr .Error ())+ debug )
216225 }
226+
217227 // Finally, check the output if we are asked to
218228 if expect .Output != nil {
219229 expect .Output (stdout , debug , gc .t )
@@ -228,19 +238,22 @@ func (gc *GenericCommand) Stderr() string {
228238func (gc * GenericCommand ) Background (timeout time.Duration ) {
229239 // Run it
230240 gc .async = true
241+
231242 i := gc .boot ()
243+
232244 gc .timeout = timeout
233245 gc .result = icmd .StartCmd (i )
234246}
235247
236248func (gc * GenericCommand ) Signal (sig os.Signal ) error {
237- return gc .result .Cmd .Process .Signal (sig )
249+ return gc .result .Cmd .Process .Signal (sig ) //nolint:wrapcheck
238250}
239251
240252func (gc * GenericCommand ) withEnv (env map [string ]string ) {
241253 if gc .Env == nil {
242254 gc .Env = map [string ]string {}
243255 }
256+
244257 for k , v := range env {
245258 gc .Env [k ] = v
246259 }
@@ -262,43 +275,48 @@ func (gc *GenericCommand) PrependArgs(args ...string) {
262275 gc .prependArgs = append (gc .prependArgs , args ... )
263276}
264277
278+ //nolint:ireturn
265279func (gc * GenericCommand ) Clone () TestableCommand {
266280 // Copy the command and return a new one - with almost everything from the parent command
267- cc := * gc
268- cc .result = nil
269- cc .stdin = nil
270- cc .timeout = 0
271- cc .rawStdErr = ""
281+ com := * gc
282+ com .result = nil
283+ com .stdin = nil
284+ com .timeout = 0
285+ com .rawStdErr = ""
272286 // Clone Env
273- cc .Env = make (map [string ]string , len (gc .Env ))
287+ com .Env = make (map [string ]string , len (gc .Env ))
274288 for k , v := range gc .Env {
275- cc .Env [k ] = v
289+ com .Env [k ] = v
276290 }
277- return & cc
291+
292+ return & com
278293}
279294
280295func (gc * GenericCommand ) T () * testing.T {
281296 return gc .t
282297}
283298
299+ //nolint:ireturn
284300func (gc * GenericCommand ) clear () TestableCommand {
285- cc := * gc
286- cc .mainBinary = ""
287- cc .helperBinary = ""
288- cc .mainArgs = []string {}
289- cc .prependArgs = []string {}
290- cc .helperArgs = []string {}
301+ com := * gc
302+ com .mainBinary = ""
303+ com .helperBinary = ""
304+ com .mainArgs = []string {}
305+ com .prependArgs = []string {}
306+ com .helperArgs = []string {}
291307 // Clone Env
292- cc .Env = make (map [string ]string , len (gc .Env ))
308+ com .Env = make (map [string ]string , len (gc .Env ))
293309 // Reset configuration
294- cc .Config = & config {}
310+ com .Config = & config {}
295311 for k , v := range gc .Env {
296- cc .Env [k ] = v
312+ com .Env [k ] = v
297313 }
298- return & cc
314+
315+ return & com
299316}
300317
301318func (gc * GenericCommand ) withT (t * testing.T ) {
319+ t .Helper ()
302320 gc .t = t
303321}
304322
@@ -317,7 +335,9 @@ func (gc *GenericCommand) boot() icmd.Cmd {
317335 }
318336
319337 binary := gc .mainBinary
338+ //nolint:gocritic
320339 args := append (gc .prependArgs , gc .mainArgs ... )
340+
321341 if gc .helperBinary != "" {
322342 args = append ([]string {binary }, args ... )
323343 args = append (gc .helperArgs , args ... )
@@ -330,16 +350,20 @@ func (gc *GenericCommand) boot() icmd.Cmd {
330350
331351 iCmdCmd := icmd .Command (binary , args ... )
332352 iCmdCmd .Env = []string {}
333- for _ , v := range os .Environ () {
353+
354+ for _ , envValue := range os .Environ () {
334355 add := true
356+
335357 for _ , b := range gc .envBlackList {
336- if strings .HasPrefix (v , b + "=" ) {
358+ if strings .HasPrefix (envValue , b + "=" ) {
337359 add = false
360+
338361 break
339362 }
340363 }
364+
341365 if add {
342- iCmdCmd .Env = append (iCmdCmd .Env , v )
366+ iCmdCmd .Env = append (iCmdCmd .Env , envValue )
343367 }
344368 }
345369
0 commit comments