forked from CircleCI-Public/circleci-cli
/
tests.go
97 lines (79 loc) · 2.28 KB
/
tests.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
package cmd
import (
"github.com/CircleCI-Public/circleci-cli/logger"
"github.com/CircleCI-Public/circleci-cli/proxy"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/bmatcuk/doublestar"
"github.com/spf13/cobra"
)
type testsOptions struct {
cfg *settings.Config
log *logger.Logger
args []string
}
func newTestsCommand(config *settings.Config) *cobra.Command {
opts := testsOptions{
cfg: config,
}
testsCmd := &cobra.Command{
Use: "tests",
Short: "Collect and split files with tests",
Hidden: true,
}
globCmd := &cobra.Command{
Use: "glob",
Short: "Glob files using pattern",
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
opts.log = logger.NewLogger(config.Debug)
},
RunE: func(_ *cobra.Command, _ []string) error {
return globRun(opts)
},
Hidden: true,
}
splitCmd := &cobra.Command{
Use: "split",
Short: "Return a split batch of provided files",
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
},
RunE: func(_ *cobra.Command, _ []string) error {
return splitRunE(opts)
},
Hidden: true,
}
splitCmd.Flags().Uint("index", 0, "index of node.")
splitCmd.Flags().Uint("total", 1, "number of nodes.")
splitCmd.Flags().String("split-by", "name", `how to weight the split, allowed values are "name", "filesize", and "timings".`)
splitCmd.Flags().String("timings-type", "filename", `lookup historical timing data by: "classname", "filename", or "testname".`)
splitCmd.Flags().Bool("show-counts", false, `print test file or test class counts to stderr (default false).`)
splitCmd.Flags().String("timings-file", "", "JSON file containing historical timing data.")
testsCmd.AddCommand(globCmd)
testsCmd.AddCommand(splitCmd)
return testsCmd
}
func expandGlobs(opts testsOptions) ([]string, error) {
result := []string{}
for _, arg := range opts.args {
matches, err := doublestar.Glob(arg)
if err != nil {
return nil, err
}
result = append(result, matches...)
}
return result, nil
}
func globRun(opts testsOptions) error {
allfiles, err := expandGlobs(opts)
if err != nil {
return err
}
for _, filename := range allfiles {
opts.log.Infoln(filename)
}
return nil
}
func splitRunE(opts testsOptions) error {
return proxy.Exec([]string{"tests", "split"}, opts.args)
}