Debian Codegraph @

internal/gcimporter/stdlib_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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.
4
5 package gcimporter_test
6
7 import (
8 "bytes"
9 "fmt"
10 "go/token"
11 "go/types"
12 "runtime"
13 "testing"
14 "unsafe"
15
16 "golang.org/x/tools/go/gcexportdata"
17 "golang.org/x/tools/go/packages"
18 "golang.org/x/tools/internal/testenv"
19 )
20
21 // TestStdlib ensures that all packages in std and x/tools can be
22 // type-checked using export data.
23 func TestStdlib(t *testing.T) {
•24 t.Skip("needs github.com/jba/printsrc")
25 testenv.NeedsGoPackages(t)
26
27 // gcexportdata.Read rapidly consumes FileSet address space,
28 // so disable the test on 32-bit machines.
29 // (We could use a fresh FileSet per type-check, but that
30 // would require us to re-parse the source using it.)
31 if unsafe.Sizeof(token.NoPos) < 8 {
32 t.Skip("skipping test on 32-bit machine")
33 }
34
35 // Load, parse and type-check the standard library.
36 // If we have the full source code for x/tools, also load and type-check that.
37 cfg := &packages.Config{Mode: packages.LoadAllSyntax}
38 patterns := []string{"std"}
39 minPkgs := 225 // 'GOOS=plan9 go1.18 list std | wc -l' reports 228; most other platforms have more.
40 switch runtime.GOOS {
41 case "android", "ios":
42 // The go_.*_exec script for mobile builders only copies over the source tree
43 // for the package under test.
44 default:
45 patterns = append(patterns, "golang.org/x/tools/...")
46 minPkgs += 160 // At the time of writing, 'GOOS=plan9 go list ./... | wc -l' reports 188.
47 }
48 pkgs, err := packages.Load(cfg, patterns...)
49 if err != nil {
50 t.Fatalf("failed to load/parse/type-check: %v", err)
51 }
52 if packages.PrintErrors(pkgs) > 0 {
53 t.Fatal("there were errors during loading")
54 }
55 if len(pkgs) < minPkgs {
56 t.Errorf("too few packages (%d) were loaded", len(pkgs))
57 }
58
59 export := make(map[string][]byte) // keys are package IDs
60
61 // Re-type check them all in post-order, using export data.
62 packages.Visit(pkgs, nil, func(pkg *packages.Package) {
63 packages := make(map[string]*types.Package) // keys are package paths
64 cfg := &types.Config{
65 Error: func(e error) {
66 t.Errorf("type error: %v", e)
67 },
68 Importer: importerFunc(func(importPath string) (*types.Package, error) {
69 // Resolve import path to (vendored?) package path.
70 imported := pkg.Imports[importPath]
71
72 if imported.PkgPath == "unsafe" {
73 return types.Unsafe, nil // unsafe has no exportdata
74 }
75
76 data, ok := export[imported.ID]
77 if !ok {
78 return nil, fmt.Errorf("missing export data for %s", importPath)
79 }
80 return gcexportdata.Read(bytes.NewReader(data), pkg.Fset, packages, imported.PkgPath)
81 }),
82 }
83
84 // Re-typecheck the syntax and save the export data in the map.
85 newPkg := types.NewPackage(pkg.PkgPath, pkg.Name)
86 check := types.NewChecker(cfg, pkg.Fset, newPkg, nil)
87 check.Files(pkg.Syntax)
88
89 var out bytes.Buffer
90 if err := gcexportdata.Write(&out, pkg.Fset, newPkg); err != nil {
91 t.Fatalf("internal error writing export data: %v", err)
92 }
93 export[pkg.ID] = out.Bytes()
94 })
95 }