Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit b5b93d4

Browse files
committed
Fix tcli log helpers
1 parent e6c79f5 commit b5b93d4

File tree

1 file changed

+92
-86
lines changed

1 file changed

+92
-86
lines changed

ci/tcli/tcli.go

Lines changed: 92 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -173,57 +173,49 @@ func (r *ContainerRunner) RunCmd(cmd *exec.Cmd) *Assertable {
173173

174174
// Assert runs the Assertable and
175175
func (a Assertable) Assert(t *testing.T, option ...Assertion) {
176-
t.Run(a.tname, func(t *testing.T) {
177-
var cmdResult CommandResult
176+
slog.Helper()
177+
var cmdResult CommandResult
178178

179-
var (
180-
stdout bytes.Buffer
181-
stderr bytes.Buffer
182-
)
179+
var (
180+
stdout bytes.Buffer
181+
stderr bytes.Buffer
182+
)
183183

184-
a.cmd.Stdout = &stdout
185-
a.cmd.Stderr = &stderr
186-
187-
start := time.Now()
188-
err := a.cmd.Run()
189-
cmdResult.Duration = time.Since(start)
190-
191-
if exitErr, ok := err.(*exec.ExitError); ok {
192-
cmdResult.ExitCode = exitErr.ExitCode()
193-
} else if err != nil {
194-
cmdResult.ExitCode = -1
195-
} else {
196-
cmdResult.ExitCode = 0
197-
}
198-
199-
cmdResult.Stdout = stdout.Bytes()
200-
cmdResult.Stderr = stderr.Bytes()
201-
202-
slogtest.Info(t, "command output",
203-
slog.F("command", a.cmd),
204-
slog.F("stdout", string(cmdResult.Stdout)),
205-
slog.F("stderr", string(cmdResult.Stderr)),
206-
slog.F("exit-code", cmdResult.ExitCode),
207-
slog.F("duration", cmdResult.Duration),
208-
)
184+
a.cmd.Stdout = &stdout
185+
a.cmd.Stderr = &stderr
209186

210-
for ix, o := range option {
211-
name := fmt.Sprintf("assertion_#%v", ix)
212-
if named, ok := o.(Named); ok {
213-
name = named.Name()
214-
}
215-
t.Run(name, func(t *testing.T) {
216-
err := o.Valid(&cmdResult)
217-
assert.Success(t, name, err)
218-
})
219-
}
220-
})
187+
start := time.Now()
188+
err := a.cmd.Run()
189+
cmdResult.Duration = time.Since(start)
190+
191+
if exitErr, ok := err.(*exec.ExitError); ok {
192+
cmdResult.ExitCode = exitErr.ExitCode()
193+
} else if err != nil {
194+
cmdResult.ExitCode = -1
195+
} else {
196+
cmdResult.ExitCode = 0
197+
}
198+
199+
cmdResult.Stdout = stdout.Bytes()
200+
cmdResult.Stderr = stderr.Bytes()
201+
202+
slogtest.Info(t, "command output",
203+
slog.F("command", a.cmd),
204+
slog.F("stdout", string(cmdResult.Stdout)),
205+
slog.F("stderr", string(cmdResult.Stderr)),
206+
slog.F("exit-code", cmdResult.ExitCode),
207+
slog.F("duration", cmdResult.Duration),
208+
)
209+
210+
for _, o := range option {
211+
o.Valid(t, &cmdResult)
212+
}
221213
}
222214

223215
// Assertion specifies an assertion on the given CommandResult.
224216
// Pass custom Assertion types to cover special cases.
225217
type Assertion interface {
226-
Valid(r *CommandResult) error
218+
Valid(t *testing.T, r *CommandResult)
227219
}
228220

229221
// Named is an optional extension of Assertion that provides a helpful label
@@ -240,12 +232,13 @@ type CommandResult struct {
240232
}
241233

242234
type simpleFuncAssert struct {
243-
valid func(r *CommandResult) error
235+
valid func(t *testing.T, r *CommandResult)
244236
name string
245237
}
246238

247-
func (s simpleFuncAssert) Valid(r *CommandResult) error {
248-
return s.valid(r)
239+
func (s simpleFuncAssert) Valid(t *testing.T, r *CommandResult) {
240+
slog.Helper()
241+
s.valid(t, r)
249242
}
250243

251244
func (s simpleFuncAssert) Name() string {
@@ -254,17 +247,16 @@ func (s simpleFuncAssert) Name() string {
254247

255248
// Success asserts that the command exited with an exit code of 0
256249
func Success() Assertion {
250+
slog.Helper()
257251
return ExitCodeIs(0)
258252
}
259253

260254
// Error asserts that the command exited with a nonzero exit code
261255
func Error() Assertion {
262256
return simpleFuncAssert{
263-
valid: func(r *CommandResult) error {
264-
if r.ExitCode == 0 {
265-
return xerrors.Errorf("expected nonzero exit code, got %v", r.ExitCode)
266-
}
267-
return nil
257+
valid: func(t *testing.T, r *CommandResult) {
258+
slog.Helper()
259+
assert.True(t, "exit code is nonzero", r.ExitCode != 0)
268260
},
269261
name: fmt.Sprintf("error"),
270262
}
@@ -273,11 +265,9 @@ func Error() Assertion {
273265
// ExitCodeIs asserts that the command exited with the given code
274266
func ExitCodeIs(code int) Assertion {
275267
return simpleFuncAssert{
276-
valid: func(r *CommandResult) error {
277-
if r.ExitCode != code {
278-
return xerrors.Errorf("exit code of %v expected, got %v, (%s)", code, r.ExitCode, string(r.Stderr))
279-
}
280-
return nil
268+
valid: func(t *testing.T, r *CommandResult) {
269+
slog.Helper()
270+
assert.Equal(t, "exit code is as expected", code, r.ExitCode)
281271
},
282272
name: fmt.Sprintf("exitcode"),
283273
}
@@ -286,8 +276,9 @@ func ExitCodeIs(code int) Assertion {
286276
// StdoutEmpty asserts that the command did not write any data to Stdout
287277
func StdoutEmpty() Assertion {
288278
return simpleFuncAssert{
289-
valid: func(r *CommandResult) error {
290-
return empty("stdout", r.Stdout)
279+
valid: func(t *testing.T, r *CommandResult) {
280+
slog.Helper()
281+
empty(t, "stdout", r.Stdout)
291282
},
292283
name: fmt.Sprintf("stdout-empty"),
293284
}
@@ -297,9 +288,10 @@ func StdoutEmpty() Assertion {
297288
// The pointer passed as "result" will be assigned to the command's *CommandResult
298289
func GetResult(result **CommandResult) Assertion {
299290
return simpleFuncAssert{
300-
valid: func(r *CommandResult) error {
291+
valid: func(t *testing.T, r *CommandResult) {
292+
slog.Helper()
293+
empty(t, "stdout", r.Stdout)
301294
*result = r
302-
return nil
303295
},
304296
name: "get-result",
305297
}
@@ -308,8 +300,9 @@ func GetResult(result **CommandResult) Assertion {
308300
// StderrEmpty asserts that the command did not write any data to Stderr
309301
func StderrEmpty() Assertion {
310302
return simpleFuncAssert{
311-
valid: func(r *CommandResult) error {
312-
return empty("stderr", r.Stderr)
303+
valid: func(t *testing.T, r *CommandResult) {
304+
slog.Helper()
305+
empty(t, "stderr", r.Stderr)
313306
},
314307
name: fmt.Sprintf("stderr-empty"),
315308
}
@@ -318,8 +311,9 @@ func StderrEmpty() Assertion {
318311
// StdoutMatches asserts that Stdout contains a substring which matches the given regexp
319312
func StdoutMatches(pattern string) Assertion {
320313
return simpleFuncAssert{
321-
valid: func(r *CommandResult) error {
322-
return matches("stdout", pattern, r.Stdout)
314+
valid: func(t *testing.T, r *CommandResult) {
315+
slog.Helper()
316+
matches(t, "stdout", pattern, r.Stdout)
323317
},
324318
name: fmt.Sprintf("stdout-matches"),
325319
}
@@ -328,8 +322,9 @@ func StdoutMatches(pattern string) Assertion {
328322
// StderrMatches asserts that Stderr contains a substring which matches the given regexp
329323
func StderrMatches(pattern string) Assertion {
330324
return simpleFuncAssert{
331-
valid: func(r *CommandResult) error {
332-
return matches("stderr", pattern, r.Stderr)
325+
valid: func(t *testing.T, r *CommandResult) {
326+
slog.Helper()
327+
matches(t, "stderr", pattern, r.Stderr)
333328
},
334329
name: fmt.Sprintf("stderr-matches"),
335330
}
@@ -338,45 +333,53 @@ func StderrMatches(pattern string) Assertion {
338333
// CombinedMatches asserts that either Stdout or Stderr a substring which matches the given regexp
339334
func CombinedMatches(pattern string) Assertion {
340335
return simpleFuncAssert{
341-
valid: func(r *CommandResult) error {
342-
//stdoutValid := StdoutMatches(pattern).Valid(r)
343-
//stderrValid := StderrMatches(pattern).Valid(r)
344-
// TODO: combine errors
345-
return nil
336+
valid: func(t *testing.T, r *CommandResult) {
337+
slog.Helper()
338+
StdoutMatches(pattern).Valid(t, r)
339+
StderrMatches(pattern).Valid(t, r)
346340
},
347341
name: fmt.Sprintf("combined-matches"),
348342
}
349343
}
350344

351-
func matches(name, pattern string, target []byte) error {
345+
func matches(t *testing.T, name, pattern string, target []byte) {
346+
slog.Helper()
347+
352348
ok, err := regexp.Match(pattern, target)
353349
if err != nil {
354-
return xerrors.Errorf("failed to attempt regexp match: %w", err)
350+
slogtest.Fatal(t, "failed to attempt regexp match", slog.Error(err),
351+
slog.F("pattern", pattern),
352+
slog.F("target", string(target)),
353+
slog.F("sink", name),
354+
)
355355
}
356356
if !ok {
357-
return xerrors.Errorf(
358-
"expected to find pattern (%s) in %s, no match found in (%v)",
359-
pattern, name, string(target),
357+
slogtest.Fatal(t, "expected to find pattern, no match found",
358+
slog.F("pattern", pattern),
359+
slog.F("target", string(target)),
360+
slog.F("sink", name),
360361
)
361362
}
362-
return nil
363363
}
364364

365-
func empty(name string, a []byte) error {
365+
func empty(t *testing.T, name string, a []byte) {
366+
slog.Helper()
366367
if len(a) > 0 {
367-
return xerrors.Errorf("expected %s to be empty, got (%s)", name, string(a))
368+
slogtest.Fatal(t, "expected "+name+" to be empty", slog.F("got", string(a)))
368369
}
369-
return nil
370370
}
371371

372372
// DurationLessThan asserts that the command completed in less than the given duration
373373
func DurationLessThan(dur time.Duration) Assertion {
374374
return simpleFuncAssert{
375-
valid: func(r *CommandResult) error {
375+
valid: func(t *testing.T, r *CommandResult) {
376+
slog.Helper()
376377
if r.Duration > dur {
377-
return xerrors.Errorf("expected duration less than %s, took %s", dur.String(), r.Duration.String())
378+
slogtest.Fatal(t, "duration longer than expected",
379+
slog.F("expected_less_than", dur.String),
380+
slog.F("actual", r.Duration.String()),
381+
)
378382
}
379-
return nil
380383
},
381384
name: fmt.Sprintf("duration-lessthan"),
382385
}
@@ -385,11 +388,14 @@ func DurationLessThan(dur time.Duration) Assertion {
385388
// DurationGreaterThan asserts that the command completed in greater than the given duration
386389
func DurationGreaterThan(dur time.Duration) Assertion {
387390
return simpleFuncAssert{
388-
valid: func(r *CommandResult) error {
391+
valid: func(t *testing.T, r *CommandResult) {
392+
slog.Helper()
389393
if r.Duration < dur {
390-
return xerrors.Errorf("expected duration greater than %s, took %s", dur.String(), r.Duration.String())
394+
slogtest.Fatal(t, "duration shorter than expected",
395+
slog.F("expected_greater_than", dur.String),
396+
slog.F("actual", r.Duration.String()),
397+
)
391398
}
392-
return nil
393399
},
394400
name: fmt.Sprintf("duration-greaterthan"),
395401
}

0 commit comments

Comments
 (0)