diff --git a/main.go b/main.go index 2a7842d..61dd22f 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,6 @@ package main import ( - "bytes" "encoding/json" "flag" "fmt" @@ -321,7 +320,9 @@ func addBuildTag(src []byte, tag string) []byte { } // printDiff prints a unified diff between orig and rewritten for path, -// using the system diff command. +// using the system diff command. Both sides are labeled with path, so the +// header does not depend on the temporary files or on which diff is installed +// (GNU and BSD diff print their timestamps differently). func printDiff(path string, orig, rewritten []byte) error { origFile, err := os.CreateTemp("", "loopvec-orig-*.go") if err != nil { @@ -345,9 +346,7 @@ func printDiff(path string, orig, rewritten []byte) error { } _ = newFile.Close() - out, _ := exec.Command("diff", "-u", origFile.Name(), newFile.Name()).Output() - out = bytes.ReplaceAll(out, []byte(origFile.Name()), []byte(path)) - out = bytes.ReplaceAll(out, []byte(newFile.Name()), []byte(path)) + out, _ := exec.Command("diff", "-u", "-L", path, "-L", path, origFile.Name(), newFile.Name()).Output() _, err = os.Stdout.Write(out) return err } diff --git a/testdata/flag_diff.txt b/testdata/flag_diff.txt index bea082e..5d7bf4a 100644 --- a/testdata/flag_diff.txt +++ b/testdata/flag_diff.txt @@ -1,11 +1,11 @@ -# -d prints a unified diff (paths in the headers, timestamps aside) and leaves -# the source untouched. +# -d prints a unified diff, labeled with the file's path on both sides so the +# headers are the same with GNU and BSD diff, and leaves the source untouched. cp ops.go ops.go.orig loopvec -d . cmpenv stderr stderr.want -stdout -count=1 '^--- \S+/ops\.go\t\S+ \S+ [+-][0-9]{4}$' -stdout -count=1 '^\+\+\+ \S+/ops\.go\t\S+ \S+ [+-][0-9]{4}$' +stdout -count=1 '^--- \S+/ops\.go$' +stdout -count=1 '^\+\+\+ \S+/ops\.go$' stdout -count=1 '^@@ -1,7 \+1,43 @@$' stdout -count=1 '^\+//go:build goexperiment.simd$' stdout -count=1 '^\+import \($' diff --git a/testdata/readme_go_tool.txt b/testdata/readme_go_tool.txt index d4acc77..077772c 100644 --- a/testdata/readme_go_tool.txt +++ b/testdata/readme_go_tool.txt @@ -6,7 +6,7 @@ go work use $LOOPVEC_SRC exec go tool loopvec -d ./... cmpenv stderr stderr.want -stdout -count=1 '^\+\+\+ \S+/mypkg/ops\.go\t' +stdout -count=1 '^\+\+\+ \S+/mypkg/ops\.go$' exec go tool loopvec -split ./mypkg/... ! stdout . diff --git a/testdata/readme_patterns.txt b/testdata/readme_patterns.txt index f68ace1..fdc79f2 100644 --- a/testdata/readme_patterns.txt +++ b/testdata/readme_patterns.txt @@ -10,10 +10,10 @@ cmp b/ops.go b/ops.go.orig # unified diff, once per changed file loopvec -d ./... -stdout -count=1 '^--- \S+/a/ops\.go\t' -stdout -count=1 '^\+\+\+ \S+/a/ops\.go\t' -stdout -count=1 '^--- \S+/b/ops\.go\t' -stdout -count=1 '^\+\+\+ \S+/b/ops\.go\t' +stdout -count=1 '^--- \S+/a/ops\.go$' +stdout -count=1 '^\+\+\+ \S+/a/ops\.go$' +stdout -count=1 '^--- \S+/b/ops\.go$' +stdout -count=1 '^\+\+\+ \S+/b/ops\.go$' stdout -count=2 '^@@ -1,7 \+1,43 @@$' cmpenv stderr both.stderr cmp a/ops.go a/ops.go.orig diff --git a/testdata/readme_workflow.txt b/testdata/readme_workflow.txt index 37ceb56..2148adb 100644 --- a/testdata/readme_workflow.txt +++ b/testdata/readme_workflow.txt @@ -4,7 +4,7 @@ # 1. Preview changes loopvec -d ./mypkg/... cmpenv stderr stderr.want -stdout -count=1 '^\+\+\+ \S+/mypkg/ops\.go\t' +stdout -count=1 '^\+\+\+ \S+/mypkg/ops\.go$' ! exists mypkg/ops_simd.go # 2. Apply: creates ops_simd.go and guards ops.go