Debian Codegraph @

gopls/internal/test/integration/workspace/didcreatefiles_test.go

raw symbols SCIP
1 // Copyright 2025 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 workspace
6
7 import (
8 "context"
9 "fmt"
10 "testing"
11
12 . "golang.org/x/tools/gopls/internal/test/integration"
13 )
14
15 // TestAutoFillPackageDecl tests that creation of a new .go file causes
16 // gopls to choose a sensible package name and fill in the package declaration.
17 func TestAutoFillPackageDecl(t *testing.T) {
18 const existFiles = `
19 -- go.mod --
20 module mod.com
21
22 go 1.12
23
24 -- dog/a_test.go --
25 package dog
26 -- fruits/apple.go --
27 package apple
28
29 fun apple() int {
30 return 0
31 }
32
33 -- license/license.go --
34 /* Copyright 2025 The Go Authors. All rights reserved.
35 Use of this source code is governed by a BSD-style
36 license that can be found in the LICENSE file. */
37
38 package license
39
40 -- license1/license.go --
41 // Copyright 2025 The Go Authors. All rights reserved.
42 // Use of this source code is governed by a BSD-style
43 // license that can be found in the LICENSE file.
44
45 package license1
46
47 -- cmd/main.go --
48 package main
49
50 -- integration/a_test.go --
51 package integration_test
52
53 -- nopkg/testfile.go --
54 package
55 `
56 for _, tc := range []struct {
57 name string
58 newfile string
59 want string
60 }{
61 {
62 name: "new file in folder with a_test.go",
63 newfile: "dog/newfile.go",
64 want: "package dog\n",
65 },
66 {
67 name: "new file in folder with go file",
68 newfile: "fruits/newfile.go",
69 want: "package apple\n",
70 },
71 {
72 name: "new test file in folder with go file",
73 newfile: "fruits/newfile_test.go",
74 want: "package apple\n",
75 },
76 {
77 name: "new file in folder with go file that contains license comment",
78 newfile: "license/newfile.go",
79 want: `/* Copyright 2025 The Go Authors. All rights reserved.
80 Use of this source code is governed by a BSD-style
81 license that can be found in the LICENSE file. */
82
83 package license
84 `,
85 },
86 {
87 name: "new file in folder with go file that contains license comment",
88 newfile: "license1/newfile.go",
89 want: `// Copyright 2025 The Go Authors. All rights reserved.
90 // Use of this source code is governed by a BSD-style
91 // license that can be found in the LICENSE file.
92
93 package license1
94 `,
95 },
96 {
97 name: "new file in folder with main package",
98 newfile: "cmd/newfile.go",
99 want: "package main\n",
100 },
101 {
102 name: "new file in empty folder",
103 newfile: "empty_folder/newfile.go",
104 want: "package emptyfolder\n",
105 },
106 {
107 name: "new file in folder with integration_test package",
108 newfile: "integration/newfile.go",
109 want: "package integration\n",
110 },
111 {
112 name: "new test file in folder with integration_test package",
113 newfile: "integration/newfile_test.go",
114 want: "package integration\n",
115 },
116 {
117 name: "new file in folder with incomplete package clause",
118 newfile: "incomplete/newfile.go",
119 want: "package incomplete\n",
120 },
121 {
122 name: "package completion for dir name with punctuation",
123 newfile: "123f_r.u~its-123/newfile.go",
124 want: "package fruits123\n",
125 },
126 {
127 name: "package completion for dir name with invalid dir name",
128 newfile: "123f_r.u~its-123/newfile.go",
129 want: "package fruits123\n",
130 },
131 } {
132 t.Run(tc.name, func(t *testing.T) {
133 createFiles := fmt.Sprintf("%s\n-- %s --", existFiles, tc.newfile)
134 Run(t, createFiles, func(t *testing.T, env *Env) {
135 env.DidCreateFiles(env.Editor.DocumentURI(tc.newfile))
136 // save buffer to ensure the edits take effects in the file system.
137 if err := env.Editor.SaveBuffer(context.Background(), tc.newfile); err != nil {
138 t.Fatal(err)
139 }
140 if got := env.FileContent(tc.newfile); tc.want != got {
141 t.Fatalf("want '%s' but got '%s'", tc.want, got)
142 }
143 })
144 })
145 }
146 }
147
148 // TestDidCreateFiles_BadURI is an integration test for go.dev/issue/74652,
149 // in which a bad URI passed to DidOpen would cause the server to panic.
150 // Now, the server uses DocumentURI (not raw string) when unmarshalling,
151 // causing it to sanitize properly.
152 func TestDidCreateFiles_BadURI(t *testing.T) {
153 Run(t, "", func(t *testing.T, env *Env) {
154 env.DidCreateFiles("badschema:badfilename")
155 env.AfterChange()
156 })
157 }