Debian Codegraph @

gopls/internal/test/integration/diagnostics/gopackagesdriver_test.go

raw symbols SCIP
1 // Copyright 2024 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 diagnostics
6
7 import (
8 "testing"
9
10 . "golang.org/x/tools/gopls/internal/test/integration"
11 )
12
13 // Test that the import error does not mention GOPATH when building with
14 // go/packages driver.
15 func TestBrokenWorkspace_GOPACKAGESDRIVER(t *testing.T) {
16 // A go.mod file is actually needed here, because the fake go/packages driver
17 // uses go list behind the scenes, and we load go/packages driver workspaces
18 // with ./...
19 const files = `
20 -- go.mod --
21 module m
22 go 1.12
23
24 -- a.go --
25 package foo
26
27 import "mod.com/hello"
28 `
29 WithOptions(
30 FakeGoPackagesDriver(t),
31 ).Run(t, files, func(t *testing.T, env *Env) {
32 env.OpenFile("a.go")
33 env.AfterChange(
34 Diagnostics(
35 env.AtRegexp("a.go", `"mod.com`),
36 WithMessage("go/packages driver"),
37 ),
38 )
39 // Deleting the import removes the error.
40 env.RegexpReplace("a.go", `import "mod.com/hello"`, "")
41 env.AfterChange(
42 NoDiagnostics(ForFile("a.go")),
43 )
44 })
45 }
46
47 func TestValidImportCheck_GoPackagesDriver(t *testing.T) {
48 const files = `
49 -- go.work --
50 use .
51
52 -- go.mod --
53 module example.com
54 go 1.0
55
56 -- a/a.go --
57 package a
58 import _ "example.com/b/internal/c"
59
60 -- b/internal/c/c.go --
61 package c
62 `
63
64 // Note that 'go list' produces an error ("use of internal package %q not allowed")
65 // and gopls produces another ("invalid use of internal package %q") with source=compiler.
66 // Here we assert that the second one is not reported with a go/packages driver.
67 // (We don't assert that the first is missing, because the test driver wraps go list!)
68
69 // go list
70 Run(t, files, func(t *testing.T, env *Env) {
71 env.OpenFile("a/a.go")
72 env.AfterChange(Diagnostics(WithMessage(`invalid use of internal package "example.com/b/internal/c"`)))
73 })
74
75 // test driver
76 WithOptions(
77 FakeGoPackagesDriver(t),
78 ).Run(t, files, func(t *testing.T, env *Env) {
79 env.OpenFile("a/a.go")
80 env.AfterChange(NoDiagnostics(WithMessage(`invalid use of internal package "example.com/b/internal/c"`)))
81 })
82 }