use of com.facebook.buck.util.cache.StackedFileHashCache 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);
}
use of com.facebook.buck.util.cache.StackedFileHashCache in project buck by facebook.
the class PythonTestDescriptionTest method calculateRuleKey.
private RuleKey calculateRuleKey(BuildRuleResolver ruleResolver, BuildRule rule) {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(new RuleKeyFieldLoader(0), new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(rule.getProjectFilesystem()))), new SourcePathResolver(ruleFinder), ruleFinder);
return ruleKeyFactory.build(rule);
}
use of com.facebook.buck.util.cache.StackedFileHashCache in project buck by facebook.
the class PythonBinaryDescriptionTest method calculateRuleKey.
private RuleKey calculateRuleKey(BuildRuleResolver ruleResolver, BuildRule rule) {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(new RuleKeyFieldLoader(0), new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(rule.getProjectFilesystem()))), new SourcePathResolver(ruleFinder), ruleFinder);
return ruleKeyFactory.build(rule);
}
use of com.facebook.buck.util.cache.StackedFileHashCache in project buck by facebook.
the class BuildInfoRecorderTest method testGetOutputHash.
@Test
public void testGetOutputHash() throws IOException {
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
FileHashCache fileHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
BuildInfoRecorder buildInfoRecorder = createBuildInfoRecorder(filesystem);
byte[] contents = "contents".getBytes();
Path file = Paths.get("file");
filesystem.writeBytesToPath(contents, file);
buildInfoRecorder.recordArtifact(file);
Path dir = Paths.get("dir");
filesystem.mkdirs(dir);
filesystem.writeBytesToPath(contents, dir.resolve("file1"));
filesystem.writeBytesToPath(contents, dir.resolve("file2"));
buildInfoRecorder.recordArtifact(dir);
fileHashCache.invalidateAll();
HashCode current = buildInfoRecorder.getOutputHash(fileHashCache);
// Test that getting the hash again results in the same hashcode.
fileHashCache.invalidateAll();
assertEquals(current, buildInfoRecorder.getOutputHash(fileHashCache));
// Verify that changing a file changes the hash.
filesystem.writeContentsToPath("something else", file);
fileHashCache.invalidateAll();
HashCode updated = buildInfoRecorder.getOutputHash(fileHashCache);
assertNotEquals(current, updated);
// Verify that changing a file under a directory changes the hash.
filesystem.writeContentsToPath("something else", dir.resolve("file1"));
current = updated;
fileHashCache.invalidateAll();
updated = buildInfoRecorder.getOutputHash(fileHashCache);
assertNotEquals(current, updated);
// Test that adding a file updates the hash.
Path added = Paths.get("added");
filesystem.writeBytesToPath(contents, added);
buildInfoRecorder.recordArtifact(added);
current = updated;
fileHashCache.invalidateAll();
updated = buildInfoRecorder.getOutputHash(fileHashCache);
assertNotEquals(current, updated);
// Test that adding a file under a recorded directory updates the hash.
Path addedUnderDir = dir.resolve("added");
filesystem.writeBytesToPath(contents, addedUnderDir);
current = updated;
fileHashCache.invalidateAll();
updated = buildInfoRecorder.getOutputHash(fileHashCache);
assertNotEquals(current, updated);
}
use of com.facebook.buck.util.cache.StackedFileHashCache in project buck by facebook.
the class RuleKeyTest method testRuleKeyDependsOnDeps.
/**
* Ensure that build rules with the same inputs but different deps have unique RuleKeys.
*/
@Test
public void testRuleKeyDependsOnDeps() throws Exception {
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
FileHashCache hashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
BuildRuleResolver ruleResolver1 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
BuildRuleResolver ruleResolver2 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder1 = new SourcePathRuleFinder(ruleResolver1);
DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, hashCache, new SourcePathResolver(ruleFinder1), ruleFinder1);
SourcePathRuleFinder ruleFinder2 = new SourcePathRuleFinder(ruleResolver2);
DefaultRuleKeyFactory ruleKeyFactory2 = new DefaultRuleKeyFactory(0, hashCache, new SourcePathResolver(ruleFinder2), ruleFinder2);
// Create a dependent build rule, //src/com/facebook/buck/cli:common.
JavaLibraryBuilder builder = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//src/com/facebook/buck/cli:common"));
BuildRule commonJavaLibrary = builder.build(ruleResolver1);
builder.build(ruleResolver2);
// Create a java_library() rule with no deps.
Path mainSrc = Paths.get("src/com/facebook/buck/cli/Main.java");
filesystem.mkdirs(mainSrc.getParent());
filesystem.writeContentsToPath("hello", mainSrc);
JavaLibraryBuilder javaLibraryBuilder = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//src/com/facebook/buck/cli:cli")).addSrc(mainSrc);
BuildRule libraryNoCommon = javaLibraryBuilder.build(ruleResolver1, filesystem);
// Create the same java_library() rule, but with a dep on //src/com/facebook/buck/cli:common.
javaLibraryBuilder.addDep(commonJavaLibrary.getBuildTarget());
BuildRule libraryWithCommon = javaLibraryBuilder.build(ruleResolver2, filesystem);
// Assert that the RuleKeys are distinct.
RuleKey r1 = ruleKeyFactory.build(libraryNoCommon);
RuleKey r2 = ruleKeyFactory2.build(libraryWithCommon);
assertThat("Rule keys should be distinct because the deps of the rules are different.", r1, not(equalTo(r2)));
}
Aggregations