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"));
}
}
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")));
}
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();
}
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;
}
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"));
}
}
Aggregations