diff options
author | Edward Welbourne <[email protected]> | 2025-06-06 13:36:57 +0200 |
---|---|---|
committer | Edward Welbourne <[email protected]> | 2025-06-10 20:11:54 +0200 |
commit | 5e9efe45dfe1319b41300b0d3ea7b243ea2c8f73 (patch) | |
tree | 118eb9da07d4df2691c3c1b738677fecbdbacab4 | |
parent | dd5f409c582fbf81219861afcd1e2264db467418 (diff) |
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.10 6.9
Change-Id: I36f02a7dfb195616ce68babedbccc61480935fb9
Reviewed-by: Mitch Curtis <[email protected]>
-rw-r--r-- | src/testlib/qtestlog.cpp | 45 |
1 files changed, 37 insertions, 8 deletions
diff --git a/src/testlib/qtestlog.cpp b/src/testlib/qtestlog.cpp index 3938c9a85f8..560ddd9a640 100644 --- a/src/testlib/qtestlog.cpp +++ b/src/testlib/qtestlog.cpp @@ -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(); } } } |