Skip to content

Commit 57a8024

Browse files
committed
exit nonzero if --list and not --write
1 parent 65adef8 commit 57a8024

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

cmd/shfmt/main.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ For more information and to report bugs, see https://github.com/mvdan/sh.
273273
name = filename.val
274274
}
275275
if err := formatStdin(name); err != nil {
276-
if err != errChangedWithDiff {
276+
if err != errUnformatted {
277277
fmt.Fprintln(os.Stderr, err)
278278
}
279279
os.Exit(1)
@@ -297,7 +297,7 @@ For more information and to report bugs, see https://github.com/mvdan/sh.
297297
// One exception is --apply-ignore, which explicitly changes this behavior.
298298
// Another is --find, whose logic depends on walkPath being called.
299299
if err := formatPath(path, false); err != nil {
300-
if err != errChangedWithDiff {
300+
if err != errUnformatted {
301301
fmt.Fprintln(os.Stderr, err)
302302
}
303303
status = 1
@@ -312,7 +312,7 @@ For more information and to report bugs, see https://github.com/mvdan/sh.
312312
case nil:
313313
case filepath.SkipDir:
314314
return err
315-
case errChangedWithDiff:
315+
case errUnformatted:
316316
status = 1
317317
default:
318318
fmt.Fprintln(os.Stderr, err)
@@ -327,7 +327,7 @@ For more information and to report bugs, see https://github.com/mvdan/sh.
327327
os.Exit(status)
328328
}
329329

330-
var errChangedWithDiff = fmt.Errorf("")
330+
var errUnformatted = fmt.Errorf("")
331331

332332
func formatStdin(name string) error {
333333
if write.val {
@@ -572,7 +572,7 @@ func formatBytes(src []byte, path string, fileLang syntax.LangVariant) error {
572572
diffBytes := diffpkg.Diff(path+".orig", src, path, res)
573573
if !color {
574574
os.Stdout.Write(diffBytes)
575-
return errChangedWithDiff
575+
return errUnformatted
576576
}
577577
// The first three lines are the header with the filenames, including --- and +++,
578578
// and are marked in bold.
@@ -596,7 +596,10 @@ func formatBytes(src []byte, path string, fileLang syntax.LangVariant) error {
596596
}
597597
os.Stdout.Write(line)
598598
}
599-
return errChangedWithDiff
599+
return errUnformatted
600+
}
601+
if list.val != "false" && !write.val {
602+
return errUnformatted
600603
}
601604
}
602605
if list.val == "false" && !write.val && !diff.val {

cmd/shfmt/testdata/script/basic.txtar

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ exec shfmt input.sh
1414
cmp stdout input.sh.golden
1515
! stderr .
1616

17-
exec shfmt -l input.sh
17+
! exec shfmt -l input.sh
1818
stdout 'input\.sh'
1919
! stdout foo
2020
! stderr .
2121
cmp input.sh input.sh.orig
2222

23-
exec shfmt -l input.sh input.sh
23+
! exec shfmt -l input.sh input.sh
2424
stdout -count=2 'input.sh'
2525

2626
exec shfmt -l -w input.sh

cmd/shfmt/testdata/script/editorconfig.txtar

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ exec shfmt input.sh
2424
cmp stdout input.sh.golden
2525
! stderr .
2626

27-
exec shfmt -l input.sh
27+
! exec shfmt -l input.sh
2828
stdout 'input\.sh'
2929
! stderr .
3030

@@ -52,14 +52,14 @@ exec shfmt -l otherknobs
5252
! stderr .
5353

5454
# Files found by walking directories are skipped if they match ignore=true properties.
55-
exec shfmt -l ignored
55+
! exec shfmt -l ignored
5656
stdout 'regular\.sh'
5757
! stdout 'ignored\.sh'
5858
! stderr .
5959

6060
# EditorConfig ignore=true properties are obeyed even when any formatting flags
6161
# are used, which cause formatting options from EditorConfig files to be skipped.
62-
exec shfmt -i=0 -l ignored
62+
! exec shfmt -i=0 -l ignored
6363
stdout 'regular\.sh'
6464
! stdout 'ignored\.sh'
6565
! stderr .
@@ -85,7 +85,7 @@ stdout -count=1 'echo foo'
8585

8686
# Formatting files directly obeys ignore=true when --apply-ignore is given.
8787
# Test the same modes that the earlier section does.
88-
exec shfmt --apply-ignore -l input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh
88+
! exec shfmt --apply-ignore -l input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh
8989
stdout -count=1 'input\.sh$'
9090
! stdout 'ignored\.sh'
9191
! stderr .

cmd/shfmt/testdata/script/walk.txtar

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ stdout 'foo'
6161
[symlink] ! stdout . # note that filepath.WalkDir does not follow symlinks
6262

6363
# writing to non-regular files is forbidden as they'd be replaced by a regular file.
64-
[symlink] exec shfmt -l symlink/symlink-ext.bash
64+
[symlink] ! exec shfmt -l symlink/symlink-ext.bash
6565
[symlink] stdout 'symlink-ext.bash'
6666
[symlink] ! exec shfmt -w symlink/symlink-ext.bash
6767
[symlink] stderr 'refusing to atomically replace'
68-
[symlink] exec shfmt -l symlink/symlink-ext.bash
68+
[symlink] ! exec shfmt -l symlink/symlink-ext.bash
6969
[symlink] stdout 'symlink-ext.bash'
7070

7171
# -f on files should still check extension and shebang

0 commit comments

Comments
 (0)