use of com.google.common.hash.HashCode 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);
}
use of com.google.common.hash.HashCode in project buck by facebook.
the class ParserTest method buildTargetGraphAndGetHashCodes.
private ImmutableMap<BuildTarget, HashCode> buildTargetGraphAndGetHashCodes(Parser parser, BuildTarget... buildTargets) throws Exception {
// Build the target graph so we can access the hash code cache.
ImmutableList<BuildTarget> buildTargetsList = ImmutableList.copyOf(buildTargets);
TargetGraph targetGraph = parser.buildTargetGraph(eventBus, cell, false, executorService, buildTargetsList);
ImmutableMap.Builder<BuildTarget, HashCode> toReturn = ImmutableMap.builder();
for (TargetNode<?, ?> node : targetGraph.getNodes()) {
toReturn.put(node.getBuildTarget(), node.getRawInputsHashCode());
}
return toReturn.build();
}
use of com.google.common.hash.HashCode in project buck by facebook.
the class ParserTest method targetWithSourceFileChangesHash.
@Test
public void targetWithSourceFileChangesHash() 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));
BuildTarget fooLibTarget = BuildTarget.builder(cellRoot, "//foo", "lib").build();
HashCode original = buildTargetGraphAndGetHashCodes(parser, fooLibTarget).get(fooLibTarget);
DefaultTypeCoercerFactory typeCoercerFactory = new DefaultTypeCoercerFactory(ObjectMappers.newDefaultInstance());
parser = new Parser(new BroadcastEventListener(), cell.getBuckConfig().getView(ParserConfig.class), typeCoercerFactory, new ConstructorArgMarshaller(typeCoercerFactory));
Path testFooJavaFile = tempDir.newFile("foo/Foo.java");
Files.write(testFooJavaFile, "// Ceci n'est pas une Javafile\n".getBytes(UTF_8));
HashCode updated = buildTargetGraphAndGetHashCodes(parser, fooLibTarget).get(fooLibTarget);
assertNotEquals(original, updated);
}
use of com.google.common.hash.HashCode in project buck by facebook.
the class ParserTest method addingDepToTargetChangesHashOfDependingTargetOnly.
@Test
public void addingDepToTargetChangesHashOfDependingTargetOnly() throws Exception {
tempDir.newFolder("foo");
Path testFooBuckFile = tempDir.newFile("foo/BUCK");
Files.write(testFooBuckFile, ("java_library(name = 'lib', deps = [], visibility=['PUBLIC'])\n" + "java_library(name = 'lib2', deps = [], visibility=['PUBLIC'])\n").getBytes(UTF_8));
BuildTarget fooLibTarget = BuildTarget.builder(cellRoot, "//foo", "lib").build();
BuildTarget fooLib2Target = BuildTarget.builder(cellRoot, "//foo", "lib2").build();
ImmutableMap<BuildTarget, HashCode> hashes = buildTargetGraphAndGetHashCodes(parser, fooLibTarget, fooLib2Target);
HashCode libKey = hashes.get(fooLibTarget);
HashCode lib2Key = hashes.get(fooLib2Target);
DefaultTypeCoercerFactory typeCoercerFactory = new DefaultTypeCoercerFactory(ObjectMappers.newDefaultInstance());
parser = new Parser(new BroadcastEventListener(), cell.getBuckConfig().getView(ParserConfig.class), typeCoercerFactory, new ConstructorArgMarshaller(typeCoercerFactory));
Files.write(testFooBuckFile, ("java_library(name = 'lib', deps = [], visibility=['PUBLIC'])\n" + "java_library(name = 'lib2', deps = [':lib'], visibility=['PUBLIC'])\n").getBytes(UTF_8));
hashes = buildTargetGraphAndGetHashCodes(parser, fooLibTarget, fooLib2Target);
assertEquals(libKey, hashes.get(fooLibTarget));
assertNotEquals(lib2Key, hashes.get(fooLib2Target));
}
use of com.google.common.hash.HashCode in project buck by facebook.
the class ResolverIntegrationTest method shouldSetUpAPrivateLibraryIfGivenAMavenCoordWithoutDeps.
@Test
public void shouldSetUpAPrivateLibraryIfGivenAMavenCoordWithoutDeps() throws Exception {
resolveWithArtifacts("com.example:no-deps:jar:1.0");
Path groupDir = thirdParty.resolve("example");
assertTrue(Files.exists(groupDir));
Path original = repo.resolve("com/example/no-deps/1.0/no-deps-1.0.jar");
HashCode expected = MorePaths.asByteSource(original).hash(Hashing.sha1());
Path jarFile = groupDir.resolve("no-deps-1.0.jar");
HashCode seen = MorePaths.asByteSource(jarFile).hash(Hashing.sha1());
assertEquals(expected, seen);
List<Map<String, Object>> rules = buildFileParser.getAll(groupDir.resolve("BUCK"));
assertEquals(1, rules.size());
Map<String, Object> rule = rules.get(0);
// Name is derived from the project identifier
assertEquals("no-deps", rule.get("name"));
// The binary jar should be set
assertEquals("no-deps-1.0.jar", rule.get("binaryJar"));
// There was no source jar in the repo
assertTrue(rule.containsKey("sourceJar"));
assertNull(rule.get("sourceJar"));
// It's a library that's requested on the CLI, so it gets full visibility.
assertEquals(ImmutableList.of("PUBLIC"), rule.get("visibility"));
// And it doesn't depend on anything
assertNull(rule.get("deps"));
}
Aggregations