(t *testing.T)
| 94 | } |
| 95 | |
| 96 | func TestInjectContextWithMiddlewareError(t *testing.T) { |
| 97 | t.Parallel() |
| 98 | |
| 99 | fnCalled := false |
| 100 | |
| 101 | type contextKey struct{} |
| 102 | |
| 103 | ctx := context.Background() |
| 104 | ctx = context.WithValue(ctx, contextKey{}, true) |
| 105 | |
| 106 | fn := func(ctx context.Context) error { |
| 107 | assert.True(t, ctx.Value(contextKey{}).(bool)) |
| 108 | clictx := CLIContextFromContext(ctx) |
| 109 | assert.NotNil(t, clictx) |
| 110 | fnCalled = true |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | var middlewareError = errors.New("a middleware error") |
| 115 | |
| 116 | middleware := []func(context.Context) (context.Context, error){ |
| 117 | func(context.Context) (context.Context, error) { |
| 118 | assert.True(t, ctx.Value(contextKey{}).(bool)) |
| 119 | return nil, middlewareError |
| 120 | }, |
| 121 | } |
| 122 | |
| 123 | got := InjectContext(ctx, fn, middleware...) |
| 124 | |
| 125 | assert.NotNil(t, got) |
| 126 | |
| 127 | // execute the function and verify middleware resulted in error |
| 128 | err := got(new(cli.Context)) |
| 129 | assert.Error(t, err) |
| 130 | assert.Equal(t, middlewareError, err) |
| 131 | assert.False(t, fnCalled) // fn is not called because of middleware error |
| 132 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…