-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow_test.go
112 lines (101 loc) · 3.18 KB
/
workflow_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
package stalk
import (
"fmt"
"testing"
"errors"
"github.com/pavelmemory/stalk/command"
"github.com/pavelmemory/stalk/common"
"github.com/pavelmemory/stalk/flag"
)
func TestWorkflow_Run(t *testing.T) {
createCmd := command.New("create").
WithFlags(flag.String("name").Required(true).WithShortcut('n')).
WithBefore(func(ctx common.Runtime) error {
fmt.Println("before create command")
return nil
}).
WithAction(func(ctx common.Runtime) error {
fmt.Println("create command")
fmt.Println("ctx.HasGlobalFlag(\"verbose\")", ctx.HasGlobalFlag("verbose"))
fmt.Println("ctx.StringFlag(\"name\")", ctx.StringFlag("name"))
fmt.Println("ctx.HasFlag(\"name\")", ctx.HasFlag("name"))
fmt.Println("ctx.GetArgs()", ctx.GetArgs())
fmt.Println("ctx.GlobalFlags()", ctx.GlobalFlags())
fmt.Println("ctx.Flags()", ctx.Flags())
v, f := ctx.Get("1")
fmt.Println("ctx.Get(\"1\")", v, f)
return nil
})
showCmd := command.New("show").
WithFlags(flag.String("name").Required(true).WithShortcut('n')).
WithAction(func(ctx common.Runtime) error {
fmt.Println("show command")
fmt.Println(ctx.StringGlobalFlag("example"))
fmt.Println(ctx.StringGlobalFlag("help"))
fmt.Println(ctx.StringFlag("name"))
return nil
})
aws := command.New("aws").
WithSubCommands(createCmd, showCmd).
WithAction(func(ctx common.Runtime) error {
ctx.Set("1", "something")
return nil
})
app := New().
WithCommands(aws).
WithGlobalFlags(
flag.String("example").WithShortcut('e'),
flag.Signal("verbose").WithShortcut('v'),
flag.Signal("help").WithShortcut('h'))
err := app.Run([]string{"--verbose", "--example", "don'tbrelieve", "aws", "create", "--name", "tattoo", "valhalla", "and", "dumb"})
if err != nil {
t.Fatal(err)
}
}
func TestWorkflow_CurrentCommand(t *testing.T) {
commandName := "c"
childCommandName := "cc"
emptyError := errors.New("")
expectedChecks := 0
checkedTimes := 0
doCheckOnError := func(expectedCmdName string) func(common.Runtime, error) {
expectedChecks++
return func(ctx common.Runtime, err error) {
cmdName := ctx.CurrentCommand().GetName()
if cmdName != expectedCmdName {
t.Errorf("%d: | expected: %s | got: %s", checkedTimes, expectedCmdName, cmdName)
}
checkedTimes++
}
}
doCheckAndReturn := func(cmdName string, err error) func(ctx common.Runtime) error {
return func(ctx common.Runtime) error {
doCheckOnError(cmdName)(ctx, err)
return err
}
}
err := New().
WithCommands(
command.New(commandName).
WithAction(doCheckAndReturn(commandName, nil)).
WithOnError(doCheckOnError(commandName)).
WithBefore(doCheckAndReturn(commandName, nil)).
WithAfter(doCheckOnError(commandName)).
WithSubCommands(
command.New(childCommandName).
WithAction(doCheckAndReturn(childCommandName, emptyError)).
WithAfter(doCheckOnError(childCommandName)).
WithOnError(doCheckOnError(childCommandName))),
).
WithOnError(doCheckOnError(commandName)).
Run([]string{commandName, childCommandName})
if err == nil {
t.Error("error expected")
}
if checkedTimes > expectedChecks {
t.Error("too many checks triggered")
}
if checkedTimes < expectedChecks {
t.Error("not all checks triggered")
}
}