Skip to content

Commit 8b8ee44

Browse files
authored
Merge pull request #102 from johnSchnake/featuresNeedTestingT
Add *testing.T to before/after feature hooks
2 parents cf494ff + 72e1963 commit 8b8ee44

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

pkg/env/action.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (a *action) runWithT(ctx context.Context, cfg *envconf.Config, t *testing.T
7171
}
7272

7373
// runWithFeature will run the action and inject a FeatureInfo object into the callback function.
74-
func (a *action) runWithFeature(ctx context.Context, cfg *envconf.Config, fi types.Feature) (context.Context, error) {
74+
func (a *action) runWithFeature(ctx context.Context, cfg *envconf.Config, t *testing.T, fi types.Feature) (context.Context, error) {
7575
switch a.role {
7676
case roleBeforeFeature, roleAfterFeature:
7777
for _, f := range a.featureFuncs {
@@ -80,7 +80,7 @@ func (a *action) runWithFeature(ctx context.Context, cfg *envconf.Config, fi typ
8080
}
8181

8282
var err error
83-
ctx, err = f(ctx, cfg, fi)
83+
ctx, err = f(ctx, cfg, t, fi)
8484
if err != nil {
8585
return ctx, err
8686
}

pkg/env/env.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (e *testEnv) processTestFeature(t *testing.T, featureName string, feature t
202202
afterFeatureActions := e.getAfterFeatureActions()
203203

204204
for _, action := range beforeFeatureActions {
205-
if e.ctx, err = action.runWithFeature(e.ctx, e.cfg, deepCopyFeature(feature)); err != nil {
205+
if e.ctx, err = action.runWithFeature(e.ctx, e.cfg, t, deepCopyFeature(feature)); err != nil {
206206
t.Fatalf("BeforeEachTest failure: %s", err)
207207
}
208208
}
@@ -212,7 +212,7 @@ func (e *testEnv) processTestFeature(t *testing.T, featureName string, feature t
212212

213213
// execute beforeFeature actions
214214
for _, action := range afterFeatureActions {
215-
if e.ctx, err = action.runWithFeature(e.ctx, e.cfg, deepCopyFeature(feature)); err != nil {
215+
if e.ctx, err = action.runWithFeature(e.ctx, e.cfg, t, deepCopyFeature(feature)); err != nil {
216216
t.Fatalf("BeforeEachTest failure: %s", err)
217217
}
218218
}

pkg/env/env_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,10 @@ func TestEnv_Test(t *testing.T) {
400400
setup: func(t *testing.T, ctx context.Context) []string {
401401
env := newTestEnv()
402402
val := []string{}
403-
env.BeforeEachFeature(func(ctx context.Context, _ *envconf.Config, info features.Feature) (context.Context, error) {
403+
env.BeforeEachFeature(func(ctx context.Context, _ *envconf.Config, _ *testing.T, info features.Feature) (context.Context, error) {
404404
val = append(val, "before-each-feature")
405405
return ctx, nil
406-
}).AfterEachFeature(func(ctx context.Context, _ *envconf.Config, info features.Feature) (context.Context, error) {
406+
}).AfterEachFeature(func(ctx context.Context, _ *envconf.Config, _ *testing.T, info features.Feature) (context.Context, error) {
407407
val = append(val, "after-each-feature")
408408
return ctx, nil
409409
})
@@ -433,7 +433,7 @@ func TestEnv_Test(t *testing.T) {
433433
setup: func(t *testing.T, ctx context.Context) []string {
434434
env := newTestEnv()
435435
val := []string{}
436-
env.BeforeEachFeature(func(ctx context.Context, _ *envconf.Config, info features.Feature) (context.Context, error) {
436+
env.BeforeEachFeature(func(ctx context.Context, _ *envconf.Config, _ *testing.T, info features.Feature) (context.Context, error) {
437437
val = append(val, "before-each-feature")
438438
t.Logf("%#v, len(steps)=%v step[0].Name: %v\n", info, len(info.Steps()), info.Steps()[0].Name())
439439

@@ -452,7 +452,7 @@ func TestEnv_Test(t *testing.T) {
452452
labelMap := info.Labels()
453453
labelMap["foo"] = "bar"
454454
return ctx, nil
455-
}).AfterEachFeature(func(ctx context.Context, _ *envconf.Config, info features.Feature) (context.Context, error) {
455+
}).AfterEachFeature(func(ctx context.Context, _ *envconf.Config, _ *testing.T, info features.Feature) (context.Context, error) {
456456
val = append(val, "after-each-feature")
457457
t.Logf("%#v, len(steps)=%v\n", info, len(info.Steps()))
458458
if info.Labels()["foo"] == "bar" {
@@ -549,13 +549,13 @@ func TestTestEnv_TestInParallel(t *testing.T) {
549549
return ctx, nil
550550
})
551551

552-
env.BeforeEachFeature(func(ctx context.Context, config *envconf.Config, feature types.Feature) (context.Context, error) {
552+
env.BeforeEachFeature(func(ctx context.Context, config *envconf.Config, _ *testing.T, feature types.Feature) (context.Context, error) {
553553
t.Logf("Running before each feature for feature %s", feature.Name())
554554
beforeFeatureCount++
555555
return ctx, nil
556556
})
557557

558-
env.AfterEachFeature(func(ctx context.Context, config *envconf.Config, feature types.Feature) (context.Context, error) {
558+
env.AfterEachFeature(func(ctx context.Context, config *envconf.Config, _ *testing.T, feature types.Feature) (context.Context, error) {
559559
t.Logf("Running after each feature for feature %s", feature.Name())
560560
afterFeatureCount++
561561
return ctx, nil

pkg/internal/types/types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ type EnvFunc func(context.Context, *envconf.Config) (context.Context, error)
3333
// can be used to customized the behavior of the
3434
// environment. Changes to context are expected to surface
3535
// to caller. Meant for use with before/after feature hooks.
36-
type FeatureEnvFunc func(context.Context, *envconf.Config, Feature) (context.Context, error)
36+
// *testing.T is provided in order to provide pass/fail context to
37+
// features.
38+
type FeatureEnvFunc func(context.Context, *envconf.Config, *testing.T, Feature) (context.Context, error)
3739

3840
// TestEnvFunc represents a user-defined operation that
3941
// can be used to customized the behavior of the

0 commit comments

Comments
 (0)