From 46778d8e612882e4b0f62fe2a33c171485ffb28c Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Fri, 4 Nov 2022 11:21:55 +0100 Subject: [PATCH] all: use internal/diff to display diffs Signed-off-by: Sebastien Binet --- brio/cmd/brio-gen/internal/gen/gen_test.go | 16 +------ brio/cmd/brio-gen/main_test.go | 16 +------ cmd/podio-gen/main_test.go | 12 +----- cmd/root2arrow/main_test.go | 7 ++-- cmd/root2csv/main_test.go | 4 +- csvutil/csv_test.go | 28 +++---------- fwk/dflow_test.go | 3 +- groot/cmd/root-dump/main_test.go | 49 ++-------------------- groot/cmd/root-gen-datareader/main_test.go | 5 +-- groot/cmd/root-gen-rfunc/main_test.go | 5 +-- groot/cmd/root-gen-streamer/main_test.go | 49 ++-------------------- groot/cmd/root-gen-type/main_test.go | 49 ++-------------------- groot/rcmd/copy_test.go | 3 +- groot/rcmd/ls_test.go | 5 +-- groot/rdict/gen_test.go | 6 +-- groot/riofs/walk_test.go | 5 +-- groot/rphys/lorentzvector_test.go | 5 +-- hepmc/hepmc_test.go | 9 ++-- hepmc/rootcnv/rootcnv_test.go | 9 ++-- 19 files changed, 51 insertions(+), 234 deletions(-) diff --git a/brio/cmd/brio-gen/internal/gen/gen_test.go b/brio/cmd/brio-gen/internal/gen/gen_test.go index fe0f68e91..1ca910c26 100644 --- a/brio/cmd/brio-gen/internal/gen/gen_test.go +++ b/brio/cmd/brio-gen/internal/gen/gen_test.go @@ -11,6 +11,7 @@ import ( "testing" "go-hep.org/x/hep/brio/cmd/brio-gen/internal/gen" + "go-hep.org/x/hep/internal/diff" ) func TestGenerator(t *testing.T) { @@ -43,19 +44,6 @@ func TestGenerator(t *testing.T) { } if !bytes.Equal(got, want) { - diff, err := exec.LookPath("diff") - hasDiff := err == nil - if hasDiff { - err = os.WriteFile(golden+"_got", got, 0644) - if err == nil { - out := new(bytes.Buffer) - cmd := exec.Command(diff, "-urN", golden+"_got", golden) - cmd.Stdout = out - cmd.Stderr = out - err = cmd.Run() - t.Fatalf("files differ. err=%v\n%v\n", err, out.String()) - } - } - t.Fatalf("files differ.\ngot = %s\nwant= %s\n", string(got), string(want)) + t.Fatalf("files differ:\n%s\n", diff.Format(string(got), string(want))) } } diff --git a/brio/cmd/brio-gen/main_test.go b/brio/cmd/brio-gen/main_test.go index 972989043..27c486b79 100644 --- a/brio/cmd/brio-gen/main_test.go +++ b/brio/cmd/brio-gen/main_test.go @@ -8,17 +8,14 @@ package main import ( "bytes" "os" - "os/exec" "testing" // make sure this has been compiled for TestGenerate _ "go-hep.org/x/hep/brio/cmd/brio-gen/internal/briotest" + "go-hep.org/x/hep/internal/diff" ) func TestGenerate(t *testing.T) { - diff, err := exec.LookPath("diff") - hasDiff := err == nil - for _, tc := range []struct { name string types []string @@ -49,16 +46,7 @@ func TestGenerate(t *testing.T) { } outfile := tc.want + "_got" if !bytes.Equal(got, want) { - err = os.WriteFile(outfile, got, 0644) - if err == nil && hasDiff { - out := new(bytes.Buffer) - cmd := exec.Command(diff, "-urN", outfile, tc.want) - cmd.Stdout = out - cmd.Stderr = out - err = cmd.Run() - t.Fatalf("generated code error: %v\n%v\n", err, out.String()) - } - t.Fatalf("generated code error.\ngot:\n%s\nwant:\n%s\n", string(got), string(want)) + t.Fatalf("generated code error:\n%s\n", diff.Format(string(got), string(want))) } // Remove output if test passes // Note: output file are referenced in .gitignore diff --git a/cmd/podio-gen/main_test.go b/cmd/podio-gen/main_test.go index 8906e804a..575dc9f98 100644 --- a/cmd/podio-gen/main_test.go +++ b/cmd/podio-gen/main_test.go @@ -6,12 +6,11 @@ package main import ( "bytes" - "fmt" "os" "strings" "testing" - "github.com/google/go-cmp/cmp" + "go-hep.org/x/hep/internal/diff" ) func TestGenerator(t *testing.T) { @@ -42,15 +41,8 @@ func TestGenerator(t *testing.T) { } if got, want := got.Bytes(), want; !bytes.Equal(got, want) { - t.Fatalf("invalid generated code:\n%s", txtDiff(got, want)) + t.Fatalf("invalid generated code:\n%s", diff.Format(string(got), string(want))) } }) } } - -func txtDiff(got, want []byte) string { - diff := cmp.Diff(string(want), string(got)) - var o strings.Builder - fmt.Fprintf(&o, "(-want +got)\n%s", diff) - return o.String() -} diff --git a/cmd/root2arrow/main_test.go b/cmd/root2arrow/main_test.go index 5bb4b98b8..094ec8526 100644 --- a/cmd/root2arrow/main_test.go +++ b/cmd/root2arrow/main_test.go @@ -10,7 +10,7 @@ import ( "os/exec" "testing" - "github.com/google/go-cmp/cmp" + "go-hep.org/x/hep/internal/diff" ) func init() { @@ -91,10 +91,9 @@ func TestFile(t *testing.T) { } if got, want := string(got), string(want); got != want { - diff := cmp.Diff(want, got) t.Fatalf( - "arrow file/stream differ: -- (-ref +got)\n%s", - diff, + "arrow file/stream differ:\n%s", + diff.Format(got, want), ) } }) diff --git a/cmd/root2csv/main_test.go b/cmd/root2csv/main_test.go index ce85e22c9..7934ea9b5 100644 --- a/cmd/root2csv/main_test.go +++ b/cmd/root2csv/main_test.go @@ -8,6 +8,8 @@ import ( "bytes" "os" "testing" + + "go-hep.org/x/hep/internal/diff" ) func TestROOT2CSV(t *testing.T) { @@ -67,7 +69,7 @@ func TestROOT2CSV(t *testing.T) { } if !bytes.Equal(got, want) { - t.Fatalf("CSV files differ") + t.Fatalf("CSV files differ:\n%s", diff.Format(string(got), string(want))) } }) } diff --git a/csvutil/csv_test.go b/csvutil/csv_test.go index 156b4a300..162549178 100644 --- a/csvutil/csv_test.go +++ b/csvutil/csv_test.go @@ -5,13 +5,12 @@ package csvutil_test import ( - "bytes" "fmt" "io" - "os/exec" "testing" "go-hep.org/x/hep/csvutil" + "go-hep.org/x/hep/internal/diff" ) func TestCSVReaderScanArgs(t *testing.T) { @@ -349,7 +348,7 @@ func TestCSVWriterArgs(t *testing.T) { t.Errorf("error closing table: %+v\n", err) } - err = diff("testdata/write-results.csv", fname) + err = diff.Files("testdata/write-results.csv", fname) if err != nil { t.Errorf("files differ: %+v\n", err) } @@ -394,7 +393,7 @@ func TestCSVWriterStruct(t *testing.T) { t.Errorf("error closing table: %+v\n", err) } - err = diff("testdata/write-results.csv", fname) + err = diff.Files("testdata/write-results.csv", fname) if err != nil { t.Errorf("files differ: %+v\n", err) } @@ -432,7 +431,7 @@ func TestCSVWriterArgsSlice(t *testing.T) { t.Fatalf("error closing table: %+v", err) } - err = diff("testdata/write-results-slice.csv", fname) + err = diff.Files("testdata/write-results-slice.csv", fname) if err != nil { t.Fatalf("files differ: %+v", err) } @@ -506,7 +505,7 @@ func TestCSVAppend(t *testing.T) { t.Fatal(err) } - err = diff("testdata/append.csv", fname) + err = diff.Files("testdata/append.csv", fname) if err != nil { t.Errorf("files differ: %+v\n", err) } @@ -649,23 +648,8 @@ func TestCSVWriterTypes(t *testing.T) { t.Errorf("error closing table: %+v\n", err) } - err = diff("testdata/types.csv.ref", fname) + err = diff.Files("testdata/types.csv.ref", fname) if err != nil { t.Errorf("files differ: %+v\n", err) } } - -func diff(ref, chk string) error { - cmd := exec.Command("diff", "-urN", ref, chk) - buf := new(bytes.Buffer) - cmd.Stdout = buf - cmd.Stderr = buf - err := cmd.Run() - if err != nil { - return fmt.Errorf("diff %v %v failed: %w\n%v\n", - ref, chk, err, - buf.String(), - ) - } - return nil -} diff --git a/fwk/dflow_test.go b/fwk/dflow_test.go index b3d27ad74..f5c44fb34 100644 --- a/fwk/dflow_test.go +++ b/fwk/dflow_test.go @@ -10,6 +10,7 @@ import ( "testing" "go-hep.org/x/hep/fwk/job" + "go-hep.org/x/hep/internal/diff" ) func TestDFlowSvcGraph(t *testing.T) { @@ -76,7 +77,7 @@ func TestDFlowSvcGraph(t *testing.T) { } if !bytes.Equal(got, want) { - t.Fatalf("dot files differ.\ngot:\n%s\nwant:\n%s\n", got, want) + t.Fatalf("dot files differ:\n%s", diff.Format(string(got), string(want))) } os.Remove(dotfile) diff --git a/groot/cmd/root-dump/main_test.go b/groot/cmd/root-dump/main_test.go index 6aacbade4..10aee58e8 100644 --- a/groot/cmd/root-dump/main_test.go +++ b/groot/cmd/root-dump/main_test.go @@ -6,11 +6,10 @@ package main import ( "bytes" - "fmt" "os" - "os/exec" - "path/filepath" "testing" + + "go-hep.org/x/hep/internal/diff" ) func TestROOTDump(t *testing.T) { @@ -37,50 +36,8 @@ func TestROOTDump(t *testing.T) { } if got, want := o.String(), string(want); got != want { - t.Fatalf("error:\n%s\n", diff(t, got, want)) + t.Fatalf("error:\n%s\n", diff.Format(got, want)) } }) } } - -func diff(t *testing.T, chk, ref string) string { - t.Helper() - - if !hasDiffCmd { - return fmt.Sprintf("=== got ===\n%s\n=== want ===\n%s\n", chk, ref) - } - - tmpdir, err := os.MkdirTemp("", "groot-diff-") - if err != nil { - t.Fatalf("could not create tmpdir: %+v", err) - } - defer os.RemoveAll(tmpdir) - - got := filepath.Join(tmpdir, "got.txt") - err = os.WriteFile(got, []byte(chk), 0644) - if err != nil { - t.Fatalf("could not create %s file: %+v", got, err) - } - - want := filepath.Join(tmpdir, "want.txt") - err = os.WriteFile(want, []byte(ref), 0644) - if err != nil { - t.Fatalf("could not create %s file: %+v", want, err) - } - - out := new(bytes.Buffer) - cmd := exec.Command("diff", "-urN", want, got) - cmd.Stdout = out - cmd.Stderr = out - err = cmd.Run() - return out.String() + "\nerror: " + err.Error() -} - -var hasDiffCmd = false - -func init() { - _, err := exec.LookPath("diff") - if err == nil { - hasDiffCmd = true - } -} diff --git a/groot/cmd/root-gen-datareader/main_test.go b/groot/cmd/root-gen-datareader/main_test.go index 1d5b3fa5c..8f3b22bc9 100644 --- a/groot/cmd/root-gen-datareader/main_test.go +++ b/groot/cmd/root-gen-datareader/main_test.go @@ -9,7 +9,7 @@ import ( "strings" "testing" - "github.com/google/go-cmp/cmp" + "go-hep.org/x/hep/internal/diff" ) func TestGenerate(t *testing.T) { @@ -63,8 +63,7 @@ func TestGenerate(t *testing.T) { } if got, want := o.String(), string(want); got != want { - diff := cmp.Diff(got, want) - t.Fatalf("invalid code generation:\n%s", diff) + t.Fatalf("invalid code generation:\n%s", diff.Format(got, want)) } }) } diff --git a/groot/cmd/root-gen-rfunc/main_test.go b/groot/cmd/root-gen-rfunc/main_test.go index cb8ba39db..8995cad26 100644 --- a/groot/cmd/root-gen-rfunc/main_test.go +++ b/groot/cmd/root-gen-rfunc/main_test.go @@ -9,7 +9,7 @@ import ( "os" "testing" - "github.com/google/go-cmp/cmp" + "go-hep.org/x/hep/internal/diff" ) func TestGenerate(t *testing.T) { @@ -115,8 +115,7 @@ func TestGenerate(t *testing.T) { } if got, want := string(got), string(want); got != want { - diff := cmp.Diff(want, got) - t.Fatalf("invalid generated code:\n%s\n", diff) + t.Fatalf("invalid generated code:\n%s\n", diff.Format(got, want)) } }) } diff --git a/groot/cmd/root-gen-streamer/main_test.go b/groot/cmd/root-gen-streamer/main_test.go index 58e1046a7..2c668df8b 100644 --- a/groot/cmd/root-gen-streamer/main_test.go +++ b/groot/cmd/root-gen-streamer/main_test.go @@ -9,11 +9,11 @@ import ( "fmt" "os" "os/exec" - "path/filepath" "strings" "testing" "go-hep.org/x/hep/groot/internal/rdatatest" + "go-hep.org/x/hep/internal/diff" ) var ( @@ -45,7 +45,7 @@ func TestGenerate(t *testing.T) { } if got, want := buf.String(), string(want); got != want { - t.Fatalf("error:\n%s\n", diff(t, got, want)) + t.Fatalf("error:\n%s\n", diff.Format(got, want)) } }) } @@ -200,51 +200,8 @@ func main() { } if got, want := out.String(), tc.want; got != want { - t.Fatalf("error:\n%v", diff(t, got, want)) + t.Fatalf("error:\n%v", diff.Format(got, want)) } }) } - -} - -func diff(t *testing.T, chk, ref string) string { - t.Helper() - - if !hasDiffCmd { - return fmt.Sprintf("=== got ===\n%s\n=== want ===\n%s\n", chk, ref) - } - - tmpdir, err := os.MkdirTemp("", "groot-diff-") - if err != nil { - t.Fatalf("could not create tmpdir: %v", err) - } - defer os.RemoveAll(tmpdir) - - got := filepath.Join(tmpdir, "got.txt") - err = os.WriteFile(got, []byte(chk), 0644) - if err != nil { - t.Fatalf("could not create %s file: %v", got, err) - } - - want := filepath.Join(tmpdir, "want.txt") - err = os.WriteFile(want, []byte(ref), 0644) - if err != nil { - t.Fatalf("could not create %s file: %v", want, err) - } - - out := new(bytes.Buffer) - cmd := exec.Command("diff", "-urN", want, got) - cmd.Stdout = out - cmd.Stderr = out - err = cmd.Run() - return out.String() + "\nerror: " + err.Error() -} - -var hasDiffCmd = false - -func init() { - _, err := exec.LookPath("diff") - if err == nil { - hasDiffCmd = true - } } diff --git a/groot/cmd/root-gen-type/main_test.go b/groot/cmd/root-gen-type/main_test.go index 9d435e913..5eed8e5f8 100644 --- a/groot/cmd/root-gen-type/main_test.go +++ b/groot/cmd/root-gen-type/main_test.go @@ -7,12 +7,13 @@ package main import ( "bytes" "flag" - "fmt" "os" "os/exec" "path/filepath" "reflect" "testing" + + "go-hep.org/x/hep/internal/diff" ) var ( @@ -81,7 +82,7 @@ func TestGenerate(t *testing.T) { } if !reflect.DeepEqual(got, want) { - t.Fatalf("error:\n%v", diff(t, string(got), string(want))) + t.Fatalf("error:\n%v", diff.Format(string(got), string(want))) } }) } @@ -243,7 +244,7 @@ func main() { } if !reflect.DeepEqual(got, want) { - t.Fatalf("error:\n%v", diff(t, string(got), string(want))) + t.Fatalf("error:\n%v", diff.Format(string(got), string(want))) } err = os.WriteFile(filepath.Join(dir, "main.go"), []byte(tc.main), 0644) @@ -285,45 +286,3 @@ func main() { }) } } - -func diff(t *testing.T, chk, ref string) string { - t.Helper() - - if !hasDiffCmd { - return fmt.Sprintf("=== got ===\n%s\n=== want ===\n%s\n", chk, ref) - } - - tmpdir, err := os.MkdirTemp("", "groot-diff-") - if err != nil { - t.Fatalf("could not create tmpdir: %v", err) - } - defer os.RemoveAll(tmpdir) - - got := filepath.Join(tmpdir, "got.txt") - err = os.WriteFile(got, []byte(chk), 0644) - if err != nil { - t.Fatalf("could not create %s file: %v", got, err) - } - - want := filepath.Join(tmpdir, "want.txt") - err = os.WriteFile(want, []byte(ref), 0644) - if err != nil { - t.Fatalf("could not create %s file: %v", want, err) - } - - out := new(bytes.Buffer) - cmd := exec.Command("diff", "-urN", want, got) - cmd.Stdout = out - cmd.Stderr = out - err = cmd.Run() - return out.String() + "\nerror: " + err.Error() -} - -var hasDiffCmd = false - -func init() { - _, err := exec.LookPath("diff") - if err == nil { - hasDiffCmd = true - } -} diff --git a/groot/rcmd/copy_test.go b/groot/rcmd/copy_test.go index 9181f6511..80883640f 100644 --- a/groot/rcmd/copy_test.go +++ b/groot/rcmd/copy_test.go @@ -17,6 +17,7 @@ import ( "go-hep.org/x/hep/groot/riofs" "go-hep.org/x/hep/groot/root" "go-hep.org/x/hep/groot/rtree" + "go-hep.org/x/hep/internal/diff" ) func TestROOTCp(t *testing.T) { @@ -296,6 +297,6 @@ func TestROOTCpTree(t *testing.T) { } if got, want := got.String(), want.String(); got != want { - t.Fatalf("dumps differ:\ngot:\n%s\n===\nwant:\n%s\n===\n", got, want) + t.Fatalf("dumps differ:\n%s\n", diff.Format(got, want)) } } diff --git a/groot/rcmd/ls_test.go b/groot/rcmd/ls_test.go index 33e0a7280..67028c58d 100644 --- a/groot/rcmd/ls_test.go +++ b/groot/rcmd/ls_test.go @@ -9,8 +9,8 @@ import ( "strings" "testing" - "github.com/google/go-cmp/cmp" "go-hep.org/x/hep/groot/rcmd" + "go-hep.org/x/hep/internal/diff" ) func TestList(t *testing.T) { @@ -87,8 +87,7 @@ version: 60600 } if got, want := out.String(), tc.want; got != want { - diff := cmp.Diff(want, got) - t.Fatalf("invalid root-ls output: -- (-ref +got)\n%s", diff) + t.Fatalf("invalid root-ls output:\n%s", diff.Format(got, want)) } }) } diff --git a/groot/rdict/gen_test.go b/groot/rdict/gen_test.go index 4d74e89aa..a7c7c8d71 100644 --- a/groot/rdict/gen_test.go +++ b/groot/rdict/gen_test.go @@ -8,7 +8,7 @@ import ( "strings" "testing" - "github.com/google/go-cmp/cmp" + "go-hep.org/x/hep/internal/diff" ) func TestGoName2Cxx(t *testing.T) { @@ -782,10 +782,8 @@ func TestGenCxxStreamerInfo(t *testing.T) { } if got, want := got.String(), tc.want; got != want { - diff := cmp.Diff(got, want) - t.Fatalf("invalid streamer representation for %q:\n%s", tc.name, diff) + t.Fatalf("invalid streamer representation for %q:\n%s", tc.name, diff.Format(got, want)) } }) } - } diff --git a/groot/riofs/walk_test.go b/groot/riofs/walk_test.go index 620614c88..c749f1a89 100644 --- a/groot/riofs/walk_test.go +++ b/groot/riofs/walk_test.go @@ -12,10 +12,10 @@ import ( "strings" "testing" - "github.com/google/go-cmp/cmp" "go-hep.org/x/hep/groot/rbase" "go-hep.org/x/hep/groot/rhist" "go-hep.org/x/hep/groot/root" + "go-hep.org/x/hep/internal/diff" ) func TestGet(t *testing.T) { @@ -478,8 +478,7 @@ data/file.root/dir2/dir21 (TDirectoryFile) data/file.root/dir2/dir22 (TDirectoryFile) ` if got != want { - diff := cmp.Diff(want, got) - t.Fatalf("invalid Walk display: -- (-ref +got)\n%s", diff) + t.Fatalf("invalid Walk display:\n%s", diff.Format(got, want)) } } diff --git a/groot/rphys/lorentzvector_test.go b/groot/rphys/lorentzvector_test.go index aba704423..ce9b3ea85 100644 --- a/groot/rphys/lorentzvector_test.go +++ b/groot/rphys/lorentzvector_test.go @@ -8,9 +8,9 @@ import ( "strings" "testing" - "github.com/google/go-cmp/cmp" "go-hep.org/x/hep/groot/rcmd" "go-hep.org/x/hep/groot/rphys" + "go-hep.org/x/hep/internal/diff" ) func TestLorentzVector(t *testing.T) { @@ -118,8 +118,7 @@ key[001]: tree;1 "my tree title" (TTree) } if got, want := got.String(), want; got != want { - diff := cmp.Diff(want, got) - t.Fatalf("invalid root-dump output: -- (-ref +got)\n%s", diff) + t.Fatalf("invalid root-dump output:\n%s", diff.Format(got, want)) } }) } diff --git a/hepmc/hepmc_test.go b/hepmc/hepmc_test.go index 533d8a77d..571bf86a4 100644 --- a/hepmc/hepmc_test.go +++ b/hepmc/hepmc_test.go @@ -10,11 +10,11 @@ import ( "io" "log" "os" - "os/exec" "testing" "go-hep.org/x/hep/fmom" "go-hep.org/x/hep/hepmc" + "go-hep.org/x/hep/internal/diff" ) func TestEventRW(t *testing.T) { @@ -100,12 +100,9 @@ func testEventRW(t *testing.T, fname, outfname string, nevts int) { } // test output files - cmd := exec.Command("diff", "-urN", fname, outfname) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err = cmd.Run() + err = diff.Files(outfname, fname) if err != nil { - t.Fatalf("file: %s. err=%v\n", fname, err) + t.Fatalf("file: %s\n%+v", fname, err) } } diff --git a/hepmc/rootcnv/rootcnv_test.go b/hepmc/rootcnv/rootcnv_test.go index 9687f40c0..a11596b8a 100644 --- a/hepmc/rootcnv/rootcnv_test.go +++ b/hepmc/rootcnv/rootcnv_test.go @@ -8,14 +8,13 @@ import ( "bytes" "os" "path/filepath" - "reflect" "testing" - "github.com/google/go-cmp/cmp" "go-hep.org/x/hep/groot" "go-hep.org/x/hep/groot/riofs" "go-hep.org/x/hep/groot/rtree" "go-hep.org/x/hep/hepmc" + "go-hep.org/x/hep/internal/diff" ) func TestRW(t *testing.T) { @@ -92,9 +91,9 @@ func TestRW(t *testing.T) { t.Fatalf("could not close hepmc writer: %+v", err) } - if got, want := buf.Bytes(), raw; !reflect.DeepEqual(got, want) { - diff := cmp.Diff(string(want), string(got)) - t.Fatalf("invalid r/w round trip:\n%s", diff) + if got, want := buf.String(), string(raw); got != want { + d := diff.Format(string(got), string(want)) + t.Fatalf("invalid r/w round trip:\n%s", d) } }) }