Skip to content

Commit 524e2bb

Browse files
authored
pkg/test: remove dependence on ctx.t (#1195)
**Description of the change:** This commit removes `Cleanup`'s dependence on `t`. **Motivation for the change:** The test library has functions that are useful outside of `go test`, such as `createFromYAML`, `ctx` resource deletion, and auto setting of resource cleanup when using the framework's client. The only function that required `t` and would fail if it did not exist was `Cleanup`, as other function could handle if it was nil.
1 parent 153a59c commit 524e2bb

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

pkg/test/context.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,19 @@ func (ctx *TestCtx) GetID() string {
6767
}
6868

6969
func (ctx *TestCtx) Cleanup() {
70-
for i := len(ctx.cleanupFns) - 1; i >= 0; i-- {
71-
err := ctx.cleanupFns[i]()
72-
if err != nil {
73-
ctx.t.Errorf("A cleanup function failed with error: (%v)\n", err)
74-
}
75-
}
76-
}
77-
78-
// cleanupNoT is a modified version of Cleanup; does not use t for logging, instead uses log
79-
// intended for use by MainEntry, which does not have a testing.T
80-
func (ctx *TestCtx) cleanupNoT() {
8170
failed := false
8271
for i := len(ctx.cleanupFns) - 1; i >= 0; i-- {
8372
err := ctx.cleanupFns[i]()
8473
if err != nil {
8574
failed = true
86-
log.Errorf("A cleanup function failed with error: (%v)", err)
75+
if ctx.t != nil {
76+
ctx.t.Errorf("A cleanup function failed with error: (%v)\n", err)
77+
} else {
78+
log.Errorf("A cleanup function failed with error: (%v)", err)
79+
}
8780
}
8881
}
89-
if failed {
82+
if ctx.t == nil && failed {
9083
log.Fatal("A cleanup function failed")
9184
}
9285
}

pkg/test/main_entry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func MainEntry(m *testing.M) {
117117
log.Infof("Local operator stdout: %s", string(localCmdOutBuf.Bytes()))
118118
log.Infof("Local operator stderr: %s", string(localCmdErrBuf.Bytes()))
119119
}
120-
ctx.cleanupNoT()
120+
ctx.Cleanup()
121121
os.Exit(exitCode)
122122
}()
123123
// create crd

0 commit comments

Comments
 (0)