use of com.facebook.buck.util.cache.ProjectFileHashCache in project buck by facebook.
the class RecordingFileHashLoaderTest method testRecordsDirectoryAndRecursivelyRecordsChildren.
@Test
public void testRecordsDirectoryAndRecursivelyRecordsChildren() throws IOException {
// Scenario:
// /a - folder
// /a/b - folder
// /a/b/c - file
// /a/b/d - folder
// /a/e - file
// => entries for all files and folders.
// => entries for dirs /a and /a/b list their direct children
assumeTrue(!Platform.detect().equals(Platform.WINDOWS));
ProjectFilesystem fs = new ProjectFilesystem(projectDir.getRoot().toPath());
Path pathDirA = Files.createDirectories(fs.getRootPath().resolve("a"));
Files.createDirectories(fs.getRootPath().resolve("a/b"));
Files.createFile(fs.getRootPath().resolve("a/b/c"));
Files.createDirectories(fs.getRootPath().resolve("a/b/d"));
Files.createFile(fs.getRootPath().resolve("a/e"));
RecordedFileHashes recordedFileHashes = new RecordedFileHashes(0);
BuildJobStateFileHashes fileHashes = recordedFileHashes.getRemoteFileHashes();
ProjectFileHashCache delegateCacheMock = EasyMock.createMock(ProjectFileHashCache.class);
expect(delegateCacheMock.getFilesystem()).andReturn(fs);
expect(delegateCacheMock.get(anyObject(Path.class))).andReturn(EXAMPLE_HASHCODE).anyTimes();
replay(delegateCacheMock);
RecordingProjectFileHashCache recordingLoader = RecordingProjectFileHashCache.createForCellRoot(delegateCacheMock, recordedFileHashes, new DistBuildConfig(FakeBuckConfig.builder().build()));
recordingLoader.get(fs.relativize(pathDirA));
assertThat(fileHashes.getEntries().size(), // all folders and files
Matchers.equalTo(5));
assertThat(fileHashes.getEntries(), IsCollectionContaining.hasItems(new FileHashEntryMatcher("a", true), new FileHashEntryMatcher("a/b", true), new FileHashEntryMatcher("a/b/c", false), new FileHashEntryMatcher("a/b/d", true), new FileHashEntryMatcher("a/e", false)));
}
use of com.facebook.buck.util.cache.ProjectFileHashCache in project buck by facebook.
the class DistBuildFileHashesTest method materializerWritesContents.
@Test
public void materializerWritesContents() throws Exception {
SingleFileFixture f = new SingleFileFixture(tempDir);
List<BuildJobStateFileHashes> fileHashes = f.distributedBuildFileHashes.getFileHashes();
ProjectFilesystem materializeProjectFilesystem = new ProjectFilesystem(tempDir.newFolder("read_hashes").getCanonicalFile().toPath());
ProjectFileHashCache mockCache = EasyMock.createMock(ProjectFileHashCache.class);
EasyMock.expect(mockCache.getFilesystem()).andReturn(materializeProjectFilesystem).atLeastOnce();
EasyMock.expect(mockCache.get(EasyMock.<Path>notNull())).andReturn(HashCode.fromInt(42)).once();
EasyMock.replay(mockCache);
MaterializerProjectFileHashCache materializer = new MaterializerProjectFileHashCache(mockCache, fileHashes.get(0), new InlineContentsProvider());
materializer.get(materializeProjectFilesystem.resolve(f.javaSrcPath));
assertThat(materializeProjectFilesystem.readFileIfItExists(f.javaSrcPath), Matchers.equalTo(Optional.of(f.writtenContents)));
}
use of com.facebook.buck.util.cache.ProjectFileHashCache in project buck by facebook.
the class CachingBuildEngine method verifyRecordedPathHashes.
private boolean verifyRecordedPathHashes(BuildTarget target, ProjectFilesystem filesystem, ImmutableMap<String, String> recordedPathHashes) throws IOException {
// Create a new `DefaultFileHashCache` to prevent caching from interfering with verification.
ProjectFileHashCache fileHashCache = DefaultFileHashCache.createDefaultFileHashCache(filesystem);
// Verify each path from the recorded path hashes entry matches the actual on-disk version.
for (Map.Entry<String, String> ent : recordedPathHashes.entrySet()) {
Path path = filesystem.getPath(ent.getKey());
HashCode cachedHashCode = HashCode.fromString(ent.getValue());
HashCode realHashCode = fileHashCache.get(path);
if (!realHashCode.equals(cachedHashCode)) {
LOG.debug("%s: recorded hash for \"%s\" doesn't match actual hash: %s (cached) != %s (real).", target, path, cachedHashCode, realHashCode);
return false;
}
}
return true;
}
use of com.facebook.buck.util.cache.ProjectFileHashCache in project buck by facebook.
the class MaterializerProjectFileHashCacheTest method testMaterializeDirectoryHelper.
private void testMaterializeDirectoryHelper(boolean materializeDuringPreloading, MaterializeFunction materializeFunction) throws IOException {
// Scenario:
// file hash entries for:
// /a - folder
// /a/b - folder
// /a/b/c - file
// /a/b/d - folder
// /a/e - file
// => preload: ensure all folders created and files touched
// => materialize(/a): ensure all folders and sub-directories/files created
assumeTrue(!Platform.detect().equals(Platform.WINDOWS));
ProjectFilesystem projectFilesystem = new ProjectFilesystem(projectDir.getRoot().toPath());
Path pathDirA = projectFilesystem.resolve("a");
Path pathDirAb = projectFilesystem.resolve("a/b");
Path pathFileAbc = projectFilesystem.resolve("a/b/c");
Path pathDirAbd = projectFilesystem.resolve("a/b/d");
Path pathFileAe = projectFilesystem.resolve("a/e");
Path relativePathDirA = Paths.get("a");
Path relativePathDirAb = Paths.get("a/b");
Path relativePathFileAbc = Paths.get("a/b/c");
Path relativePathDirAbd = Paths.get("a/b/d");
Path relativePathFileAe = Paths.get("a/e");
BuildJobStateFileHashes fileHashes = new BuildJobStateFileHashes();
BuildJobStateFileHashEntry dirAFileHashEntry = new BuildJobStateFileHashEntry();
dirAFileHashEntry.setPath(unixPath(relativePathDirA));
dirAFileHashEntry.setHashCode(EXAMPLE_HASHCODE.toString());
dirAFileHashEntry.setIsDirectory(true);
dirAFileHashEntry.setChildren(ImmutableList.of(unixPath(relativePathDirAb), unixPath(relativePathFileAe)));
dirAFileHashEntry.setMaterializeDuringPreloading(materializeDuringPreloading);
fileHashes.addToEntries(dirAFileHashEntry);
BuildJobStateFileHashEntry dirAbFileHashEntry = new BuildJobStateFileHashEntry();
dirAbFileHashEntry.setPath(unixPath(relativePathDirAb));
dirAbFileHashEntry.setHashCode(EXAMPLE_HASHCODE.toString());
dirAbFileHashEntry.setIsDirectory(true);
dirAbFileHashEntry.setChildren(ImmutableList.of(unixPath(relativePathFileAbc), unixPath(relativePathDirAbd)));
dirAbFileHashEntry.setMaterializeDuringPreloading(materializeDuringPreloading);
fileHashes.addToEntries(dirAbFileHashEntry);
BuildJobStateFileHashEntry fileAbcFileHashEntry = new BuildJobStateFileHashEntry();
fileAbcFileHashEntry.setPath(unixPath(relativePathFileAbc));
fileAbcFileHashEntry.setHashCode(EXAMPLE_HASHCODE.toString());
fileAbcFileHashEntry.setContents(FILE_CONTENTS.getBytes(StandardCharsets.UTF_8));
fileAbcFileHashEntry.setIsDirectory(false);
fileAbcFileHashEntry.setMaterializeDuringPreloading(materializeDuringPreloading);
fileHashes.addToEntries(fileAbcFileHashEntry);
BuildJobStateFileHashEntry dirAbdFileHashEntry = new BuildJobStateFileHashEntry();
dirAbdFileHashEntry.setPath(unixPath(relativePathDirAbd));
dirAbdFileHashEntry.setHashCode(EXAMPLE_HASHCODE.toString());
dirAbdFileHashEntry.setIsDirectory(true);
dirAbdFileHashEntry.setChildren(ImmutableList.of());
dirAbdFileHashEntry.setMaterializeDuringPreloading(materializeDuringPreloading);
fileHashes.addToEntries(dirAbdFileHashEntry);
BuildJobStateFileHashEntry fileAeFileHashEntry = new BuildJobStateFileHashEntry();
fileAeFileHashEntry.setPath(unixPath(relativePathFileAe));
fileAeFileHashEntry.setHashCode(EXAMPLE_HASHCODE.toString());
fileAeFileHashEntry.setContents(FILE_CONTENTS_TWO.getBytes(StandardCharsets.UTF_8));
fileAeFileHashEntry.setIsDirectory(false);
fileAeFileHashEntry.setMaterializeDuringPreloading(materializeDuringPreloading);
fileHashes.addToEntries(fileAeFileHashEntry);
InlineContentsProvider inlineProvider = new InlineContentsProvider();
ProjectFileHashCache mockFileHashCache = EasyMock.createNiceMock(ProjectFileHashCache.class);
expect(mockFileHashCache.getFilesystem()).andReturn(projectFilesystem).atLeastOnce();
replay(mockFileHashCache);
MaterializerProjectFileHashCache fileMaterializer = new MaterializerProjectFileHashCache(mockFileHashCache, fileHashes, inlineProvider);
assertFalse(pathDirA.toFile().exists());
assertFalse(pathDirAb.toFile().exists());
assertFalse(pathFileAbc.toFile().exists());
assertFalse(pathDirAbd.toFile().exists());
assertFalse(pathFileAe.toFile().exists());
materializeFunction.execute(fileMaterializer, pathDirA);
assertTrue(pathDirA.toFile().exists());
assertTrue(pathDirAb.toFile().exists());
assertTrue(pathFileAbc.toFile().exists());
assertTrue(pathDirAbd.toFile().exists());
assertTrue(pathFileAe.toFile().exists());
}
use of com.facebook.buck.util.cache.ProjectFileHashCache in project buck by facebook.
the class MaterializerProjectFileHashCacheTest method testSymlinkToFileWithinExternalDirectory.
public void testSymlinkToFileWithinExternalDirectory(HashCode fileHashEntryHashCode, HashCode actualHashCode, MaterializeFunction materializeFunction, int expectCallsToGetHashMethod) throws IOException {
// Scenario:
// path: /project/linktoexternaldir/externalfile
// symlink root: /project/linktoexternaldir -> /externalDir
// => check that /project/linktoexternaldir/externalfile -> /externalDir/externalfile
assumeTrue(!Platform.detect().equals(Platform.WINDOWS));
ProjectFilesystem projectFilesystem = new ProjectFilesystem(projectDir.getRoot().toPath());
File externalFile = externalDir.newFile("externalfile");
Path symlinkRoot = projectFilesystem.resolve("linktoexternaldir");
Path relativeSymlinkRoot = Paths.get("linktoexternaldir");
// /project/linktoexternaldir/externalfile
Path symlink = symlinkRoot.resolve("externalfile");
Path relativeSymlink = projectFilesystem.getPathRelativeToProjectRoot(symlink).get();
BuildJobStateFileHashEntry symlinkFileHashEntry = new BuildJobStateFileHashEntry();
symlinkFileHashEntry.setRootSymLink(unixPath(relativeSymlinkRoot));
symlinkFileHashEntry.setRootSymLinkTarget(unixPath(externalDir.getRoot().toPath()));
symlinkFileHashEntry.setPath(unixPath(relativeSymlink));
symlinkFileHashEntry.setHashCode(fileHashEntryHashCode.toString());
BuildJobStateFileHashes fileHashes = new BuildJobStateFileHashes();
fileHashes.addToEntries(symlinkFileHashEntry);
FileContentsProvider mockFileProvider = EasyMock.createMock(FileContentsProvider.class);
ProjectFileHashCache mockFileHashCache = EasyMock.createNiceMock(ProjectFileHashCache.class);
expect(mockFileHashCache.getFilesystem()).andReturn(projectFilesystem).atLeastOnce();
if (expectCallsToGetHashMethod > 0) {
expect(mockFileHashCache.get(relativeSymlink)).andReturn(actualHashCode).times(expectCallsToGetHashMethod);
}
replay(mockFileHashCache);
MaterializerProjectFileHashCache fileMaterializer = new MaterializerProjectFileHashCache(mockFileHashCache, fileHashes, mockFileProvider);
assertFalse(symlink.toFile().exists());
materializeFunction.execute(fileMaterializer, relativeSymlink);
assertTrue(symlink.toFile().exists());
assertThat(symlink.toRealPath(), Matchers.equalTo(externalFile.toPath().toRealPath()));
verify(mockFileHashCache);
}
Aggregations