Skip to content

Commit

Permalink
all: use internal/diff to display diffs
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastien Binet <[email protected]>
  • Loading branch information
sbinet committed Nov 4, 2022
1 parent e79cf64 commit 46778d8
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 234 deletions.
16 changes: 2 additions & 14 deletions brio/cmd/brio-gen/internal/gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)))
}
}
16 changes: 2 additions & 14 deletions brio/cmd/brio-gen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions cmd/podio-gen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
}
7 changes: 3 additions & 4 deletions cmd/root2arrow/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os/exec"
"testing"

"github.com/google/go-cmp/cmp"
"go-hep.org/x/hep/internal/diff"
)

func init() {
Expand Down Expand Up @@ -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),
)
}
})
Expand Down
4 changes: 3 additions & 1 deletion cmd/root2csv/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"bytes"
"os"
"testing"

"go-hep.org/x/hep/internal/diff"
)

func TestROOT2CSV(t *testing.T) {
Expand Down Expand Up @@ -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)))
}
})
}
Expand Down
28 changes: 6 additions & 22 deletions csvutil/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion fwk/dflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 3 additions & 46 deletions groot/cmd/root-dump/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
}
5 changes: 2 additions & 3 deletions groot/cmd/root-gen-datareader/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
}
})
}
Expand Down
5 changes: 2 additions & 3 deletions groot/cmd/root-gen-rfunc/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
}
})
}
Expand Down
Loading

0 comments on commit 46778d8

Please sign in to comment.