Skip to content

Commit

Permalink
chore: Reorder run and verify steps for CLI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
irvinlim committed Jan 25, 2024
1 parent 0407b58 commit 9b7c826
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pkg/runtime/testing/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package testing

import (
"bytes"
"context"
"fmt"
"regexp"
Expand Down Expand Up @@ -133,24 +134,27 @@ func (c *CommandTest) Run(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

ctrlContext := mock.NewContext()
common.SetCtrlContext(ctrlContext)

var stdout, stderr *bytes.Buffer
done := make(chan struct{})
go func() {
c.run(ctx, t)
stdout, stderr = c.run(ctx, ctrlContext, t)
close(done)
}()

select {
case <-done:
c.verify(t, ctrlContext, stdout, stderr)
return
case <-ctx.Done():
t.Fatalf("test did not complete: %v", ctx.Err())
}
}

func (c *CommandTest) run(ctx context.Context, t *testing.T) {
func (c *CommandTest) run(ctx context.Context, ctrlContext *mock.Context, t *testing.T) (stdout, stderr *bytes.Buffer) {
// Override the shared context.
ctrlContext := mock.NewContext()
common.SetCtrlContext(ctrlContext)
client := ctrlContext.MockClientsets()
assert.NoError(t, InitFixtures(ctx, client, c.Fixtures))
client.ClearActions()
Expand All @@ -168,6 +172,12 @@ func (c *CommandTest) run(ctx context.Context, t *testing.T) {
return
}

return stdout, stderr
}

func (c *CommandTest) verify(t *testing.T, ctrlContext *mock.Context, stdout, stderr *bytes.Buffer) {
client := ctrlContext.MockClientsets()

// Ensure that output matches.
c.checkOutput(t, "stdout", stdout.String(), c.Stdout)
c.checkOutput(t, "stderr", stderr.String(), c.Stderr)
Expand Down

0 comments on commit 9b7c826

Please sign in to comment.