@Test public void shouldSetUpAPrivateLibraryIfGivenAMavenCoordWithoutDeps() throws Exception { resolver.resolve("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")); // Nothing depends on this, so it's not visible assertEquals(ImmutableList.of(), rule.get("visibility")); // And it doesn't depend on anything assertEquals(ImmutableList.of(), rule.get("deps")); }
@Test public void shouldIncludeSourceJarIfOneIsPresent() throws Exception { resolver.resolve("com.example:with-sources:jar:1.0"); Path groupDir = thirdParty.resolve("example"); List<Map<String, Object>> rules = buildFileParser.getAll(groupDir.resolve("BUCK")); Map<String, Object> rule = rules.get(0); assertEquals("with-sources-1.0-sources.jar", rule.get("sourceJar")); }
private ProjectBuildFileParser getBuildFileParser(Cell cell) { Map<Cell, ProjectBuildFileParser> threadLocalParsers = parsers.get(); if (threadLocalParsers.containsKey(cell)) { return threadLocalParsers.get(cell); } final ProjectBuildFileParser parser = cell.createBuildFileParser(marshaller, console, eventBus); parser.setEnableProfiling(enableProfiling); threadLocalParsers.put(cell, parser); closer.register( new Closeable() { @Override public void close() throws IOException { try { parser.close(); } catch (BuildFileParseException | InterruptedException e) { new IOException(e); } } }); return parser; }
@Test public void shouldSetVisibilityOfTargetToGiveDependenciesAccess() throws Exception { resolver.resolve("com.example:with-deps:jar:1.0"); Path exampleDir = thirdPartyRelative.resolve("example"); Map<String, Object> withDeps = buildFileParser.getAll(buckRepoRoot.resolve(exampleDir).resolve("BUCK")).get(0); Path otherDir = thirdPartyRelative.resolve("othercorp"); Map<String, Object> noDeps = buildFileParser.getAll(buckRepoRoot.resolve(otherDir).resolve("BUCK")).get(0); @SuppressWarnings("unchecked") List<String> visibility = (List<String>) noDeps.get("visibility"); assertEquals(1, visibility.size()); assertEquals(ImmutableList.of(String.format("//%s:with-deps", exampleDir)), visibility); assertEquals(ImmutableList.of(), noDeps.get("deps")); assertEquals(ImmutableList.of(), withDeps.get("visibility")); @SuppressWarnings("unchecked") List<String> deps = (List<String>) withDeps.get("deps"); assertEquals(1, deps.size()); assertEquals(ImmutableList.of(String.format("//%s:no-deps", otherDir)), deps); }
/** * @param buildFile the build file to execute to generate build rules if they are not cached. * @param defaultIncludes the files to include before executing the build file. * @return a list of raw build rules generated by executing the build file. */ public List<Map<String, Object>> parseBuildFile( File buildFile, Iterable<String> defaultIncludes, ProjectBuildFileParser buildFileParser) throws BuildFileParseException, BuildTargetException, IOException { Preconditions.checkNotNull(buildFile); Preconditions.checkNotNull(defaultIncludes); Preconditions.checkNotNull(buildFileParser); if (!isCached(buildFile, defaultIncludes)) { if (console.getVerbosity().shouldPrintCommand()) { console .getStdErr() .printf("Parsing %s file: %s\n", BuckConstant.BUILD_RULES_FILE_NAME, buildFile); } parseRawRulesInternal(buildFileParser.getAllRulesAndMetaRules(buildFile.getPath())); } return parsedBuildFiles.get(normalize(buildFile.toPath())); }
/** * Populates the collection of known build targets that this Parser will use to construct a * dependency graph using all build files inside the given project root and returns an optionally * filtered set of build targets. * * @param filesystem The project filesystem. * @param includes A list of files that should be included by each build file. * @param filter if specified, applied to each rule in rules. All matching rules will be included * in the List returned by this method. If filter is null, then this method returns null. * @return The build targets in the project filtered by the given filter. */ public synchronized List<BuildTarget> filterAllTargetsInProject( ProjectFilesystem filesystem, Iterable<String> includes, @Nullable RawRulePredicate filter) throws BuildFileParseException, BuildTargetException, IOException { Preconditions.checkNotNull(filesystem); Preconditions.checkNotNull(includes); if (!projectFilesystem.getProjectRoot().equals(filesystem.getProjectRoot())) { throw new HumanReadableException( String.format( "Unsupported root path change from %s to %s", projectFilesystem.getProjectRoot(), filesystem.getProjectRoot())); } if (!isCacheComplete(includes)) { knownBuildTargets.clear(); parsedBuildFiles.clear(); parseRawRulesInternal( ProjectBuildFileParser.getAllRulesInProject(buildFileParserFactory, includes)); allBuildFilesParsed = true; } return filterTargets(filter); }
@Test public void shouldOmitTargetsInTheSameBuildFileInVisibilityArguments() throws Exception { resolver.resolve("com.example:deps-in-same-project:jar:1.0"); Path exampleDir = thirdPartyRelative.resolve("example"); List<Map<String, Object>> allTargets = buildFileParser.getAll(buckRepoRoot.resolve(exampleDir).resolve("BUCK")); assertEquals(2, allTargets.size()); Map<String, Object> noDeps = null; for (Map<String, Object> target : allTargets) { if ("no-deps".equals(target.get("name"))) { noDeps = target; break; } } assertNotNull(noDeps); // Although the "deps-in-same-project" could be in the visibility param, it doesn't need to be // because it's declared in the same build file. assertEquals(0, ((Collection<?>) noDeps.get("visibility")).size()); }
@AfterClass public static void closeParser() throws BuildFileParseException, InterruptedException { buildFileParser.close(); }