Skip to content

Commit

Permalink
internal/lsp/mod: test go.mod is unchanged when tempModfile=true
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ridersofrohan committed Jan 10, 2020
1 parent e2f2652 commit 428f1ab
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
38 changes: 38 additions & 0 deletions internal/lsp/mod/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions internal/lsp/mod/testdata/unchanged/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module unchanged
6 changes: 6 additions & 0 deletions internal/lsp/mod/testdata/unchanged/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Package unchanged does something
package unchanged

func Yo() {
println("yo")
}

0 comments on commit 428f1ab

Please sign in to comment.