|
| 1 | +package cachedvfs_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/microsoft/typescript-go/internal/vfs" |
| 7 | + "github.com/microsoft/typescript-go/internal/vfs/cachedvfs" |
| 8 | + "github.com/microsoft/typescript-go/internal/vfs/vfsmock" |
| 9 | + "github.com/microsoft/typescript-go/internal/vfs/vfstest" |
| 10 | + "gotest.tools/v3/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func createMockFS() *vfsmock.FSMock { |
| 14 | + return vfsmock.Wrap(vfstest.FromMap(map[string]string{ |
| 15 | + "/some/path/file.txt": "hello world", |
| 16 | + }, true)) |
| 17 | +} |
| 18 | + |
| 19 | +func TestDirectoryExists(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + underlying := createMockFS() |
| 23 | + cached := cachedvfs.From(underlying) |
| 24 | + |
| 25 | + cached.DirectoryExists("/some/path") |
| 26 | + assert.Equal(t, 1, len(underlying.DirectoryExistsCalls())) |
| 27 | + |
| 28 | + cached.DirectoryExists("/some/path") |
| 29 | + assert.Equal(t, 1, len(underlying.DirectoryExistsCalls())) |
| 30 | + |
| 31 | + cached.ClearCache() |
| 32 | + cached.DirectoryExists("/some/path") |
| 33 | + assert.Equal(t, 2, len(underlying.DirectoryExistsCalls())) |
| 34 | + |
| 35 | + cached.DirectoryExists("/other/path") |
| 36 | + assert.Equal(t, 3, len(underlying.DirectoryExistsCalls())) |
| 37 | +} |
| 38 | + |
| 39 | +func TestFileExists(t *testing.T) { |
| 40 | + t.Parallel() |
| 41 | + |
| 42 | + underlying := createMockFS() |
| 43 | + cached := cachedvfs.From(underlying) |
| 44 | + |
| 45 | + cached.FileExists("/some/path/file.txt") |
| 46 | + assert.Equal(t, 1, len(underlying.FileExistsCalls())) |
| 47 | + |
| 48 | + cached.FileExists("/some/path/file.txt") |
| 49 | + assert.Equal(t, 1, len(underlying.FileExistsCalls())) |
| 50 | + |
| 51 | + cached.ClearCache() |
| 52 | + cached.FileExists("/some/path/file.txt") |
| 53 | + assert.Equal(t, 2, len(underlying.FileExistsCalls())) |
| 54 | + |
| 55 | + cached.FileExists("/other/path/file.txt") |
| 56 | + assert.Equal(t, 3, len(underlying.FileExistsCalls())) |
| 57 | +} |
| 58 | + |
| 59 | +func TestGetAccessibleEntries(t *testing.T) { |
| 60 | + t.Parallel() |
| 61 | + |
| 62 | + underlying := createMockFS() |
| 63 | + cached := cachedvfs.From(underlying) |
| 64 | + |
| 65 | + cached.GetAccessibleEntries("/some/path") |
| 66 | + assert.Equal(t, 1, len(underlying.GetAccessibleEntriesCalls())) |
| 67 | + |
| 68 | + cached.GetAccessibleEntries("/some/path") |
| 69 | + assert.Equal(t, 1, len(underlying.GetAccessibleEntriesCalls())) |
| 70 | + |
| 71 | + cached.ClearCache() |
| 72 | + cached.GetAccessibleEntries("/some/path") |
| 73 | + assert.Equal(t, 2, len(underlying.GetAccessibleEntriesCalls())) |
| 74 | + |
| 75 | + cached.GetAccessibleEntries("/other/path") |
| 76 | + assert.Equal(t, 3, len(underlying.GetAccessibleEntriesCalls())) |
| 77 | +} |
| 78 | + |
| 79 | +func TestRealpath(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + |
| 82 | + underlying := createMockFS() |
| 83 | + cached := cachedvfs.From(underlying) |
| 84 | + |
| 85 | + cached.Realpath("/some/path") |
| 86 | + assert.Equal(t, 1, len(underlying.RealpathCalls())) |
| 87 | + |
| 88 | + cached.Realpath("/some/path") |
| 89 | + assert.Equal(t, 1, len(underlying.RealpathCalls())) |
| 90 | + |
| 91 | + cached.ClearCache() |
| 92 | + cached.Realpath("/some/path") |
| 93 | + assert.Equal(t, 2, len(underlying.RealpathCalls())) |
| 94 | + |
| 95 | + cached.Realpath("/other/path") |
| 96 | + assert.Equal(t, 3, len(underlying.RealpathCalls())) |
| 97 | +} |
| 98 | + |
| 99 | +func TestStat(t *testing.T) { |
| 100 | + t.Parallel() |
| 101 | + |
| 102 | + underlying := createMockFS() |
| 103 | + cached := cachedvfs.From(underlying) |
| 104 | + |
| 105 | + cached.Stat("/some/path") |
| 106 | + assert.Equal(t, 1, len(underlying.StatCalls())) |
| 107 | + |
| 108 | + cached.Stat("/some/path") |
| 109 | + assert.Equal(t, 1, len(underlying.StatCalls())) |
| 110 | + |
| 111 | + cached.ClearCache() |
| 112 | + cached.Stat("/some/path") |
| 113 | + assert.Equal(t, 2, len(underlying.StatCalls())) |
| 114 | + |
| 115 | + cached.Stat("/other/path") |
| 116 | + assert.Equal(t, 3, len(underlying.StatCalls())) |
| 117 | +} |
| 118 | + |
| 119 | +func TestReadFile(t *testing.T) { |
| 120 | + t.Parallel() |
| 121 | + |
| 122 | + underlying := createMockFS() |
| 123 | + cached := cachedvfs.From(underlying) |
| 124 | + |
| 125 | + cached.ReadFile("/some/path/file.txt") |
| 126 | + assert.Equal(t, 1, len(underlying.ReadFileCalls())) |
| 127 | + |
| 128 | + cached.ReadFile("/some/path/file.txt") |
| 129 | + assert.Equal(t, 2, len(underlying.ReadFileCalls())) |
| 130 | + |
| 131 | + cached.ClearCache() |
| 132 | + cached.ReadFile("/some/path/file.txt") |
| 133 | + assert.Equal(t, 3, len(underlying.ReadFileCalls())) |
| 134 | +} |
| 135 | + |
| 136 | +func TestUseCaseSensitiveFileNames(t *testing.T) { |
| 137 | + t.Parallel() |
| 138 | + |
| 139 | + underlying := createMockFS() |
| 140 | + cached := cachedvfs.From(underlying) |
| 141 | + |
| 142 | + cached.UseCaseSensitiveFileNames() |
| 143 | + assert.Equal(t, 1, len(underlying.UseCaseSensitiveFileNamesCalls())) |
| 144 | + |
| 145 | + cached.UseCaseSensitiveFileNames() |
| 146 | + assert.Equal(t, 2, len(underlying.UseCaseSensitiveFileNamesCalls())) |
| 147 | + |
| 148 | + cached.ClearCache() |
| 149 | + cached.UseCaseSensitiveFileNames() |
| 150 | + assert.Equal(t, 3, len(underlying.UseCaseSensitiveFileNamesCalls())) |
| 151 | +} |
| 152 | + |
| 153 | +func TestWalkDir(t *testing.T) { |
| 154 | + t.Parallel() |
| 155 | + |
| 156 | + underlying := createMockFS() |
| 157 | + cached := cachedvfs.From(underlying) |
| 158 | + |
| 159 | + walkFn := vfs.WalkDirFunc(func(path string, info vfs.DirEntry, err error) error { |
| 160 | + return nil |
| 161 | + }) |
| 162 | + |
| 163 | + _ = cached.WalkDir("/some/path", walkFn) |
| 164 | + assert.Equal(t, 1, len(underlying.WalkDirCalls())) |
| 165 | + |
| 166 | + _ = cached.WalkDir("/some/path", walkFn) |
| 167 | + assert.Equal(t, 2, len(underlying.WalkDirCalls())) |
| 168 | + |
| 169 | + cached.ClearCache() |
| 170 | + _ = cached.WalkDir("/some/path", walkFn) |
| 171 | + assert.Equal(t, 3, len(underlying.WalkDirCalls())) |
| 172 | +} |
| 173 | + |
| 174 | +func TestRemove(t *testing.T) { |
| 175 | + t.Parallel() |
| 176 | + |
| 177 | + underlying := createMockFS() |
| 178 | + cached := cachedvfs.From(underlying) |
| 179 | + |
| 180 | + _ = cached.Remove("/some/path/file.txt") |
| 181 | + assert.Equal(t, 1, len(underlying.RemoveCalls())) |
| 182 | + |
| 183 | + _ = cached.Remove("/some/path/file.txt") |
| 184 | + assert.Equal(t, 2, len(underlying.RemoveCalls())) |
| 185 | + |
| 186 | + cached.ClearCache() |
| 187 | + _ = cached.Remove("/some/path/file.txt") |
| 188 | + assert.Equal(t, 3, len(underlying.RemoveCalls())) |
| 189 | +} |
| 190 | + |
| 191 | +func TestWriteFile(t *testing.T) { |
| 192 | + t.Parallel() |
| 193 | + |
| 194 | + underlying := createMockFS() |
| 195 | + cached := cachedvfs.From(underlying) |
| 196 | + |
| 197 | + _ = cached.WriteFile("/some/path/file.txt", "new content", false) |
| 198 | + assert.Equal(t, 1, len(underlying.WriteFileCalls())) |
| 199 | + |
| 200 | + _ = cached.WriteFile("/some/path/file.txt", "another content", true) |
| 201 | + assert.Equal(t, 2, len(underlying.WriteFileCalls())) |
| 202 | + |
| 203 | + cached.ClearCache() |
| 204 | + _ = cached.WriteFile("/some/path/file.txt", "third content", false) |
| 205 | + assert.Equal(t, 3, len(underlying.WriteFileCalls())) |
| 206 | + |
| 207 | + call := underlying.WriteFileCalls()[2] |
| 208 | + assert.Equal(t, "/some/path/file.txt", call.Path) |
| 209 | + assert.Equal(t, "third content", call.Data) |
| 210 | + assert.Equal(t, false, call.WriteByteOrderMark) |
| 211 | +} |
0 commit comments