gopls/internal/lsp/mod: remove TestModfileRemainsUnchanged

Testdata for this test includes a go.mod file, which is problematic for
a number of reasons (e.g. running from the mod cache; go work use -r).

Replace it with a regtest. But really we should just delete the
"tempModfile" setting (golang/go#61970).

Fixes golang/go#57784

Change-Id: I79726c6106f3118d021a8f9ef52f385f1393d4a8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/518976
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Peter Weinberger <pjw@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
diff --git a/gopls/internal/lsp/mod/mod_test.go b/gopls/internal/lsp/mod/mod_test.go
deleted file mode 100644
index 4ec3067..0000000
--- a/gopls/internal/lsp/mod/mod_test.go
+++ /dev/null
@@ -1,59 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package mod
-
-import (
-	"io/ioutil"
-	"os"
-	"path/filepath"
-	"testing"
-
-	"golang.org/x/tools/gopls/internal/lsp/cache"
-	"golang.org/x/tools/gopls/internal/lsp/source"
-	"golang.org/x/tools/gopls/internal/lsp/tests"
-	"golang.org/x/tools/gopls/internal/span"
-	"golang.org/x/tools/internal/testenv"
-)
-
-func TestMain(m *testing.M) {
-	testenv.ExitIfSmallMachine()
-	os.Exit(m.Run())
-}
-
-func TestModfileRemainsUnchanged(t *testing.T) {
-	testenv.NeedsExec(t)
-
-	ctx := tests.Context(t)
-	session := cache.NewSession(ctx, cache.New(nil), nil)
-	options := source.DefaultOptions().Clone()
-	tests.DefaultOptions(options)
-	options.TempModfile = true
-	options.Env = map[string]string{"GOPACKAGESDRIVER": "off", "GOROOT": ""}
-
-	// 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 := tests.CopyFolderToTempDir(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)
-	}
-	_, _, release, err := session.NewView(ctx, "diagnostics_test", span.URIFromPath(folder), options)
-	if err != nil {
-		t.Fatal(err)
-	}
-	release()
-	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")
-	}
-}
diff --git a/gopls/internal/lsp/mod/testdata/unchanged/go.mod b/gopls/internal/lsp/mod/testdata/unchanged/go.mod
deleted file mode 100644
index e3d13ce..0000000
--- a/gopls/internal/lsp/mod/testdata/unchanged/go.mod
+++ /dev/null
@@ -1 +0,0 @@
-module unchanged
diff --git a/gopls/internal/lsp/mod/testdata/unchanged/main.go b/gopls/internal/lsp/mod/testdata/unchanged/main.go
deleted file mode 100644
index b258445..0000000
--- a/gopls/internal/lsp/mod/testdata/unchanged/main.go
+++ /dev/null
@@ -1,6 +0,0 @@
-// Package unchanged does something
-package unchanged
-
-func Yo() {
-	println("yo")
-}
diff --git a/gopls/internal/regtest/modfile/tempmodfile_test.go b/gopls/internal/regtest/modfile/tempmodfile_test.go
new file mode 100644
index 0000000..8b0926a
--- /dev/null
+++ b/gopls/internal/regtest/modfile/tempmodfile_test.go
@@ -0,0 +1,41 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package modfile
+
+import (
+	"testing"
+
+	. "golang.org/x/tools/gopls/internal/lsp/regtest"
+)
+
+// This test replaces an older, problematic test (golang/go#57784). But it has
+// been a long time since the go command would mutate go.mod files.
+//
+// TODO(golang/go#61970): the tempModfile setting should be removed entirely.
+func TestTempModfileUnchanged(t *testing.T) {
+	// badMod has a go.mod file that is missing a go directive.
+	const badMod = `
+-- go.mod --
+module badmod.test/p
+-- p.go --
+package p
+`
+
+	WithOptions(
+		Modes(Default), // no reason to test this with a remote gopls
+		ProxyFiles(workspaceProxy),
+		Settings{
+			"tempModfile": true,
+		},
+	).Run(t, badMod, func(t *testing.T, env *Env) {
+		env.OpenFile("p.go")
+		env.AfterChange()
+		want := "module badmod.test/p\n"
+		got := env.ReadWorkspaceFile("go.mod")
+		if got != want {
+			t.Errorf("go.mod content:\n%s\nwant:\n%s", got, want)
+		}
+	})
+}