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 <ville.voutilainen@qt.io>
This commit is contained in:
Simon Hausmann 2019-06-04 12:07:20 +02:00
parent fae5691c7f
commit 5c954b89a9
4 changed files with 31 additions and 8 deletions

View File

@ -1565,7 +1565,10 @@ function(add_qt_test name)
"${CMAKE_CURRENT_BINARY_DIR}"
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
"${arg_INCLUDE_DIRECTORIES}"
DEFINES "${arg_DEFINES}"
DEFINES
"${arg_DEFINES}"
QT_TESTCASE_BUILDDIR="${CMAKE_CURRENT_BINARY_DIR}"
QT_TESTCASE_SOURCEDIR="${CMAKE_CURRENT_SOURCE_DIR}"
PUBLIC_LIBRARIES ${QT_CMAKE_EXPORT_NAMESPACE}::Core ${QT_CMAKE_EXPORT_NAMESPACE}::Test
LIBRARIES ${arg_LIBRARIES}
COMPILE_OPTIONS ${arg_COMPILE_OPTIONS}

View File

@ -1 +0,0 @@
set_property(TARGET @QT_CMAKE_EXPORT_NAMESPACE@::Test APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS QT_TESTCASE_BUILDDIR="${CMAKE_BINARY_DIR}")

View File

@ -2157,7 +2157,8 @@ QSharedPointer<QTemporaryDir> 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

View File

@ -213,8 +213,13 @@ do {\
QTest::qWarn(static_cast<const char *>(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<QTemporaryDir> 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);