1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
20 enabled = true // to allow GODEBUG=goindex=0 go test, when things are very broken
23 func TestIndex(t *testing.T) {
24 src := filepath.Join(runtime.GOROOT(), "src")
25 checkPkg := func(t *testing.T, m *Module, pkg string, data []byte) {
27 bp, err := p.Import(build.Default, build.ImportComment)
31 bp1, err := build.Default.Import(".", filepath.Join(src, pkg), build.ImportComment)
36 if !reflect.DeepEqual(bp, bp1) {
38 t.Logf("index:\n%s", hex.Dump(data))
40 js, err := json.MarshalIndent(bp, "", "\t")
44 js1, err := json.MarshalIndent(bp1, "", "\t")
48 t.Logf("diff:\n%s", diff.Diff("index", js, "correct", js1))
53 // Check packages in increasing complexity, one at a time.
62 var raws []*rawPackage
63 for _, pkg := range pkgs {
64 raw := importRaw(src, pkg)
65 raws = append(raws, raw)
66 t.Run(pkg, func(t *testing.T) {
67 data := encodeModuleBytes([]*rawPackage{raw})
68 m, err := fromBytes(src, data)
72 checkPkg(t, m, pkg, data)
76 // Check that a multi-package index works too.
77 t.Run("all", func(t *testing.T) {
78 data := encodeModuleBytes(raws)
79 m, err := fromBytes(src, data)
83 for _, pkg := range pkgs {
84 checkPkg(t, m, pkg, data)
89 func TestImportRaw_IgnoreNonGo(t *testing.T) {
90 path := filepath.Join("testdata", "ignore_non_source")
91 p := importRaw(path, ".")
93 wantFiles := []string{"a.syso", "b.go", "c.c"}
96 for i := range p.sourceFiles {
97 gotFiles = append(gotFiles, p.sourceFiles[i].name)
100 if !reflect.DeepEqual(gotFiles, wantFiles) {
101 t.Errorf("names of files in importRaw(testdata/ignore_non_source): got %v; want %v",