use of com.facebook.buck.util.sha1.Sha1HashCode in project buck by facebook.
the class DefaultProjectFilesystemDelegate method computeSha1.
@Override
public Sha1HashCode computeSha1(Path pathRelativeToProjectRootOrJustAbsolute) throws IOException {
final Path fileToHash = getPathForRelativePath(pathRelativeToProjectRootOrJustAbsolute);
// Normally, we would just use `Files.hash(fileToHash.toFile(), Hashing.sha1())`, but if
// fileToHash is backed by Jimfs, its toFile() method throws an UnsupportedOperationException.
// Creating the input stream via java.nio.file.Files.newInputStream() avoids this issue.
ByteSource source = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
// which already buffers.
return Files.newInputStream(fileToHash);
}
};
HashCode hashCode = source.hash(Hashing.sha1());
return Sha1HashCode.fromHashCode(hashCode);
}
use of com.facebook.buck.util.sha1.Sha1HashCode in project buck by facebook.
the class SmartDexingStepTest method testDxPseudoRuleCaching.
/**
* Tests whether pseudo rule cache detection is working properly.
*/
@Test
public void testDxPseudoRuleCaching() throws IOException {
File testIn = new File(tmpDir.getRoot(), "testIn");
try (ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(testIn)))) {
zipOut.putNextEntry(new ZipEntry("foobar"));
zipOut.write(new byte[] { 0 });
}
File outputFile = tmpDir.newFile("out.dex");
Path outputHashFile = new File(tmpDir.getRoot(), "out.dex.hash").toPath();
Files.write("dummy", outputHashFile.toFile(), Charsets.UTF_8);
ProjectFilesystem filesystem = new ProjectFilesystem(tmpDir.getRoot().toPath());
Sha1HashCode actualHashCode = Sha1HashCode.of(Strings.repeat("a", 40));
DxPseudoRule rule = new DxPseudoRule(filesystem, ImmutableMap.of(testIn.toPath(), actualHashCode), ImmutableSet.of(testIn.toPath()), outputFile.toPath(), outputHashFile, EnumSet.of(DxStep.Option.NO_OPTIMIZE), Optional.empty(), Optional.empty());
assertFalse("'dummy' is not a matching input hash", rule.checkIsCached());
// Write the real hash into the output hash file and ensure that checkIsCached now
// yields true.
String actualHash = rule.hashInputs();
assertFalse(actualHash.isEmpty());
Files.write(actualHash, outputHashFile.toFile(), Charsets.UTF_8);
assertTrue("Matching input hash should be considered cached", rule.checkIsCached());
}
Aggregations