(t *testing.T)
| 53 | } |
| 54 | |
| 55 | func TestInjectContextWithMiddleware(t *testing.T) { |
| 56 | t.Parallel() |
| 57 | |
| 58 | fnCalled := false |
| 59 | |
| 60 | type contextKey struct{} |
| 61 | type firstMiddlewareContextKey struct{} |
| 62 | type secondMiddlewareContextKey struct{} |
| 63 | |
| 64 | ctx := context.Background() |
| 65 | ctx = context.WithValue(ctx, contextKey{}, true) |
| 66 | |
| 67 | fn := func(ctx context.Context) error { |
| 68 | assert.True(t, ctx.Value(contextKey{}).(bool)) |
| 69 | clictx := CLIContextFromContext(ctx) |
| 70 | assert.NotNil(t, clictx) |
| 71 | assert.Equal(t, 42, ctx.Value(firstMiddlewareContextKey{}).(int)) |
| 72 | assert.Equal(t, "value", ctx.Value(secondMiddlewareContextKey{}).(string)) |
| 73 | fnCalled = true |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | middleware := []func(context.Context) (context.Context, error){ |
| 78 | func(ctx context.Context) (context.Context, error) { |
| 79 | return context.WithValue(ctx, firstMiddlewareContextKey{}, 42), nil |
| 80 | }, |
| 81 | func(ctx context.Context) (context.Context, error) { |
| 82 | return context.WithValue(ctx, secondMiddlewareContextKey{}, "value"), nil |
| 83 | }, |
| 84 | } |
| 85 | |
| 86 | got := InjectContext(ctx, fn, middleware...) |
| 87 | |
| 88 | assert.NotNil(t, got) |
| 89 | |
| 90 | // execute the function to ensure injected context is OK |
| 91 | err := got(new(cli.Context)) |
| 92 | assert.NoError(t, err) |
| 93 | assert.True(t, fnCalled) |
| 94 | } |
| 95 | |
| 96 | func TestInjectContextWithMiddlewareError(t *testing.T) { |
| 97 | t.Parallel() |
nothing calls this directly
no test coverage detected
searching dependent graphs…