-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup_test.go
116 lines (108 loc) · 2.13 KB
/
setup_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
package gomods
import (
"bytes"
"testing"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/spf13/afero"
)
func Test_parse(t *testing.T) {
tests := []struct {
configFile string
config Config
}{
{
`
gomods
`,
Config{
GoBinary: DefaultGoBinaryPath,
Workers: DefaultGomodsWorkers,
},
},
{
`
gomods {
cache
}
`,
Config{
GoBinary: DefaultGoBinaryPath,
Workers: DefaultGomodsWorkers,
Cache: Cache{
Enable: true,
Type: DefaultGomodsCacheType,
Path: "/tmp/txtdirect/gomods",
},
},
},
{
`
gomods {
gobinary /my/go/binary
cache {
type local
path /my/cache/path
}
}
`,
Config{
GoBinary: "/my/go/binary",
Workers: DefaultGomodsWorkers,
Cache: Cache{
Enable: true,
Type: "local",
Path: "/my/cache/path",
},
},
},
{
`
gomods {
gobinary /my/go/binary
workers 5
cache {
type local
path /my/cache/path
}
}
`,
Config{
GoBinary: "/my/go/binary",
Cache: Cache{
Enable: true,
Type: "local",
Path: "/my/cache/path",
},
Workers: 5,
},
},
}
for _, test := range tests {
buf := bytes.NewBuffer([]byte(test.configFile))
block, err := caddyfile.Parse("Caddyfile", buf)
if err != nil {
t.Errorf("Couldn't read the config: %s", err.Error())
}
// Extract the config tokens from the server blocks
var tokens []caddyfile.Token
for _, segment := range block[0].Segments {
for _, token := range segment {
tokens = append(tokens, token)
}
}
d := caddyfile.NewDispenser(tokens)
g := &Gomods{}
if err := g.UnmarshalCaddyfile(d); err != nil {
t.Errorf("Couldn't parse the config: %s", err.Error())
}
// Fs field gets filled by default when parsing the config
test.config.fs = g.Config.fs
// Set the default cache path for expected config if cache type is tmp
if g.Config.Cache.Type == "tmp" {
test.config.Cache.Path = afero.GetTempDir(test.config.fs, "")
}
if g.Config != test.config {
t.Errorf("Expected config to be %+v, but got %+v", test.config, g.Config)
}
}
}