Skip to content

Commit 077558c

Browse files
committed
Lint fixes
Signed-off-by: apostasie <[email protected]>
1 parent 7f04990 commit 077558c

File tree

10 files changed

+188
-84
lines changed

10 files changed

+188
-84
lines changed

mod/tigron/require/requirement.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
func Binary(name string) *test.Requirement {
2828
return &test.Requirement{
29-
Check: func(data test.Data, helpers test.Helpers) (bool, string) {
29+
Check: func(_ test.Data, _ test.Helpers) (bool, string) {
3030
mess := fmt.Sprintf("executable %q has been found in PATH", name)
3131
ret := true
3232
if _, err := exec.LookPath(name); err != nil {
@@ -41,7 +41,7 @@ func Binary(name string) *test.Requirement {
4141

4242
func OS(os string) *test.Requirement {
4343
return &test.Requirement{
44-
Check: func(data test.Data, helpers test.Helpers) (bool, string) {
44+
Check: func(_ test.Data, _ test.Helpers) (bool, string) {
4545
mess := fmt.Sprintf("current operating system is %q", runtime.GOOS)
4646
ret := true
4747
if runtime.GOOS != os {
@@ -55,7 +55,7 @@ func OS(os string) *test.Requirement {
5555

5656
func Arch(arch string) *test.Requirement {
5757
return &test.Requirement{
58-
Check: func(data test.Data, helpers test.Helpers) (bool, string) {
58+
Check: func(_ test.Data, _ test.Helpers) (bool, string) {
5959
mess := fmt.Sprintf("current architecture is %q", runtime.GOARCH)
6060
ret := true
6161
if runtime.GOARCH != arch {
@@ -67,18 +67,22 @@ func Arch(arch string) *test.Requirement {
6767
}
6868
}
6969

70-
var Amd64 = Arch("amd64")
71-
var Arm64 = Arch("arm64")
72-
var Windows = OS("windows")
73-
var Linux = OS("linux")
74-
var Darwin = OS("darwin")
70+
//nolint:gochecknoglobals
71+
var (
72+
Amd64 = Arch("amd64")
73+
Arm64 = Arch("arm64")
74+
Windows = OS("windows")
75+
Linux = OS("linux")
76+
Darwin = OS("darwin")
77+
)
7578

76-
// NOTE: Not will always lose setups and cleanups...
79+
// NOTE: Not will always ignore any setup and cleanup inside the wrapped requirement.
7780

7881
func Not(requirement *test.Requirement) *test.Requirement {
7982
return &test.Requirement{
8083
Check: func(data test.Data, helpers test.Helpers) (bool, string) {
8184
ret, mess := requirement.Check(data, helpers)
85+
8286
return !ret, mess
8387
},
8488
}
@@ -93,10 +97,12 @@ func All(requirements ...*test.Requirement) *test.Requirement {
9397
for _, requirement := range requirements {
9498
ret, subMess = requirement.Check(data, helpers)
9599
mess += "\n" + subMess
100+
96101
if !ret {
97102
return ret, mess
98103
}
99104
}
105+
100106
return ret, mess
101107
},
102108
Setup: func(data test.Data, helpers test.Helpers) {

mod/tigron/test/case.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ type Case struct {
5959
parent *Case
6060
}
6161

62-
// Run prepares and executes the test, and any possible subtests
62+
// Run prepares and executes the test, and any possible subtests.
63+
//
64+
//nolint:gocognit
6365
func (test *Case) Run(t *testing.T) {
6466
t.Helper()
6567
// Run the test
68+
//nolint:thelper
6669
testRun := func(subT *testing.T) {
6770
subT.Helper()
6871

@@ -81,10 +84,13 @@ func (test *Case) Run(t *testing.T) {
8184

8285
// If we have a parent, get parent env, data and config
8386
var parentData Data
87+
8488
var parentConfig Config
89+
8590
if test.parent != nil {
8691
parentData = test.parent.Data
8792
parentConfig = test.parent.Config
93+
8894
for k, v := range test.parent.Env {
8995
if _, ok := test.Env[k]; !ok {
9096
test.Env[k] = v
@@ -96,23 +102,22 @@ func (test *Case) Run(t *testing.T) {
96102
test.Data = configureData(test.t, test.Data, parentData)
97103
test.Config = configureConfig(test.Config, parentConfig)
98104

99-
var b CustomizableCommand
105+
var custCom CustomizableCommand
100106
if registeredTestable == nil {
101-
b = &GenericCommand{}
107+
custCom = &GenericCommand{}
102108
} else {
103-
b = registeredTestable.CustomCommand(test, test.t)
109+
custCom = registeredTestable.CustomCommand(test, test.t)
104110
}
105111

106-
b.WithCwd(test.Data.TempDir())
107-
108-
b.withT(test.t)
109-
b.withTempDir(test.Data.TempDir())
110-
b.withEnv(test.Env)
111-
b.withConfig(test.Config)
112+
custCom.WithCwd(test.Data.TempDir())
113+
custCom.withT(test.t)
114+
custCom.withTempDir(test.Data.TempDir())
115+
custCom.withEnv(test.Env)
116+
custCom.withConfig(test.Config)
112117

113118
// Attach the base command, and t
114119
test.helpers = &helpersInternal{
115-
cmdInternal: b,
120+
cmdInternal: custCom,
116121
t: test.t,
117122
}
118123

@@ -125,9 +130,11 @@ func (test *Case) Run(t *testing.T) {
125130
if !shouldRun {
126131
test.t.Skipf("test skipped as: %s", message)
127132
}
133+
128134
if test.Require.Setup != nil {
129135
setups = append(setups, test.Require.Setup)
130136
}
137+
131138
if test.Require.Cleanup != nil {
132139
cleanups = append(cleanups, test.Require.Cleanup)
133140
}
@@ -154,35 +161,46 @@ func (test *Case) Run(t *testing.T) {
154161
}
155162

156163
// Execute cleanups now
164+
test.t.Log("")
157165
test.t.Log("======================== Pre-test cleanup ========================")
166+
158167
for _, cleanup := range cleanups {
159168
cleanup(test.Data, test.helpers)
160169
}
161170

162171
// Register the cleanups, in reverse
163172
test.t.Cleanup(func() {
173+
test.t.Log("")
164174
test.t.Log("======================== Post-test cleanup ========================")
175+
165176
slices.Reverse(cleanups)
177+
166178
for _, cleanup := range cleanups {
167179
cleanup(test.Data, test.helpers)
168180
}
169181
})
170182

171183
// Run the setups
184+
test.t.Log("")
172185
test.t.Log("======================== Test setup ========================")
186+
173187
for _, setup := range setups {
174188
setup(test.Data, test.helpers)
175189
}
176190

177191
// Run the command if any, with expectations
178192
// Note: if we have a command, we already know we DO have Expected
193+
test.t.Log("")
179194
test.t.Log("======================== Test Run ========================")
195+
180196
if test.Command != nil {
181197
test.Command(test.Data, test.helpers).Run(test.Expected(test.Data, test.helpers))
182198
}
183199

184200
// Now go for the subtests
201+
test.t.Log("")
185202
test.t.Log("======================== Processing subtests ========================")
203+
186204
for _, subTest := range test.SubTests {
187205
subTest.parent = test
188206
subTest.Run(test.t)

mod/tigron/test/command.go

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
6969
type 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 {
228238
func (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

236248
func (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

240252
func (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
265279
func (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

280295
func (gc *GenericCommand) T() *testing.T {
281296
return gc.t
282297
}
283298

299+
//nolint:ireturn
284300
func (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

301318
func (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

Comments
 (0)