File: filemap_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (112 lines) | stat: -rw-r--r-- 2,793 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2023 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 cache

import (
	"path/filepath"
	"sort"
	"testing"

	"github.com/google/go-cmp/cmp"
	"golang.org/x/tools/gopls/internal/file"
	"golang.org/x/tools/gopls/internal/protocol"
)

func TestFileMap(t *testing.T) {
	const (
		set = iota
		del
	)
	type op struct {
		op      int // set or remove
		path    string
		overlay bool
	}
	tests := []struct {
		label        string
		ops          []op
		wantFiles    []string
		wantOverlays []string
		wantDirs     []string
	}{
		{"empty", nil, nil, nil, nil},
		{"singleton", []op{
			{set, "/a/b", false},
		}, []string{"/a/b"}, nil, []string{"/", "/a"}},
		{"overlay", []op{
			{set, "/a/b", true},
		}, []string{"/a/b"}, []string{"/a/b"}, []string{"/", "/a"}},
		{"replace overlay", []op{
			{set, "/a/b", true},
			{set, "/a/b", false},
		}, []string{"/a/b"}, nil, []string{"/", "/a"}},
		{"multi dir", []op{
			{set, "/a/b", false},
			{set, "/c/d", false},
		}, []string{"/a/b", "/c/d"}, nil, []string{"/", "/a", "/c"}},
		{"empty dir", []op{
			{set, "/a/b", false},
			{set, "/c/d", false},
			{del, "/a/b", false},
		}, []string{"/c/d"}, nil, []string{"/", "/c"}},
	}

	// Normalize paths for windows compatibility.
	normalize := func(path string) string {
		y := filepath.ToSlash(path)
		// Windows paths may start with a drive letter
		if len(y) > 2 && y[1] == ':' && y[0] >= 'A' && y[0] <= 'Z' {
			y = y[2:]
		}
		return y
	}

	for _, test := range tests {
		t.Run(test.label, func(t *testing.T) {
			m := newFileMap()
			for _, op := range test.ops {
				uri := protocol.URIFromPath(filepath.FromSlash(op.path))
				switch op.op {
				case set:
					var fh file.Handle
					if op.overlay {
						fh = &overlay{uri: uri}
					} else {
						fh = &diskFile{uri: uri}
					}
					m.set(uri, fh)
				case del:
					m.delete(uri)
				}
			}

			var gotFiles []string
			m.foreach(func(uri protocol.DocumentURI, _ file.Handle) {
				gotFiles = append(gotFiles, normalize(uri.Path()))
			})
			sort.Strings(gotFiles)
			if diff := cmp.Diff(test.wantFiles, gotFiles); diff != "" {
				t.Errorf("Files mismatch (-want +got):\n%s", diff)
			}

			var gotOverlays []string
			for _, o := range m.getOverlays() {
				gotOverlays = append(gotOverlays, normalize(o.URI().Path()))
			}
			if diff := cmp.Diff(test.wantOverlays, gotOverlays); diff != "" {
				t.Errorf("Overlays mismatch (-want +got):\n%s", diff)
			}

			var gotDirs []string
			m.getDirs().Range(func(dir string) {
				gotDirs = append(gotDirs, normalize(dir))
			})
			sort.Strings(gotDirs)
			if diff := cmp.Diff(test.wantDirs, gotDirs); diff != "" {
				t.Errorf("Dirs mismatch (-want +got):\n%s", diff)
			}
		})
	}
}