]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gofmt: change -d to exit 1 if diffs exist
authorRobbie McMichael <robbie.mcmichael.git@gmail.com>
Mon, 6 Oct 2025 12:47:34 +0000 (12:47 +0000)
committerGopher Robot <gobot@golang.org>
Wed, 8 Oct 2025 20:56:57 +0000 (13:56 -0700)
When using the -d flag, set the exit code to 1 if there is a diff.

Fixes #46289

Change-Id: I802e8ccd1798ed7f4448696bec4bc82835ec71a2
GitHub-Last-Rev: db2207fba9a8f7a2f50138ec1f086ac6a74e1b10
GitHub-Pull-Request: golang/go#75649
Reviewed-on: https://go-review.googlesource.com/c/go/+/707635
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Sean Liao <sean@liao.dev>
Auto-Submit: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/gofmt/gofmt.go
src/cmd/gofmt/gofmt_test.go
src/cmd/gofmt/testdata/exitcode.golden [new file with mode: 0644]
src/cmd/gofmt/testdata/exitcode.input [new file with mode: 0644]

index bbb8b4fd15c2f74e64ac0cbeba6395cbf4856241..ad6ad636524479053b5609a023c1a69a03f378e8 100644 (file)
@@ -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.
@@ -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 {
@@ -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
                }
        }
 
index 6b80673af148f58a2dea38d71536d8c5755c1e6e..2aba0f03ff09e9faa6b3ab0e897281d7ced43b95 100644 (file)
@@ -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)
@@ -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
diff --git a/src/cmd/gofmt/testdata/exitcode.golden b/src/cmd/gofmt/testdata/exitcode.golden
new file mode 100644 (file)
index 0000000..06ab7d0
--- /dev/null
@@ -0,0 +1 @@
+package main
diff --git a/src/cmd/gofmt/testdata/exitcode.input b/src/cmd/gofmt/testdata/exitcode.input
new file mode 100644 (file)
index 0000000..4f2f092
--- /dev/null
@@ -0,0 +1 @@
+       package main