AddCommand executes the add command command is the command which should be added to the test suite existed holds the existing yaml content
(command string, existed []byte)
| 13 | // command is the command which should be added to the test suite |
| 14 | // existed holds the existing yaml content |
| 15 | func AddCommand(command string, existed []byte) ([]byte, error) { |
| 16 | conf := suite.YAMLSuiteConf{ |
| 17 | Tests: make(map[string]suite.YAMLTest), |
| 18 | Config: suite.YAMLTestConfigConf{}, |
| 19 | } |
| 20 | c := cmd.NewCommand(command) |
| 21 | |
| 22 | if err := c.Execute(); err != nil { |
| 23 | return []byte{}, err |
| 24 | } |
| 25 | |
| 26 | //If a suite existed before adding the new command it is need to parse it and re-add it |
| 27 | if len(existed) > 0 { |
| 28 | err := yaml.UnmarshalStrict(existed, &conf) |
| 29 | if err != nil { |
| 30 | panic(err.Error()) |
| 31 | } |
| 32 | |
| 33 | for k, t := range conf.Tests { |
| 34 | test := suite.YAMLTest{ |
| 35 | Title: t.Title, |
| 36 | Stdout: t.Stdout.(runtime.ExpectedOut), |
| 37 | Stderr: t.Stderr.(runtime.ExpectedOut), |
| 38 | ExitCode: t.ExitCode, |
| 39 | Config: convertConfig(t.Config), |
| 40 | } |
| 41 | |
| 42 | //If title and command are not equal add the command property to the struct |
| 43 | if t.Title != t.Command { |
| 44 | test.Command = t.Command |
| 45 | } |
| 46 | |
| 47 | conf.Tests[k] = test |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | stdout := strings.TrimSpace(c.Stdout()) |
| 52 | stderr := strings.TrimSpace(c.Stderr()) |
| 53 | conf.Tests[command] = suite.YAMLTest{ |
| 54 | Title: command, |
| 55 | Stdout: runtime.ExpectedOut{Contains: []string{stdout}}, |
| 56 | Stderr: runtime.ExpectedOut{Contains: []string{stderr}}, |
| 57 | ExitCode: c.ExitCode(), |
| 58 | } |
| 59 | |
| 60 | out, err := yaml.Marshal(conf) |
| 61 | if err != nil { |
| 62 | return []byte{}, err |
| 63 | } |
| 64 | |
| 65 | return out, nil |
| 66 | } |
| 67 | |
| 68 | func convertConfig(config suite.YAMLTestConfigConf) suite.YAMLTestConfigConf { |
| 69 | if config.Dir == "" && len(config.Env) == 0 && config.Timeout == "" { |