use of com.facebook.buck.rules.keys.InputBasedRuleKeyFactory in project buck by facebook.
the class DefaultJavaLibraryTest method testInputBasedRuleKeySourceChange.
/**
* Tests that input-based rule keys work properly with generated sources.
*/
@Test
public void testInputBasedRuleKeySourceChange() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
// Setup a Java library consuming a source generated by a genrule and grab its rule key.
BuildRule genSrc = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:gen_srcs")).setOut("Test.java").setCmd("something").build(ruleResolver, filesystem);
filesystem.writeContentsToPath("class Test {}", pathResolver.getRelativePath(genSrc.getSourcePathToOutput()));
JavaLibrary library = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:lib")).addSrc(genSrc.getSourcePathToOutput()).build(ruleResolver, filesystem);
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 genrule such that its rule key changes, but it's output stays the same (since
// we don't change it). This should *not* affect the input-based rule key of the consuming
// java library, since it only cares about the contents of the source.
ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
genSrc = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:gen_srcs")).setOut("Test.java").setCmd("something else").build(ruleResolver, filesystem);
library = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:lib")).addSrc(genSrc.getSourcePathToOutput()).build(ruleResolver, filesystem);
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 modify the source, which should make the input-based rule key change.
ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
genSrc = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:gen_srcs")).setOut("Test.java").setCmd("something else").build(ruleResolver, filesystem);
filesystem.writeContentsToPath("class Test2 {}", pathResolver.getRelativePath(genSrc.getSourcePathToOutput()));
library = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:lib")).addSrc(genSrc.getSourcePathToOutput()).build(ruleResolver, filesystem);
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)));
}
use of com.facebook.buck.rules.keys.InputBasedRuleKeyFactory in project buck by facebook.
the class SymlinkTreeTest method testSymlinkTreeInputBasedRuleKeysAreImmuneToLinkSourceContentChanges.
@Test
public void testSymlinkTreeInputBasedRuleKeysAreImmuneToLinkSourceContentChanges() throws Exception {
Genrule dep = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:dep")).setOut("out").build(ruleResolver);
symlinkTreeBuildRule = new SymlinkTree(buildTarget, projectFilesystem, outputPath, ImmutableMap.of(Paths.get("link"), dep.getSourcePathToOutput()), ruleFinder);
// Generate an input-based rule key for the symlink tree with the contents of the link
// target hashing to "aaaa".
FakeFileHashCache hashCache = FakeFileHashCache.createFromStrings(ImmutableMap.of("out", "aaaa"));
InputBasedRuleKeyFactory inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, hashCache, pathResolver, ruleFinder);
RuleKey ruleKey1 = inputBasedRuleKeyFactory.build(symlinkTreeBuildRule);
// Generate an input-based rule key for the symlink tree with the contents of the link
// target hashing to a different value: "bbbb".
hashCache = FakeFileHashCache.createFromStrings(ImmutableMap.of("out", "bbbb"));
inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, hashCache, pathResolver, ruleFinder);
RuleKey ruleKey2 = inputBasedRuleKeyFactory.build(symlinkTreeBuildRule);
// Verify that the rules keys are the same.
assertEquals(ruleKey1, ruleKey2);
}
use of com.facebook.buck.rules.keys.InputBasedRuleKeyFactory in project buck by facebook.
the class CalculateAbiTest method inputRuleKeyDoesNotChangeIfGeneratedBinaryJarDoesNotChange.
@Test
public void inputRuleKeyDoesNotChangeIfGeneratedBinaryJarDoesNotChange() throws Exception {
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
ProjectFilesystem filesystem = new FakeProjectFilesystem();
// Setup the initial java library.
Path input = Paths.get("input.java");
BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//:library");
JavaLibraryBuilder builder = JavaLibraryBuilder.createBuilder(javaLibraryTarget).addSrc(new PathSourcePath(filesystem, input));
DefaultJavaLibrary javaLibrary = builder.build(resolver, filesystem);
// Write something to the library source and geneated JAR, so they exist to generate rule keys.
filesystem.writeContentsToPath("stuff", input);
filesystem.writeContentsToPath("stuff", pathResolver.getRelativePath(javaLibrary.getSourcePathToOutput()));
BuildTarget target = BuildTargetFactory.newInstance("//:library-abi");
CalculateAbi calculateAbi = CalculateAbi.of(target, ruleFinder, builder.createBuildRuleParams(resolver, filesystem), new DefaultBuildTargetSourcePath(javaLibraryTarget));
FileHashCache initialHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
DefaultRuleKeyFactory initialRuleKeyFactory = new DefaultRuleKeyFactory(0, initialHashCache, pathResolver, ruleFinder);
RuleKey initialKey = initialRuleKeyFactory.build(calculateAbi);
RuleKey initialInputKey = new InputBasedRuleKeyFactory(0, initialHashCache, pathResolver, ruleFinder).build(calculateAbi);
// Write something to the library source and geneated JAR, so they exist to generate rule keys.
filesystem.writeContentsToPath("new stuff", input);
// Re-setup some entities to drop internal rule key caching.
resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
ruleFinder = new SourcePathRuleFinder(resolver);
pathResolver = new SourcePathResolver(ruleFinder);
builder.build(resolver, filesystem);
FileHashCache alteredHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
DefaultRuleKeyFactory alteredRuleKeyFactory = new DefaultRuleKeyFactory(0, alteredHashCache, pathResolver, ruleFinder);
RuleKey alteredKey = alteredRuleKeyFactory.build(calculateAbi);
RuleKey alteredInputKey = new InputBasedRuleKeyFactory(0, alteredHashCache, pathResolver, ruleFinder).build(calculateAbi);
assertThat(initialKey, Matchers.not(Matchers.equalTo(alteredKey)));
assertThat(initialInputKey, Matchers.equalTo(alteredInputKey));
}
use of com.facebook.buck.rules.keys.InputBasedRuleKeyFactory in project buck by facebook.
the class CalculateAbiTest method ruleKeysChangeIfGeneratedBinaryJarChanges.
@Test
public void ruleKeysChangeIfGeneratedBinaryJarChanges() throws Exception {
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
ProjectFilesystem filesystem = new FakeProjectFilesystem();
// Setup the initial java library.
Path input = Paths.get("input.java");
BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//:library");
JavaLibraryBuilder builder = JavaLibraryBuilder.createBuilder(javaLibraryTarget).addSrc(new PathSourcePath(filesystem, input));
DefaultJavaLibrary javaLibrary = builder.build(resolver, filesystem);
// Write something to the library source and geneated JAR, so they exist to generate rule keys.
filesystem.writeContentsToPath("stuff", input);
filesystem.writeContentsToPath("stuff", pathResolver.getRelativePath(javaLibrary.getSourcePathToOutput()));
BuildTarget target = BuildTargetFactory.newInstance("//:library-abi");
CalculateAbi calculateAbi = CalculateAbi.of(target, ruleFinder, builder.createBuildRuleParams(resolver, filesystem), new DefaultBuildTargetSourcePath(javaLibraryTarget));
FileHashCache initialHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
DefaultRuleKeyFactory initialRuleKeyFactory = new DefaultRuleKeyFactory(0, initialHashCache, pathResolver, ruleFinder);
RuleKey initialKey = initialRuleKeyFactory.build(calculateAbi);
RuleKey initialInputKey = new InputBasedRuleKeyFactory(0, initialHashCache, pathResolver, ruleFinder).build(calculateAbi);
// Write something to the library source and geneated JAR, so they exist to generate rule keys.
filesystem.writeContentsToPath("new stuff", input);
filesystem.writeContentsToPath("new stuff", pathResolver.getRelativePath(javaLibrary.getSourcePathToOutput()));
// Re-setup some entities to drop internal rule key caching.
resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
ruleFinder = new SourcePathRuleFinder(resolver);
pathResolver = new SourcePathResolver(ruleFinder);
builder.build(resolver, filesystem);
FileHashCache alteredHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
DefaultRuleKeyFactory alteredRuleKeyFactory = new DefaultRuleKeyFactory(0, alteredHashCache, pathResolver, ruleFinder);
RuleKey alteredKey = alteredRuleKeyFactory.build(calculateAbi);
RuleKey alteredInputKey = new InputBasedRuleKeyFactory(0, alteredHashCache, pathResolver, ruleFinder).build(calculateAbi);
assertThat(initialKey, Matchers.not(Matchers.equalTo(alteredKey)));
assertThat(initialInputKey, Matchers.not(Matchers.equalTo(alteredInputKey)));
}
use of com.facebook.buck.rules.keys.InputBasedRuleKeyFactory in project buck by facebook.
the class GenruleTest method inputBasedRuleKeyLocationMacro.
@Test
public void inputBasedRuleKeyLocationMacro() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
GenruleBuilder ruleBuilder = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:rule")).setCmd("run $(location //:dep)").setOut("output");
// Create an initial input-based rule key
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
BuildRule dep = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:dep")).setOut("dep.out").setCmd("something").build(resolver);
filesystem.writeContentsToPath("something", pathResolver.getRelativePath(dep.getSourcePathToOutput()));
BuildRule rule = ruleBuilder.build(resolver);
DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
InputBasedRuleKeyFactory inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
RuleKey originalRuleKey = ruleKeyFactory.build(rule);
RuleKey originalInputRuleKey = inputBasedRuleKeyFactory.build(rule);
// Change the genrule's command, which will change its normal rule key, but since we're keeping
// its output the same, the input-based rule key for the consuming rule will stay the same.
// This is because the input-based rule key for the consuming rule only cares about the contents
// of the output this rule produces.
resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:dep")).setOut("dep.out").setCmd("something else").build(resolver);
rule = ruleBuilder.build(resolver);
ruleFinder = new SourcePathRuleFinder(resolver);
pathResolver = new SourcePathResolver(ruleFinder);
ruleKeyFactory = new DefaultRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
RuleKey unchangedRuleKey = ruleKeyFactory.build(rule);
RuleKey unchangedInputBasedRuleKey = inputBasedRuleKeyFactory.build(rule);
assertThat(unchangedRuleKey, Matchers.not(Matchers.equalTo(originalRuleKey)));
assertThat(unchangedInputBasedRuleKey, Matchers.equalTo(originalInputRuleKey));
// Make a change to the dep's output, which *should* affect the input-based rule key.
resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
ruleFinder = new SourcePathRuleFinder(resolver);
pathResolver = new SourcePathResolver(ruleFinder);
dep = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:dep")).setOut("dep.out").setCmd("something").build(resolver);
filesystem.writeContentsToPath("something else", pathResolver.getRelativePath(dep.getSourcePathToOutput()));
rule = ruleBuilder.build(resolver);
inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
RuleKey changedInputBasedRuleKey = inputBasedRuleKeyFactory.build(rule);
assertThat(changedInputBasedRuleKey, Matchers.not(Matchers.equalTo(originalInputRuleKey)));
}
Aggregations