Search in sources :

Example 1 with ChunkAccumulator

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();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) NamedNodeMap(org.w3c.dom.NamedNodeMap) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ResultType(com.facebook.buck.test.result.type.ResultType) Document(org.w3c.dom.Document) TestResultSummary(com.facebook.buck.test.TestResultSummary) SAXException(org.xml.sax.SAXException) BufferedReader(java.io.BufferedReader) ChunkAccumulator(com.facebook.buck.util.ChunkAccumulator)

Aggregations

TestResultSummary (com.facebook.buck.test.TestResultSummary)1 ResultType (com.facebook.buck.test.result.type.ResultType)1 ChunkAccumulator (com.facebook.buck.util.ChunkAccumulator)1 ImmutableList (com.google.common.collect.ImmutableList)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 CharsetDecoder (java.nio.charset.CharsetDecoder)1 Matcher (java.util.regex.Matcher)1 Document (org.w3c.dom.Document)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 SAXException (org.xml.sax.SAXException)1