-
Notifications
You must be signed in to change notification settings - Fork 3
/
context_test.go
166 lines (126 loc) · 3.22 KB
/
context_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package gitdir
import (
"context"
"testing"
"github.com/go-git/go-billy/v5/memfs"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/belak/go-gitdir/models"
)
func TestContextKey(t *testing.T) {
t.Parallel()
var tests = []struct { //nolint:gofumpt
Input contextKey
Base string
Expected string
}{
{
contextKey("hello world"),
"hello world",
"Context key: hello world",
},
{
contextKeyConfig,
"gitdir-config",
"Context key: gitdir-config",
},
{
contextKeyUser,
"gitdir-user",
"Context key: gitdir-user",
},
{
contextKeyLogger,
"gitdir-logger",
"Context key: gitdir-logger",
},
{
contextKeyPublicKey,
"gitdir-public-key",
"Context key: gitdir-public-key",
},
}
baseCtx := context.Background()
for _, test := range tests {
assert.Equal(t, test.Expected, test.Input.String())
ctx := context.WithValue(baseCtx, test.Input, "hello world")
// Make sure you can't pull values out with the raw string
assert.Nil(t, ctx.Value(test.Base))
// Assert values come out properly
assert.Equal(t, "hello world", ctx.Value(test.Input))
}
}
func TestCtxExtract(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger, config, user := CtxExtract(ctx)
config.fs = nil
assert.Equal(t, &log.Logger, logger)
assert.Equal(t, NewConfig(nil), config)
assert.Equal(t, AnonymousUser, user)
}
func TestCtxSetConfig(t *testing.T) {
t.Skip("not implemented")
t.Parallel()
}
func TestCtxConfig(t *testing.T) {
t.Parallel()
ctx := context.Background()
// Check the default value
config := CtxConfig(ctx)
config.fs = nil
assert.Equal(t, NewConfig(nil), config)
// Check that when we set a value, this properly extracts it.
config = NewConfig(memfs.New())
ctx = context.WithValue(ctx, contextKeyConfig, config)
assert.Equal(t, config, CtxConfig(ctx))
}
func TestCtxSetUser(t *testing.T) {
t.Skip("not implemented")
t.Parallel()
}
func TestCtxUser(t *testing.T) {
t.Parallel()
ctx := context.Background()
// Check the default value
assert.Equal(t, AnonymousUser, CtxUser(ctx))
// Check that when we set a value, this properly extracts it.
user := &User{
Username: "belak",
IsAdmin: true,
}
ctx = context.WithValue(ctx, contextKeyUser, user)
assert.Equal(t, user, CtxUser(ctx))
}
func TestCtxSetLogger(t *testing.T) {
t.Skip("not implemented")
t.Parallel()
}
func TestWithLogger(t *testing.T) {
t.Skip("not implemented")
t.Parallel()
}
func TestCtxLogger(t *testing.T) {
t.Parallel()
ctx := context.Background()
// Check the default value
assert.Equal(t, &log.Logger, CtxLogger(ctx))
// Check that when we set a value, this properly extracts it.
logger := log.With().Str("hello", "world").Logger()
ctx = context.WithValue(ctx, contextKeyLogger, &logger)
assert.Equal(t, &logger, CtxLogger(ctx))
}
func TestCtxSetPublicKey(t *testing.T) {
t.Skip("not implemented")
t.Parallel()
}
func TestCtxPublicKey(t *testing.T) {
t.Parallel()
ctx := context.Background()
// Check the default value
assert.Nil(t, CtxPublicKey(ctx))
// Check that when we set a value, this properly extracts it.
pk := &models.PublicKey{}
ctx = context.WithValue(ctx, contextKeyPublicKey, pk)
assert.Equal(t, pk, CtxPublicKey(ctx))
}