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. 4 5package fake 6 7import ( 8 "bytes" 9 "context" 10 "crypto/sha256" 11 "fmt" 12 "io/ioutil" 13 "os" 14 "path/filepath" 15 "strings" 16 "sync" 17 18 "golang.org/x/tools/internal/lsp/protocol" 19 "golang.org/x/tools/internal/span" 20 errors "golang.org/x/xerrors" 21) 22 23// FileEvent wraps the protocol.FileEvent so that it can be associated with a 24// workdir-relative path. 25type FileEvent struct { 26 Path, Content string 27 ProtocolEvent protocol.FileEvent 28} 29 30// RelativeTo is a helper for operations relative to a given directory. 31type RelativeTo string 32 33// AbsPath returns an absolute filesystem path for the workdir-relative path. 34func (r RelativeTo) AbsPath(path string) string { 35 fp := filepath.FromSlash(path) 36 if filepath.IsAbs(fp) { 37 return fp 38 } 39 return filepath.Join(string(r), filepath.FromSlash(path)) 40} 41 42// RelPath returns a '/'-encoded path relative to the working directory (or an 43// absolute path if the file is outside of workdir) 44func (r RelativeTo) RelPath(fp string) string { 45 root := string(r) 46 if rel, err := filepath.Rel(root, fp); err == nil && !strings.HasPrefix(rel, "..") { 47 return filepath.ToSlash(rel) 48 } 49 return filepath.ToSlash(fp) 50} 51 52func writeTxtar(txt string, rel RelativeTo) error { 53 files := UnpackTxt(txt) 54 for name, data := range files { 55 if err := WriteFileData(name, data, rel); err != nil { 56 return errors.Errorf("writing to workdir: %w", err) 57 } 58 } 59 return nil 60} 61 62// WriteFileData writes content to the relative path, replacing the special 63// token $SANDBOX_WORKDIR with the relative root given by rel. 64func WriteFileData(path string, content []byte, rel RelativeTo) error { 65 content = bytes.ReplaceAll(content, []byte("$SANDBOX_WORKDIR"), []byte(rel)) 66 fp := rel.AbsPath(path) 67 if err := os.MkdirAll(filepath.Dir(fp), 0755); err != nil { 68 return errors.Errorf("creating nested directory: %w", err) 69 } 70 if err := ioutil.WriteFile(fp, []byte(content), 0644); err != nil { 71 return errors.Errorf("writing %q: %w", path, err) 72 } 73 return nil 74} 75 76// Workdir is a temporary working directory for tests. It exposes file 77// operations in terms of relative paths, and fakes file watching by triggering 78// events on file operations. 79type Workdir struct { 80 RelativeTo 81 82 watcherMu sync.Mutex 83 watchers []func(context.Context, []FileEvent) 84 85 fileMu sync.Mutex 86 files map[string]string 87} 88 89// NewWorkdir writes the txtar-encoded file data in txt to dir, and returns a 90// Workir for operating on these files using 91func NewWorkdir(dir string) *Workdir { 92 return &Workdir{RelativeTo: RelativeTo(dir)} 93} 94 95func hashFile(data []byte) string { 96 return fmt.Sprintf("%x", sha256.Sum256(data)) 97} 98 99func (w *Workdir) writeInitialFiles(files map[string][]byte) error { 100 w.files = map[string]string{} 101 for name, data := range files { 102 w.files[name] = hashFile(data) 103 if err := WriteFileData(name, data, w.RelativeTo); err != nil { 104 return errors.Errorf("writing to workdir: %w", err) 105 } 106 } 107 return nil 108} 109 110// RootURI returns the root URI for this working directory of this scratch 111// environment. 112func (w *Workdir) RootURI() protocol.DocumentURI { 113 return toURI(string(w.RelativeTo)) 114} 115 116// AddWatcher registers the given func to be called on any file change. 117func (w *Workdir) AddWatcher(watcher func(context.Context, []FileEvent)) { 118 w.watcherMu.Lock() 119 w.watchers = append(w.watchers, watcher) 120 w.watcherMu.Unlock() 121} 122 123// URI returns the URI to a the workdir-relative path. 124func (w *Workdir) URI(path string) protocol.DocumentURI { 125 return toURI(w.AbsPath(path)) 126} 127 128// URIToPath converts a uri to a workdir-relative path (or an absolute path, 129// if the uri is outside of the workdir). 130func (w *Workdir) URIToPath(uri protocol.DocumentURI) string { 131 fp := uri.SpanURI().Filename() 132 return w.RelPath(fp) 133} 134 135func toURI(fp string) protocol.DocumentURI { 136 return protocol.DocumentURI(span.URIFromPath(fp)) 137} 138 139// ReadFile reads a text file specified by a workdir-relative path. 140func (w *Workdir) ReadFile(path string) (string, error) { 141 b, err := ioutil.ReadFile(w.AbsPath(path)) 142 if err != nil { 143 return "", err 144 } 145 return string(b), nil 146} 147 148func (w *Workdir) RegexpRange(path, re string) (Pos, Pos, error) { 149 content, err := w.ReadFile(path) 150 if err != nil { 151 return Pos{}, Pos{}, err 152 } 153 return regexpRange(content, re) 154} 155 156// RegexpSearch searches the file corresponding to path for the first position 157// matching re. 158func (w *Workdir) RegexpSearch(path string, re string) (Pos, error) { 159 content, err := w.ReadFile(path) 160 if err != nil { 161 return Pos{}, err 162 } 163 start, _, err := regexpRange(content, re) 164 return start, err 165} 166 167// ChangeFilesOnDisk executes the given on-disk file changes in a batch, 168// simulating the action of changing branches outside of an editor. 169func (w *Workdir) ChangeFilesOnDisk(ctx context.Context, events []FileEvent) error { 170 for _, e := range events { 171 switch e.ProtocolEvent.Type { 172 case protocol.Deleted: 173 fp := w.AbsPath(e.Path) 174 if err := os.Remove(fp); err != nil { 175 return errors.Errorf("removing %q: %w", e.Path, err) 176 } 177 case protocol.Changed, protocol.Created: 178 if _, err := w.writeFile(ctx, e.Path, e.Content); err != nil { 179 return err 180 } 181 } 182 } 183 w.sendEvents(ctx, events) 184 return nil 185} 186 187// RemoveFile removes a workdir-relative file path. 188func (w *Workdir) RemoveFile(ctx context.Context, path string) error { 189 fp := w.AbsPath(path) 190 if err := os.RemoveAll(fp); err != nil { 191 return errors.Errorf("removing %q: %w", path, err) 192 } 193 w.fileMu.Lock() 194 defer w.fileMu.Unlock() 195 196 evts := []FileEvent{{ 197 Path: path, 198 ProtocolEvent: protocol.FileEvent{ 199 URI: w.URI(path), 200 Type: protocol.Deleted, 201 }, 202 }} 203 w.sendEvents(ctx, evts) 204 delete(w.files, path) 205 return nil 206} 207 208func (w *Workdir) sendEvents(ctx context.Context, evts []FileEvent) { 209 if len(evts) == 0 { 210 return 211 } 212 w.watcherMu.Lock() 213 watchers := make([]func(context.Context, []FileEvent), len(w.watchers)) 214 copy(watchers, w.watchers) 215 w.watcherMu.Unlock() 216 for _, w := range watchers { 217 w(ctx, evts) 218 } 219} 220 221// WriteFiles writes the text file content to workdir-relative paths. 222// It batches notifications rather than sending them consecutively. 223func (w *Workdir) WriteFiles(ctx context.Context, files map[string]string) error { 224 var evts []FileEvent 225 for filename, content := range files { 226 evt, err := w.writeFile(ctx, filename, content) 227 if err != nil { 228 return err 229 } 230 evts = append(evts, evt) 231 } 232 w.sendEvents(ctx, evts) 233 return nil 234} 235 236// WriteFile writes text file content to a workdir-relative path. 237func (w *Workdir) WriteFile(ctx context.Context, path, content string) error { 238 evt, err := w.writeFile(ctx, path, content) 239 if err != nil { 240 return err 241 } 242 w.sendEvents(ctx, []FileEvent{evt}) 243 return nil 244} 245 246func (w *Workdir) writeFile(ctx context.Context, path, content string) (FileEvent, error) { 247 fp := w.AbsPath(path) 248 _, err := os.Stat(fp) 249 if err != nil && !os.IsNotExist(err) { 250 return FileEvent{}, errors.Errorf("checking if %q exists: %w", path, err) 251 } 252 var changeType protocol.FileChangeType 253 if os.IsNotExist(err) { 254 changeType = protocol.Created 255 } else { 256 changeType = protocol.Changed 257 } 258 if err := WriteFileData(path, []byte(content), w.RelativeTo); err != nil { 259 return FileEvent{}, err 260 } 261 return FileEvent{ 262 Path: path, 263 ProtocolEvent: protocol.FileEvent{ 264 URI: w.URI(path), 265 Type: changeType, 266 }, 267 }, nil 268} 269 270// listFiles lists files in the given directory, returning a map of relative 271// path to modification time. 272func (w *Workdir) listFiles(dir string) (map[string]string, error) { 273 files := make(map[string]string) 274 absDir := w.AbsPath(dir) 275 if err := filepath.Walk(absDir, func(fp string, info os.FileInfo, err error) error { 276 if err != nil { 277 return err 278 } 279 if info.IsDir() { 280 return nil 281 } 282 path := w.RelPath(fp) 283 data, err := ioutil.ReadFile(fp) 284 if err != nil { 285 return err 286 } 287 files[path] = hashFile(data) 288 return nil 289 }); err != nil { 290 return nil, err 291 } 292 return files, nil 293} 294 295// CheckForFileChanges walks the working directory and checks for any files 296// that have changed since the last poll. 297func (w *Workdir) CheckForFileChanges(ctx context.Context) error { 298 evts, err := w.pollFiles() 299 if err != nil { 300 return err 301 } 302 w.sendEvents(ctx, evts) 303 return nil 304} 305 306// pollFiles updates w.files and calculates FileEvents corresponding to file 307// state changes since the last poll. It does not call sendEvents. 308func (w *Workdir) pollFiles() ([]FileEvent, error) { 309 w.fileMu.Lock() 310 defer w.fileMu.Unlock() 311 312 files, err := w.listFiles(".") 313 if err != nil { 314 return nil, err 315 } 316 var evts []FileEvent 317 // Check which files have been added or modified. 318 for path, hash := range files { 319 oldhash, ok := w.files[path] 320 delete(w.files, path) 321 var typ protocol.FileChangeType 322 switch { 323 case !ok: 324 typ = protocol.Created 325 case oldhash != hash: 326 typ = protocol.Changed 327 default: 328 continue 329 } 330 evts = append(evts, FileEvent{ 331 Path: path, 332 ProtocolEvent: protocol.FileEvent{ 333 URI: w.URI(path), 334 Type: typ, 335 }, 336 }) 337 } 338 // Any remaining files must have been deleted. 339 for path := range w.files { 340 evts = append(evts, FileEvent{ 341 Path: path, 342 ProtocolEvent: protocol.FileEvent{ 343 URI: w.URI(path), 344 Type: protocol.Deleted, 345 }, 346 }) 347 } 348 w.files = files 349 return evts, nil 350} 351