diff --git a/internal/lsp/mod/mod_test.go b/internal/lsp/mod/mod_test.go index 4c5a48929c5..558bb77f69e 100644 --- a/internal/lsp/mod/mod_test.go +++ b/internal/lsp/mod/mod_test.go @@ -27,6 +27,44 @@ func TestMain(m *testing.M) { 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 00000000000..40d2d027ac5 --- /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 00000000000..b258445f438 --- /dev/null +++ b/internal/lsp/mod/testdata/unchanged/main.go @@ -0,0 +1,6 @@ +// Package unchanged does something +package unchanged + +func Yo() { + println("yo") +}