From 447b503c8ba6e20bc730585499fc01c81a78c621 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 22 Aug 2018 10:55:55 -0400 Subject: [PATCH] go/packages: fix flaky TestJSON Visit packages in deterministic order for predictable JSON. (I don't know why the test only appears to fail on darwin; I would expect it to randomly fail in linux 50% of the time.) Change-Id: I5270804077fc9ca8f529a1f4657e1c35f0586579 Reviewed-on: https://go-review.googlesource.com/130755 Reviewed-by: Michael Matloob --- go/packages/packages_test.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index ccd089be..b2f6a076 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -1179,13 +1179,22 @@ func TestJSON(t *testing.T) { return } seen[pkg.ID] = true - // trim the source lists for stable results + + // Trim the source lists for stable results. pkg.GoFiles = cleanPaths(pkg.GoFiles) pkg.CompiledGoFiles = cleanPaths(pkg.CompiledGoFiles) pkg.OtherFiles = cleanPaths(pkg.OtherFiles) - for _, ipkg := range pkg.Imports { - visit(ipkg) + + // Visit imports. + var importPaths []string + for path := range pkg.Imports { + importPaths = append(importPaths, path) } + sort.Strings(importPaths) // for determinism + for _, path := range importPaths { + visit(pkg.Imports[path]) + } + if err := enc.Encode(pkg); err != nil { t.Fatal(err) } @@ -1353,8 +1362,6 @@ func importGraph(initial []*packages.Package) (string, map[string]*packages.Pack initialSet[p] = true } - // We can't use packages.All because - // we need to prune the traversal. var nodes, edges []string res := make(map[string]*packages.Package) seen := make(map[*packages.Package]bool)