Search in sources :

Example 91 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class ParserTest method deletingSourceFileChangesHash.

@Test
public void deletingSourceFileChangesHash() throws Exception {
    tempDir.newFolder("foo");
    Path testFooBuckFile = tempDir.newFile("foo/BUCK");
    Files.write(testFooBuckFile, "java_library(name = 'lib', srcs=glob(['*.java']), visibility=['PUBLIC'])\n".getBytes(UTF_8));
    Path testFooJavaFile = tempDir.newFile("foo/Foo.java");
    Files.write(testFooJavaFile, "// Ceci n'est pas une Javafile\n".getBytes(UTF_8));
    Path testBarJavaFile = tempDir.newFile("foo/Bar.java");
    Files.write(testBarJavaFile, "// Seriously, no Java here\n".getBytes(UTF_8));
    BuildTarget fooLibTarget = BuildTarget.builder(cellRoot, "//foo", "lib").build();
    HashCode originalHash = buildTargetGraphAndGetHashCodes(parser, fooLibTarget).get(fooLibTarget);
    Files.delete(testBarJavaFile);
    WatchEvent<Path> deleteEvent = createPathEvent(Paths.get("foo/Bar.java"), StandardWatchEventKinds.ENTRY_DELETE);
    parser.onFileSystemChange(deleteEvent);
    HashCode updatedHash = buildTargetGraphAndGetHashCodes(parser, fooLibTarget).get(fooLibTarget);
    assertNotEquals(originalHash, updatedHash);
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) HashCode(com.google.common.hash.HashCode) BuildTarget(com.facebook.buck.model.BuildTarget) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) Test(org.junit.Test)

Example 92 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class ParserTest method renamingSourceFileChangesHash.

@Test
public void renamingSourceFileChangesHash() throws Exception {
    tempDir.newFolder("foo");
    Path testFooBuckFile = tempDir.newFile("foo/BUCK");
    Files.write(testFooBuckFile, "java_library(name = 'lib', srcs=glob(['*.java']), visibility=['PUBLIC'])\n".getBytes(UTF_8));
    Path testFooJavaFile = tempDir.newFile("foo/Foo.java");
    Files.write(testFooJavaFile, "// Ceci n'est pas une Javafile\n".getBytes(UTF_8));
    BuildTarget fooLibTarget = BuildTarget.builder(cellRoot, "//foo", "lib").build();
    HashCode originalHash = buildTargetGraphAndGetHashCodes(parser, fooLibTarget).get(fooLibTarget);
    Files.move(testFooJavaFile, testFooJavaFile.resolveSibling("Bar.java"));
    WatchEvent<Path> deleteEvent = createPathEvent(Paths.get("foo/Foo.java"), StandardWatchEventKinds.ENTRY_DELETE);
    WatchEvent<Path> createEvent = createPathEvent(Paths.get("foo/Bar.java"), StandardWatchEventKinds.ENTRY_CREATE);
    parser.onFileSystemChange(deleteEvent);
    parser.onFileSystemChange(createEvent);
    HashCode updatedHash = buildTargetGraphAndGetHashCodes(parser, fooLibTarget).get(fooLibTarget);
    assertNotEquals(originalHash, updatedHash);
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) HashCode(com.google.common.hash.HashCode) BuildTarget(com.facebook.buck.model.BuildTarget) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) Test(org.junit.Test)

Example 93 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class FakeProjectFilesystem method computeSha1.

/**
   * Does not support symlinks.
   */
@Override
public Sha1HashCode computeSha1(Path pathRelativeToProjectRootOrJustAbsolute) throws IOException {
    if (!exists(pathRelativeToProjectRootOrJustAbsolute)) {
        throw new NoSuchFileException(pathRelativeToProjectRootOrJustAbsolute.toString());
    }
    // Because this class is a fake, the file contents may not be available as a stream, so we load
    // all of the contents into memory as a byte[] and then hash them.
    byte[] fileContents = getFileBytes(pathRelativeToProjectRootOrJustAbsolute);
    HashCode hashCode = Hashing.sha1().newHasher().putBytes(fileContents).hash();
    return Sha1HashCode.fromHashCode(hashCode);
}
Also used : Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) HashCode(com.google.common.hash.HashCode) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 94 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class RecordingProjectFileHashCache method get.

@Override
public HashCode get(Path relPath) throws IOException {
    checkIsRelative(relPath);
    Queue<Path> remainingPaths = new LinkedList<>();
    remainingPaths.add(relPath);
    while (remainingPaths.size() > 0) {
        Path nextPath = remainingPaths.remove();
        HashCode hashCode = delegate.get(nextPath);
        List<PathWithUnixSeparators> children = ImmutableList.of();
        if (projectFilesystem.isDirectory(nextPath)) {
            children = processDirectory(nextPath, remainingPaths);
        }
        synchronized (this) {
            if (!remoteFileHashes.containsAndAddPath(nextPath)) {
                record(nextPath, Optional.empty(), hashCode, children);
            }
        }
    }
    return delegate.get(relPath);
}
Also used : ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) Path(java.nio.file.Path) HashCode(com.google.common.hash.HashCode) LinkedList(java.util.LinkedList) PathWithUnixSeparators(com.facebook.buck.distributed.thrift.PathWithUnixSeparators)

Example 95 with HashCode

use of com.google.common.hash.HashCode in project buck by facebook.

the class DexProducedFromJavaLibraryThatContainsClassFilesTest method testGetBuildStepsWhenThereAreClassesToDex.

@Test
public void testGetBuildStepsWhenThereAreClassesToDex() throws IOException, InterruptedException {
    ProjectFilesystem filesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    FakeJavaLibrary javaLibraryRule = new FakeJavaLibrary(BuildTargetFactory.newInstance(filesystem.getRootPath(), "//foo:bar"), pathResolver, filesystem, ImmutableSortedSet.of()) {

        @Override
        public ImmutableSortedMap<String, HashCode> getClassNamesToHashes() {
            return ImmutableSortedMap.of("com/example/Foo", HashCode.fromString("cafebabe"));
        }
    };
    resolver.addToIndex(javaLibraryRule);
    Path jarOutput = BuildTargets.getGenPath(filesystem, javaLibraryRule.getBuildTarget(), "%s.jar");
    javaLibraryRule.setOutputFile(jarOutput.toString());
    BuildContext context = FakeBuildContext.withSourcePathResolver(pathResolver);
    FakeBuildableContext buildableContext = new FakeBuildableContext();
    Path dexOutput = BuildTargets.getGenPath(filesystem, javaLibraryRule.getBuildTarget().withFlavors(AndroidBinaryGraphEnhancer.DEX_FLAVOR), "%s.dex.jar");
    createFiles(filesystem, dexOutput.toString(), jarOutput.toString());
    BuildTarget buildTarget = BuildTargetFactory.newInstance(filesystem.getRootPath(), "//foo:bar#dex");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).setProjectFilesystem(filesystem).build();
    DexProducedFromJavaLibrary preDex = new DexProducedFromJavaLibrary(params, javaLibraryRule);
    List<Step> steps = preDex.getBuildSteps(context, buildableContext);
    AndroidPlatformTarget androidPlatformTarget = createMock(AndroidPlatformTarget.class);
    expect(androidPlatformTarget.getDxExecutable()).andStubReturn(Paths.get("/usr/bin/dx"));
    EasyMock.replay(androidPlatformTarget);
    ExecutionContext executionContext = TestExecutionContext.newBuilder().setAndroidPlatformTargetSupplier(Suppliers.ofInstance(androidPlatformTarget)).build();
    String expectedDxCommand = String.format("%s --dex --no-optimize --force-jumbo --output %s %s", Paths.get("/usr/bin/dx"), filesystem.resolve(dexOutput), filesystem.resolve(jarOutput));
    MoreAsserts.assertSteps("Generate bar.dex.jar.", ImmutableList.of(String.format("rm -f %s", filesystem.resolve(dexOutput)), String.format("mkdir -p %s", filesystem.resolve(dexOutput).getParent()), "estimate_dex_weight", "(cd " + filesystem.getRootPath() + " && " + expectedDxCommand + ")", String.format("zip-scrub %s", dexOutput), "record_dx_success"), steps, executionContext);
    ((EstimateDexWeightStep) steps.get(2)).setWeightEstimateForTesting(250);
    Step recordArtifactAndMetadataStep = steps.get(5);
    int exitCode = recordArtifactAndMetadataStep.execute(executionContext).getExitCode();
    assertEquals(0, exitCode);
    assertEquals("The generated .dex.jar file should be in the set of recorded artifacts.", ImmutableSet.of(BuildTargets.getGenPath(filesystem, buildTarget, "%s.dex.jar")), buildableContext.getRecordedArtifacts());
    buildableContext.assertContainsMetadataMapping(DexProducedFromJavaLibrary.WEIGHT_ESTIMATE, "250");
}
Also used : Path(java.nio.file.Path) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) EstimateDexWeightStep(com.facebook.buck.dalvik.EstimateDexWeightStep) Step(com.facebook.buck.step.Step) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) HashCode(com.google.common.hash.HashCode) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) EstimateDexWeightStep(com.facebook.buck.dalvik.EstimateDexWeightStep) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Aggregations

HashCode (com.google.common.hash.HashCode)115 Path (java.nio.file.Path)41 Test (org.junit.Test)39 BuildTarget (com.facebook.buck.model.BuildTarget)19 FakeFileHashCache (com.facebook.buck.testutil.FakeFileHashCache)13 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 IOException (java.io.IOException)12 PathSourcePath (com.facebook.buck.rules.PathSourcePath)11 FileHashCache (com.facebook.buck.util.cache.FileHashCache)11 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)10 Sha1HashCode (com.facebook.buck.util.sha1.Sha1HashCode)10 Hasher (com.google.common.hash.Hasher)10 Map (java.util.Map)8 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)7 SourcePath (com.facebook.buck.rules.SourcePath)7 BuckEventBus (com.facebook.buck.event.BuckEventBus)6 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)6 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)5 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)5