2020-02-02 10:53:30 -07:00
|
|
|
// Copyright 2020 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 fake
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-28 22:00:52 -06:00
|
|
|
"io/ioutil"
|
2020-04-27 15:00:34 -06:00
|
|
|
"os"
|
|
|
|
"sort"
|
2020-02-02 10:53:30 -07:00
|
|
|
"testing"
|
2020-04-27 15:00:34 -06:00
|
|
|
"time"
|
2020-02-02 10:53:30 -07:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
const data = `
|
|
|
|
-- go.mod --
|
|
|
|
go 1.12
|
|
|
|
-- nested/README.md --
|
|
|
|
Hello World!
|
|
|
|
`
|
|
|
|
|
2020-04-28 22:00:52 -06:00
|
|
|
func newWorkdir(t *testing.T) (*Workdir, <-chan []FileEvent, func()) {
|
2020-02-02 10:53:30 -07:00
|
|
|
t.Helper()
|
|
|
|
|
2020-04-28 22:00:52 -06:00
|
|
|
tmpdir, err := ioutil.TempDir("", "goplstest-workdir-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-07-22 19:15:22 -06:00
|
|
|
wd := NewWorkdir(tmpdir)
|
|
|
|
if err := wd.WriteInitialFiles(data); err != nil {
|
2020-02-02 10:53:30 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cleanup := func() {
|
2020-04-28 22:00:52 -06:00
|
|
|
if err := os.RemoveAll(tmpdir); err != nil {
|
|
|
|
t.Error(err)
|
2020-02-02 10:53:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileEvents := make(chan []FileEvent)
|
|
|
|
watch := func(_ context.Context, events []FileEvent) {
|
|
|
|
fileEvents <- events
|
|
|
|
}
|
2020-04-28 22:00:52 -06:00
|
|
|
wd.AddWatcher(watch)
|
|
|
|
return wd, fileEvents, cleanup
|
2020-02-02 10:53:30 -07:00
|
|
|
}
|
|
|
|
|
2020-04-28 22:00:52 -06:00
|
|
|
func TestWorkdir_ReadFile(t *testing.T) {
|
|
|
|
wd, _, cleanup := newWorkdir(t)
|
2020-02-02 10:53:30 -07:00
|
|
|
defer cleanup()
|
|
|
|
|
2020-04-28 22:00:52 -06:00
|
|
|
got, err := wd.ReadFile("nested/README.md")
|
2020-02-02 10:53:30 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
want := "Hello World!\n"
|
|
|
|
if got != want {
|
2020-04-28 22:00:52 -06:00
|
|
|
t.Errorf("reading workdir file, got %q, want %q", got, want)
|
2020-02-02 10:53:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 22:00:52 -06:00
|
|
|
func TestWorkdir_WriteFile(t *testing.T) {
|
|
|
|
wd, events, cleanup := newWorkdir(t)
|
2020-02-02 10:53:30 -07:00
|
|
|
defer cleanup()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
wantType protocol.FileChangeType
|
|
|
|
}{
|
|
|
|
{"data.txt", protocol.Created},
|
|
|
|
{"nested/README.md", protocol.Changed},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2020-04-28 22:00:52 -06:00
|
|
|
if err := wd.WriteFile(ctx, test.path, "42"); err != nil {
|
2020-02-02 10:53:30 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
es := <-events
|
|
|
|
if got := len(es); got != 1 {
|
|
|
|
t.Fatalf("len(events) = %d, want 1", got)
|
|
|
|
}
|
|
|
|
if es[0].Path != test.path {
|
|
|
|
t.Errorf("event.Path = %q, want %q", es[0].Path, test.path)
|
|
|
|
}
|
|
|
|
if es[0].ProtocolEvent.Type != test.wantType {
|
|
|
|
t.Errorf("event type = %v, want %v", es[0].ProtocolEvent.Type, test.wantType)
|
|
|
|
}
|
2020-04-28 22:00:52 -06:00
|
|
|
got, err := wd.ReadFile(test.path)
|
2020-02-02 10:53:30 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
want := "42"
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("ws.ReadFile(%q) = %q, want %q", test.path, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 15:28:37 -06:00
|
|
|
|
2020-04-28 22:00:52 -06:00
|
|
|
func TestWorkdir_ListFiles(t *testing.T) {
|
|
|
|
wd, _, cleanup := newWorkdir(t)
|
2020-04-27 15:00:34 -06:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
checkFiles := func(dir string, want []string) {
|
2020-04-28 22:00:52 -06:00
|
|
|
files, err := wd.ListFiles(dir)
|
2020-04-27 15:00:34 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sort.Strings(want)
|
|
|
|
var got []string
|
|
|
|
for p := range files {
|
|
|
|
got = append(got, p)
|
|
|
|
}
|
|
|
|
sort.Strings(got)
|
|
|
|
if len(got) != len(want) {
|
|
|
|
t.Fatalf("ListFiles(): len = %d, want %d; got=%v; want=%v", len(got), len(want), got, want)
|
|
|
|
}
|
|
|
|
for i, f := range got {
|
|
|
|
if f != want[i] {
|
|
|
|
t.Errorf("ListFiles()[%d] = %s, want %s", i, f, want[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkFiles(".", []string{"go.mod", "nested/README.md"})
|
|
|
|
checkFiles("nested", []string{"nested/README.md"})
|
|
|
|
}
|
|
|
|
|
2020-04-28 22:00:52 -06:00
|
|
|
func TestWorkdir_CheckForFileChanges(t *testing.T) {
|
2020-05-04 15:40:48 -06:00
|
|
|
t.Skip("broken on darwin-amd64-10_12")
|
2020-04-28 22:00:52 -06:00
|
|
|
wd, events, cleanup := newWorkdir(t)
|
2020-04-27 15:00:34 -06:00
|
|
|
defer cleanup()
|
2020-04-28 22:00:52 -06:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
2020-04-27 15:00:34 -06:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
checkChange := func(path string, typ protocol.FileChangeType) {
|
2020-04-28 22:00:52 -06:00
|
|
|
if err := wd.CheckForFileChanges(ctx); err != nil {
|
2020-04-27 15:00:34 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var gotEvt FileEvent
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Fatal(ctx.Err())
|
|
|
|
case ev := <-events:
|
|
|
|
gotEvt = ev[0]
|
|
|
|
}
|
|
|
|
// Only check relative path and Type
|
|
|
|
if gotEvt.Path != path || gotEvt.ProtocolEvent.Type != typ {
|
|
|
|
t.Errorf("file events: got %v, want {Path: %s, Type: %v}", gotEvt, path, typ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Sleep some positive amount of time to ensure a distinct mtime.
|
2020-04-28 22:00:52 -06:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
if err := wd.writeFileData("go.mod", "module foo.test\n"); err != nil {
|
2020-04-27 15:00:34 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
checkChange("go.mod", protocol.Changed)
|
2020-04-28 22:00:52 -06:00
|
|
|
if err := wd.writeFileData("newFile", "something"); err != nil {
|
2020-04-27 15:00:34 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
checkChange("newFile", protocol.Created)
|
2020-04-28 22:00:52 -06:00
|
|
|
fp := wd.filePath("newFile")
|
2020-04-27 15:00:34 -06:00
|
|
|
if err := os.Remove(fp); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
checkChange("newFile", protocol.Deleted)
|
|
|
|
}
|
|
|
|
|
2020-04-14 15:28:37 -06:00
|
|
|
func TestSplitModuleVersionPath(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
wantModule, wantVersion, wantSuffix string
|
|
|
|
}{
|
|
|
|
{"foo.com@v1.2.3/bar", "foo.com", "v1.2.3", "bar"},
|
|
|
|
{"foo.com/module@v1.2.3/bar", "foo.com/module", "v1.2.3", "bar"},
|
|
|
|
{"foo.com@v1.2.3", "foo.com", "v1.2.3", ""},
|
|
|
|
{"std@v1.14.0", "std", "v1.14.0", ""},
|
|
|
|
{"another/module/path", "another/module/path", "", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
module, version, suffix := splitModuleVersionPath(test.path)
|
|
|
|
if module != test.wantModule || version != test.wantVersion || suffix != test.wantSuffix {
|
|
|
|
t.Errorf("splitModuleVersionPath(%q) =\n\t(%q, %q, %q)\nwant\n\t(%q, %q, %q)",
|
|
|
|
test.path, module, version, suffix, test.wantModule, test.wantVersion, test.wantSuffix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|