Skip to content

Commit d945600

Browse files
robbiemcmichaelgopherbot
authored andcommitted
cmd/gofmt: change -d to exit 1 if diffs exist
When using the -d flag, set the exit code to 1 if there is a diff. Fixes golang#46289 Change-Id: I802e8ccd1798ed7f4448696bec4bc82835ec71a2 GitHub-Last-Rev: db2207f GitHub-Pull-Request: golang#75649 Reviewed-on: https://go-review.googlesource.com/c/go/+/707635 Reviewed-by: Carlos Amedee <[email protected]> Reviewed-by: Michael Pratt <[email protected]> Reviewed-by: Sean Liao <[email protected]> Auto-Submit: Sean Liao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent d4830c6 commit d945600

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

src/cmd/gofmt/gofmt.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ var (
4141

4242
// debugging
4343
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")
44+
45+
// errors
46+
errFormattingDiffers = fmt.Errorf("formatting differs from gofmt's")
4447
)
4548

4649
// Keep these in sync with go/format/format.go.
@@ -218,8 +221,12 @@ func (r *reporter) Report(err error) {
218221
panic("Report with nil error")
219222
}
220223
st := r.getState()
221-
scanner.PrintError(st.err, err)
222-
st.exitCode = 2
224+
if err == errFormattingDiffers {
225+
st.exitCode = 1
226+
} else {
227+
scanner.PrintError(st.err, err)
228+
st.exitCode = 2
229+
}
223230
}
224231

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

src/cmd/gofmt/gofmt_test.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,19 @@ func gofmtFlags(filename string, maxLines int) string {
5353
return ""
5454
}
5555

56-
func runTest(t *testing.T, in, out string) {
57-
// process flags
58-
*simplifyAST = false
56+
// Reset global variables for all flags to their default value.
57+
func resetFlags() {
58+
*list = false
59+
*write = false
5960
*rewriteRule = ""
61+
*simplifyAST = false
62+
*doDiff = false
63+
*allErrors = false
64+
*cpuprofile = ""
65+
}
66+
67+
func runTest(t *testing.T, in, out string) {
68+
resetFlags()
6069
info, err := os.Lstat(in)
6170
if err != nil {
6271
t.Error(err)
@@ -159,6 +168,46 @@ func TestRewrite(t *testing.T) {
159168
}
160169
}
161170

171+
// TestDiff runs gofmt with the -d flag on the input files and checks that the
172+
// expected exit code is set.
173+
func TestDiff(t *testing.T) {
174+
tests := []struct {
175+
in string
176+
exitCode int
177+
}{
178+
{in: "testdata/exitcode.input", exitCode: 1},
179+
{in: "testdata/exitcode.golden", exitCode: 0},
180+
}
181+
182+
for _, tt := range tests {
183+
resetFlags()
184+
*doDiff = true
185+
186+
initParserMode()
187+
initRewrite()
188+
189+
info, err := os.Lstat(tt.in)
190+
if err != nil {
191+
t.Error(err)
192+
return
193+
}
194+
195+
const maxWeight = 2 << 20
196+
var buf, errBuf bytes.Buffer
197+
s := newSequencer(maxWeight, &buf, &errBuf)
198+
s.Add(fileWeight(tt.in, info), func(r *reporter) error {
199+
return processFile(tt.in, info, nil, r)
200+
})
201+
if errBuf.Len() > 0 {
202+
t.Logf("%q", errBuf.Bytes())
203+
}
204+
205+
if s.GetExitCode() != tt.exitCode {
206+
t.Errorf("%s: expected exit code %d, got %d", tt.in, tt.exitCode, s.GetExitCode())
207+
}
208+
}
209+
}
210+
162211
// Test case for issue 3961.
163212
func TestCRLF(t *testing.T) {
164213
const input = "testdata/crlf.input" // must contain CR/LF's
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main

0 commit comments

Comments
 (0)