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 <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/internal/lsp/mod/mod_test.go b/internal/lsp/mod/mod_test.go
index 4c5a489..558bb77 100644
--- a/internal/lsp/mod/mod_test.go
+++ b/internal/lsp/mod/mod_test.go
@@ -27,6 +27,44 @@
os.Exit(m.Run())
}
+func TestModfileRemainsUnchanged(t *testing.T) {
+ ctx := tests.Context(t)
+ cache := cache.New(nil)
+ session := cache.NewSession(ctx)
+ options := tests.DefaultOptions()
+ options.TempModfile = true
+ options.Env = append(os.Environ(), "GOPACKAGESDRIVER=off", "GOROOT=")
+
+ // TODO: Once we refactor this to work with go/packages/packagestest. We do not
+ // need to copy to a temporary directory.
+ // Make sure to copy the test directory to a temporary directory so we do not
+ // modify the test code or add go.sum files when we run the tests.
+ folder, err := copyToTempDir(filepath.Join("testdata", "unchanged"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(folder)
+
+ before, err := ioutil.ReadFile(filepath.Join(folder, "go.mod"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, snapshot, err := session.NewView(ctx, "diagnostics_test", span.FileURI(folder), options)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !hasTempModfile(ctx, snapshot) {
+ return
+ }
+ after, err := ioutil.ReadFile(filepath.Join(folder, "go.mod"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(before) != string(after) {
+ t.Errorf("the real go.mod file was changed even when tempModfile=true")
+ }
+}
+
func TestDiagnostics(t *testing.T) {
ctx := tests.Context(t)
cache := cache.New(nil)
diff --git a/internal/lsp/mod/testdata/unchanged/go.mod b/internal/lsp/mod/testdata/unchanged/go.mod
new file mode 100644
index 0000000..40d2d02
--- /dev/null
+++ b/internal/lsp/mod/testdata/unchanged/go.mod
@@ -0,0 +1 @@
+module unchanged
\ No newline at end of file
diff --git a/internal/lsp/mod/testdata/unchanged/main.go b/internal/lsp/mod/testdata/unchanged/main.go
new file mode 100644
index 0000000..b258445
--- /dev/null
+++ b/internal/lsp/mod/testdata/unchanged/main.go
@@ -0,0 +1,6 @@
+// Package unchanged does something
+package unchanged
+
+func Yo() {
+ println("yo")
+}