Make QTest::failOnWarning() fail also on any message type >= warning

Particularly for the no-parameter case, which fails on any warning, it
makes no sense to not also fail on a critical.

Pick-to: 6.9
Change-Id: I36f02a7dfb195616ce68babedbccc61480935fb9
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
(cherry picked from commit 5e9efe45dfe1319b41300b0d3ea7b243ea2c8f73)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Edward Welbourne 2025-06-06 13:36:57 +02:00 committed by Qt Cherry-pick Bot
parent 1c82a674bb
commit 5fc456bfe2

View File

@ -241,6 +241,18 @@ namespace QTest {
return false;
}
static void handleFatal()
{
/* Right now, we're inside the custom message handler and we're
being qt_message_output in qglobal.cpp. After we return from this
function, it will proceed with calling exit() and abort() and
hence crash. Therefore, we call these logging functions such that
we wrap up nicely, and in particular produce well-formed XML.
*/
QTestLog::leaveTestFunction();
QTestLog::stopLogging();
}
static bool handleFailOnWarning(const QMessageLogContext &context, const QString &message)
{
// failOnWarning can be called multiple times per test function, so let
@ -267,6 +279,26 @@ namespace QTest {
return false;
}
static constexpr bool isWarnOrWorse(QtMsgType type)
{
// ## TODO Inline this once we get to Qt 7 !
#if QT_VERSION_MAJOR == 7 || defined(QT_BOOTSTRAPPED) // To match QtMsgType decl
return type >= QtWarningMsg;
#else
// Until Qt 6, Info was > Fatal :-(
switch (type) {
case QtWarningMsg:
case QtCriticalMsg:
case QtFatalMsg:
return true;
case QtDebugMsg:
case QtInfoMsg:
return false;
}
Q_UNREACHABLE_RETURN(false);
#endif
}
static void messageHandler(QtMsgType type, const QMessageLogContext & context, const QString &message)
{
static QBasicAtomicInt counter = Q_BASIC_ATOMIC_INITIALIZER(QTest::maxWarnings);
@ -286,8 +318,11 @@ namespace QTest {
return;
}
if (type == QtWarningMsg && handleFailOnWarning(context, message))
if (isWarnOrWorse(type) && handleFailOnWarning(context, message)) {
if (type == QtFatalMsg)
handleFatal();
return;
}
if (type != QtFatalMsg) {
if (counter.loadRelaxed() <= 0)
@ -307,14 +342,8 @@ namespace QTest {
logger->addMessage(type, context, message);
if (type == QtFatalMsg) {
/* Right now, we're inside the custom message handler and we're
* being qt_message_output in qglobal.cpp. After we return from
* this function, it will proceed with calling exit() and abort()
* and hence crash. Therefore, we call these logging functions such
* that we wrap up nicely, and in particular produce well-formed XML. */
QTestResult::addFailure("Received a fatal error.", context.file, context.line);
QTestLog::leaveTestFunction();
QTestLog::stopLogging();
handleFatal();
}
}
}