mirror of
https://github.com/golang/go
synced 2024-11-18 14:54:40 -07:00
47c907e258
Refactor internal/lsp/cache to use a new temporary go.mod file for each go command invocation. This cleans up the abstraction in the source package, as we no longer are aware of temporary go.mod files. This will also fix the raciness of reusing the same temporary go.mod file for each invocation. Updates golang/go#37318. Fixes golang/go#39504. Change-Id: I90bc17a678b5df222ab598c8f7dbf6c6fdd393f6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/237517 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
// 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/internal/lsp/cache"
|
|
"golang.org/x/tools/internal/lsp/tests"
|
|
"golang.org/x/tools/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.NeedsGo1Point(t, 14)
|
|
|
|
ctx := tests.Context(t)
|
|
cache := cache.New(ctx, nil)
|
|
session := cache.NewSession(ctx)
|
|
options := tests.DefaultOptions()
|
|
options.TempModfile = true
|
|
options.Env = append(os.Environ(), "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)
|
|
}
|
|
if _, _, err := session.NewView(ctx, "diagnostics_test", span.URIFromPath(folder), options); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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")
|
|
}
|
|
}
|