Debian Codegraph @

gopls/internal/test/integration/misc/misc_test.go

raw symbols SCIP
1 // Copyright 2020 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 misc
6
7 import (
8 "os"
9 "strings"
10 "testing"
11
12 "golang.org/x/telemetry/counter/countertest"
13 "golang.org/x/tools/gopls/internal/protocol"
14 . "golang.org/x/tools/gopls/internal/test/integration"
15 "golang.org/x/tools/gopls/internal/util/bug"
16 )
17
18 func TestMain(m *testing.M) {
19 bug.PanicOnBugs = true
20 tmp, err := os.MkdirTemp("", "gopls-misc-test-counters")
21 if err != nil {
22 panic(err)
23 }
24 countertest.Open(tmp)
25 code := Main(m)
26 os.RemoveAll(tmp) // ignore error (cleanup fails on Windows; golang/go#68243)
27 os.Exit(code)
28 }
29
30 // TestDocumentURIFix ensures that a DocumentURI supplied by the
31 // client is subject to the "fixing" operation documented at
32 // [protocol.DocumentURI.UnmarshalText]. The details of the fixing are
33 // tested in the protocol package; here we aim to test only that it
34 // occurs at all.
35 func TestDocumentURIFix(t *testing.T) {
36 const mod = `
37 -- go.mod --
38 module testdata
39 go 1.18
40
41 -- a.go --
42 package a
43
44 const K = 1
45 `
46 Run(t, mod, func(t *testing.T, env *Env) {
47 env.OpenFile("a.go")
48 loc := env.RegexpSearch("a.go", "K")
49 path := strings.TrimPrefix(string(loc.URI), "file://") // (absolute)
50
51 check := func() {
52 t.Helper()
53 t.Logf("URI = %s", loc.URI)
54 content, _ := env.Hover(loc) // must succeed
55 if content == nil || !strings.Contains(content.Value, "const K") {
56 t.Errorf("wrong content: %#v", content)
57 }
58 }
59
60 // Regular URI (e.g. file://$TMPDIR/TestDocumentURIFix/default/work/a.go)
61 check()
62
63 // URL-encoded path (e.g. contains %2F instead of last /)
64 loc.URI = protocol.DocumentURI("file://" + strings.Replace(path, "/a.go", "%2Fa.go", 1))
65 check()
66
67 // We intentionally do not test further cases (e.g.
68 // file:// without a third slash) as it would quickly
69 // get bogged down in irrelevant details of the
70 // fake editor's own handling of URIs.
71 })
72 }