Skip to content

Commit

Permalink
Handle strange AST traversal
Browse files Browse the repository at this point in the history
It is not clear to me why this behaviour changed in go-cmp 0.3, but it seems to behave
differently in newer go versions
  • Loading branch information
dnephin committed Aug 7, 2019
1 parent 85ad333 commit e852c27
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
37 changes: 25 additions & 12 deletions assert/opt/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

gocmp "github.com/google/go-cmp/cmp"
"gotest.tools/assert"
"gotest.tools/internal/source"
)

func TestDurationWithThreshold(t *testing.T) {
Expand Down Expand Up @@ -188,9 +189,12 @@ func TestPathStringFromStruct(t *testing.T) {
matches := matchPaths(fixture, PathString(spec))
expected := []string{
`{opt.node}.Ref.Children[1].Labels["first"].Value`,
// as of google/go-cmp 0.3.0 PathFilter seems to traverse some parts
}

if !source.GoVersionLessThan(11) {
// as of google/go-cmp 0.3.0 and go1.11 PathFilter seems to traverse some parts
// of the tree more than once.
`{opt.node}.Ref.Children[1].Labels["first"].Value`,
expected = append(expected, `{opt.node}.Ref.Children[1].Labels["first"].Value`)
}
assert.DeepEqual(t, matches, expected)
}
Expand Down Expand Up @@ -218,11 +222,16 @@ func TestPathStringFromSlice(t *testing.T) {
matches := matchPaths(fixture, PathString(spec))
expected := []string{
`{[]opt.node}[0].Ref.Children[1].Labels["second"].Ref.Value`,
// as of google/go-cmp 0.3.0 PathFilter seems to traverse some parts
}

if !source.GoVersionLessThan(11) {
// as of google/go-cmp 0.3.0 and go1.11 PathFilter seems to traverse some parts
// of the tree more than once.
`{[]opt.node}[0].Ref.Children[1].Labels["second"].Ref.Value`,
`{[]opt.node}[0].Ref.Children[1].Labels["second"].Ref.Value`,
`{[]opt.node}[0].Ref.Children[1].Labels["second"].Ref.Value`,
expected = append(expected,
`{[]opt.node}[0].Ref.Children[1].Labels["second"].Ref.Value`,
`{[]opt.node}[0].Ref.Children[1].Labels["second"].Ref.Value`,
`{[]opt.node}[0].Ref.Children[1].Labels["second"].Ref.Value`,
)
}
assert.DeepEqual(t, matches, expected)
}
Expand All @@ -245,13 +254,17 @@ func TestPathField(t *testing.T) {
"{opt.node}.Children[1].Value.Value",
"{opt.node}.Children[2].Value.Value",
"{opt.node}.Children[2].Ref.Value.Value",
}

// as of google/go-cmp 0.3.0 PathFilter seems to traverse some parts
// of the tree twice.
"{opt.node}.Children[0].Value.Value",
"{opt.node}.Children[1].Value.Value",
"{opt.node}.Children[2].Value.Value",
"{opt.node}.Children[2].Ref.Value.Value",
if !source.GoVersionLessThan(11) {
// as of google/go-cmp 0.3.0 and go1.11 PathFilter seems to traverse some parts
// of the tree more than once.
expected = append(expected,
"{opt.node}.Children[0].Value.Value",
"{opt.node}.Children[1].Value.Value",
"{opt.node}.Children[2].Value.Value",
"{opt.node}.Children[2].Ref.Value.Value",
)
}
assert.DeepEqual(t, matches, expected)
}
Expand Down
10 changes: 6 additions & 4 deletions internal/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func nodePosition(fileset *token.FileSet, node ast.Node) token.Position {
return fileset.Position(node.Pos())
}

var goVersionBefore19 = func() bool {
func GoVersionLessThan(minor int64) bool {
version := runtime.Version()
// not a release version
if !strings.HasPrefix(version, "go") {
Expand All @@ -103,9 +103,11 @@ var goVersionBefore19 = func() bool {
if len(parts) < 2 {
return false
}
minor, err := strconv.ParseInt(parts[1], 10, 32)
return err == nil && parts[0] == "1" && minor < 9
}()
actual, err := strconv.ParseInt(parts[1], 10, 32)
return err == nil && parts[0] == "1" && actual < minor
}

var goVersionBefore19 = GoVersionLessThan(9)

func getCallExprArgs(node ast.Node) ([]ast.Expr, error) {
visitor := &callExprVisitor{}
Expand Down

0 comments on commit e852c27

Please sign in to comment.