MCPcopy
hub / github.com/smallstep/cli / TestInjectContextWithMiddlewareError

Function TestInjectContextWithMiddlewareError

internal/command/inject_test.go:96–132  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

94}
95
96func 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}

Callers

nothing calls this directly

Calls 3

CLIContextFromContextFunction · 0.85
InjectContextFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…