From 00ef067a2df34742202a1e4d23a7731752c83418 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 17 Jan 2025 10:13:32 -0800 Subject: [PATCH] QMessageLogger: test that QT_FATAL_{WARNINGS,CRITICALS} work Pick-to: 6.8 Change-Id: I8a7f56d67043bae14e10fffdfbaf3060486edbf1 Reviewed-by: Ahmad Samir (cherry picked from commit 1dcc5a6dd28606c7959d0626f74f8c05e112b1fc) Reviewed-by: Marc Mutz --- .../corelib/global/qlogging/CMakeLists.txt | 2 +- .../corelib/global/qlogging/tst_qlogging.cpp | 80 +++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/tests/auto/corelib/global/qlogging/CMakeLists.txt b/tests/auto/corelib/global/qlogging/CMakeLists.txt index bc81d6f58bb..1df23266e51 100644 --- a/tests/auto/corelib/global/qlogging/CMakeLists.txt +++ b/tests/auto/corelib/global/qlogging/CMakeLists.txt @@ -10,7 +10,7 @@ endif() qt_internal_add_executable(qlogging_helper NO_INSTALL OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - SOURCES qlogging_helper.cpp + SOURCES qlogging_helper.cpp ../../../../shared/disablecoredumps.cpp DEFINES QT_MESSAGELOGCONTEXT LIBRARIES Qt::Core) diff --git a/tests/auto/corelib/global/qlogging/tst_qlogging.cpp b/tests/auto/corelib/global/qlogging/tst_qlogging.cpp index 24d994784ce..42e91726f29 100644 --- a/tests/auto/corelib/global/qlogging/tst_qlogging.cpp +++ b/tests/auto/corelib/global/qlogging/tst_qlogging.cpp @@ -11,6 +11,11 @@ #include #include #include +#include + +#ifdef Q_OS_UNIX +# include +#endif class tst_qmessagehandler : public QObject { @@ -38,6 +43,9 @@ private slots: void qMessagePattern(); void setMessagePattern(); + void fatalWarnings_data(); + void fatalWarnings(); + void formatLogMessage_data(); void formatLogMessage(); @@ -917,6 +925,78 @@ void tst_qmessagehandler::setMessagePattern() #endif // QT_CONFIG(process) } +void tst_qmessagehandler::fatalWarnings_data() +{ + QTest::addColumn("varName"); + QTest::addColumn("varValue"); + QTest::addColumn("presentOutput"); + QTest::addColumn("absentOutput"); + + QTest::newRow("QT_FATAL_WARNINGS=1") + << "QT_FATAL_WARNINGS" << "1" + << QByteArray("[warning] qWarning") << QByteArray("[critical] qCritical"); + QTest::newRow("QT_FATAL_CRITICALS=1") + << "QT_FATAL_CRITICALS" << "1" + << QByteArray("[critical] qCritical") << QByteArray("[warning] qDebug with category"); + QTest::newRow("QT_FATAL_WARNINGS=2") + << "QT_FATAL_WARNINGS" << "2" + << QByteArray("[warning] qDebug with category") << QByteArray("[debug] qDebug2"); + +#if !QT_CONFIG(process) + QSKIP("This test requires QProcess support"); +#endif +} + +void tst_qmessagehandler::fatalWarnings() +{ +#ifdef Q_OS_ANDROID + QSKIP("This test is disabled on Android"); +#endif +#if QT_CONFIG(process) + QFETCH(QString, varName); + QFETCH(QString, varValue); + QFETCH(QByteArray, presentOutput); + QFETCH(QByteArray, absentOutput); + + QProcess process; + const QString appExe(backtraceHelperPath()); + + // + // test QT_FATAL_WARNINGS / QT_FATAL_CRITICALS + // + QProcessEnvironment environment = m_baseEnvironment; + environment.insert(varName, varValue); + process.setProcessEnvironment(environment); + + process.start(appExe, {}, QIODevice::Text | QIODevice::ReadWrite); + QVERIFY2(process.waitForStarted(), qPrintable( + QString::fromLatin1("Could not start %1: %2").arg(appExe, process.errorString()))); + process.waitForFinished(); + + QCOMPARE(process.exitStatus(), QProcess::CrashExit); +# ifdef Q_OS_UNIX + QCOMPARE(process.exitCode(), SIGABRT); +# elif defined(Q_CC_MSVC) + // __fastfail produces STATUS_STACK_BUFFER_OVERRUN + QCOMPARE(process.exitCode(), int(0xc0000409)); +# else + // RaiseFastFail produces STATUS_FAIL_FAST_EXCEPTION + QCOMPARE(process.exitCode(), int(0xc0000602)); +# endif + + QList lines = process.readAllStandardError().split('\n'); + auto outputOnError = qScopeGuard([&lines] { + qDebug("Output was:\n"); + for (const QByteArray &line : lines) + qDebug() << line; + }); + + QVERIFY2(lines.contains(presentOutput), presentOutput); + QVERIFY2(!lines.contains(absentOutput), absentOutput); + outputOnError.dismiss(); +#endif // QT_CONFIG(process) +} + Q_DECLARE_METATYPE(QtMsgType) void tst_qmessagehandler::formatLogMessage_data()