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 <matloob@golang.org>
This commit is contained in:
Alan Donovan 2018-08-22 10:55:55 -04:00
parent ef27ca3af5
commit 447b503c8b
1 changed files with 12 additions and 5 deletions

View File

@ -1179,13 +1179,22 @@ func TestJSON(t *testing.T) {
return return
} }
seen[pkg.ID] = true seen[pkg.ID] = true
// trim the source lists for stable results
// Trim the source lists for stable results.
pkg.GoFiles = cleanPaths(pkg.GoFiles) pkg.GoFiles = cleanPaths(pkg.GoFiles)
pkg.CompiledGoFiles = cleanPaths(pkg.CompiledGoFiles) pkg.CompiledGoFiles = cleanPaths(pkg.CompiledGoFiles)
pkg.OtherFiles = cleanPaths(pkg.OtherFiles) 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 { if err := enc.Encode(pkg); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -1353,8 +1362,6 @@ func importGraph(initial []*packages.Package) (string, map[string]*packages.Pack
initialSet[p] = true initialSet[p] = true
} }
// We can't use packages.All because
// we need to prune the traversal.
var nodes, edges []string var nodes, edges []string
res := make(map[string]*packages.Package) res := make(map[string]*packages.Package)
seen := make(map[*packages.Package]bool) seen := make(map[*packages.Package]bool)