diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2024-03-22 18:07:49 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2024-04-14 08:07:48 +0200 |
commit | 9fd895e3c1c0472a2b43c7935a9c3ae168ab7080 (patch) | |
tree | af05b12891aeca3e3a05912eb30018136884951a | |
parent | 4183fa4c0b75a76fbc687298f4513971a9230152 (diff) |
tst_moc: handle QFile::open failures from non-test functions
Code that sets a test function up should not be fallible, or should be
designed to log the failure. In tst_moc this is not the case --
QFile::open is called from within a nested lambda. If that fails, print
the error and return an empty optional.
Task-number: QTBUG-123623
Change-Id: I8372a6ac7dc22e68d145fa2215fe04ba682ac81b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | tests/auto/tools/moc/tst_moc.cpp | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp index a671d92dad..f450cc5696 100644 --- a/tests/auto/tools/moc/tst_moc.cpp +++ b/tests/auto/tools/moc/tst_moc.cpp @@ -5,6 +5,7 @@ #include <QTest> #include <QSignalSpy> #include <stdio.h> +#include <optional> #include <qobject.h> #include <qmetaobject.h> #include <qjsondocument.h> @@ -4251,9 +4252,12 @@ QT_WARNING_POP void tst_Moc::mocJsonOutput() { - const auto readFile = [](const QString &fileName) { + const auto readFile = [](const QString &fileName) -> std::optional<QJsonDocument> { QFile f(fileName); - f.open(QIODevice::ReadOnly); + if (!f.open(QIODevice::ReadOnly)) { + qWarning() << "Could not open file" << fileName << f.errorString(); + return std::nullopt; + } return QJsonDocument::fromJson(f.readAll()); }; @@ -4269,8 +4273,10 @@ void tst_Moc::mocJsonOutput() QVERIFY2(QFile::exists(actualFile), qPrintable(actualFile)); QVERIFY2(QFile::exists(expectedFile), qPrintable(expectedFile)); - QJsonDocument actualOutput = readFile(actualFile); - QJsonDocument expectedOutput = readFile(expectedFile); + std::optional<QJsonDocument> actualOutput = readFile(actualFile); + QVERIFY(actualOutput); + std::optional<QJsonDocument> expectedOutput = readFile(expectedFile); + QVERIFY(expectedOutput); const auto showPotentialDiff = [](const QJsonDocument &actual, const QJsonDocument &expected) -> QByteArray { #if defined(Q_OS_UNIX) @@ -4305,7 +4311,7 @@ void tst_Moc::mocJsonOutput() #endif }; - QVERIFY2(actualOutput == expectedOutput, showPotentialDiff(actualOutput, expectedOutput).constData()); + QVERIFY2(*actualOutput == *expectedOutput, showPotentialDiff(*actualOutput, *expectedOutput).constData()); } void TestFwdProperties::setProp1(const FwdClass1 &v) |