From 5c954b89a9bd94b085a49d05dfa25644b2eda1c8 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Tue, 4 Jun 2019 12:07:20 +0200 Subject: Fix qFindTestData with cmake and ninja generator In order for test lib to locate the file requested via QFINDTESTDATA, it needs the build directory of the test (for example $builddir/tests/auto/foo/bar) and __FILE__ expanding to a path to the source relative to this build directory. With ninja, __FILE__ is a path that is always relative to the top-level build directory, not the per-test case one. Therefore the path resolution in testlib fails. To accommodate this, add_qt_test() now always sets QT_TESTCASE_BUILDDIR as well as the newly introduced QT_TESTCASE_SOURCEDIR, which, as an absolute path, removes the need to use __FILE__. Change-Id: I16c2b0001e38162e6da9fdb1a61f4f8ce634fe46 Reviewed-by: Ville Voutilainen --- src/testlib/Qt5TestConfigExtras.cmake.in | 1 - src/testlib/qtestcase.cpp | 22 +++++++++++++++++++--- src/testlib/qtestcase.h | 11 ++++++++--- 3 files changed, 27 insertions(+), 7 deletions(-) delete mode 100644 src/testlib/Qt5TestConfigExtras.cmake.in (limited to 'src') diff --git a/src/testlib/Qt5TestConfigExtras.cmake.in b/src/testlib/Qt5TestConfigExtras.cmake.in deleted file mode 100644 index 86a706ea9d..0000000000 --- a/src/testlib/Qt5TestConfigExtras.cmake.in +++ /dev/null @@ -1 +0,0 @@ -set_property(TARGET @QT_CMAKE_EXPORT_NAMESPACE@::Test APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS QT_TESTCASE_BUILDDIR="${CMAKE_BINARY_DIR}") diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 5faafba38d..26ab61b793 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -2157,7 +2157,8 @@ QSharedPointer QTest::qExtractTestData(const QString &dirName) /*! \internal */ -QString QTest::qFindTestData(const QString& base, const char *file, int line, const char *builddir) +QString QTest::qFindTestData(const QString& base, const char *file, int line, const char *builddir, + const char *sourcedir) { QString found; @@ -2267,6 +2268,20 @@ QString QTest::qFindTestData(const QString& base, const char *file, int line, co } } + // 7. Try the supplied source directory + if (found.isEmpty() && sourcedir) { + const QString candidate = QFile::decodeName(sourcedir) % QLatin1Char('/') % base; + if (QFileInfo::exists(candidate)) { + found = candidate; + } else if (QTestLog::verboseLevel() >= 2) { + QTestLog::info(qPrintable( + QString::fromLatin1("testdata %1 not found in supplied source directory [%2]") + .arg(base, QDir::toNativeSeparators(candidate))), + file, line); + } + } + + if (found.isEmpty()) { QTest::qWarn(qPrintable( QString::fromLatin1("testdata %1 could not be located!").arg(base)), @@ -2282,9 +2297,10 @@ QString QTest::qFindTestData(const QString& base, const char *file, int line, co /*! \internal */ -QString QTest::qFindTestData(const char *base, const char *file, int line, const char *builddir) +QString QTest::qFindTestData(const char *base, const char *file, int line, const char *builddir, + const char *sourcedir) { - return qFindTestData(QFile::decodeName(base), file, line, builddir); + return qFindTestData(QFile::decodeName(base), file, line, builddir, sourcedir); } /*! \internal diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 794283ff78..8136452791 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -213,8 +213,13 @@ do {\ QTest::qWarn(static_cast(msg), __FILE__, __LINE__) #ifdef QT_TESTCASE_BUILDDIR + +#ifndef QT_TESTCASE_SOURCEDIR +#define QT_TESTCASE_SOURCEDIR nullptr +#endif + # define QFINDTESTDATA(basepath)\ - QTest::qFindTestData(basepath, __FILE__, __LINE__, QT_TESTCASE_BUILDDIR) + QTest::qFindTestData(basepath, __FILE__, __LINE__, QT_TESTCASE_BUILDDIR, QT_TESTCASE_SOURCEDIR) #else # define QFINDTESTDATA(basepath)\ QTest::qFindTestData(basepath, __FILE__, __LINE__) @@ -309,8 +314,8 @@ namespace QTest #if QT_CONFIG(temporaryfile) Q_TESTLIB_EXPORT QSharedPointer qExtractTestData(const QString &dirName); #endif - Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = nullptr, int line = 0, const char* builddir = nullptr); - Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = nullptr, int line = 0, const char* builddir = nullptr); + Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = nullptr, int line = 0, const char* builddir = nullptr, const char* sourcedir = nullptr); + Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = nullptr, int line = 0, const char* builddir = nullptr, const char *sourcedir = nullptr); Q_TESTLIB_EXPORT void *qData(const char *tagName, int typeId); Q_TESTLIB_EXPORT void *qGlobalData(const char *tagName, int typeId); -- cgit v1.2.3