|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + gdtcontext "github.com/gdt-dev/core/context" |
| 10 | + "github.com/gdt-dev/core/run" |
| 11 | + "github.com/gdt-dev/core/scenario" |
| 12 | + "github.com/gdt-dev/core/suite" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + |
| 15 | + "github.com/gdt-dev/gdt/cmd/gdt/pkg/cli" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + debugPrefix = "[gdt]" |
| 20 | + runUsage = `run <subject> [<subject> ...]` |
| 21 | + runDescLong = `Check test scenarios or test suites for parse errors. |
| 22 | +
|
| 23 | +The command will run gdt test scenarios or test suites pointed to by <subject>. |
| 24 | +
|
| 25 | +<subject> should be a path to a YAML file or a directory containing YAML files. |
| 26 | +
|
| 27 | +Returns 0 on if all subject test scenarios complete without failure, 1 |
| 28 | +otherwise. |
| 29 | +` |
| 30 | +) |
| 31 | + |
| 32 | +var RunCmd = &cobra.Command{ |
| 33 | + Use: runUsage, |
| 34 | + Short: "run test scenario/suites.", |
| 35 | + Long: runDescLong, |
| 36 | + Aliases: []string{"exec"}, |
| 37 | + RunE: doRun, |
| 38 | +} |
| 39 | + |
| 40 | +func init() { |
| 41 | + RunCmd.Flags().BoolVarP( |
| 42 | + &optQuiet, |
| 43 | + "quiet", |
| 44 | + "q", |
| 45 | + false, |
| 46 | + optQuietUsage, |
| 47 | + ) |
| 48 | +} |
| 49 | + |
| 50 | +func doRun(cmd *cobra.Command, args []string) error { |
| 51 | + if len(args) == 0 { |
| 52 | + return fmt.Errorf("supply <subject> containing filepath to YAML file or directory.") |
| 53 | + } |
| 54 | + if cli.CommonOptions.Debug { |
| 55 | + cli.CommonOptions.Verbose = true |
| 56 | + } |
| 57 | + ctx := gdtcontext.New(gdtcontext.WithDebugPrefix(debugPrefix)) |
| 58 | + run := run.New() |
| 59 | + for _, path := range args { |
| 60 | + fi, err := os.Stat(path) |
| 61 | + if err != nil { |
| 62 | + if os.IsNotExist(err) { |
| 63 | + return fmt.Errorf("%q not found.", path) |
| 64 | + } |
| 65 | + return err |
| 66 | + } |
| 67 | + if fi.IsDir() { |
| 68 | + cli.Df("loading suite from directory %q ...", path) |
| 69 | + su, err := suite.FromDir(path) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + err = su.Run(ctx, run) |
| 74 | + if err != nil { |
| 75 | + // Run() only returns RuntimeErrors. The `run` object will |
| 76 | + // contain assertion failures, which are not considered |
| 77 | + // RuntimeErrors. |
| 78 | + return err |
| 79 | + } |
| 80 | + } else { |
| 81 | + cli.Df("loading scenario from file %q ...", path) |
| 82 | + f, err := os.Open(path) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + defer f.Close() |
| 87 | + |
| 88 | + sc, err := scenario.FromReader(f, scenario.WithPath(path)) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + err = sc.Run(ctx, run) |
| 93 | + if err != nil { |
| 94 | + // Run() only returns RuntimeErrors. The `run` object will |
| 95 | + // contain assertion failures, which are not considered |
| 96 | + // RuntimeErrors. |
| 97 | + return err |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if !optQuiet { |
| 103 | + paths := run.ScenarioPaths() |
| 104 | + for _, path := range paths { |
| 105 | + if cli.CommonOptions.Verbose { |
| 106 | + fmt.Printf("=== RUN: %s\n", path) |
| 107 | + } |
| 108 | + var scenElapsed time.Duration |
| 109 | + |
| 110 | + results := run.ScenarioResults(path) |
| 111 | + scenOK := true |
| 112 | + for _, res := range results { |
| 113 | + scenElapsed += res.Elapsed() |
| 114 | + scenOK = scenOK && res.OK() |
| 115 | + printTestUnitResult(res) |
| 116 | + } |
| 117 | + |
| 118 | + if !optQuiet { |
| 119 | + if scenOK { |
| 120 | + if cli.CommonOptions.Verbose { |
| 121 | + fmt.Printf("PASS (%s)\n", scenElapsed) |
| 122 | + } else { |
| 123 | + fmt.Printf("ok\t%s\t%s\n", path, scenElapsed) |
| 124 | + } |
| 125 | + } else { |
| 126 | + fmt.Printf("FAIL\t%s\t%s\n", path, scenElapsed) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + if !run.OK() { |
| 132 | + if cli.CommonOptions.Verbose { |
| 133 | + fmt.Println("FAIL") |
| 134 | + } |
| 135 | + os.Exit(1) |
| 136 | + } else { |
| 137 | + if cli.CommonOptions.Verbose { |
| 138 | + fmt.Println("PASS") |
| 139 | + } |
| 140 | + } |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
| 144 | +func printTestUnitResult(r run.TestUnitResult) { |
| 145 | + if r.Skipped() { |
| 146 | + if cli.CommonOptions.Verbose { |
| 147 | + fmt.Printf("--- SKIP: %s (%s)\n", r.Name(), r.Elapsed()) |
| 148 | + } |
| 149 | + } else if r.OK() { |
| 150 | + if cli.CommonOptions.Verbose { |
| 151 | + fmt.Printf("--- PASS: %s (%s)\n", r.Name(), r.Elapsed()) |
| 152 | + } |
| 153 | + } else { |
| 154 | + for _, fail := range r.Failures() { |
| 155 | + indentFail := indent(fail.Error(), 1) |
| 156 | + if !optQuiet { |
| 157 | + fmt.Printf( |
| 158 | + "--- FAIL: %s (%s)\n%s\n", |
| 159 | + r.Name(), r.Elapsed(), indentFail, |
| 160 | + ) |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if cli.CommonOptions.Debug || !r.OK() { |
| 166 | + detail := r.Detail() |
| 167 | + if len(detail) > 0 { |
| 168 | + cli.HorizontalSectionHeader("detail") |
| 169 | + fmt.Printf("%s", r.Detail()) |
| 170 | + cli.HorizontalBar() |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func indent(subject string, level int) string { |
| 176 | + indentStr := strings.Repeat(" ", level*4) |
| 177 | + b := strings.Builder{} |
| 178 | + lines := strings.Split(subject, "\n") |
| 179 | + for _, line := range lines { |
| 180 | + b.WriteString(fmt.Sprintf("%s%s", indentStr, line)) |
| 181 | + } |
| 182 | + return b.String() |
| 183 | +} |
0 commit comments