Debian Codegraph @

gopls/internal/cache/port_test.go

raw symbols SCIP
1 // Copyright 2023 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 cache
6
7 import (
8 "os"
9 "testing"
10
11 "github.com/google/go-cmp/cmp"
12 "golang.org/x/sync/errgroup"
13 "golang.org/x/tools/go/packages"
14 "golang.org/x/tools/gopls/internal/file"
15 "golang.org/x/tools/gopls/internal/protocol"
16 "golang.org/x/tools/gopls/internal/util/bug"
17 "golang.org/x/tools/internal/testenv"
18 )
19
20 func TestMain(m *testing.M) {
21 bug.PanicOnBugs = true
22 os.Exit(m.Run())
23 }
24
25 func TestMatchingPortsStdlib(t *testing.T) {
26 // This test checks that we don't encounter a bug when matching ports, and
27 // sanity checks that the optimization to use trimmed/fake file content
28 // before delegating to go/build.Context.MatchFile does not affect
29 // correctness.
30 if testing.Short() {
31 t.Skip("skipping in short mode: takes to long on slow file systems")
32 }
33
34 testenv.NeedsTool(t, "go")
35
36 // Load, parse and type-check the program.
37 cfg := &packages.Config{
38 Mode: packages.LoadFiles,
39 Tests: true,
40 }
41 pkgs, err := packages.Load(cfg, "std", "cmd")
42 if err != nil {
43 t.Fatal(err)
44 }
45
46 var g errgroup.Group
47 packages.Visit(pkgs, nil, func(pkg *packages.Package) {
48 for _, f := range pkg.CompiledGoFiles {
49 g.Go(func() error {
50 content, err := os.ReadFile(f)
51 // We report errors via t.Error, not by returning,
52 // so that a single test can report multiple test failures.
53 if err != nil {
54 t.Errorf("failed to read %s: %v", f, err)
55 return nil
56 }
57 fh := makeFakeFileHandle(protocol.URIFromPath(f), content)
58 fastPorts := matchingPreferredPorts(t, fh, true)
59 slowPorts := matchingPreferredPorts(t, fh, false)
60 if diff := cmp.Diff(fastPorts, slowPorts); diff != "" {
61 t.Errorf("%s: ports do not match (-trimmed +untrimmed):\n%s", f, diff)
62 return nil
63 }
64 return nil
65 })
66 }
67 })
68 _ = g.Wait() // can't fail
69 }
70
71 func matchingPreferredPorts(tb testing.TB, fh file.Handle, trimContent bool) map[port]unit {
72 content, err := fh.Content()
73 if err != nil {
74 tb.Fatal(err)
75 }
76 if trimContent {
77 content = trimContentForPortMatch(content)
78 }
79 path := fh.URI().Path()
80 matching := make(map[port]unit)
81 for _, port := range preferredPorts {
82 if port.matches(path, content) {
83 matching[port] = unit{}
84 }
85 }
86 return matching
87 }
88
89 func BenchmarkMatchingPreferredPorts(b *testing.B) {
90 // Copy of robustio_posix.go
91 const src = `
92 // Copyright 2022 The Go Authors. All rights reserved.
93 // Use of this source code is governed by a BSD-style
94 // license that can be found in the LICENSE file.
95
96 //go:build unix
97 // +build unix
98
99 package robustio
100
101 import (
102 "os"
103 "syscall"
104 "time"
105 )
106
107 func getFileID(filename string) (FileID, time.Time, error) {
108 fi, err := os.Stat(filename)
109 if err != nil {
110 return FileID{}, time.Time{}, err
111 }
112 stat := fi.Sys().(*syscall.Stat_t)
113 return FileID{
114 device: uint64(stat.Dev), // (int32 on darwin, uint64 on linux)
115 inode: stat.Ino,
116 }, fi.ModTime(), nil
117 }
118 `
119 fh := makeFakeFileHandle("file:///path/to/test/file.go", []byte(src))
120 for b.Loop() {
121 _ = matchingPreferredPorts(b, fh, true)
122 }
123 }