-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathfake_parser.go
More file actions
79 lines (68 loc) · 2.1 KB
/
Copy pathfake_parser.go
File metadata and controls
79 lines (68 loc) · 2.1 KB
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
package parser
import (
"kool-dev/kool/core/builder"
"sort"
"strings"
)
// FakeParser implements all fake behaviors for using parser in tests.
type FakeParser struct {
CalledAddLookupPath bool
TargetFiles []string
CalledParse bool
CalledParseAvailableScripts bool
CalledParseAvailableDetails bool
MockParsedCommands map[string][]builder.Command
MockParseError map[string]error
MockScripts []string
MockScriptDetails []ScriptDetail
MockParseAvailableScriptsError error
MockParseAvailableDetailsError error
}
// AddLookupPath implements fake AddLookupPath behavior
func (f *FakeParser) AddLookupPath(rootPath string) (err error) {
f.CalledAddLookupPath = true
f.TargetFiles = append(f.TargetFiles, "kool.yml")
return
}
// Parse implements fake Parse behavior
func (f *FakeParser) Parse(script string) (commands []builder.Command, err error) {
f.CalledParse = true
commands = f.MockParsedCommands[script]
err = f.MockParseError[script]
return
}
// ParseAvailableScripts implements fake ParseAvailableScripts behavior
func (f *FakeParser) ParseAvailableScripts(filter string) (scripts []string, err error) {
f.CalledParseAvailableScripts = true
if filter == "" {
scripts = f.MockScripts
} else {
for _, script := range f.MockScripts {
if strings.HasPrefix(script, filter) {
scripts = append(scripts, script)
}
}
}
err = f.MockParseAvailableScriptsError
return
}
// ParseAvailableScriptsDetails implements fake ParseAvailableScriptsDetails behavior
func (f *FakeParser) ParseAvailableScriptsDetails(filter string) (details []ScriptDetail, err error) {
f.CalledParseAvailableDetails = true
if filter == "" {
details = append(details, f.MockScriptDetails...)
} else {
for _, detail := range f.MockScriptDetails {
if strings.HasPrefix(detail.Name, filter) {
details = append(details, detail)
}
}
}
if len(details) > 1 {
sort.Slice(details, func(i, j int) bool {
return details[i].Name < details[j].Name
})
}
err = f.MockParseAvailableDetailsError
return
}