Search in sources :

Example 1 with ResultType

use of com.facebook.buck.test.result.type.ResultType in project buck by facebook.

the class GoTest method parseTestResults.

private ImmutableList<TestResultSummary> parseTestResults() throws IOException {
    ImmutableList.Builder<TestResultSummary> summariesBuilder = ImmutableList.builder();
    try (BufferedReader reader = Files.newBufferedReader(getProjectFilesystem().resolve(getPathToTestResults()), Charsets.UTF_8)) {
        Optional<String> currentTest = Optional.empty();
        List<String> stdout = Lists.newArrayList();
        String line;
        while ((line = reader.readLine()) != null) {
            Matcher matcher;
            if ((matcher = TEST_START_PATTERN.matcher(line)).matches()) {
                currentTest = Optional.of(matcher.group("name"));
            } else if ((matcher = TEST_FINISHED_PATTERN.matcher(line)).matches()) {
                if (!currentTest.orElse("").equals(matcher.group("name"))) {
                    throw new RuntimeException(String.format("Error parsing test output: test case end '%s' does not match start '%s'", matcher.group("name"), currentTest.orElse("")));
                }
                ResultType result = ResultType.FAILURE;
                if ("PASS".equals(matcher.group("status"))) {
                    result = ResultType.SUCCESS;
                } else if ("SKIP".equals(matcher.group("status"))) {
                    result = ResultType.ASSUMPTION_VIOLATION;
                }
                double timeTaken = 0.0;
                try {
                    timeTaken = Float.parseFloat(matcher.group("duration"));
                } catch (NumberFormatException ex) {
                    Throwables.throwIfUnchecked(ex);
                }
                summariesBuilder.add(new TestResultSummary("go_test", matcher.group("name"), result, (long) (timeTaken * 1000), "", "", Joiner.on(System.lineSeparator()).join(stdout), ""));
                currentTest = Optional.empty();
                stdout.clear();
            } else {
                stdout.add(line);
            }
        }
        if (currentTest.isPresent()) {
            // This can happen in case of e.g. a panic.
            summariesBuilder.add(new TestResultSummary("go_test", currentTest.get(), ResultType.FAILURE, 0, "incomplete", "", Joiner.on(System.lineSeparator()).join(stdout), ""));
        }
    }
    return summariesBuilder.build();
}
Also used : Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) BufferedReader(java.io.BufferedReader) ResultType(com.facebook.buck.test.result.type.ResultType) TestResultSummary(com.facebook.buck.test.TestResultSummary)

Example 2 with ResultType

use of com.facebook.buck.test.result.type.ResultType in project buck by facebook.

the class SuperConsoleEventBusListener method testSummaryFinished.

@Subscribe
public void testSummaryFinished(TestSummaryEvent.Finished finished) {
    threadsToRunningTestSummaryEvent.put(finished.getThreadId(), Optional.empty());
    TestResultSummary testResult = finished.getTestResultSummary();
    ResultType resultType = testResult.getType();
    switch(resultType) {
        case SUCCESS:
            numPassingTests.incrementAndGet();
            break;
        case FAILURE:
            numFailingTests.incrementAndGet();
            // We don't use TestResultFormatter.reportResultSummary() here since that also
            // includes the stack trace and stdout/stderr.
            logEvents.add(ConsoleEvent.severe(String.format(locale, "%s %s %s: %s", testResult.getType().toString(), testResult.getTestCaseName(), testResult.getTestName(), testResult.getMessage())));
            break;
        case ASSUMPTION_VIOLATION:
            numAssumptionViolationTests.incrementAndGet();
            break;
        case DISABLED:
            numDisabledTests.incrementAndGet();
            break;
        case DRY_RUN:
            numDryRunTests.incrementAndGet();
            break;
        case EXCLUDED:
            numExcludedTests.incrementAndGet();
            break;
    }
}
Also used : ResultType(com.facebook.buck.test.result.type.ResultType) TestResultSummary(com.facebook.buck.test.TestResultSummary) Subscribe(com.google.common.eventbus.Subscribe)

Example 3 with ResultType

use of com.facebook.buck.test.result.type.ResultType in project buck by facebook.

the class XmlTestResultParser method doParse.

private static TestCaseSummary doParse(String xml) throws IOException, SAXException {
    Document doc = XmlDomParser.parse(new InputSource(new StringReader(xml)), /* namespaceAware */
    true);
    Element root = doc.getDocumentElement();
    Preconditions.checkState("testcase".equals(root.getTagName()));
    String testCaseName = root.getAttribute("name");
    NodeList testElements = doc.getElementsByTagName("test");
    List<TestResultSummary> testResults = Lists.newArrayListWithCapacity(testElements.getLength());
    for (int i = 0; i < testElements.getLength(); i++) {
        Element node = (Element) testElements.item(i);
        String testName = node.getAttribute("name");
        long time = Long.parseLong(node.getAttribute("time"));
        String typeString = node.getAttribute("type");
        ResultType type = ResultType.valueOf(typeString);
        String message;
        String stacktrace;
        if (type == ResultType.SUCCESS) {
            message = null;
            stacktrace = null;
        } else {
            message = node.getAttribute("message");
            stacktrace = node.getAttribute("stacktrace");
        }
        NodeList stdoutElements = node.getElementsByTagName("stdout");
        String stdOut;
        if (stdoutElements.getLength() == 1) {
            stdOut = stdoutElements.item(0).getTextContent();
        } else {
            stdOut = null;
        }
        NodeList stderrElements = node.getElementsByTagName("stderr");
        String stdErr;
        if (stderrElements.getLength() == 1) {
            stdErr = stderrElements.item(0).getTextContent();
        } else {
            stdErr = null;
        }
        TestResultSummary testResult = new TestResultSummary(testCaseName, testName, type, time, message, stacktrace, stdOut, stdErr);
        testResults.add(testResult);
    }
    return new TestCaseSummary(testCaseName, testResults);
}
Also used : InputSource(org.xml.sax.InputSource) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) ResultType(com.facebook.buck.test.result.type.ResultType) Document(org.w3c.dom.Document)

Example 4 with ResultType

use of com.facebook.buck.test.result.type.ResultType in project buck by facebook.

the class XmlTestResultParser method getTestResultFromElement.

private static TestResultSummary getTestResultFromElement(Element node, String testCaseName) {
    double time = Float.parseFloat(node.getAttribute("time"));
    String testName = node.getAttribute("name");
    String message = null;
    String stacktrace = null;
    String stdOut = null;
    String stdErr = null;
    ResultType type = ResultType.SUCCESS;
    NodeList failure = node.getElementsByTagName("failure");
    if (failure.getLength() == 1) {
        stacktrace = failure.item(0).getTextContent();
        type = ResultType.FAILURE;
        String[] firstLineParts = stacktrace.split("\n")[0].split(":", 2);
        message = firstLineParts.length > 1 ? firstLineParts[1].trim() : "";
    }
    return new TestResultSummary(testCaseName, testName, type, Math.round(time * 1000), message, stacktrace, stdOut, stdErr);
}
Also used : NodeList(org.w3c.dom.NodeList) ResultType(com.facebook.buck.test.result.type.ResultType)

Example 5 with ResultType

use of com.facebook.buck.test.result.type.ResultType 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

ResultType (com.facebook.buck.test.result.type.ResultType)7 TestResultSummary (com.facebook.buck.test.TestResultSummary)5 NodeList (org.w3c.dom.NodeList)4 ImmutableList (com.google.common.collect.ImmutableList)3 BufferedReader (java.io.BufferedReader)3 Matcher (java.util.regex.Matcher)3 NamedNodeMap (org.w3c.dom.NamedNodeMap)2 Node (org.w3c.dom.Node)2 BuildTarget (com.facebook.buck.model.BuildTarget)1 BuildTargets (com.facebook.buck.model.BuildTargets)1 AbstractBuildRule (com.facebook.buck.rules.AbstractBuildRule)1 AddToRuleKey (com.facebook.buck.rules.AddToRuleKey)1 BinaryBuildRule (com.facebook.buck.rules.BinaryBuildRule)1 BuildContext (com.facebook.buck.rules.BuildContext)1 BuildRule (com.facebook.buck.rules.BuildRule)1 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)1 BuildableContext (com.facebook.buck.rules.BuildableContext)1 ExternalTestRunnerRule (com.facebook.buck.rules.ExternalTestRunnerRule)1 ExternalTestRunnerTestSpec (com.facebook.buck.rules.ExternalTestRunnerTestSpec)1 ForwardingBuildTargetSourcePath (com.facebook.buck.rules.ForwardingBuildTargetSourcePath)1