Skip to content

Commit 6c448bb

Browse files
authored
fix: Return a non-zero error code if there are any warnings during fix. (#81)
Also, report warnings even if the file is already "fixed". This seems like another bug that was lurking. This change required less work than I was expecting. `go run` helpfully prints `error status %d` to stderr for us. Fixes #77
1 parent 1e5a257 commit 6c448bb

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
lines changed

cmd/cmd.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,30 +214,36 @@ func Run(c *Config, files []string) (ok bool, err error) {
214214
}
215215

216216
func fix(fixer *keepsorted.Fixer, filenames []string, modifiedLines []keepsorted.LineRange) (ok bool, err error) {
217+
ok = true
218+
217219
for _, fn := range filenames {
218220
contents, err := read(fn)
219221
if err != nil {
220222
return false, err
221223
}
222-
if want, alreadyFixed, warnings := fixer.Fix(fn, contents, modifiedLines); fn == stdin || !alreadyFixed {
224+
want, alreadyFixed, warnings := fixer.Fix(fn, contents, modifiedLines)
225+
if fn == stdin || !alreadyFixed {
223226
if err := write(fn, want); err != nil {
224227
return false, err
225228
}
226-
for _, warn := range warnings {
227-
log := log.Warn()
228-
if warn.Path != stdin {
229-
log = log.Str("file", warn.Path)
230-
}
231-
if warn.Lines.Start == warn.Lines.End {
232-
log = log.Int("line", warn.Lines.Start)
233-
} else {
234-
log = log.Int("start", warn.Lines.Start).Int("end", warn.Lines.End)
235-
}
236-
log.Msg(warn.Message)
229+
}
230+
231+
for _, warn := range warnings {
232+
ok = false
233+
log := log.Warn()
234+
if warn.Path != stdin {
235+
log = log.Str("file", warn.Path)
237236
}
237+
if warn.Lines.Start == warn.Lines.End {
238+
log = log.Int("line", warn.Lines.Start)
239+
} else {
240+
log = log.Int("start", warn.Lines.Start).Int("end", warn.Lines.End)
241+
}
242+
log.Msg(warn.Message)
238243
}
239244
}
240-
return true, nil
245+
246+
return ok, nil
241247
}
242248

243249
func lint(fixer *keepsorted.Fixer, filenames []string, modifiedLines []keepsorted.LineRange) (ok bool, err error) {

goldens/by_regex.err

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
WRN while parsing option "by_regex": error parsing regexp: missing argument to repetition operator: `*` line=85
22
WRN by_regex cannot be used with ignore_prefixes (consider adding a non-capturing group to the start of your regex instead of ignore_prefixes: "(?:foo|bar)") line=92
3+
exit status 1

goldens/generate-goldens.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ for i in "$@"; do
2525
out="${i%%in}out"
2626
err="${i%%in}err"
2727

28-
go run "${git_dir}" --id=keep-sorted-test --omit-timestamps - <"${i}" >"${out}" 2>"${err}"
28+
go run "${git_dir}" --id=keep-sorted-test --omit-timestamps - <"${i}" >"${out}" 2>"${err}" \
29+
|| true # Ignore any non-zero exit codes.
2930
if (( $(wc -l < "${err}") == 0 )); then
3031
rm "${err}"
3132
fi

goldens/golden_test.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestGoldens(t *testing.T) {
9191
wantErr = []byte(strings.ReplaceAll(string(wantErr), "\r\n", "\n"))
9292
wantErr = []byte(strings.ReplaceAll(string(wantErr), "\r", "\n"))
9393

94-
gotOut, gotErr, err := runKeepSorted(in)
94+
gotOut, gotErr, exitCode, err := runKeepSorted(in)
9595
if err != nil {
9696
t.Errorf("Had trouble running keep-sorted: %v", err)
9797
}
@@ -104,10 +104,13 @@ func TestGoldens(t *testing.T) {
104104
needsRegen <- inFile
105105
}
106106

107-
gotOut2, _, err := runKeepSorted(strings.NewReader(gotOut))
107+
gotOut2, _, exitCode2, err := runKeepSorted(strings.NewReader(gotOut))
108108
if err != nil {
109109
t.Errorf("Had trouble running keep-sorted on keep-sorted output: %v", err)
110110
}
111+
if exitCode != exitCode2 {
112+
t.Errorf("Running keep-sorted on keep-sorted output returned a different exit code (should be idempotent): got %d want %d", exitCode2, exitCode)
113+
}
111114
if diff := cmp.Diff(gotOut, gotOut2); diff != "" {
112115
t.Errorf("keep-sorted diff on keep-sorted output (should be idempotent) (-want +got)\n%s", diff)
113116
}
@@ -131,20 +134,20 @@ func showTopLevel(dir string) (string, error) {
131134
return strings.TrimSpace(string(b)), err
132135
}
133136

134-
func runKeepSorted(stdin io.Reader) (stdout, stderr string, err error) {
137+
func runKeepSorted(stdin io.Reader) (stdout, stderr string, exitCode int, err error) {
135138
cmd := exec.Command("go", "run", gitDir, "--id=keep-sorted-test", "--omit-timestamps", "-")
136139
cmd.Stdin = stdin
137140
outPipe, err := cmd.StdoutPipe()
138141
if err != nil {
139-
return "", "", fmt.Errorf("could not create stdout pipe: %w", err)
142+
return "", "", -1, fmt.Errorf("could not create stdout pipe: %w", err)
140143
}
141144
errPipe, err := cmd.StderrPipe()
142145
if err != nil {
143-
return "", "", fmt.Errorf("could not create stderr pipe: %w", err)
146+
return "", "", -1, fmt.Errorf("could not create stderr pipe: %w", err)
144147
}
145148

146149
if err := cmd.Start(); err != nil {
147-
return "", "", fmt.Errorf("could not start keep-sorted: %w", err)
150+
return "", "", -1, fmt.Errorf("could not start keep-sorted: %w", err)
148151
}
149152

150153
var errs []error
@@ -159,8 +162,13 @@ func runKeepSorted(stdin io.Reader) (stdout, stderr string, err error) {
159162
}
160163

161164
if err := cmd.Wait(); err != nil {
162-
errs = append(errs, fmt.Errorf("keep-sorted failed: %w", err))
165+
var ee *exec.ExitError
166+
if errors.As(err, &ee) {
167+
exitCode = ee.ExitCode()
168+
} else {
169+
errs = append(errs, fmt.Errorf("keep-sorted failed: %w", err))
170+
}
163171
}
164172

165-
return string(gotOut), string(gotErr), errors.Join(errs...)
173+
return string(gotOut), string(gotErr), exitCode, errors.Join(errs...)
166174
}

goldens/simple.err

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
WRN skip_lines has invalid value: -1 line=105
22
WRN unrecognized option "foo" line=105
33
WRN while parsing option "ignore_prefixes": content appears to be an unterminated YAML list: "[abc, foo" line=112
4+
exit status 1

0 commit comments

Comments
 (0)