diff options
author | Christian Stenger <christian.stenger@qt.io> | 2019-11-06 14:26:40 +0100 |
---|---|---|
committer | Christian Stenger <christian.stenger@qt.io> | 2019-11-13 13:09:00 +0000 |
commit | 333b8f98128b7b822e97cc7d51f79485b4c23fb9 (patch) | |
tree | 032f86a57385a915bba6893750ecc264b6523451 /src/plugins/autotest | |
parent | e58f37606803a561ec9470dab049a3618cdb857f (diff) |
AutoTest: Allow colored commandline output
Some test frameworks allow to print their output colorful
to further indicate meanings of messages or test results.
Provide a highlighter for the textual output of the results
and enable this functionality for GTest and Boost UTF.
Keep at least a small backdoor for overwriting this by
the user.
Fixes: QTCREATORBUG-22297
Change-Id: Iddd2b734416de807635d90c6519553081f7372f2
Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/plugins/autotest')
-rw-r--r-- | src/plugins/autotest/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/plugins/autotest/autotest.pro | 2 | ||||
-rw-r--r-- | src/plugins/autotest/autotest.qbs | 2 | ||||
-rw-r--r-- | src/plugins/autotest/autotestconstants.h | 3 | ||||
-rw-r--r-- | src/plugins/autotest/boost/boosttestconfiguration.cpp | 5 | ||||
-rw-r--r-- | src/plugins/autotest/boost/boosttestoutputreader.cpp | 2 | ||||
-rw-r--r-- | src/plugins/autotest/gtest/gtestconfiguration.cpp | 5 | ||||
-rw-r--r-- | src/plugins/autotest/gtest/gtestoutputreader.cpp | 2 | ||||
-rw-r--r-- | src/plugins/autotest/outputhighlighter.cpp | 170 | ||||
-rw-r--r-- | src/plugins/autotest/outputhighlighter.h | 49 | ||||
-rw-r--r-- | src/plugins/autotest/testoutputreader.cpp | 20 | ||||
-rw-r--r-- | src/plugins/autotest/testoutputreader.h | 4 | ||||
-rw-r--r-- | src/plugins/autotest/testresultspane.cpp | 9 | ||||
-rw-r--r-- | src/plugins/autotest/testresultspane.h | 2 | ||||
-rw-r--r-- | src/plugins/autotest/testrunner.cpp | 1 |
15 files changed, 269 insertions, 8 deletions
diff --git a/src/plugins/autotest/CMakeLists.txt b/src/plugins/autotest/CMakeLists.txt index 2c2051647f2..3cd7471812d 100644 --- a/src/plugins/autotest/CMakeLists.txt +++ b/src/plugins/autotest/CMakeLists.txt @@ -36,6 +36,7 @@ add_qtc_plugin(AutoTest itestframework.h itestparser.cpp itestparser.h itestsettingspage.h + outputhighlighter.cpp outputhighlighter.h projectsettingswidget.cpp projectsettingswidget.h qtest/qttest_utils.cpp qtest/qttest_utils.h qtest/qttestconfiguration.cpp qtest/qttestconfiguration.h diff --git a/src/plugins/autotest/autotest.pro b/src/plugins/autotest/autotest.pro index c849d8c4d47..f0f516e0178 100644 --- a/src/plugins/autotest/autotest.pro +++ b/src/plugins/autotest/autotest.pro @@ -8,6 +8,7 @@ DEFINES += AUTOTEST_LIBRARY SOURCES += \ autotestplugin.cpp \ itestparser.cpp \ + outputhighlighter.cpp \ projectsettingswidget.cpp \ testcodeparser.cpp \ testconfiguration.cpp \ @@ -72,6 +73,7 @@ HEADERS += \ itestframework.h \ itestparser.h \ itestsettingspage.h \ + outputhighlighter.h \ projectsettingswidget.h \ testcodeparser.h \ testconfiguration.h \ diff --git a/src/plugins/autotest/autotest.qbs b/src/plugins/autotest/autotest.qbs index 0336ec75eb5..e78ad344c46 100644 --- a/src/plugins/autotest/autotest.qbs +++ b/src/plugins/autotest/autotest.qbs @@ -37,6 +37,8 @@ QtcPlugin { "autotestconstants.h", "autotestplugin.cpp", "autotestplugin.h", + "outputhighlighter.cpp", + "outputhighlighter.h", "projectsettingswidget.cpp", "projectsettingswidget.h", "testcodeparser.cpp", diff --git a/src/plugins/autotest/autotestconstants.h b/src/plugins/autotest/autotestconstants.h index 7bed3cf3466..d3e56824c7d 100644 --- a/src/plugins/autotest/autotestconstants.h +++ b/src/plugins/autotest/autotestconstants.h @@ -59,4 +59,7 @@ enum class TestRunMode DebugWithoutDeploy, RunAfterBuild }; + +enum class OutputChannel { StdOut, StdErr }; + } // namespace Autotest diff --git a/src/plugins/autotest/boost/boosttestconfiguration.cpp b/src/plugins/autotest/boost/boosttestconfiguration.cpp index 276c83bc6dd..e24b45196da 100644 --- a/src/plugins/autotest/boost/boosttestconfiguration.cpp +++ b/src/plugins/autotest/boost/boosttestconfiguration.cpp @@ -57,7 +57,7 @@ static QStringList interfering(InterferingType type) { const QStringList knownInterfering { "log_level", "log_format", "log_sink", "report_level", "report_format", "report_sink", - "output_format", "color_output", "no_color_output", + "output_format", "catch_system_errors", "no_catch_system_errors", "detect_fp_exceptions", "no_detect_fp_exceptions", "detect_memory_leaks", "random", "run_test", @@ -117,7 +117,6 @@ QStringList BoostTestConfiguration::argumentsForTestRunner(QStringList *omitted) QStringList arguments; arguments << "-l" << BoostTestSettings::logLevelToOption(boostSettings->logLevel); arguments << "-r" << BoostTestSettings::reportLevelToOption(boostSettings->reportLevel); - arguments << "--no_color_output"; // ensure that colored output is not used as default if (boostSettings->randomize) arguments << QString("--random=").append(QString::number(boostSettings->seed)); @@ -145,6 +144,8 @@ Utils::Environment BoostTestConfiguration::filteredEnvironment(const Utils::Envi const QStringList interferingEnv = interfering(InterferingType::EnvironmentVariables); Utils::Environment result = original; + if (!result.hasKey("BOOST_TEST_COLOR_OUTPUT")) + result.set("BOOST_TEST_COLOR_OUTPUT", "1"); // use colored output by default for (const QString &key : interferingEnv) result.unset(key); return result; diff --git a/src/plugins/autotest/boost/boosttestoutputreader.cpp b/src/plugins/autotest/boost/boosttestoutputreader.cpp index 26e3f4ee3dc..2fe0a1c9283 100644 --- a/src/plugins/autotest/boost/boosttestoutputreader.cpp +++ b/src/plugins/autotest/boost/boosttestoutputreader.cpp @@ -210,7 +210,7 @@ void BoostTestOutputReader::processOutputLine(const QByteArray &outputLine) "test module \"(.*}\"; see standard output for details"); QString noErrors("*** No errors detected"); - const QString line = QString::fromUtf8(outputLine); + const QString line = removeCommandlineColors(QString::fromUtf8(outputLine)); if (line.trimmed().isEmpty()) return; diff --git a/src/plugins/autotest/gtest/gtestconfiguration.cpp b/src/plugins/autotest/gtest/gtestconfiguration.cpp index 49f151da588..0608c3db721 100644 --- a/src/plugins/autotest/gtest/gtestconfiguration.cpp +++ b/src/plugins/autotest/gtest/gtestconfiguration.cpp @@ -54,7 +54,6 @@ QStringList filterInterfering(const QStringList &provided, QStringList *omitted) "--gtest_stream_result_to=", "--gtest_break_on_failure", "--gtest_throw_on_failure", - "--gtest_color=", "--gtest_print_time=" }; @@ -110,11 +109,13 @@ QStringList GTestConfiguration::argumentsForTestRunner(QStringList *omitted) con Utils::Environment GTestConfiguration::filteredEnvironment(const Utils::Environment &original) const { - const QStringList interfering{"GTEST_FILTER", "GTEST_COLOR", "GTEST_ALSO_RUN_DISABLED_TESTS", + const QStringList interfering{"GTEST_FILTER", "GTEST_ALSO_RUN_DISABLED_TESTS", "GTEST_REPEAT", "GTEST_SHUFFLE", "GTEST_RANDOM_SEED", "GTEST_OUTPUT", "GTEST_BREAK_ON_FAILURE", "GTEST_PRINT_TIME", "GTEST_CATCH_EXCEPTIONS"}; Utils::Environment result = original; + if (!result.hasKey("GTEST_COLOR")) + result.set("GTEST_COLOR", "1"); // use colored output by default for (const QString &key : interfering) result.unset(key); return result; diff --git a/src/plugins/autotest/gtest/gtestoutputreader.cpp b/src/plugins/autotest/gtest/gtestoutputreader.cpp index fd6b63ce1a9..912294ecff6 100644 --- a/src/plugins/autotest/gtest/gtestoutputreader.cpp +++ b/src/plugins/autotest/gtest/gtestoutputreader.cpp @@ -74,7 +74,7 @@ void GTestOutputReader::processOutputLine(const QByteArray &outputLine) static const QRegularExpression iterations("^Repeating all tests " "\\(iteration (\\d+)\\) \\. \\. \\.$"); - const QString line = QString::fromLatin1(outputLine); + const QString line = removeCommandlineColors(QString::fromLatin1(outputLine)); if (line.trimmed().isEmpty()) return; diff --git a/src/plugins/autotest/outputhighlighter.cpp b/src/plugins/autotest/outputhighlighter.cpp new file mode 100644 index 00000000000..727d2d6267b --- /dev/null +++ b/src/plugins/autotest/outputhighlighter.cpp @@ -0,0 +1,170 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "outputhighlighter.h" +#include "testresultspane.h" + +#include <utils/algorithm.h> +#include <utils/theme/theme.h> + +#include <QRegularExpression> +#include <QTextDocument> + +namespace Autotest { +namespace Internal { + +class OutputHighlighterBlockData : public QTextBlockUserData +{ +public: + OutputHighlighterBlockData(const QTextCharFormat &format, OutputChannel channel) + : lastCharFormat(format), outputChannel(channel) {} + + QTextCharFormat lastCharFormat; + OutputChannel outputChannel; +}; + +OutputHighlighter::OutputHighlighter(QTextDocument *parent) + : QSyntaxHighlighter(parent) +{ +} + +static QColor translateCommandlineColor(const QString &cmdlineEscapeString) +{ + const QColor defaultColor = Utils::creatorTheme()->color(Utils::Theme::PaletteWindowText); + if (cmdlineEscapeString.isEmpty()) + return defaultColor; + const QRegularExpression regex("^\\d+(;\\d+)*$"); + QRegularExpressionMatch matcher = regex.match(cmdlineEscapeString); + if (!matcher.hasMatch()) + return defaultColor; + + QList<int> values = Utils::transform(matcher.captured().split(';'), + [](const QString &str) { return str.toInt(); }); + Utils::sort(values); + + bool isBright = false; + for (int value : values) { + if (value < 10) { // special formats (bright/bold, underline,..) + isBright = value == 1; + continue; + } + switch (value) { // so far only foreground + case 30: return isBright ? QColor(76, 76, 76) : QColor(0, 0, 0); + case 31: return isBright ? QColor(205, 0, 0) : QColor(180, 0, 0); + case 32: return isBright ? QColor(0, 205, 0) : QColor(0, 180, 0); + case 33: return isBright ? QColor(205, 205, 0) : QColor(180, 180, 0); + case 34: return isBright ? QColor(30, 140, 255) : QColor(70, 130, 180); + case 35: return isBright ? QColor(205, 0, 205) : QColor(180, 0, 180); + case 36: return isBright ? QColor(0, 205, 205) : QColor(0, 180, 180); + case 37: return isBright ? QColor(255, 255, 255) : QColor(230, 230, 230); + case 39: return defaultColor; // use default color + default: continue; // ignore others like background + } + } + return defaultColor; +} + +void OutputHighlighter::highlightBlock(const QString &text) +{ + struct PositionsAndColors + { + PositionsAndColors(const QRegularExpressionMatch &match) + : start(match.capturedStart()) + , end(match.capturedEnd()) + , length(match.capturedLength()) + , color(translateCommandlineColor(match.captured(1))) + {} + int start; // start of the escape sequence + int end; // end of the escape sequence + int length; // length of the escape sequence + QColor color; // color to be used + }; + + if (text.isEmpty() || currentBlock().userData()) + return; + + auto resultsPane = TestResultsPane::instance(); + OutputChannel channel = resultsPane->channelForBlockNumber(currentBlock().blockNumber()); + + const QRegularExpression pattern("\u001B\\[(.*?)m"); + QTextCursor cursor(currentBlock()); + + QList<PositionsAndColors> escapeSequences; + QRegularExpressionMatchIterator it = pattern.globalMatch(text); + while (it.hasNext()) + escapeSequences.append(PositionsAndColors(it.next())); + + int removed = 0; + const int blockPosition = currentBlock().position(); + + QTextCharFormat modified = startCharFormat(); + cursor.select(QTextCursor::BlockUnderCursor); + cursor.setCharFormat(modified); + + for (int i = 0, max = escapeSequences.length(); i < max; ++i) { + auto seq = escapeSequences.at(i); + + cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor); + if (seq.length > 0) { + // delete color escape sequence + cursor.setPosition(blockPosition + seq.start - removed); + cursor.setPosition(blockPosition + seq.end - removed, QTextCursor::KeepAnchor); + cursor.removeSelectedText(); + removed += seq.length; + } + + // highlight it until next sequence starts or EOL + if (i < max - 1) + cursor.setPosition(blockPosition + escapeSequences.at(i + 1).start - removed, QTextCursor::KeepAnchor); + else + cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); + modified.setForeground(seq.color); + cursor.setCharFormat(modified); + } + + currentBlock().setUserData(new OutputHighlighterBlockData(modified, channel)); +} + +QTextCharFormat OutputHighlighter::startCharFormat() const +{ + QTextBlock current = currentBlock(); + OutputChannel channel = TestResultsPane::instance()->channelForBlockNumber( + current.blockNumber()); + + do { + if (auto data = static_cast<OutputHighlighterBlockData *>(current.userData())) { + if (data->outputChannel == channel) + return data->lastCharFormat; + } + current = current.previous(); + } while (current.isValid()); + + QTextCharFormat format = currentBlock().charFormat(); + format.setForeground(Utils::creatorTheme()->color(Utils::Theme::PaletteWindowText)); + return format; +} + +} // namespace Internal +} // namespace Autotest diff --git a/src/plugins/autotest/outputhighlighter.h b/src/plugins/autotest/outputhighlighter.h new file mode 100644 index 00000000000..b0b1f5b8c68 --- /dev/null +++ b/src/plugins/autotest/outputhighlighter.h @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include <QSyntaxHighlighter> + +#pragma once + +namespace Autotest { + +enum class OutputChannel; + +namespace Internal { + +class OutputHighlighter : public QSyntaxHighlighter +{ +public: + explicit OutputHighlighter(QTextDocument *parent = nullptr); + +protected: + void highlightBlock(const QString &text) override; + +private: + QTextCharFormat startCharFormat() const; +}; + +} // namespace Internal +} // namespace Autotest diff --git a/src/plugins/autotest/testoutputreader.cpp b/src/plugins/autotest/testoutputreader.cpp index 411e384f4f2..d23b779fe51 100644 --- a/src/plugins/autotest/testoutputreader.cpp +++ b/src/plugins/autotest/testoutputreader.cpp @@ -93,6 +93,26 @@ void TestOutputReader::createAndReportResult(const QString &message, ResultType reportResult(result); } +void TestOutputReader::resetCommandlineColor() +{ + emit newOutputLineAvailable("\u001B[m", OutputChannel::StdOut); + emit newOutputLineAvailable("\u001B[m", OutputChannel::StdErr); +} + +QString TestOutputReader::removeCommandlineColors(const QString &original) +{ + static const QRegularExpression pattern("\u001B\\[.*?m"); + QString result = original; + while (!result.isEmpty()) { + QRegularExpressionMatch match = pattern.match(result); + if (match.hasMatch()) + result.remove(match.capturedStart(), match.captured().length()); + else + break; + } + return result; +} + void TestOutputReader::reportResult(const TestResultPtr &result) { m_futureInterface.reportResult(result); diff --git a/src/plugins/autotest/testoutputreader.h b/src/plugins/autotest/testoutputreader.h index 6e8409604a2..bd065eaf76f 100644 --- a/src/plugins/autotest/testoutputreader.h +++ b/src/plugins/autotest/testoutputreader.h @@ -34,8 +34,6 @@ namespace Autotest { -enum class OutputChannel { StdOut, StdErr }; - class TestOutputReader : public QObject { Q_OBJECT @@ -54,9 +52,11 @@ public: void setId(const QString &id) { m_id = id; } QString id() const { return m_id; } + void resetCommandlineColor(); signals: void newOutputLineAvailable(const QByteArray &outputLine, OutputChannel channel); protected: + QString removeCommandlineColors(const QString &original); virtual void processOutputLine(const QByteArray &outputLine) = 0; virtual TestResultPtr createDefaultResult() const = 0; diff --git a/src/plugins/autotest/testresultspane.cpp b/src/plugins/autotest/testresultspane.cpp index 0ea36be5d2a..234c521b87e 100644 --- a/src/plugins/autotest/testresultspane.cpp +++ b/src/plugins/autotest/testresultspane.cpp @@ -34,6 +34,7 @@ #include "testcodeparser.h" #include "testeditormark.h" #include "testoutputreader.h" +#include "outputhighlighter.h" #include <aggregation/aggregate.h> #include <coreplugin/actionmanager/actionmanager.h> @@ -135,6 +136,7 @@ TestResultsPane::TestResultsPane(QObject *parent) : m_textOutput->setFont(font); m_textOutput->setWordWrapMode(QTextOption::WordWrap); m_textOutput->setReadOnly(true); + new OutputHighlighter(m_textOutput->document()); m_outputWidget->addWidget(m_textOutput); auto agg = new Aggregation::Aggregate; @@ -707,5 +709,12 @@ void TestResultsPane::showTestResult(const QModelIndex &index) } } +OutputChannel TestResultsPane::channelForBlockNumber(int blockNumber) const +{ + QTC_ASSERT(blockNumber > -1 && blockNumber < m_outputChannels.size(), + return OutputChannel::StdOut); + return m_outputChannels.at(blockNumber); +} + } // namespace Internal } // namespace Autotest diff --git a/src/plugins/autotest/testresultspane.h b/src/plugins/autotest/testresultspane.h index b92463468ac..3372cee06bd 100644 --- a/src/plugins/autotest/testresultspane.h +++ b/src/plugins/autotest/testresultspane.h @@ -98,6 +98,8 @@ public: void addOutputLine(const QByteArray &outputLine, OutputChannel channel); void showTestResult(const QModelIndex &index); + OutputChannel channelForBlockNumber(int blockNumber) const; + private: explicit TestResultsPane(QObject *parent = nullptr); diff --git a/src/plugins/autotest/testrunner.cpp b/src/plugins/autotest/testrunner.cpp index f9488260dd1..3cac44d15d8 100644 --- a/src/plugins/autotest/testrunner.cpp +++ b/src/plugins/autotest/testrunner.cpp @@ -291,6 +291,7 @@ void TestRunner::onProcessFinished() if (m_currentOutputReader->hasSummary()) emit reportSummary(m_currentOutputReader->id(), m_currentOutputReader->summary()); + m_currentOutputReader->resetCommandlineColor(); resetInternalPointers(); if (!m_fakeFutureInterface) { |