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

Commit 50c4e75

Browse files
committed
Add GetResult Assertion
1 parent 5c118b8 commit 50c4e75

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

ci/tcli/tcli.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (a Assertable) Assert(t *testing.T, option ...Assertion) {
157157
}
158158
t.Run(name, func(t *testing.T) {
159159
t.Parallel()
160-
err := o.Valid(cmdResult)
160+
err := o.Valid(&cmdResult)
161161
assert.Success(t, name, err)
162162
})
163163
}
@@ -167,7 +167,7 @@ func (a Assertable) Assert(t *testing.T, option ...Assertion) {
167167
// Assertion specifies an assertion on the given CommandResult.
168168
// Pass custom Assertion types to cover special cases.
169169
type Assertion interface {
170-
Valid(r CommandResult) error
170+
Valid(r *CommandResult) error
171171
}
172172

173173
// Named is an optional extension of Assertion that provides a helpful label
@@ -184,11 +184,11 @@ type CommandResult struct {
184184
}
185185

186186
type simpleFuncAssert struct {
187-
valid func(r CommandResult) error
187+
valid func(r *CommandResult) error
188188
name string
189189
}
190190

191-
func (s simpleFuncAssert) Valid(r CommandResult) error {
191+
func (s simpleFuncAssert) Valid(r *CommandResult) error {
192192
return s.valid(r)
193193
}
194194

@@ -204,7 +204,7 @@ func Success() Assertion {
204204
// ExitCodeIs asserts that the command exited with the given code
205205
func ExitCodeIs(code int) Assertion {
206206
return simpleFuncAssert{
207-
valid: func(r CommandResult) error {
207+
valid: func(r *CommandResult) error {
208208
if r.ExitCode != code {
209209
return xerrors.Errorf("exit code of %v expected, got %v", code, r.ExitCode)
210210
}
@@ -217,17 +217,29 @@ func ExitCodeIs(code int) Assertion {
217217
// StdoutEmpty asserts that the command did not write any data to Stdout
218218
func StdoutEmpty() Assertion {
219219
return simpleFuncAssert{
220-
valid: func(r CommandResult) error {
220+
valid: func(r *CommandResult) error {
221221
return empty("stdout", r.Stdout)
222222
},
223223
name: fmt.Sprintf("stdout-empty"),
224224
}
225225
}
226226

227+
// GetResult offers an escape hatch from tcli
228+
// The passed pointer will be assigned to the commands *CommandResult
229+
func GetResult(result **CommandResult) Assertion {
230+
return simpleFuncAssert{
231+
valid: func(r *CommandResult) error {
232+
*result = r
233+
return nil
234+
},
235+
name: "get-stdout",
236+
}
237+
}
238+
227239
// StderrEmpty asserts that the command did not write any data to Stderr
228240
func StderrEmpty() Assertion {
229241
return simpleFuncAssert{
230-
valid: func(r CommandResult) error {
242+
valid: func(r *CommandResult) error {
231243
return empty("stderr", r.Stderr)
232244
},
233245
name: fmt.Sprintf("stderr-empty"),
@@ -237,7 +249,7 @@ func StderrEmpty() Assertion {
237249
// StdoutMatches asserts that Stdout contains a substring which matches the given regexp
238250
func StdoutMatches(pattern string) Assertion {
239251
return simpleFuncAssert{
240-
valid: func(r CommandResult) error {
252+
valid: func(r *CommandResult) error {
241253
return matches("stdout", pattern, r.Stdout)
242254
},
243255
name: fmt.Sprintf("stdout-matches"),
@@ -247,7 +259,7 @@ func StdoutMatches(pattern string) Assertion {
247259
// StderrMatches asserts that Stderr contains a substring which matches the given regexp
248260
func StderrMatches(pattern string) Assertion {
249261
return simpleFuncAssert{
250-
valid: func(r CommandResult) error {
262+
valid: func(r *CommandResult) error {
251263
return matches("stderr", pattern, r.Stderr)
252264
},
253265
name: fmt.Sprintf("stderr-matches"),
@@ -257,7 +269,7 @@ func StderrMatches(pattern string) Assertion {
257269
// CombinedMatches asserts that either Stdout or Stderr a substring which matches the given regexp
258270
func CombinedMatches(pattern string) Assertion {
259271
return simpleFuncAssert{
260-
valid: func(r CommandResult) error {
272+
valid: func(r *CommandResult) error {
261273
//stdoutValid := StdoutMatches(pattern).Valid(r)
262274
//stderrValid := StderrMatches(pattern).Valid(r)
263275
// TODO: combine errors
@@ -291,7 +303,7 @@ func empty(name string, a []byte) error {
291303
// DurationLessThan asserts that the command completed in less than the given duration
292304
func DurationLessThan(dur time.Duration) Assertion {
293305
return simpleFuncAssert{
294-
valid: func(r CommandResult) error {
306+
valid: func(r *CommandResult) error {
295307
if r.Duration > dur {
296308
return xerrors.Errorf("expected duration less than %s, took %s", dur.String(), r.Duration.String())
297309
}
@@ -304,7 +316,7 @@ func DurationLessThan(dur time.Duration) Assertion {
304316
// DurationGreaterThan asserts that the command completed in greater than the given duration
305317
func DurationGreaterThan(dur time.Duration) Assertion {
306318
return simpleFuncAssert{
307-
valid: func(r CommandResult) error {
319+
valid: func(r *CommandResult) error {
308320
if r.Duration < dur {
309321
return xerrors.Errorf("expected duration greater than %s, took %s", dur.String(), r.Duration.String())
310322
}

0 commit comments

Comments
 (0)