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>
This commit is contained in:
Christian Stenger
2019-11-06 14:26:40 +01:00
parent e58f376068
commit 333b8f9812
15 changed files with 269 additions and 8 deletions

View File

@@ -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

View File

@@ -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 \

View File

@@ -37,6 +37,8 @@ QtcPlugin {
"autotestconstants.h",
"autotestplugin.cpp",
"autotestplugin.h",
"outputhighlighter.cpp",
"outputhighlighter.h",
"projectsettingswidget.cpp",
"projectsettingswidget.h",
"testcodeparser.cpp",

View File

@@ -59,4 +59,7 @@ enum class TestRunMode
DebugWithoutDeploy,
RunAfterBuild
};
enum class OutputChannel { StdOut, StdErr };
} // namespace Autotest

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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

View File

@@ -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

View File

@@ -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);

View File

@@ -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;

View File

@@ -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

View File

@@ -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);

View File

@@ -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) {