1 // Copyright 2020 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.
15 "golang.org/x/tools/internal/lsp/protocol"
21 -- nested/README.md --
25 func newWorkdir(t *testing.T) (*Workdir, <-chan []FileEvent, func()) {
28 tmpdir, err := ioutil.TempDir("", "goplstest-workdir-")
32 wd := NewWorkdir(tmpdir)
33 if err := wd.writeInitialFiles(data); err != nil {
37 if err := os.RemoveAll(tmpdir); err != nil {
42 fileEvents := make(chan []FileEvent)
43 watch := func(_ context.Context, events []FileEvent) {
47 return wd, fileEvents, cleanup
50 func TestWorkdir_ReadFile(t *testing.T) {
51 wd, _, cleanup := newWorkdir(t)
54 got, err := wd.ReadFile("nested/README.md")
58 want := "Hello World!\n"
60 t.Errorf("reading workdir file, got %q, want %q", got, want)
64 func TestWorkdir_WriteFile(t *testing.T) {
65 wd, events, cleanup := newWorkdir(t)
67 ctx := context.Background()
71 wantType protocol.FileChangeType
73 {"data.txt", protocol.Created},
74 {"nested/README.md", protocol.Changed},
77 for _, test := range tests {
78 if err := wd.WriteFile(ctx, test.path, "42"); err != nil {
82 if got := len(es); got != 1 {
83 t.Fatalf("len(events) = %d, want 1", got)
85 if es[0].Path != test.path {
86 t.Errorf("event.Path = %q, want %q", es[0].Path, test.path)
88 if es[0].ProtocolEvent.Type != test.wantType {
89 t.Errorf("event type = %v, want %v", es[0].ProtocolEvent.Type, test.wantType)
91 got, err := wd.ReadFile(test.path)
97 t.Errorf("ws.ReadFile(%q) = %q, want %q", test.path, got, want)
102 func TestWorkdir_ListFiles(t *testing.T) {
103 wd, _, cleanup := newWorkdir(t)
106 checkFiles := func(dir string, want []string) {
107 files, err := wd.ListFiles(dir)
113 for p := range files {
117 if len(got) != len(want) {
118 t.Fatalf("ListFiles(): len = %d, want %d; got=%v; want=%v", len(got), len(want), got, want)
120 for i, f := range got {
122 t.Errorf("ListFiles()[%d] = %s, want %s", i, f, want[i])
127 checkFiles(".", []string{"go.mod", "nested/README.md"})
128 checkFiles("nested", []string{"nested/README.md"})
131 func TestWorkdir_CheckForFileChanges(t *testing.T) {
132 t.Skip("broken on darwin-amd64-10_12")
133 wd, events, cleanup := newWorkdir(t)
135 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
138 checkChange := func(path string, typ protocol.FileChangeType) {
139 if err := wd.CheckForFileChanges(ctx); err != nil {
149 // Only check relative path and Type
150 if gotEvt.Path != path || gotEvt.ProtocolEvent.Type != typ {
151 t.Errorf("file events: got %v, want {Path: %s, Type: %v}", gotEvt, path, typ)
154 // Sleep some positive amount of time to ensure a distinct mtime.
155 time.Sleep(100 * time.Millisecond)
156 if err := WriteFileData("go.mod", []byte("module foo.test\n"), wd.RelativeTo); err != nil {
159 checkChange("go.mod", protocol.Changed)
160 if err := WriteFileData("newFile", []byte("something"), wd.RelativeTo); err != nil {
163 checkChange("newFile", protocol.Created)
164 fp := wd.AbsPath("newFile")
165 if err := os.Remove(fp); err != nil {
168 checkChange("newFile", protocol.Deleted)
171 func TestSplitModuleVersionPath(t *testing.T) {
174 wantModule, wantVersion, wantSuffix string
176 {"foo.com@v1.2.3/bar", "foo.com", "v1.2.3", "bar"},
177 {"foo.com/module@v1.2.3/bar", "foo.com/module", "v1.2.3", "bar"},
178 {"foo.com@v1.2.3", "foo.com", "v1.2.3", ""},
179 {"std@v1.14.0", "std", "v1.14.0", ""},
180 {"another/module/path", "another/module/path", "", ""},
183 for _, test := range tests {
184 module, version, suffix := splitModuleVersionPath(test.path)
185 if module != test.wantModule || version != test.wantVersion || suffix != test.wantSuffix {
186 t.Errorf("splitModuleVersionPath(%q) =\n\t(%q, %q, %q)\nwant\n\t(%q, %q, %q)",
187 test.path, module, version, suffix, test.wantModule, test.wantVersion, test.wantSuffix)