From 492ca1cbcf10d9acb73def0a4468e0e80fec8eb0 Mon Sep 17 00:00:00 2001
From: Nikolai Kosjar <nikolai.kosjar@digia.com>
Date: Tue, 6 Nov 2012 12:41:05 +0100
Subject: [PATCH] Tests: C++: Make tests run from shadow builds.

For shadow builds some tests couldn't find their test data. Solved by
using the same pattern as in Qt tests: Inject $$PWD as SRCDIR via a
DEFINES statement.

Note: It was considered to use QFINDTESTDATA for Qt5, but this was
rejected due to:
 1) It's Qt5 only (would mean to introduce some wrapper...)
 2) As the doc states, it will not work with QTEST_APPLESS_MAIN tests.

Change-Id: Ie6cf59570fe61725b7f6f83b7da650331ddb38fc
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
---
 tests/auto/cplusplus/cxx11/cxx11.pro                   | 4 ++++
 tests/auto/cplusplus/cxx11/tst_cxx11.cpp               | 3 ++-
 tests/auto/cplusplus/preprocessor/preprocessor.pro     | 5 ++++-
 tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp | 4 ++--
 4 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/tests/auto/cplusplus/cxx11/cxx11.pro b/tests/auto/cplusplus/cxx11/cxx11.pro
index f5525de36b1..410965df4ec 100644
--- a/tests/auto/cplusplus/cxx11/cxx11.pro
+++ b/tests/auto/cplusplus/cxx11/cxx11.pro
@@ -1,5 +1,9 @@
 include(../../qttest.pri)
 include(../shared/shared.pri)
+
+# Inject the source dir for referencing test data from shadow builds.
+DEFINES += SRCDIR=\\\"$$PWD\\\"
+
 SOURCES += tst_cxx11.cpp
 OTHER_FILES += \
     data/inlineNamespace.1.cpp \
diff --git a/tests/auto/cplusplus/cxx11/tst_cxx11.cpp b/tests/auto/cplusplus/cxx11/tst_cxx11.cpp
index 940bfbec17f..31657dc86c2 100644
--- a/tests/auto/cplusplus/cxx11/tst_cxx11.cpp
+++ b/tests/auto/cplusplus/cxx11/tst_cxx11.cpp
@@ -56,7 +56,8 @@ class tst_cxx11: public QObject
     */
     static QString testdata(const QString &name = QString())
     {
-        static const QString dataDirectory = QDir::currentPath() + QLatin1String("/data");
+        static const QString dataDirectory = QLatin1String(SRCDIR "/data");
+
         QString result = dataDirectory;
         if (!name.isEmpty()) {
             result += QLatin1Char('/');
diff --git a/tests/auto/cplusplus/preprocessor/preprocessor.pro b/tests/auto/cplusplus/preprocessor/preprocessor.pro
index 05c463833aa..cee378663ac 100644
--- a/tests/auto/cplusplus/preprocessor/preprocessor.pro
+++ b/tests/auto/cplusplus/preprocessor/preprocessor.pro
@@ -1,7 +1,10 @@
 include(../../qttest.pri)
 include(../shared/shared.pri)
-SOURCES += tst_preprocessor.cpp
 
+# Inject the source dir for referencing test data from shadow builds.
+DEFINES += SRCDIR=\\\"$$PWD\\\"
+
+SOURCES += tst_preprocessor.cpp
 OTHER_FILES = \
     data/noPP.1.cpp \
     data/noPP.2.cpp \
diff --git a/tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp b/tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp
index 9b49ba45ef3..945d0825f5a 100644
--- a/tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp
+++ b/tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp
@@ -39,7 +39,7 @@ using namespace CPlusPlus;
 
 QByteArray loadSource(const QString &fileName)
 {
-    QFile inf(fileName);
+    QFile inf(QLatin1String(SRCDIR) + QLatin1Char('/') + fileName);
     if (!inf.open(QIODevice::ReadOnly | QIODevice::Text)) {
         qDebug("Cannot open \"%s\"", fileName.toUtf8().constData());
         return QByteArray();
@@ -53,7 +53,7 @@ QByteArray loadSource(const QString &fileName)
 
 void saveData(const QByteArray &data, const QString &fileName)
 {
-    QFile inf(fileName);
+    QFile inf(QLatin1String(SRCDIR) + QLatin1Char('/') + fileName);
     if (!inf.open(QIODevice::WriteOnly | QIODevice::Text)) {
         qDebug("Cannot open \"%s\"", fileName.toUtf8().constData());
         return;
-- 
GitLab