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) {
49 return wd, fileEvents, cleanup
52 func TestWorkdir_ReadFile(t *testing.T) {
53 wd, _, cleanup := newWorkdir(t)
56 got, err := wd.ReadFile("nested/README.md")
60 want := "Hello World!\n"
62 t.Errorf("reading workdir file, got %q, want %q", got, want)
66 func TestWorkdir_WriteFile(t *testing.T) {
67 wd, events, cleanup := newWorkdir(t)
69 ctx := context.Background()
73 wantType protocol.FileChangeType
75 {"data.txt", protocol.Created},
76 {"nested/README.md", protocol.Changed},
79 for _, test := range tests {
80 if err := wd.WriteFile(ctx, test.path, "42"); err != nil {
84 if got := len(es); got != 1 {
85 t.Fatalf("len(events) = %d, want 1", got)
87 if es[0].Path != test.path {
88 t.Errorf("event.Path = %q, want %q", es[0].Path, test.path)
90 if es[0].ProtocolEvent.Type != test.wantType {
91 t.Errorf("event type = %v, want %v", es[0].ProtocolEvent.Type, test.wantType)
93 got, err := wd.ReadFile(test.path)
99 t.Errorf("ws.ReadFile(%q) = %q, want %q", test.path, got, want)
104 func TestWorkdir_ListFiles(t *testing.T) {
105 wd, _, cleanup := newWorkdir(t)
108 checkFiles := func(dir string, want []string) {
109 files, err := wd.listFiles(dir)
115 for p := range files {
119 if len(got) != len(want) {
120 t.Fatalf("ListFiles(): len = %d, want %d; got=%v; want=%v", len(got), len(want), got, want)
122 for i, f := range got {
124 t.Errorf("ListFiles()[%d] = %s, want %s", i, f, want[i])
129 checkFiles(".", []string{"go.mod", "nested/README.md"})
130 checkFiles("nested", []string{"nested/README.md"})
133 func TestWorkdir_CheckForFileChanges(t *testing.T) {
134 t.Skip("broken on darwin-amd64-10_12")
135 wd, events, cleanup := newWorkdir(t)
137 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
140 checkChange := func(path string, typ protocol.FileChangeType) {
141 if err := wd.CheckForFileChanges(ctx); err != nil {
151 // Only check relative path and Type
152 if gotEvt.Path != path || gotEvt.ProtocolEvent.Type != typ {
153 t.Errorf("file events: got %v, want {Path: %s, Type: %v}", gotEvt, path, typ)
156 // Sleep some positive amount of time to ensure a distinct mtime.
157 time.Sleep(100 * time.Millisecond)
158 if err := WriteFileData("go.mod", []byte("module foo.test\n"), wd.RelativeTo); err != nil {
161 checkChange("go.mod", protocol.Changed)
162 if err := WriteFileData("newFile", []byte("something"), wd.RelativeTo); err != nil {
165 checkChange("newFile", protocol.Created)
166 fp := wd.AbsPath("newFile")
167 if err := os.Remove(fp); err != nil {
170 checkChange("newFile", protocol.Deleted)
173 func TestSplitModuleVersionPath(t *testing.T) {
176 wantModule, wantVersion, wantSuffix string
178 {"foo.com@v1.2.3/bar", "foo.com", "v1.2.3", "bar"},
179 {"foo.com/module@v1.2.3/bar", "foo.com/module", "v1.2.3", "bar"},
180 {"foo.com@v1.2.3", "foo.com", "v1.2.3", ""},
181 {"std@v1.14.0", "std", "v1.14.0", ""},
182 {"another/module/path", "another/module/path", "", ""},
185 for _, test := range tests {
186 module, version, suffix := splitModuleVersionPath(test.path)
187 if module != test.wantModule || version != test.wantVersion || suffix != test.wantSuffix {
188 t.Errorf("splitModuleVersionPath(%q) =\n\t(%q, %q, %q)\nwant\n\t(%q, %q, %q)",
189 test.path, module, version, suffix, test.wantModule, test.wantVersion, test.wantSuffix)