Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions cmd/shfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ For more information and to report bugs, see https://github.com/mvdan/sh.
name = filename.val
}
if err := formatStdin(name); err != nil {
if err != errChangedWithDiff {
if err != errFormattingDiffers {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
Expand All @@ -297,7 +297,7 @@ For more information and to report bugs, see https://github.com/mvdan/sh.
// One exception is --apply-ignore, which explicitly changes this behavior.
// Another is --find, whose logic depends on walkPath being called.
if err := formatPath(path, false); err != nil {
if err != errChangedWithDiff {
if err != errFormattingDiffers {
fmt.Fprintln(os.Stderr, err)
}
status = 1
Expand All @@ -312,7 +312,7 @@ For more information and to report bugs, see https://github.com/mvdan/sh.
case nil:
case filepath.SkipDir:
return err
case errChangedWithDiff:
case errFormattingDiffers:
status = 1
default:
fmt.Fprintln(os.Stderr, err)
Expand All @@ -327,7 +327,7 @@ For more information and to report bugs, see https://github.com/mvdan/sh.
os.Exit(status)
}

var errChangedWithDiff = fmt.Errorf("")
var errFormattingDiffers = fmt.Errorf("")

func formatStdin(name string) error {
if write.val {
Expand Down Expand Up @@ -572,7 +572,7 @@ func formatBytes(src []byte, path string, fileLang syntax.LangVariant) error {
diffBytes := diffpkg.Diff(path+".orig", src, path, res)
if !color {
os.Stdout.Write(diffBytes)
return errChangedWithDiff
return errFormattingDiffers
}
// The first three lines are the header with the filenames, including --- and +++,
// and are marked in bold.
Expand All @@ -596,7 +596,10 @@ func formatBytes(src []byte, path string, fileLang syntax.LangVariant) error {
}
os.Stdout.Write(line)
}
return errChangedWithDiff
return errFormattingDiffers
}
if list.val != "false" && !write.val {
return errFormattingDiffers
}
}
if list.val == "false" && !write.val && !diff.val {
Expand Down
4 changes: 2 additions & 2 deletions cmd/shfmt/testdata/script/basic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ exec shfmt input.sh
cmp stdout input.sh.golden
! stderr .

exec shfmt -l input.sh
! exec shfmt -l input.sh
stdout 'input\.sh'
! stdout foo
! stderr .
cmp input.sh input.sh.orig

exec shfmt -l input.sh input.sh
! exec shfmt -l input.sh input.sh
stdout -count=2 'input.sh'

exec shfmt -l -w input.sh
Expand Down
8 changes: 4 additions & 4 deletions cmd/shfmt/testdata/script/editorconfig.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exec shfmt input.sh
cmp stdout input.sh.golden
! stderr .

exec shfmt -l input.sh
! exec shfmt -l input.sh
stdout 'input\.sh'
! stderr .

Expand Down Expand Up @@ -52,14 +52,14 @@ exec shfmt -l otherknobs
! stderr .

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

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

# Formatting files directly obeys ignore=true when --apply-ignore is given.
# Test the same modes that the earlier section does.
exec shfmt --apply-ignore -l input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh
! exec shfmt --apply-ignore -l input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh
stdout -count=1 'input\.sh$'
! stdout 'ignored\.sh'
! stderr .
Expand Down
4 changes: 2 additions & 2 deletions cmd/shfmt/testdata/script/walk.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ stdout 'foo'
[symlink] ! stdout . # note that filepath.WalkDir does not follow symlinks

# writing to non-regular files is forbidden as they'd be replaced by a regular file.
[symlink] exec shfmt -l symlink/symlink-ext.bash
[symlink] ! exec shfmt -l symlink/symlink-ext.bash
[symlink] stdout 'symlink-ext.bash'
[symlink] ! exec shfmt -w symlink/symlink-ext.bash
[symlink] stderr 'refusing to atomically replace'
[symlink] exec shfmt -l symlink/symlink-ext.bash
[symlink] ! exec shfmt -l symlink/symlink-ext.bash
[symlink] stdout 'symlink-ext.bash'

# -f on files should still check extension and shebang
Expand Down