forked from commander-cli/commander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commander.go
161 lines (137 loc) · 3.32 KB
/
commander.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/reubenmiller/commander/v3/pkg/app"
"github.com/urfave/cli"
)
var version string
func main() {
run(os.Args)
}
func run(args []string) bool {
log.SetOutput(ioutil.Discard)
cliapp := createCliApp()
if err := cliapp.Run(args); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
return true
}
func createCliApp() *cli.App {
cliapp := cli.NewApp()
cliapp.Name = app.AppName
cliapp.Usage = "CLI app testing"
cliapp.Version = version
cliapp.Commands = []cli.Command{
createTestCommand(),
createAddCommand(),
}
return cliapp
}
func createTestCommand() cli.Command {
return cli.Command{
Name: "test",
Usage: `Execute cli app tests
By default it will use the commander.yaml from your current directory.
Tests are always executed in alphabetical order.
Examples:
Directory test:
commander test --dir /your/dir/
Stdin test:
cat commander.yaml | commander test -
HTTP test:
commander test https://your-url/commander_test.yaml
Filtering tests:
commander test commander.yaml --filter="my test"
Multiple filters:
commander test commander.yaml --filter=filter1 --filter=filter2
Regex filters:
commander test commander.yaml --filter="^filter1$"
`,
ArgsUsage: "[file] [--filter]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "no-color",
EnvVar: "COMMANDER_NO_COLOR",
Usage: "Activate or deactivate colored output",
},
cli.BoolFlag{
Name: "verbose",
Usage: "More output for debugging",
EnvVar: "COMMANDER_VERBOSE",
},
cli.BoolFlag{
Name: "dir",
Usage: "Execute all test files in a directory sorted by file name (recursively) - e.g. /path/to/test_files/",
},
cli.StringFlag{
Name: "workdir",
Usage: "Set the working directory of commander's execution",
},
cli.StringFlag{
Name: "config",
Usage: "Default config file to be applied to all test files. Lowest level config values take precednce",
},
cli.StringSliceFlag{
Name: "filter",
Usage: `Filter tests by a given regex pattern. Tests are filtered by its title.`,
},
},
Action: func(c *cli.Context) error {
return app.TestCommand(c.Args().First(), app.NewTestContextFromCli(c))
},
}
}
func createAddCommand() cli.Command {
return cli.Command{
Name: "add",
Usage: "Automatically add a test to your test suite",
ArgsUsage: "[command]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "stdout",
Usage: "Output test file to stdout",
},
cli.BoolFlag{
Name: "no-file",
Usage: "Don't create a commander.yaml",
},
cli.StringFlag{
Name: "file",
Usage: "Write to another file, default is commander.yaml",
},
},
Action: func(c *cli.Context) error {
file := ""
var existedContent []byte
if !c.Bool("no-file") {
dir, _ := os.Getwd()
file = path.Join(dir, app.CommanderFile)
if c.String("file") != "" {
file = c.String("file")
}
existedContent, _ = ioutil.ReadFile(file)
}
content, err := app.AddCommand(strings.Join(c.Args(), " "), existedContent)
if err != nil {
return err
}
if c.Bool("stdout") {
fmt.Println(string(content))
}
if !c.Bool("no-file") {
fmt.Println("written to", file)
err := ioutil.WriteFile(file, content, 0755)
if err != nil {
return err
}
}
return nil
},
}
}