-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
71 lines (63 loc) · 2.5 KB
/
parser_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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestListCommands(t *testing.T) {
cfg := GenerateConfig()
err := cfg.Load([]byte(fullYaml))
assert.NoError(t, err)
assert.ElementsMatch(t, cfg.ListCommands(), []string{"ls", "bundle"})
}
func TestGenerateHandler(t *testing.T) {
tests := map[string]struct {
cfg *Cfg
handlerName string
settings map[string]interface{}
expErr string
}{
"valid handler": {&Cfg{handler: map[string]Handler{}}, "test", map[string]interface{}{"handler": "docker_compose_run", "service": "app"}, ""},
"invalid handler": {&Cfg{}, "test", map[string]interface{}{"handler": "docker_compose_run"}, "error in strategy test: field service required but not set"},
"additional fields": {&Cfg{}, "foo", map[string]interface{}{"handler": "docker_compose_run", "service": "test", "other": "field"}, "error in strategy foo: additional field(s) detected: other"},
"invalid type": {&Cfg{}, "foo", map[string]interface{}{"handler": "docker_compose_run", "service": true, "remove": "foo"}, "error in strategy foo: 2 error(s) decoding:\n\n* 'Remove' expected type 'bool', got unconvertible type 'string'\n* 'Service' expected type 'string', got unconvertible type 'bool'"},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
err := test.cfg.generateHandler(test.handlerName, test.settings)
if test.expErr != "" {
assert.EqualError(t, err, test.expErr)
} else {
assert.NoError(t, err)
assert.NotNil(t, test.cfg.handler[test.handlerName])
}
})
}
}
func TestGetHandlerFor(t *testing.T) {
cfg := GenerateConfig()
err := cfg.Load([]byte(fullYaml))
assert.NoError(t, err)
tests := map[string]struct {
command string
strictMode bool
fallbackMode bool
expErr error
}{
"known cmd": {command: "ls"},
"unknown cmd": {command: "some-cmd"},
"known cmd with path, strict": {command: "ls", strictMode: true, fallbackMode: true},
"unknown cmd, strict,": {command: "some-cmd", strictMode: true, expErr: ErrUndefinedCommand},
"unknown cmd, strict, fallback": {command: "some-cmd", strictMode: true, expErr: ErrUndefinedCommand},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
handler, err := cfg.GetHandlerFor(test.command, test.strictMode, test.fallbackMode)
if test.expErr != nil {
assert.EqualError(t, err, test.expErr.Error())
} else {
assert.NoError(t, err)
assert.NotNil(t, handler)
}
})
}
}