Skip to content

Commit

Permalink
Update tests to use cmp.Diff instead of reflect.DeepEqual (#1244)
Browse files Browse the repository at this point in the history
  • Loading branch information
thempatel committed May 8, 2022
1 parent 56d35f8 commit d740db9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
1 change: 1 addition & 0 deletions language/go/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ go_test(
"//testtools",
"//walk",
"@com_github_bazelbuild_buildtools//build:go_default_library",
"@com_github_google_go_cmp//cmp",
"@org_golang_x_tools//go/vcs",
],
)
Expand Down
26 changes: 15 additions & 11 deletions language/go/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package golang
import (
"path"
"path/filepath"
"reflect"
"strings"
"testing"

Expand All @@ -29,6 +28,7 @@ import (
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/bazelbuild/bazel-gazelle/testtools"
"github.com/bazelbuild/bazel-gazelle/walk"
"github.com/google/go-cmp/cmp"
)

func testConfig(t *testing.T, args ...string) (*config.Config, []language.Language, []config.Configurer) {
Expand Down Expand Up @@ -121,14 +121,15 @@ func TestDirectives(t *testing.T) {
if !gc.goGrpcCompilersSet {
t.Error("expected goGrpcCompilersSet to be set")
}
if !reflect.DeepEqual(gc.goGrpcCompilers, []string{"abc", "def"}) {
t.Errorf("got goGrpcCompilers %v; want [abc def]", gc.goGrpcCompilers)
if diff := cmp.Diff([]string{"abc", "def"}, gc.goGrpcCompilers); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

if !gc.goProtoCompilersSet {
t.Error("expected goProtoCompilersSet to be set")
}
if !reflect.DeepEqual(gc.goProtoCompilers, []string{"foo", "bar"}) {
t.Errorf("got goProtoCompilers %v; want [foo bar]", gc.goProtoCompilers)
if diff := cmp.Diff(gc.goProtoCompilers, []string{"foo", "bar"}); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

subContent := []byte(`
Expand All @@ -146,15 +147,17 @@ func TestDirectives(t *testing.T) {
if gc.goGrpcCompilersSet {
t.Error("expected goGrpcCompilersSet to be unset")
}
if !reflect.DeepEqual(gc.goGrpcCompilers, defaultGoGrpcCompilers) {
t.Errorf("got goGrpcCompilers %v; want %v", gc.goGrpcCompilers, defaultGoGrpcCompilers)
if diff := cmp.Diff(defaultGoGrpcCompilers, gc.goGrpcCompilers); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

if gc.goProtoCompilersSet {
t.Error("expected goProtoCompilersSet to be unset")
}
if !reflect.DeepEqual(gc.goProtoCompilers, defaultGoProtoCompilers) {
t.Errorf("got goProtoCompilers %v; want %v", gc.goProtoCompilers, defaultGoProtoCompilers)
if diff := cmp.Diff(defaultGoProtoCompilers, gc.goProtoCompilers); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

}

func TestVendorConfig(t *testing.T) {
Expand Down Expand Up @@ -340,8 +343,9 @@ func TestSplitValue(t *testing.T) {
},
} {
parts := splitValue(tc.value)
if !reflect.DeepEqual(tc.parts, parts) {
t.Errorf("want %v; got %v", tc.parts, parts)
if diff := cmp.Diff(tc.parts, parts); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

}
}
41 changes: 32 additions & 9 deletions language/go/fileinfo_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

var (
fileInfoCmpOption = cmp.AllowUnexported(
fileInfo{},
fileEmbed{},
taggedOpts{},
)
)

func TestGoFileInfo(t *testing.T) {
Expand Down Expand Up @@ -178,9 +187,10 @@ var src string
got.embeds[i] = fileEmbed{path: got.embeds[i].path}
}

if !reflect.DeepEqual(got, tc.want) {
t.Errorf("case %q: got %#v; want %#v", tc.desc, got, tc.want)
if diff := cmp.Diff(tc.want, got, fileInfoCmpOption); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

})
}
}
Expand All @@ -205,9 +215,10 @@ func TestGoFileInfoFailure(t *testing.T) {
goos: "linux",
goarch: "amd64",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %#v ; want %#v", got, want)
if diff := cmp.Diff(want, got, fileInfoCmpOption); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

}

func TestCgo(t *testing.T) {
Expand Down Expand Up @@ -331,9 +342,10 @@ import ("C")
clinkopts: got.clinkopts,
}

if !reflect.DeepEqual(got, tc.want) {
t.Errorf("case %q: got %#v; want %#v", tc.desc, got, tc.want)
if diff := cmp.Diff(tc.want, got, fileInfoCmpOption); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

})
}
}
Expand Down Expand Up @@ -371,6 +383,16 @@ func TestExpandSrcDir(t *testing.T) {
}
}

var (
goPackageCmpOption = cmp.AllowUnexported(
goPackage{},
goTarget{},
protoTarget{},
platformStringsBuilder{},
platformStringInfo{},
)
)

func TestExpandSrcDirRepoRelative(t *testing.T) {
repo, err := ioutil.TempDir(os.Getenv("TEST_TEMPDIR"), "repo")
if err != nil {
Expand Down Expand Up @@ -409,9 +431,10 @@ import "C"
}
want.library.sources.addGenericString("sub.go")
want.library.copts.addGenericString("-Isub/..")
if !reflect.DeepEqual(got, want) {
t.Errorf("got %#v ; want %#v", got, want)
if diff := cmp.Diff(want, got, goPackageCmpOption); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

}

// Copied from go/build build_test.go
Expand Down
11 changes: 7 additions & 4 deletions language/go/fileinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"path/filepath"
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestOtherFileInfo(t *testing.T) {
Expand Down Expand Up @@ -55,9 +57,10 @@ func TestOtherFileInfo(t *testing.T) {

// Only check that we can extract tags. Everything else is covered
// by other tests.
if !reflect.DeepEqual(got.tags, tc.wantTags) {
t.Errorf("got %#v; want %#v", got.tags, tc.wantTags)
if diff := cmp.Diff(tc.wantTags, got.tags, fileInfoCmpOption); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

})
}
}
Expand Down Expand Up @@ -331,8 +334,8 @@ package main`,

if got, err := readTags(path); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(got, tc.want) {
t.Errorf("case %q: got %#v; want %#v", tc.desc, got, tc.want)
} else if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}
}
}
Expand Down

0 comments on commit d740db9

Please sign in to comment.