Search in sources :

Example 16 with Sha1HashCode

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);
}
Also used : Path(java.nio.file.Path) HashCode(com.google.common.hash.HashCode) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) ByteSource(com.google.common.io.ByteSource)

Example 17 with Sha1HashCode

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());
}
Also used : Path(java.nio.file.Path) DxPseudoRule(com.facebook.buck.android.SmartDexingStep.DxPseudoRule) ZipOutputStream(java.util.zip.ZipOutputStream) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Test(org.junit.Test)

Aggregations

Sha1HashCode (com.facebook.buck.util.sha1.Sha1HashCode)17 Path (java.nio.file.Path)10 Test (org.junit.Test)6 ImmutableList (com.google.common.collect.ImmutableList)4 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)3 SourcePath (com.facebook.buck.rules.SourcePath)3 AbstractExecutionStep (com.facebook.buck.step.AbstractExecutionStep)3 ExecutionContext (com.facebook.buck.step.ExecutionContext)3 Step (com.facebook.buck.step.Step)3 StepExecutionResult (com.facebook.buck.step.StepExecutionResult)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 HashCode (com.google.common.hash.HashCode)3 IOException (java.io.IOException)3 BuildTarget (com.facebook.buck.model.BuildTarget)2 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)2 MkdirStep (com.facebook.buck.step.fs.MkdirStep)2 BuckBuildLog (com.facebook.buck.testutil.integration.BuckBuildLog)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 AbstractMap (java.util.AbstractMap)2 List (java.util.List)2