Search in sources :

Example 21 with HashCode

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);
}
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)

Example 22 with HashCode

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();
}
Also used : HashCode(com.google.common.hash.HashCode) BuildTarget(com.facebook.buck.model.BuildTarget) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) TargetGraph(com.facebook.buck.rules.TargetGraph) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 23 with HashCode

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);
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) HashCode(com.google.common.hash.HashCode) BuildTarget(com.facebook.buck.model.BuildTarget) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BroadcastEventListener(com.facebook.buck.event.listener.BroadcastEventListener) DefaultTypeCoercerFactory(com.facebook.buck.rules.coercer.DefaultTypeCoercerFactory) Test(org.junit.Test)

Example 24 with HashCode

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));
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) HashCode(com.google.common.hash.HashCode) BuildTarget(com.facebook.buck.model.BuildTarget) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BroadcastEventListener(com.facebook.buck.event.listener.BroadcastEventListener) DefaultTypeCoercerFactory(com.facebook.buck.rules.coercer.DefaultTypeCoercerFactory) Test(org.junit.Test)

Example 25 with HashCode

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"));
}
Also used : Path(java.nio.file.Path) HashCode(com.google.common.hash.HashCode) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Aggregations

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