use of com.facebook.buck.util.ChunkAccumulator in project buck by facebook.
the class CxxGtestTest method parseResults.
@Override
protected ImmutableList<TestResultSummary> parseResults(Path exitCode, Path output, Path results) throws IOException, SAXException {
// Try to parse the results file first, which should be written if the test suite exited
// normally (even in the event of a failing test). If this fails, just construct a test
// summary with the output we have.
Document doc;
try {
doc = XmlDomParser.parse(getProjectFilesystem().resolve(results));
} catch (SAXException e) {
return ImmutableList.of(getProgramFailureSummary("test program aborted before finishing", getProjectFilesystem().readFileIfItExists(output).orElse("")));
}
ImmutableList.Builder<TestResultSummary> summariesBuilder = ImmutableList.builder();
// It's possible the test output had invalid characters in it's output, so make sure to
// ignore these as we parse the output lines.
Optional<String> currentTest = Optional.empty();
Map<String, ChunkAccumulator> stdout = Maps.newHashMap();
CharsetDecoder decoder = Charsets.UTF_8.newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
try (InputStream input = getProjectFilesystem().newFileInputStream(output);
BufferedReader reader = new BufferedReader(new InputStreamReader(input, decoder))) {
String line;
while ((line = reader.readLine()) != null) {
Matcher matcher;
if ((matcher = START.matcher(line.trim())).matches()) {
String test = matcher.group(1);
currentTest = Optional.of(test);
stdout.put(test, new ChunkAccumulator(Charsets.UTF_8, maxTestOutputSize));
} else if (END.matcher(line.trim()).matches()) {
currentTest = Optional.empty();
} else if (currentTest.isPresent()) {
Preconditions.checkNotNull(stdout.get(currentTest.get())).append(line);
}
}
}
NodeList testcases = doc.getElementsByTagName("testcase");
for (int index = 0; index < testcases.getLength(); index++) {
Node testcase = testcases.item(index);
NamedNodeMap attributes = testcase.getAttributes();
String testCase = attributes.getNamedItem("classname").getNodeValue();
String testName = attributes.getNamedItem("name").getNodeValue();
String testFull = String.format("%s.%s", testCase, testName);
Double time = Double.parseDouble(attributes.getNamedItem("time").getNodeValue()) * 1000;
// Prepare the result message and type
ResultType type = ResultType.SUCCESS;
String message = "";
if (testcase.getChildNodes().getLength() > 0) {
Node failure = testcase.getChildNodes().item(1);
type = ResultType.FAILURE;
message = failure.getAttributes().getNamedItem("message").getNodeValue();
} else if (attributes.getNamedItem("status").getNodeValue().equals(NOTRUN)) {
type = ResultType.ASSUMPTION_VIOLATION;
message = "DISABLED";
}
// Prepare the tests stdout.
String testStdout = "";
if (stdout.containsKey(testFull)) {
testStdout = Joiner.on(System.lineSeparator()).join(stdout.get(testFull).getChunks());
}
summariesBuilder.add(new TestResultSummary(testCase, testName, type, time.longValue(), message, "", testStdout, ""));
}
return summariesBuilder.build();
}
Aggregations