Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix value propagation in context for test actions #432

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (e *testEnv) processTestActions(ctx context.Context, t *testing.T, actions
var err error
out := ctx
for _, action := range actions {
out, err = action.runWithT(ctx, e.cfg, t)
out, err = action.runWithT(out, e.cfg, t)
if err != nil {
t.Fatalf("%s failure: %s", action.role, err)
}
Expand Down
28 changes: 24 additions & 4 deletions pkg/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,12 @@ func TestEnv_Test(t *testing.T) {
name: "context value propagation with with multiple features, before, during, and after test",
ctx: context.TODO(),
expected: []string{
"before-each-test",
"before-each-test-1",
"before-each-test-2",
"test-feat-1",
"test-feat-2",
"after-each-test",
"after-each-test-1",
"after-each-test-2",
},
setup: func(ctx context.Context, t *testing.T) []string {
env, err := NewWithContext(context.WithValue(ctx, &ctxTestKeyString{}, []string{}), envconf.New())
Expand All @@ -340,7 +342,16 @@ func TestEnv_Test(t *testing.T) {
if !ok {
t.Fatal("context value was not []string")
}
val = append(val, "before-each-test")
val = append(val, "before-each-test-1")
return context.WithValue(ctx, &ctxTestKeyString{}, val), nil
})
env.BeforeEachTest(func(ctx context.Context, _ *envconf.Config, t *testing.T) (context.Context, error) {
// update before test
val, ok := ctx.Value(&ctxTestKeyString{}).([]string)
if !ok {
t.Fatal("context value was not []string")
}
val = append(val, "before-each-test-2")
return context.WithValue(ctx, &ctxTestKeyString{}, val), nil
})
env.AfterEachTest(func(ctx context.Context, _ *envconf.Config, t *testing.T) (context.Context, error) {
Expand All @@ -349,7 +360,16 @@ func TestEnv_Test(t *testing.T) {
if !ok {
t.Fatal("context value was not []string")
}
val = append(val, "after-each-test")
val = append(val, "after-each-test-1")
return context.WithValue(ctx, &ctxTestKeyString{}, val), nil
})
env.AfterEachTest(func(ctx context.Context, _ *envconf.Config, t *testing.T) (context.Context, error) {
// update after the test
val, ok := ctx.Value(&ctxTestKeyString{}).([]string)
if !ok {
t.Fatal("context value was not []string")
}
val = append(val, "after-each-test-2")
return context.WithValue(ctx, &ctxTestKeyString{}, val), nil
})
f1 := features.New("test-feat").Assess("assess", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
Expand Down