|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/pterm/pterm" |
| 11 | + |
| 12 | + "github.com/errata-ai/vale/v3/internal/core" |
| 13 | + "github.com/errata-ai/vale/v3/internal/testsuite" |
| 14 | +) |
| 15 | + |
| 16 | +// errTestFailed reports cases that ran and did not pass. |
| 17 | +// |
| 18 | +// It is not an error in the sense the others are -- Vale worked, the |
| 19 | +// configuration did not -- so main exits 1 with it rather than printing it as |
| 20 | +// a failure of Vale's own. |
| 21 | +var errTestFailed = errors.New("test cases failed") |
| 22 | + |
| 23 | +// testReport is the JSON form of a run. |
| 24 | +type testReport struct { |
| 25 | + Passed int `json:"passed"` |
| 26 | + Failed int `json:"failed"` |
| 27 | + Results []testResult `json:"results"` |
| 28 | +} |
| 29 | + |
| 30 | +type testResult struct { |
| 31 | + File string `json:"file"` |
| 32 | + Name string `json:"name"` |
| 33 | + Passed bool `json:"passed"` |
| 34 | + Reason string `json:"reason,omitempty"` |
| 35 | + Got string `json:"got,omitempty"` |
| 36 | + Want string `json:"want,omitempty"` |
| 37 | +} |
| 38 | + |
| 39 | +// runTests runs the test cases in the given files or directories. |
| 40 | +func runTests(args []string, flags *core.CLIFlags) error { |
| 41 | + paths, err := testsuite.Find(args) |
| 42 | + if err != nil { |
| 43 | + return core.NewE100("test", err) |
| 44 | + } else if len(paths) == 0 { |
| 45 | + return core.NewE100("test", errors.New("no test files found")) |
| 46 | + } |
| 47 | + |
| 48 | + runner := testsuite.NewRunner(flags) |
| 49 | + |
| 50 | + var results []testsuite.Result |
| 51 | + for _, path := range paths { |
| 52 | + cases, lErr := testsuite.Load(path) |
| 53 | + if lErr != nil { |
| 54 | + return core.NewE100("test", lErr) |
| 55 | + } |
| 56 | + |
| 57 | + for _, c := range cases { |
| 58 | + results = append(results, runner.Run(c)) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // A case that could not be run at all is a broken configuration, not a |
| 63 | + // failing assertion: report it as Vale's own error and stop. |
| 64 | + for _, r := range results { |
| 65 | + if r.Err != nil { |
| 66 | + return core.NewE100(filepath.Base(r.Case.Path), r.Err) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if flags.Output == "JSON" { |
| 71 | + return reportTestsJSON(results) |
| 72 | + } |
| 73 | + |
| 74 | + return reportTests(results, len(paths)) |
| 75 | +} |
| 76 | + |
| 77 | +func reportTests(results []testsuite.Result, files int) error { |
| 78 | + failed := 0 |
| 79 | + |
| 80 | + for _, r := range results { |
| 81 | + if !r.Failed() { |
| 82 | + continue |
| 83 | + } |
| 84 | + failed++ |
| 85 | + |
| 86 | + fmt.Printf("\n%s %s %s\n\n", |
| 87 | + pterm.Red("✗"), |
| 88 | + pterm.Bold.Sprint(r.Case.Name), |
| 89 | + pterm.Gray("— "+r.Reason)) |
| 90 | + |
| 91 | + if r.Case.About != "" { |
| 92 | + fmt.Printf(" %s %s\n", pterm.Gray("about"), r.Case.About) |
| 93 | + } |
| 94 | + fmt.Printf(" %s %s\n\n", pterm.Gray("from"), relPath(r.Case.Path)) |
| 95 | + |
| 96 | + if r.Case.Want != nil { |
| 97 | + fmt.Print(renderDiff(testsuite.Diff(*r.Case.Want, r.Got))) |
| 98 | + } else { |
| 99 | + fmt.Print(indentBlock(blockOrNone(r.Got))) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + summary := fmt.Sprintf("%d %s — %d passed, %d failed", |
| 104 | + files, pluralize("file", files), len(results)-failed, failed) |
| 105 | + |
| 106 | + if failed > 0 { |
| 107 | + fmt.Println() |
| 108 | + pterm.Error.Println(summary) |
| 109 | + return errTestFailed |
| 110 | + } |
| 111 | + |
| 112 | + pterm.Success.Println(summary) |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func reportTestsJSON(results []testsuite.Result) error { |
| 117 | + report := testReport{Results: make([]testResult, 0, len(results))} |
| 118 | + |
| 119 | + for _, r := range results { |
| 120 | + if r.Failed() { |
| 121 | + report.Failed++ |
| 122 | + } else { |
| 123 | + report.Passed++ |
| 124 | + } |
| 125 | + |
| 126 | + out := testResult{ |
| 127 | + File: r.Case.Path, |
| 128 | + Name: r.Case.Name, |
| 129 | + Passed: !r.Failed(), |
| 130 | + Reason: r.Reason, |
| 131 | + Got: r.Got, |
| 132 | + } |
| 133 | + if r.Case.Want != nil { |
| 134 | + out.Want = *r.Case.Want |
| 135 | + } |
| 136 | + |
| 137 | + report.Results = append(report.Results, out) |
| 138 | + } |
| 139 | + |
| 140 | + if err := printJSON(report); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + if report.Failed > 0 { |
| 144 | + return errTestFailed |
| 145 | + } |
| 146 | + |
| 147 | + return nil |
| 148 | +} |
| 149 | + |
| 150 | +// renderDiff colours a diff and indents it under its heading. |
| 151 | +func renderDiff(diff []testsuite.Line) string { |
| 152 | + if len(diff) == 0 { |
| 153 | + return indentBlock("(no alerts)") |
| 154 | + } |
| 155 | + |
| 156 | + var b strings.Builder |
| 157 | + for _, line := range diff { |
| 158 | + text := fmt.Sprintf("%c %s", line.Op, line.Text) |
| 159 | + |
| 160 | + switch line.Op { |
| 161 | + case testsuite.Del: |
| 162 | + text = pterm.Red(text) |
| 163 | + case testsuite.Add: |
| 164 | + text = pterm.Green(text) |
| 165 | + case testsuite.Same: |
| 166 | + text = pterm.Gray(text) |
| 167 | + } |
| 168 | + |
| 169 | + fmt.Fprintf(&b, " %s\n", text) |
| 170 | + } |
| 171 | + |
| 172 | + fmt.Fprintf(&b, "\n %s %s\n", |
| 173 | + pterm.Red("- expected"), pterm.Green("+ actual")) |
| 174 | + |
| 175 | + return b.String() |
| 176 | +} |
| 177 | + |
| 178 | +// relPath shortens a path against the working directory, since that is where |
| 179 | +// the reader is standing. |
| 180 | +func relPath(path string) string { |
| 181 | + wd, err := os.Getwd() |
| 182 | + if err != nil { |
| 183 | + return path |
| 184 | + } |
| 185 | + |
| 186 | + rel, err := filepath.Rel(wd, path) |
| 187 | + if err != nil || strings.HasPrefix(rel, "..") { |
| 188 | + return path |
| 189 | + } |
| 190 | + |
| 191 | + return rel |
| 192 | +} |
| 193 | + |
| 194 | +// indentBlock indents a rendered block so it sits under its heading. |
| 195 | +func indentBlock(s string) string { |
| 196 | + var b strings.Builder |
| 197 | + for _, line := range strings.Split(strings.TrimRight(s, "\n"), "\n") { |
| 198 | + fmt.Fprintf(&b, " %s\n", line) |
| 199 | + } |
| 200 | + return b.String() |
| 201 | +} |
| 202 | + |
| 203 | +func blockOrNone(s string) string { |
| 204 | + if strings.TrimSpace(s) == "" { |
| 205 | + return "(no alerts)" |
| 206 | + } |
| 207 | + return s |
| 208 | +} |
0 commit comments