Search in sources :

Example 41 with RuleKey

use of com.facebook.buck.rules.RuleKey in project buck by facebook.

the class DefaultJavaLibraryTest method testRuleKeyIsOrderInsensitiveForSourcesAndResources.

@Test
public void testRuleKeyIsOrderInsensitiveForSourcesAndResources() throws Exception {
    // Note that these filenames were deliberately chosen to have identical hashes to maximize
    // the chance of order-sensitivity when being inserted into a HashMap.  Just using
    // {foo,bar}.{java,txt} resulted in a passing test even for the old broken code.
    ProjectFilesystem filesystem = new AllExistingProjectFilesystem() {

        @Override
        public boolean isDirectory(Path path, LinkOption... linkOptionsk) {
            return false;
        }
    };
    BuildRuleResolver resolver1 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder1 = new SourcePathRuleFinder(resolver1);
    SourcePathResolver pathResolver1 = new SourcePathResolver(ruleFinder1);
    DefaultJavaLibrary rule1 = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//lib:lib")).addSrc(Paths.get("agifhbkjdec.java")).addSrc(Paths.get("bdeafhkgcji.java")).addSrc(Paths.get("bdehgaifjkc.java")).addSrc(Paths.get("cfiabkjehgd.java")).addResource(new FakeSourcePath("becgkaifhjd.txt")).addResource(new FakeSourcePath("bkhajdifcge.txt")).addResource(new FakeSourcePath("cabfghjekid.txt")).addResource(new FakeSourcePath("chkdbafijge.txt")).build(resolver1, filesystem);
    BuildRuleResolver resolver2 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder2 = new SourcePathRuleFinder(resolver2);
    SourcePathResolver pathResolver2 = new SourcePathResolver(ruleFinder2);
    DefaultJavaLibrary rule2 = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//lib:lib")).addSrc(Paths.get("cfiabkjehgd.java")).addSrc(Paths.get("bdehgaifjkc.java")).addSrc(Paths.get("bdeafhkgcji.java")).addSrc(Paths.get("agifhbkjdec.java")).addResource(new FakeSourcePath("chkdbafijge.txt")).addResource(new FakeSourcePath("cabfghjekid.txt")).addResource(new FakeSourcePath("bkhajdifcge.txt")).addResource(new FakeSourcePath("becgkaifhjd.txt")).build(resolver2, filesystem);
    ImmutableMap.Builder<String, String> fileHashes = ImmutableMap.builder();
    for (String filename : ImmutableList.of("agifhbkjdec.java", "bdeafhkgcji.java", "bdehgaifjkc.java", "cfiabkjehgd.java", "becgkaifhjd.txt", "bkhajdifcge.txt", "cabfghjekid.txt", "chkdbafijge.txt")) {
        fileHashes.put(filename, Hashing.sha1().hashString(filename, Charsets.UTF_8).toString());
    }
    DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, FakeFileHashCache.createFromStrings(fileHashes.build()), pathResolver1, ruleFinder1);
    DefaultRuleKeyFactory ruleKeyFactory2 = new DefaultRuleKeyFactory(0, FakeFileHashCache.createFromStrings(fileHashes.build()), pathResolver2, ruleFinder2);
    RuleKey key1 = ruleKeyFactory.build(rule1);
    RuleKey key2 = ruleKeyFactory2.build(rule2);
    assertEquals(key1, key2);
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) LinkOption(java.nio.file.LinkOption) RuleKey(com.facebook.buck.rules.RuleKey) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ImmutableMap(com.google.common.collect.ImmutableMap) AllExistingProjectFilesystem(com.facebook.buck.testutil.AllExistingProjectFilesystem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) AllExistingProjectFilesystem(com.facebook.buck.testutil.AllExistingProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 42 with RuleKey

use of com.facebook.buck.rules.RuleKey in project buck by facebook.

the class DefaultJavaLibraryTest method testInputBasedRuleKeyWithRecursiveExportedDeps.

/**
   * Tests that input-based rule keys work properly with a Java library dep exported through
   * multiple Java library dependencies.
   */
@Test
public void testInputBasedRuleKeyWithRecursiveExportedDeps() throws Exception {
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    // Setup a Java library which builds against another Java library dep exporting another Java
    // library dep.
    TargetNode<JavaLibraryDescription.Arg, ?> exportedDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:edep"), filesystem).addSrc(Paths.get("Source1.java")).build();
    TargetNode<?, ?> dep2Node = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep2"), filesystem).addExportedDep(exportedDepNode.getBuildTarget()).build();
    TargetNode<?, ?> dep1Node = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep1"), filesystem).addExportedDep(dep2Node.getBuildTarget()).build();
    TargetNode<?, ?> libraryNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:lib"), filesystem).addDep(dep1Node.getBuildTarget()).build();
    TargetGraph targetGraph = TargetGraphFactory.newInstance(exportedDepNode, dep2Node, dep1Node, libraryNode);
    ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    JavaLibrary exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
    JavaLibrary library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
    filesystem.writeContentsToPath("JAR contents", pathResolver.getRelativePath(exportedDep.getSourcePathToOutput()));
    writeAbiJar(filesystem, pathResolver.getRelativePath(ruleResolver.requireRule(exportedDep.getAbiJar().get()).getSourcePathToOutput()), "Source1.class", "ABI JAR contents");
    FileHashCache originalHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
    InputBasedRuleKeyFactory factory = new InputBasedRuleKeyFactory(0, originalHashCache, pathResolver, ruleFinder);
    RuleKey originalRuleKey = factory.build(library);
    // Now change the exported Java library dependency such that its rule key changes, and change
    // its JAR contents, but keep its ABI JAR the same.  This should *not* affect the input-based
    // rule key of the consuming java library, since it only cares about the contents of the ABI
    // JAR.
    exportedDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:edep"), filesystem).addSrc(Paths.get("Source1.java")).setResourcesRoot(Paths.get("some root that changes the rule key")).build();
    targetGraph = TargetGraphFactory.newInstance(exportedDepNode, dep2Node, dep1Node, libraryNode);
    ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    ruleFinder = new SourcePathRuleFinder(ruleResolver);
    pathResolver = new SourcePathResolver(ruleFinder);
    exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
    library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
    filesystem.writeContentsToPath("different JAR contents", pathResolver.getRelativePath(exportedDep.getSourcePathToOutput()));
    FileHashCache unaffectedHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
    factory = new InputBasedRuleKeyFactory(0, unaffectedHashCache, pathResolver, ruleFinder);
    RuleKey unaffectedRuleKey = factory.build(library);
    assertThat(originalRuleKey, equalTo(unaffectedRuleKey));
    // Now actually change the exproted Java library dependency's ABI JAR.  This *should* affect
    // the input-based rule key of the consuming java library.
    ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    ruleFinder = new SourcePathRuleFinder(ruleResolver);
    pathResolver = new SourcePathResolver(ruleFinder);
    exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
    library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
    writeAbiJar(filesystem, pathResolver.getRelativePath(ruleResolver.requireRule(exportedDep.getAbiJar().get()).getSourcePathToOutput()), "Source1.class", "changed ABI JAR contents");
    FileHashCache affectedHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
    factory = new InputBasedRuleKeyFactory(0, affectedHashCache, pathResolver, ruleFinder);
    RuleKey affectedRuleKey = factory.build(library);
    assertThat(originalRuleKey, Matchers.not(equalTo(affectedRuleKey)));
}
Also used : DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) InputBasedRuleKeyFactory(com.facebook.buck.rules.keys.InputBasedRuleKeyFactory) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) TargetGraph(com.facebook.buck.rules.TargetGraph) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) AllExistingProjectFilesystem(com.facebook.buck.testutil.AllExistingProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 43 with RuleKey

use of com.facebook.buck.rules.RuleKey in project buck by facebook.

the class DefaultJavaLibraryTest method testInputBasedRuleKeyWithExportedDeps.

/**
   * Tests that input-based rule keys work properly with a Java library dep exported by a
   * first-order dep.
   */
@Test
public void testInputBasedRuleKeyWithExportedDeps() throws Exception {
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    // Setup a Java library which builds against another Java library dep exporting another Java
    // library dep.
    TargetNode<JavaLibraryDescription.Arg, ?> exportedDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:edep"), filesystem).addSrc(Paths.get("Source1.java")).build();
    TargetNode<?, ?> depNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep"), filesystem).addExportedDep(exportedDepNode.getBuildTarget()).build();
    TargetNode<?, ?> libraryNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:lib"), filesystem).addDep(depNode.getBuildTarget()).build();
    TargetGraph targetGraph = TargetGraphFactory.newInstance(exportedDepNode, depNode, libraryNode);
    ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    JavaLibrary exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
    JavaLibrary library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
    filesystem.writeContentsToPath("JAR contents", pathResolver.getRelativePath(exportedDep.getSourcePathToOutput()));
    writeAbiJar(filesystem, pathResolver.getRelativePath(ruleResolver.requireRule(exportedDep.getAbiJar().get()).getSourcePathToOutput()), "Source1.class", "ABI JAR contents");
    FileHashCache originalHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
    InputBasedRuleKeyFactory factory = new InputBasedRuleKeyFactory(0, originalHashCache, pathResolver, ruleFinder);
    RuleKey originalRuleKey = factory.build(library);
    // Now change the exported Java library dependency such that its rule key changes, and change
    // its JAR contents, but keep its ABI JAR the same.  This should *not* affect the input-based
    // rule key of the consuming java library, since it only cares about the contents of the ABI
    // JAR.
    exportedDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:edep"), filesystem).addSrc(Paths.get("Source1.java")).setResourcesRoot(Paths.get("some root that changes the rule key")).build();
    targetGraph = TargetGraphFactory.newInstance(exportedDepNode, depNode, libraryNode);
    ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    ruleFinder = new SourcePathRuleFinder(ruleResolver);
    pathResolver = new SourcePathResolver(ruleFinder);
    exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
    library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
    filesystem.writeContentsToPath("different JAR contents", pathResolver.getRelativePath(exportedDep.getSourcePathToOutput()));
    FileHashCache unaffectedHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
    factory = new InputBasedRuleKeyFactory(0, unaffectedHashCache, pathResolver, ruleFinder);
    RuleKey unaffectedRuleKey = factory.build(library);
    assertThat(originalRuleKey, equalTo(unaffectedRuleKey));
    // Now actually change the exproted Java library dependency's ABI JAR.  This *should* affect
    // the input-based rule key of the consuming java library.
    ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    ruleFinder = new SourcePathRuleFinder(ruleResolver);
    pathResolver = new SourcePathResolver(ruleFinder);
    exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
    library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
    writeAbiJar(filesystem, pathResolver.getRelativePath(ruleResolver.requireRule(exportedDep.getAbiJar().get()).getSourcePathToOutput()), "Source1.class", "changed ABI JAR contents");
    FileHashCache affectedHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
    factory = new InputBasedRuleKeyFactory(0, affectedHashCache, pathResolver, ruleFinder);
    RuleKey affectedRuleKey = factory.build(library);
    assertThat(originalRuleKey, Matchers.not(equalTo(affectedRuleKey)));
}
Also used : DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) InputBasedRuleKeyFactory(com.facebook.buck.rules.keys.InputBasedRuleKeyFactory) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) TargetGraph(com.facebook.buck.rules.TargetGraph) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) AllExistingProjectFilesystem(com.facebook.buck.testutil.AllExistingProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 44 with RuleKey

use of com.facebook.buck.rules.RuleKey in project buck by facebook.

the class JavaLibrarySymbolsFinderTest method onlyNonGeneratedSrcsShouldAffectRuleKey.

@Test
@SuppressWarnings("PMD.PrematureDeclaration")
public void onlyNonGeneratedSrcsShouldAffectRuleKey() throws IOException {
    TestDataHelper.createProjectWorkspaceForScenario(this, "java_library_symbols_finder", tmp).setUp();
    final ProjectFilesystem projectFilesystem = new ProjectFilesystem(tmp.getRoot());
    Function<String, SourcePath> convert = src -> new PathSourcePath(projectFilesystem, Paths.get(src));
    SourcePath example1 = convert.apply("Example1.java");
    SourcePath example2 = convert.apply("Example2.java");
    final BuildTarget fakeBuildTarget = BuildTargetFactory.newInstance("//foo:GenEx.java");
    SourcePath generated = new DefaultBuildTargetSourcePath(fakeBuildTarget);
    final boolean shouldRecordRequiredSymbols = true;
    JavaLibrarySymbolsFinder example1Finder = new JavaLibrarySymbolsFinder(ImmutableSortedSet.of(example1), javaFileParser, shouldRecordRequiredSymbols);
    JavaLibrarySymbolsFinder example2Finder = new JavaLibrarySymbolsFinder(ImmutableSortedSet.of(example2), javaFileParser, shouldRecordRequiredSymbols);
    JavaLibrarySymbolsFinder example1AndGeneratedSrcFinder = new JavaLibrarySymbolsFinder(ImmutableSortedSet.of(example1, generated), javaFileParser, shouldRecordRequiredSymbols);
    // Mock out calls to a SourcePathResolver so we can create a legitimate
    // DefaultRuleKeyFactory.
    final SourcePathRuleFinder ruleFinder = createMock(SourcePathRuleFinder.class);
    final SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    expect(ruleFinder.getRule(anyObject(SourcePath.class))).andAnswer(() -> {
        SourcePath input = (SourcePath) EasyMock.getCurrentArguments()[0];
        if (input instanceof ExplicitBuildTargetSourcePath) {
            return Optional.of(new FakeBuildRule(fakeBuildTarget, pathResolver));
        } else {
            return Optional.empty();
        }
    }).anyTimes();
    // Calculates the RuleKey for a JavaSymbolsRule with the specified JavaLibrarySymbolsFinder.
    final FileHashCache fileHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(projectFilesystem)));
    final DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, fileHashCache, pathResolver, ruleFinder);
    Function<JavaLibrarySymbolsFinder, RuleKey> createRuleKey = finder -> {
        JavaSymbolsRule javaSymbolsRule = new JavaSymbolsRule(BuildTargetFactory.newInstance("//foo:rule"), finder, ImmutableSortedSet.of(), ObjectMappers.newDefaultInstance(), projectFilesystem);
        return ruleKeyFactory.build(javaSymbolsRule);
    };
    RuleKey key1 = createRuleKey.apply(example1Finder);
    RuleKey key2 = createRuleKey.apply(example2Finder);
    RuleKey key3 = createRuleKey.apply(example1AndGeneratedSrcFinder);
    assertNotNull(key1);
    assertNotNull(key2);
    assertNotNull(key3);
    assertNotEquals("Two instances of a JavaSymbolsRule with different srcs should change the RuleKey.", key1, key2);
    assertEquals("Introducing an extra generated .java file to the srcs should not change the RuleKey.", key1, key3);
}
Also used : FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) ObjectMappers(com.facebook.buck.util.ObjectMappers) SourcePath(com.facebook.buck.rules.SourcePath) TestDataHelper(com.facebook.buck.testutil.integration.TestDataHelper) TemporaryPaths(com.facebook.buck.testutil.integration.TemporaryPaths) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ImmutableList(com.google.common.collect.ImmutableList) JavaFileParser(com.facebook.buck.jvm.java.JavaFileParser) RuleKey(com.facebook.buck.rules.RuleKey) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildTargetFactory(com.facebook.buck.model.BuildTargetFactory) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) EasyMock.createMock(org.easymock.EasyMock.createMock) EasyMock.anyObject(org.easymock.EasyMock.anyObject) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) IOException(java.io.IOException) EasyMock.expect(org.easymock.EasyMock.expect) JavacOptions(com.facebook.buck.jvm.java.JavacOptions) EasyMock(org.easymock.EasyMock) DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) BuildTarget(com.facebook.buck.model.BuildTarget) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) Rule(org.junit.Rule) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) FileHashCache(com.facebook.buck.util.cache.FileHashCache) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Optional(java.util.Optional) Assert.assertEquals(org.junit.Assert.assertEquals) DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) RuleKey(com.facebook.buck.rules.RuleKey) PathSourcePath(com.facebook.buck.rules.PathSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) Test(org.junit.Test)

Example 45 with RuleKey

use of com.facebook.buck.rules.RuleKey in project buck by facebook.

the class PrebuiltJarSymbolsFinderTest method contentsOfBinaryJarShouldAffectRuleKey.

@Test
public void contentsOfBinaryJarShouldAffectRuleKey() throws IOException {
    // The path to the JAR file to use as the binaryJar of the PrebuiltJarSymbolsFinder.
    final Path relativePathToJar = Paths.get("common.jar");
    final Path absolutePathToJar = tmp.getRoot().resolve(relativePathToJar);
    // Mock out calls to a SourcePathResolver so we can create a legitimate
    // DefaultRuleKeyFactory.
    final SourcePathRuleFinder ruleFinder = createMock(SourcePathRuleFinder.class);
    final SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    createMock(SourcePathResolver.class);
    expect(ruleFinder.getRule(anyObject(SourcePath.class))).andReturn(Optional.empty()).anyTimes();
    // Calculates the RuleKey for a JavaSymbolsRule with a PrebuiltJarSymbolsFinder whose binaryJar
    // is a JAR file with the specified entries.
    Function<ImmutableSet<String>, RuleKey> createRuleKey = entries -> {
        File jarFile = absolutePathToJar.toFile();
        JavaSymbolsRule javaSymbolsRule;
        FakeFileHashCache fileHashCache;
        try {
            PrebuiltJarSymbolsFinder finder = createFinderForFileWithEntries(relativePathToJar.getFileName().toString(), entries);
            HashCode hash = Files.hash(jarFile, Hashing.sha1());
            Map<Path, HashCode> pathsToHashes = ImmutableMap.of(absolutePathToJar, hash);
            fileHashCache = new FakeFileHashCache(pathsToHashes);
            javaSymbolsRule = new JavaSymbolsRule(BuildTargetFactory.newInstance("//foo:rule"), finder, ImmutableSortedSet.of(), ObjectMappers.newDefaultInstance(), new ProjectFilesystem(tmp.getRoot()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        RuleKey ruleKey = new DefaultRuleKeyFactory(0, fileHashCache, pathResolver, ruleFinder).build(javaSymbolsRule);
        jarFile.delete();
        return ruleKey;
    };
    RuleKey key1 = createRuleKey.apply(ImmutableSet.of("entry1", "entry2"));
    RuleKey key2 = createRuleKey.apply(ImmutableSet.of("entry1", "entry2"));
    RuleKey key3 = createRuleKey.apply(ImmutableSet.of("entry1", "entry2", "entry3"));
    assertNotNull(key1);
    assertNotNull(key2);
    assertNotNull(key3);
    assertEquals("Two instances of a JavaSymbolsRule with the same inputs should have the same RuleKey.", key1, key2);
    assertNotEquals("Changing the contents of the binaryJar for the PrebuiltJarSymbolsFinder should change " + "the RuleKey of the JavaSymbolsRule that contains it.", key1, key3);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Iterables(com.google.common.collect.Iterables) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) ObjectMappers(com.facebook.buck.util.ObjectMappers) SourcePath(com.facebook.buck.rules.SourcePath) Hashing(com.google.common.hash.Hashing) TemporaryPaths(com.facebook.buck.testutil.integration.TemporaryPaths) BufferedOutputStream(java.io.BufferedOutputStream) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Strings(com.google.common.base.Strings) RuleKey(com.facebook.buck.rules.RuleKey) Files(com.google.common.io.Files) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Map(java.util.Map) BuildTargetFactory(com.facebook.buck.model.BuildTargetFactory) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) Clock(com.facebook.buck.timing.Clock) EasyMock.createMock(org.easymock.EasyMock.createMock) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) EasyMock.anyObject(org.easymock.EasyMock.anyObject) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) OutputStream(java.io.OutputStream) ZipOutputStreams(com.facebook.buck.zip.ZipOutputStreams) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertNotNull(org.junit.Assert.assertNotNull) HashCode(com.google.common.hash.HashCode) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) EasyMock.expect(org.easymock.EasyMock.expect) BuildTarget(com.facebook.buck.model.BuildTarget) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) File(java.io.File) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) FakeClock(com.facebook.buck.timing.FakeClock) Rule(org.junit.Rule) Paths(java.nio.file.Paths) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Optional(java.util.Optional) Assert.assertEquals(org.junit.Assert.assertEquals) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) IOException(java.io.IOException) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) HashCode(com.google.common.hash.HashCode) ImmutableSet(com.google.common.collect.ImmutableSet) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) File(java.io.File) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Aggregations

RuleKey (com.facebook.buck.rules.RuleKey)149 Test (org.junit.Test)112 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)71 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)71 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)71 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)70 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)57 Path (java.nio.file.Path)57 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)42 BuildTarget (com.facebook.buck.model.BuildTarget)42 BuildRule (com.facebook.buck.rules.BuildRule)42 LazyPath (com.facebook.buck.io.LazyPath)34 DefaultRuleKeyFactory (com.facebook.buck.rules.keys.DefaultRuleKeyFactory)34 PathSourcePath (com.facebook.buck.rules.PathSourcePath)33 AddToRuleKey (com.facebook.buck.rules.AddToRuleKey)30 SourcePath (com.facebook.buck.rules.SourcePath)28 FakeFileHashCache (com.facebook.buck.testutil.FakeFileHashCache)27 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)25 IOException (java.io.IOException)25 StackedFileHashCache (com.facebook.buck.util.cache.StackedFileHashCache)23