Skip to content

Commit 428f1ab

Browse files
committed
internal/lsp/mod: test go.mod is unchanged when tempModfile=true
This change adds a test to ensure that your go.mod file remains unchanged when the tempModfile flag is activated. Specifically, it adds a test to ensure that a go directive does not get added to a user's go.mod file when there was not one included before. Updates golang/go#36247 Change-Id: If8db5508ace5b7222112408255ffa66e4d38797f Reviewed-on: https://go-review.googlesource.com/c/tools/+/214260 Reviewed-by: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent e2f2652 commit 428f1ab

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

internal/lsp/mod/mod_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,44 @@ func TestMain(m *testing.M) {
2727
os.Exit(m.Run())
2828
}
2929

30+
func TestModfileRemainsUnchanged(t *testing.T) {
31+
ctx := tests.Context(t)
32+
cache := cache.New(nil)
33+
session := cache.NewSession(ctx)
34+
options := tests.DefaultOptions()
35+
options.TempModfile = true
36+
options.Env = append(os.Environ(), "GOPACKAGESDRIVER=off", "GOROOT=")
37+
38+
// TODO: Once we refactor this to work with go/packages/packagestest. We do not
39+
// need to copy to a temporary directory.
40+
// Make sure to copy the test directory to a temporary directory so we do not
41+
// modify the test code or add go.sum files when we run the tests.
42+
folder, err := copyToTempDir(filepath.Join("testdata", "unchanged"))
43+
if err != nil {
44+
t.Fatal(err)
45+
}
46+
defer os.RemoveAll(folder)
47+
48+
before, err := ioutil.ReadFile(filepath.Join(folder, "go.mod"))
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
_, snapshot, err := session.NewView(ctx, "diagnostics_test", span.FileURI(folder), options)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
if !hasTempModfile(ctx, snapshot) {
57+
return
58+
}
59+
after, err := ioutil.ReadFile(filepath.Join(folder, "go.mod"))
60+
if err != nil {
61+
t.Fatal(err)
62+
}
63+
if string(before) != string(after) {
64+
t.Errorf("the real go.mod file was changed even when tempModfile=true")
65+
}
66+
}
67+
3068
func TestDiagnostics(t *testing.T) {
3169
ctx := tests.Context(t)
3270
cache := cache.New(nil)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module unchanged
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Package unchanged does something
2+
package unchanged
3+
4+
func Yo() {
5+
println("yo")
6+
}

0 commit comments

Comments
 (0)