|
13 | 13 | // limitations under the License.
|
14 | 14 | package com.google.devtools.build.lib.remote;
|
15 | 15 |
|
| 16 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
16 | 17 | import static com.google.common.truth.Truth.assertThat;
|
17 | 18 | import static org.junit.Assert.assertThrows;
|
18 | 19 |
|
19 | 20 | import com.google.common.collect.ImmutableList;
|
20 | 21 | import com.google.devtools.build.lib.actions.ActionInputMap;
|
21 | 22 | import com.google.devtools.build.lib.actions.Artifact;
|
| 23 | +import com.google.devtools.build.lib.vfs.Dirent; |
22 | 24 | import com.google.devtools.build.lib.vfs.FileSystem;
|
| 25 | +import com.google.devtools.build.lib.vfs.Path; |
23 | 26 | import com.google.devtools.build.lib.vfs.PathFragment;
|
| 27 | +import com.google.devtools.build.lib.vfs.Symlinks; |
24 | 28 | import java.io.FileNotFoundException;
|
25 | 29 | import java.io.IOException;
|
26 | 30 | import org.junit.Test;
|
@@ -209,4 +213,119 @@ public void createDirectory_createLocallyAndRemotely() throws Exception {
|
209 | 213 | assertThat(getRemoteFileSystem(actionFs).getPath(path).isDirectory()).isTrue();
|
210 | 214 | assertThat(getLocalFileSystem(actionFs).getPath(path).isDirectory()).isTrue();
|
211 | 215 | }
|
| 216 | + |
| 217 | + interface DirectoryEntriesProvider { |
| 218 | + ImmutableList<String> getDirectoryEntries(Path path) throws IOException; |
| 219 | + } |
| 220 | + |
| 221 | + private void readdirNonEmptyLocalDirectoryReadFromLocal( |
| 222 | + DirectoryEntriesProvider directoryEntriesProvider) throws IOException { |
| 223 | + FileSystem actionFs = createActionFileSystem(); |
| 224 | + PathFragment dir = getOutputPath("parent/dir"); |
| 225 | + actionFs.getPath(dir).createDirectoryAndParents(); |
| 226 | + writeLocalFile(actionFs, dir.getChild("file1"), "content1"); |
| 227 | + writeLocalFile(actionFs, dir.getChild("file2"), "content2"); |
| 228 | + |
| 229 | + ImmutableList<String> entries = |
| 230 | + directoryEntriesProvider.getDirectoryEntries(actionFs.getPath(dir)); |
| 231 | + |
| 232 | + assertThat(entries).containsExactly("file1", "file2"); |
| 233 | + } |
| 234 | + |
| 235 | + private void readdirNonEmptyInMemoryDirectoryReadFromMemory( |
| 236 | + DirectoryEntriesProvider directoryEntriesProvider) throws IOException { |
| 237 | + FileSystem actionFs = createActionFileSystem(); |
| 238 | + PathFragment dir = getOutputPath("parent/dir"); |
| 239 | + actionFs.getPath(dir).createDirectoryAndParents(); |
| 240 | + injectRemoteFile(actionFs, dir.getChild("file1"), "content1"); |
| 241 | + injectRemoteFile(actionFs, dir.getChild("file2"), "content2"); |
| 242 | + |
| 243 | + ImmutableList<String> entries = |
| 244 | + directoryEntriesProvider.getDirectoryEntries(actionFs.getPath(dir)); |
| 245 | + |
| 246 | + assertThat(entries).containsExactly("file1", "file2"); |
| 247 | + } |
| 248 | + |
| 249 | + private void readdirNonEmptyLocalAndInMemoryDirectoryCombineThem( |
| 250 | + DirectoryEntriesProvider directoryEntriesProvider) throws IOException { |
| 251 | + FileSystem actionFs = createActionFileSystem(); |
| 252 | + PathFragment dir = getOutputPath("parent/dir"); |
| 253 | + actionFs.getPath(dir).createDirectoryAndParents(); |
| 254 | + writeLocalFile(actionFs, dir.getChild("file1"), "content1"); |
| 255 | + writeLocalFile(actionFs, dir.getChild("file2"), "content2"); |
| 256 | + injectRemoteFile(actionFs, dir.getChild("file2"), "content2inmemory"); |
| 257 | + injectRemoteFile(actionFs, dir.getChild("file3"), "content3"); |
| 258 | + |
| 259 | + ImmutableList<String> entries = |
| 260 | + directoryEntriesProvider.getDirectoryEntries(actionFs.getPath(dir)); |
| 261 | + |
| 262 | + assertThat(entries).containsExactly("file1", "file2", "file3"); |
| 263 | + } |
| 264 | + |
| 265 | + private void readdirNothingThereThrowsFileNotFound( |
| 266 | + DirectoryEntriesProvider directoryEntriesProvider) throws IOException { |
| 267 | + FileSystem actionFs = createActionFileSystem(); |
| 268 | + PathFragment dir = getOutputPath("parent/dir"); |
| 269 | + |
| 270 | + assertThrows( |
| 271 | + FileNotFoundException.class, |
| 272 | + () -> directoryEntriesProvider.getDirectoryEntries(actionFs.getPath(dir))); |
| 273 | + } |
| 274 | + |
| 275 | + @Test |
| 276 | + public void readdir_nonEmptyLocalDirectory_readFromLocal() throws IOException { |
| 277 | + readdirNonEmptyLocalDirectoryReadFromLocal( |
| 278 | + path -> |
| 279 | + path.readdir(Symlinks.FOLLOW).stream().map(Dirent::getName).collect(toImmutableList())); |
| 280 | + } |
| 281 | + |
| 282 | + @Test |
| 283 | + public void readdir_nonEmptyInMemoryDirectory_readFromMemory() throws IOException { |
| 284 | + readdirNonEmptyInMemoryDirectoryReadFromMemory( |
| 285 | + path -> |
| 286 | + path.readdir(Symlinks.FOLLOW).stream().map(Dirent::getName).collect(toImmutableList())); |
| 287 | + } |
| 288 | + |
| 289 | + @Test |
| 290 | + public void readdir_nonEmptyLocalAndInMemoryDirectory_combineThem() throws IOException { |
| 291 | + readdirNonEmptyLocalAndInMemoryDirectoryCombineThem( |
| 292 | + path -> |
| 293 | + path.readdir(Symlinks.FOLLOW).stream().map(Dirent::getName).collect(toImmutableList())); |
| 294 | + } |
| 295 | + |
| 296 | + @Test |
| 297 | + public void readdir_nothingThere_throwsFileNotFound() throws IOException { |
| 298 | + readdirNothingThereThrowsFileNotFound( |
| 299 | + path -> |
| 300 | + path.readdir(Symlinks.FOLLOW).stream().map(Dirent::getName).collect(toImmutableList())); |
| 301 | + } |
| 302 | + |
| 303 | + @Test |
| 304 | + public void getDirectoryEntries_nonEmptyLocalDirectory_readFromLocal() throws IOException { |
| 305 | + readdirNonEmptyLocalDirectoryReadFromLocal( |
| 306 | + path -> |
| 307 | + path.getDirectoryEntries().stream().map(Path::getBaseName).collect(toImmutableList())); |
| 308 | + } |
| 309 | + |
| 310 | + @Test |
| 311 | + public void getDirectoryEntries_nonEmptyInMemoryDirectory_readFromMemory() throws IOException { |
| 312 | + readdirNonEmptyInMemoryDirectoryReadFromMemory( |
| 313 | + path -> |
| 314 | + path.getDirectoryEntries().stream().map(Path::getBaseName).collect(toImmutableList())); |
| 315 | + } |
| 316 | + |
| 317 | + @Test |
| 318 | + public void getDirectoryEntries_nonEmptyLocalAndInMemoryDirectory_combineThem() |
| 319 | + throws IOException { |
| 320 | + readdirNonEmptyLocalAndInMemoryDirectoryCombineThem( |
| 321 | + path -> |
| 322 | + path.getDirectoryEntries().stream().map(Path::getBaseName).collect(toImmutableList())); |
| 323 | + } |
| 324 | + |
| 325 | + @Test |
| 326 | + public void getDirectoryEntries_nothingThere_throwsFileNotFound() throws IOException { |
| 327 | + readdirNothingThereThrowsFileNotFound( |
| 328 | + path -> |
| 329 | + path.getDirectoryEntries().stream().map(Path::getBaseName).collect(toImmutableList())); |
| 330 | + } |
212 | 331 | }
|
0 commit comments