diff options
Diffstat (limited to 'src/cmd')
| -rw-r--r-- | src/cmd/fix/main.go | 10 | ||||
| -rw-r--r-- | src/cmd/fix/main_test.go | 18 | ||||
| -rw-r--r-- | src/cmd/gofmt/gofmt.go | 49 | ||||
| -rw-r--r-- | src/cmd/gofmt/gofmt_test.go | 76 | ||||
| -rw-r--r-- | src/cmd/internal/diff/diff.go | 78 |
5 files changed, 15 insertions, 216 deletions
diff --git a/src/cmd/fix/main.go b/src/cmd/fix/main.go index 3229b71ec4..4e5c08731b 100644 --- a/src/cmd/fix/main.go +++ b/src/cmd/fix/main.go @@ -13,6 +13,7 @@ import ( "go/parser" "go/scanner" "go/token" + "internal/diff" "io" "io/fs" "os" @@ -20,8 +21,6 @@ import ( "sort" "strconv" "strings" - - "cmd/internal/diff" ) var ( @@ -228,12 +227,7 @@ func processFile(filename string, useStdin bool) error { } if *doDiff { - data, err := diff.Diff("go-fix", src, newSrc) - if err != nil { - return fmt.Errorf("computing diff: %s", err) - } - fmt.Printf("diff %s fixed/%s\n", filename, filename) - os.Stdout.Write(data) + os.Stdout.Write(diff.Diff(filename, src, "fixed/"+filename, newSrc)) return nil } diff --git a/src/cmd/fix/main_test.go b/src/cmd/fix/main_test.go index 1baa95c545..755007bc0d 100644 --- a/src/cmd/fix/main_test.go +++ b/src/cmd/fix/main_test.go @@ -7,10 +7,9 @@ package main import ( "go/ast" "go/parser" + "internal/diff" "strings" "testing" - - "cmd/internal/diff" ) type testCase struct { @@ -52,7 +51,7 @@ func parseFixPrint(t *testing.T, fn func(*ast.File) bool, desc, in string, mustB if s := string(outb); in != s && mustBeGofmt { t.Errorf("not gofmt-formatted.\n--- %s\n%s\n--- %s | gofmt\n%s", desc, in, desc, s) - tdiff(t, in, s) + tdiff(t, "want", in, "have", s) return } @@ -109,7 +108,7 @@ func TestRewrite(t *testing.T) { if !strings.HasPrefix(tt.Name, "testdata/") { t.Errorf("--- have\n%s\n--- want\n%s", out, tt.Out) } - tdiff(t, out, tt.Out) + tdiff(t, "have", out, "want", tt.Out) return } @@ -132,17 +131,12 @@ func TestRewrite(t *testing.T) { if out2 != out { t.Errorf("changed output after second round of fixes.\n--- output after first round\n%s\n--- output after second round\n%s", out, out2) - tdiff(t, out, out2) + tdiff(t, "first", out, "second", out2) } }) } } -func tdiff(t *testing.T, a, b string) { - data, err := diff.Diff("go-fix-test", []byte(a), []byte(b)) - if err != nil { - t.Error(err) - return - } - t.Error(string(data)) +func tdiff(t *testing.T, aname, a, bname, b string) { + t.Errorf("%s", diff.Diff(aname, []byte(a), bname, []byte(b))) } diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go index 4280ed4459..8efc88df88 100644 --- a/src/cmd/gofmt/gofmt.go +++ b/src/cmd/gofmt/gofmt.go @@ -14,6 +14,7 @@ import ( "go/printer" "go/scanner" "go/token" + "internal/diff" "io" "io/fs" "os" @@ -22,8 +23,6 @@ import ( "runtime/pprof" "strings" - "cmd/internal/diff" - "golang.org/x/sync/semaphore" ) @@ -287,12 +286,9 @@ func processFile(filename string, info fs.FileInfo, in io.Reader, r *reporter) e } } if *doDiff { - data, err := diffWithReplaceTempFile(src, res, filename) - if err != nil { - return fmt.Errorf("computing diff: %s", err) - } - fmt.Fprintf(r, "diff -u %s %s\n", filepath.ToSlash(filename+".orig"), filepath.ToSlash(filename)) - r.Write(data) + newName := filepath.ToSlash(filename) + oldName := newName + ".orig" + r.Write(diff.Diff(oldName, src, newName, res)) } } @@ -464,43 +460,6 @@ func fileWeight(path string, info fs.FileInfo) int64 { return info.Size() } -func diffWithReplaceTempFile(b1, b2 []byte, filename string) ([]byte, error) { - data, err := diff.Diff("gofmt", b1, b2) - if len(data) > 0 { - return replaceTempFilename(data, filename) - } - return data, err -} - -// replaceTempFilename replaces temporary filenames in diff with actual one. -// -// --- /tmp/gofmt316145376 2017-02-03 19:13:00.280468375 -0500 -// +++ /tmp/gofmt617882815 2017-02-03 19:13:00.280468375 -0500 -// ... -// -> -// --- path/to/file.go.orig 2017-02-03 19:13:00.280468375 -0500 -// +++ path/to/file.go 2017-02-03 19:13:00.280468375 -0500 -// ... -func replaceTempFilename(diff []byte, filename string) ([]byte, error) { - bs := bytes.SplitN(diff, []byte{'\n'}, 3) - if len(bs) < 3 { - return nil, fmt.Errorf("got unexpected diff for %s", filename) - } - // Preserve timestamps. - var t0, t1 []byte - if i := bytes.LastIndexByte(bs[0], '\t'); i != -1 { - t0 = bs[0][i:] - } - if i := bytes.LastIndexByte(bs[1], '\t'); i != -1 { - t1 = bs[1][i:] - } - // Always print filepath with slash separator. - f := filepath.ToSlash(filename) - bs[0] = []byte(fmt.Sprintf("--- %s%s", f+".orig", t0)) - bs[1] = []byte(fmt.Sprintf("+++ %s%s", f, t1)) - return bytes.Join(bs, []byte{'\n'}), nil -} - const chmodSupported = runtime.GOOS != "windows" // backupFile writes data to a new file named filename<number> with permissions perm, diff --git a/src/cmd/gofmt/gofmt_test.go b/src/cmd/gofmt/gofmt_test.go index 676c5b43ed..641e0ea415 100644 --- a/src/cmd/gofmt/gofmt_test.go +++ b/src/cmd/gofmt/gofmt_test.go @@ -7,10 +7,9 @@ package main import ( "bytes" "flag" + "internal/diff" "os" - "os/exec" "path/filepath" - "runtime" "strings" "testing" "text/scanner" @@ -119,11 +118,8 @@ func runTest(t *testing.T, in, out string) { t.Errorf("WARNING: -update did not rewrite input file %s", in) } - t.Errorf("(gofmt %s) != %s (see %s.gofmt)", in, out, in) - d, err := diffWithReplaceTempFile(expected, got, in) - if err == nil { - t.Errorf("%s", d) - } + t.Errorf("(gofmt %s) != %s (see %s.gofmt)\n%s", in, out, in, + diff.Diff("expected", expected, "got", got)) if err := os.WriteFile(in+".gofmt", got, 0666); err != nil { t.Error(err) } @@ -194,69 +190,3 @@ func TestBackupFile(t *testing.T) { } t.Logf("Created: %s", name) } - -func TestDiff(t *testing.T) { - if _, err := exec.LookPath("diff"); err != nil { - t.Skipf("skip test on %s: diff command is required", runtime.GOOS) - } - in := []byte("first\nsecond\n") - out := []byte("first\nthird\n") - filename := "difftest.txt" - b, err := diffWithReplaceTempFile(in, out, filename) - if err != nil { - t.Fatal(err) - } - - if runtime.GOOS == "windows" { - b = bytes.ReplaceAll(b, []byte{'\r', '\n'}, []byte{'\n'}) - } - - bs := bytes.SplitN(b, []byte{'\n'}, 3) - line0, line1 := bs[0], bs[1] - - if prefix := "--- difftest.txt.orig"; !bytes.HasPrefix(line0, []byte(prefix)) { - t.Errorf("diff: first line should start with `%s`\ngot: %s", prefix, line0) - } - - if prefix := "+++ difftest.txt"; !bytes.HasPrefix(line1, []byte(prefix)) { - t.Errorf("diff: second line should start with `%s`\ngot: %s", prefix, line1) - } - - want := `@@ -1,2 +1,2 @@ - first --second -+third -` - - if got := string(bs[2]); got != want { - t.Errorf("diff: got:\n%s\nwant:\n%s", got, want) - } -} - -func TestReplaceTempFilename(t *testing.T) { - diff := []byte(`--- /tmp/tmpfile1 2017-02-08 00:53:26.175105619 +0900 -+++ /tmp/tmpfile2 2017-02-08 00:53:38.415151275 +0900 -@@ -1,2 +1,2 @@ - first --second -+third -`) - want := []byte(`--- path/to/file.go.orig 2017-02-08 00:53:26.175105619 +0900 -+++ path/to/file.go 2017-02-08 00:53:38.415151275 +0900 -@@ -1,2 +1,2 @@ - first --second -+third -`) - // Check path in diff output is always slash regardless of the - // os.PathSeparator (`/` or `\`). - sep := string(os.PathSeparator) - filename := strings.Join([]string{"path", "to", "file.go"}, sep) - got, err := replaceTempFilename(diff, filename) - if err != nil { - t.Fatal(err) - } - if !bytes.Equal(got, want) { - t.Errorf("os.PathSeparator='%s': replacedDiff:\ngot:\n%s\nwant:\n%s", sep, got, want) - } -} diff --git a/src/cmd/internal/diff/diff.go b/src/cmd/internal/diff/diff.go deleted file mode 100644 index 0ec2d7f8f9..0000000000 --- a/src/cmd/internal/diff/diff.go +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package diff implements a Diff function that compare two inputs -// using the 'diff' tool. -package diff - -import ( - "bytes" - exec "internal/execabs" - "io/ioutil" - "os" - "runtime" -) - -// Returns diff of two arrays of bytes in diff tool format. -func Diff(prefix string, b1, b2 []byte) ([]byte, error) { - f1, err := writeTempFile(prefix, b1) - if err != nil { - return nil, err - } - defer os.Remove(f1) - - f2, err := writeTempFile(prefix, b2) - if err != nil { - return nil, err - } - defer os.Remove(f2) - - cmd := "diff" - if runtime.GOOS == "plan9" { - cmd = "/bin/ape/diff" - } - - data, err := exec.Command(cmd, "-u", f1, f2).CombinedOutput() - if len(data) > 0 { - // diff exits with a non-zero status when the files don't match. - // Ignore that failure as long as we get output. - err = nil - } - - // If we are on Windows and the diff is Cygwin diff, - // machines can get into a state where every Cygwin - // command works fine but prints a useless message like: - // - // Cygwin WARNING: - // Couldn't compute FAST_CWD pointer. This typically occurs if you're using - // an older Cygwin version on a newer Windows. Please update to the latest - // available Cygwin version from https://cygwin.com/. If the problem persists, - // please see https://cygwin.com/problems.html - // - // Skip over that message and just return the actual diff. - if len(data) > 0 && !bytes.HasPrefix(data, []byte("--- ")) { - i := bytes.Index(data, []byte("\n--- ")) - if i >= 0 && i < 80*10 && bytes.Contains(data[:i], []byte("://cygwin.com/")) { - data = data[i+1:] - } - } - - return data, err -} - -func writeTempFile(prefix string, data []byte) (string, error) { - file, err := ioutil.TempFile("", prefix) - if err != nil { - return "", err - } - _, err = file.Write(data) - if err1 := file.Close(); err == nil { - err = err1 - } - if err != nil { - os.Remove(file.Name()) - return "", err - } - return file.Name(), nil -} |
