/
add_command.go
73 lines (61 loc) · 1.79 KB
/
add_command.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
package app
import (
"strings"
"github.com/commander-cli/cmd"
"github.com/commander-cli/commander/v2/pkg/runtime"
"github.com/commander-cli/commander/v2/pkg/suite"
"gopkg.in/yaml.v2"
)
// AddCommand executes the add command
// command is the command which should be added to the test suite
// existed holds the existing yaml content
func AddCommand(command string, existed []byte) ([]byte, error) {
conf := suite.YAMLSuiteConf{
Tests: make(map[string]suite.YAMLTest),
Config: suite.YAMLTestConfigConf{},
}
c := cmd.NewCommand(command)
if err := c.Execute(); err != nil {
return []byte{}, err
}
//If a suite existed before adding the new command it is need to parse it and re-add it
if len(existed) > 0 {
err := yaml.UnmarshalStrict(existed, &conf)
if err != nil {
panic(err.Error())
}
for k, t := range conf.Tests {
test := suite.YAMLTest{
Title: t.Title,
Stdout: t.Stdout.(runtime.ExpectedOut),
Stderr: t.Stderr.(runtime.ExpectedOut),
ExitCode: t.ExitCode,
Config: convertConfig(t.Config),
}
//If title and command are not equal add the command property to the struct
if t.Title != t.Command {
test.Command = t.Command
}
conf.Tests[k] = test
}
}
stdout := strings.TrimSpace(c.Stdout())
stderr := strings.TrimSpace(c.Stderr())
conf.Tests[command] = suite.YAMLTest{
Title: command,
Stdout: runtime.ExpectedOut{Contains: []string{stdout}},
Stderr: runtime.ExpectedOut{Contains: []string{stderr}},
ExitCode: c.ExitCode(),
}
out, err := yaml.Marshal(conf)
if err != nil {
return []byte{}, err
}
return out, nil
}
func convertConfig(config suite.YAMLTestConfigConf) suite.YAMLTestConfigConf {
if config.Dir == "" && len(config.Env) == 0 && config.Timeout == "" {
return suite.YAMLTestConfigConf{}
}
return config
}