Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions src/cmd/gofmt/gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ var (

// debugging
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")

// errors
errFormattingDiffers = fmt.Errorf("formatting differs from gofmt's")
)

// Keep these in sync with go/format/format.go.
Expand Down Expand Up @@ -218,8 +221,12 @@ func (r *reporter) Report(err error) {
panic("Report with nil error")
}
st := r.getState()
scanner.PrintError(st.err, err)
st.exitCode = 2
if err == errFormattingDiffers {
st.exitCode = 1
} else {
scanner.PrintError(st.err, err)
st.exitCode = 2
}
}

func (r *reporter) ExitCode() int {
Expand Down Expand Up @@ -281,6 +288,7 @@ func processFile(filename string, info fs.FileInfo, in io.Reader, r *reporter) e
newName := filepath.ToSlash(filename)
oldName := newName + ".orig"
r.Write(diff.Diff(oldName, src, newName, res))
return errFormattingDiffers
}
}

Expand Down
55 changes: 52 additions & 3 deletions src/cmd/gofmt/gofmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@ func gofmtFlags(filename string, maxLines int) string {
return ""
}

func runTest(t *testing.T, in, out string) {
// process flags
*simplifyAST = false
// Reset global variables for all flags to their default value.
func resetFlags() {
*list = false
*write = false
*rewriteRule = ""
*simplifyAST = false
*doDiff = false
*allErrors = false
*cpuprofile = ""
}

func runTest(t *testing.T, in, out string) {
resetFlags()
info, err := os.Lstat(in)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -159,6 +168,46 @@ func TestRewrite(t *testing.T) {
}
}

// TestDiff runs gofmt with the -d flag on the input files and checks that the
// expected exit code is set.
func TestDiff(t *testing.T) {
tests := []struct {
in string
exitCode int
}{
{in: "testdata/exitcode.input", exitCode: 1},
{in: "testdata/exitcode.golden", exitCode: 0},
}

for _, tt := range tests {
resetFlags()
*doDiff = true

initParserMode()
initRewrite()

info, err := os.Lstat(tt.in)
if err != nil {
t.Error(err)
return
}

const maxWeight = 2 << 20
var buf, errBuf bytes.Buffer
s := newSequencer(maxWeight, &buf, &errBuf)
s.Add(fileWeight(tt.in, info), func(r *reporter) error {
return processFile(tt.in, info, nil, r)
})
if errBuf.Len() > 0 {
t.Logf("%q", errBuf.Bytes())
}

if s.GetExitCode() != tt.exitCode {
t.Errorf("%s: expected exit code %d, got %d", tt.in, tt.exitCode, s.GetExitCode())
}
}
}

// Test case for issue 3961.
func TestCRLF(t *testing.T) {
const input = "testdata/crlf.input" // must contain CR/LF's
Expand Down
1 change: 1 addition & 0 deletions src/cmd/gofmt/testdata/exitcode.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
1 change: 1 addition & 0 deletions src/cmd/gofmt/testdata/exitcode.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main