github.com/v2fly/tools@v0.100.0/internal/lsp/mod/mod_test.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mod
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"github.com/v2fly/tools/internal/lsp/cache"
    14  	"github.com/v2fly/tools/internal/lsp/source"
    15  	"github.com/v2fly/tools/internal/lsp/tests"
    16  	"github.com/v2fly/tools/internal/span"
    17  	"github.com/v2fly/tools/internal/testenv"
    18  )
    19  
    20  func TestMain(m *testing.M) {
    21  	testenv.ExitIfSmallMachine()
    22  	os.Exit(m.Run())
    23  }
    24  
    25  func TestModfileRemainsUnchanged(t *testing.T) {
    26  	testenv.NeedsGo1Point(t, 14)
    27  
    28  	ctx := tests.Context(t)
    29  	cache := cache.New(nil)
    30  	session := cache.NewSession(ctx)
    31  	options := source.DefaultOptions().Clone()
    32  	tests.DefaultOptions(options)
    33  	options.TempModfile = true
    34  	options.Env = map[string]string{"GOPACKAGESDRIVER": "off", "GOROOT": ""}
    35  
    36  	// Make sure to copy the test directory to a temporary directory so we do not
    37  	// modify the test code or add go.sum files when we run the tests.
    38  	folder, err := tests.CopyFolderToTempDir(filepath.Join("testdata", "unchanged"))
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	defer os.RemoveAll(folder)
    43  
    44  	before, err := ioutil.ReadFile(filepath.Join(folder, "go.mod"))
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	_, _, release, err := session.NewView(ctx, "diagnostics_test", span.URIFromPath(folder), "", options)
    49  	release()
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	after, err := ioutil.ReadFile(filepath.Join(folder, "go.mod"))
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	if string(before) != string(after) {
    58  		t.Errorf("the real go.mod file was changed even when tempModfile=true")
    59  	}
    60  }