Search in sources :

Example 6 with ProjectBuildFileParser

use of com.facebook.buck.json.ProjectBuildFileParser in project buck by facebook.

the class ProjectBuildFileParserTest method whenSubprocessReturnsNonSyntaxErrorThenExceptionContainsFullStackTrace.

@Test
public void whenSubprocessReturnsNonSyntaxErrorThenExceptionContainsFullStackTrace() throws IOException, BuildFileParseException, InterruptedException {
    // This test depends on unix utilities that don't exist on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    TestProjectBuildFileParserFactory buildFileParserFactory = new TestProjectBuildFileParserFactory(cell.getRoot(), cell.getKnownBuildRuleTypes());
    thrown.expect(BuildFileParseException.class);
    thrown.expectMessage("Parse error for build file foo/BUCK:\n" + "ZeroDivisionError: integer division or modulo by zero\n" + "Call stack:\n" + "  File \"bar/BUCK\", line 42, in lets_divide_by_zero\n" + "    foo / bar\n" + "\n" + "  File \"foo/BUCK\", line 23\n" + "    lets_divide_by_zero()\n" + "\n");
    try (ProjectBuildFileParser buildFileParser = buildFileParserFactory.createNoopParserThatAlwaysReturnsErrorWithException(BuckEventBusFactory.newInstance(new FakeClock(0)), "This is an error", "parser", ImmutableMap.<String, Object>builder().put("type", "ZeroDivisionError").put("value", "integer division or modulo by zero").put("filename", "bar/BUCK").put("lineno", 42).put("offset", 24).put("text", "foo / bar\n").put("traceback", ImmutableList.of(ImmutableMap.of("filename", "bar/BUCK", "line_number", 42, "function_name", "lets_divide_by_zero", "text", "foo / bar\n"), ImmutableMap.of("filename", "foo/BUCK", "line_number", 23, "function_name", "<module>", "text", "lets_divide_by_zero()\n"))).build())) {
        buildFileParser.initIfNeeded();
        buildFileParser.getAllRulesAndMetaRules(Paths.get("foo/BUCK"));
    }
}
Also used : ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) FakeClock(com.facebook.buck.timing.FakeClock) Test(org.junit.Test)

Example 7 with ProjectBuildFileParser

use of com.facebook.buck.json.ProjectBuildFileParser in project buck by facebook.

the class ProjectBuildFileParserTest method whenSubprocessReturnsErrorThenConsoleEventPublished.

@Test
public void whenSubprocessReturnsErrorThenConsoleEventPublished() throws IOException, BuildFileParseException, InterruptedException {
    // This test depends on unix utilities that don't exist on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    TestProjectBuildFileParserFactory buildFileParserFactory = new TestProjectBuildFileParserFactory(cell.getRoot(), cell.getKnownBuildRuleTypes());
    BuckEventBus buckEventBus = BuckEventBusFactory.newInstance(new FakeClock(0));
    final List<ConsoleEvent> consoleEvents = new ArrayList<>();
    class EventListener {

        @Subscribe
        public void onConsoleEvent(ConsoleEvent consoleEvent) {
            consoleEvents.add(consoleEvent);
        }
    }
    EventListener eventListener = new EventListener();
    buckEventBus.register(eventListener);
    try (ProjectBuildFileParser buildFileParser = buildFileParserFactory.createNoopParserThatAlwaysReturnsSuccessWithError(buckEventBus, "This is an error", "parser")) {
        buildFileParser.initIfNeeded();
        buildFileParser.getAllRulesAndMetaRules(Paths.get("foo"));
    }
    assertThat(consoleEvents, Matchers.contains(Matchers.hasToString("Error raised by BUCK file parser: This is an error")));
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) FakeClock(com.facebook.buck.timing.FakeClock) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with ProjectBuildFileParser

use of com.facebook.buck.json.ProjectBuildFileParser in project buck by facebook.

the class JavaSymbolFinder method getTargetsForSourceFiles.

/**
   * For all the possible BUCK files above each of the given source files, parse them to JSON to
   * find the targets that actually include these source files, and return a map of them. We do this
   * over a collection of source files, rather than a single file at a time, because instantiating
   * the BUCK file parser is expensive. (It spawns a Python subprocess.)
   */
private ImmutableMultimap<Path, BuildTarget> getTargetsForSourceFiles(Collection<Path> sourceFilePaths) throws InterruptedException, IOException {
    Map<Path, List<Map<String, Object>>> parsedBuildFiles = Maps.newHashMap();
    ImmutableSetMultimap.Builder<Path, BuildTarget> sourceFileTargetsMultimap = ImmutableSetMultimap.builder();
    try (ProjectBuildFileParser parser = projectBuildFileParserFactory.createParser(marshaller, console, environment, buckEventBus, /* ignoreBuckAutodepsFiles */
    false)) {
        for (Path sourceFile : sourceFilePaths) {
            for (Path buckFile : possibleBuckFilesForSourceFile(sourceFile)) {
                List<Map<String, Object>> rules;
                // Avoid parsing the same BUCK file twice.
                if (parsedBuildFiles.containsKey(buckFile)) {
                    rules = parsedBuildFiles.get(buckFile);
                } else {
                    rules = parser.getAll(buckFile);
                    parsedBuildFiles.put(buckFile, rules);
                }
                for (Map<String, Object> ruleMap : rules) {
                    String type = (String) ruleMap.get(BuckPyFunction.TYPE_PROPERTY_NAME);
                    if (javaRuleTypes.contains(type)) {
                        @SuppressWarnings("unchecked") List<String> srcs = (List<String>) Preconditions.checkNotNull(ruleMap.get("srcs"));
                        if (isSourceFilePathInSrcsList(sourceFile, srcs, buckFile.getParent())) {
                            Path buckFileDir = buckFile.getParent();
                            String baseName = "//" + (buckFileDir != null ? MorePaths.pathWithUnixSeparators(buckFileDir) : "");
                            String shortName = (String) Preconditions.checkNotNull(ruleMap.get("name"));
                            sourceFileTargetsMultimap.put(sourceFile, BuildTarget.builder(projectFilesystem.getRootPath(), baseName, shortName).build());
                        }
                    }
                }
            }
        }
    } catch (BuildFileParseException e) {
        buckEventBus.post(ThrowableConsoleEvent.create(e, "Error while searching for targets."));
    }
    return sourceFileTargetsMultimap.build();
}
Also used : Path(java.nio.file.Path) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 9 with ProjectBuildFileParser

use of com.facebook.buck.json.ProjectBuildFileParser in project buck by facebook.

the class PerBuildState method createBuildFileParser.

private ProjectBuildFileParser createBuildFileParser(Cell cell, boolean ignoreBuckAutodepsFiles) {
    ProjectBuildFileParser parser = cell.createBuildFileParser(this.parser.getMarshaller(), console, eventBus, ignoreBuckAutodepsFiles);
    parser.setEnableProfiling(enableProfiling);
    return parser;
}
Also used : ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser)

Example 10 with ProjectBuildFileParser

use of com.facebook.buck.json.ProjectBuildFileParser in project buck by facebook.

the class ProjectBuildFileParserTest method whenSubprocessReturnsSyntaxErrorInFileBeingParsedThenExceptionContainsFileNameOnce.

@Test
public void whenSubprocessReturnsSyntaxErrorInFileBeingParsedThenExceptionContainsFileNameOnce() throws IOException, BuildFileParseException, InterruptedException {
    // This test depends on unix utilities that don't exist on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    TestProjectBuildFileParserFactory buildFileParserFactory = new TestProjectBuildFileParserFactory(cell.getRoot(), cell.getKnownBuildRuleTypes());
    thrown.expect(BuildFileParseException.class);
    thrown.expectMessage("Parse error for build file foo/BUCK:\n" + "Syntax error on line 23, column 16:\n" + "java_test(name=*@!&#(!@&*()\n" + "               ^");
    try (ProjectBuildFileParser buildFileParser = buildFileParserFactory.createNoopParserThatAlwaysReturnsErrorWithException(BuckEventBusFactory.newInstance(new FakeClock(0)), "This is an error", "parser", ImmutableMap.<String, Object>builder().put("type", "SyntaxError").put("value", "You messed up").put("filename", "foo/BUCK").put("lineno", 23).put("offset", 16).put("text", "java_test(name=*@!&#(!@&*()\n").put("traceback", ImmutableList.of(ImmutableMap.of("filename", "foo/BUCK", "line_number", 23, "function_name", "<module>", "text", "java_test(name=*@!&#(!@&*()\n"))).build())) {
        buildFileParser.initIfNeeded();
        buildFileParser.getAllRulesAndMetaRules(Paths.get("foo/BUCK"));
    }
}
Also used : ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) FakeClock(com.facebook.buck.timing.FakeClock) Test(org.junit.Test)

Aggregations

ProjectBuildFileParser (com.facebook.buck.json.ProjectBuildFileParser)13 Test (org.junit.Test)9 FakeClock (com.facebook.buck.timing.FakeClock)8 BuckEventBus (com.facebook.buck.event.BuckEventBus)5 ArrayList (java.util.ArrayList)5 ConsoleEvent (com.facebook.buck.event.ConsoleEvent)4 WatchmanDiagnosticEvent (com.facebook.buck.io.WatchmanDiagnosticEvent)2 BuildFileParseException (com.facebook.buck.json.BuildFileParseException)2 Path (java.nio.file.Path)2 List (java.util.List)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 BuckPyFunction (com.facebook.buck.rules.BuckPyFunction)1 Cell (com.facebook.buck.rules.Cell)1 ConstructorArgMarshaller (com.facebook.buck.rules.ConstructorArgMarshaller)1 DefaultTypeCoercerFactory (com.facebook.buck.rules.coercer.DefaultTypeCoercerFactory)1 Escaper (com.facebook.buck.util.Escaper)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1