1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtCore/QCoreApplication>
31#include <QtTest/QtTest>
32
33#define TESTFILE "testfile"
34
35class FindTestData: public QObject
36{
37 Q_OBJECT
38private slots:
39 void initTestCase();
40 void cleanupTestCase();
41
42 void paths();
43
44private:
45 bool touch(QString const&);
46};
47
48void FindTestData::initTestCase()
49{
50 // verify that our qt.conf is working as expected.
51 QString app_path = QCoreApplication::applicationDirPath();
52 QString install_path = app_path
53#ifdef Q_OS_MAC
54 + "/Contents"
55#endif
56 + "/tests";
57 QVERIFY(QDir("/").mkpath(install_path));
58 QVERIFY(QDir("/").mkpath(install_path + "/findtestdata"));
59 QCOMPARE(QLibraryInfo::location(QLibraryInfo::TestsPath), install_path);
60
61 // make fake source and build directories
62 QVERIFY(QDir("/").mkpath(app_path + "/fakesrc"));
63 QVERIFY(QDir("/").mkpath(app_path + "/fakebuild"));
64}
65
66void FindTestData::cleanupTestCase()
67{
68 QString app_path = QCoreApplication::applicationDirPath();
69 QFile(app_path + "/tests/findtestdata/" TESTFILE).remove();
70 QFile(app_path + "/tests/" TESTFILE).remove();
71 QFile(app_path + "/fakesrc/" TESTFILE).remove();
72 QDir("/").rmpath(dirPath: app_path + "/tests/findtestdata");
73 QDir("/").rmpath(dirPath: app_path + "/fakesrc");
74 QDir("/").rmpath(dirPath: app_path + "/fakebuild");
75}
76
77// Create an empty file at the specified path (or return false)
78bool FindTestData::touch(QString const& path)
79{
80 QFile file(path);
81 if (!file.open(flags: QIODevice::WriteOnly)) {
82 qWarning(msg: "Failed to create %s: %s", qPrintable(path), qPrintable(file.errorString()));
83 return false;
84 }
85 return true;
86}
87
88void FindTestData::paths()
89{
90 // There are three possible locations for the testdata.
91 // In this test, we will put the testdata at all three locations,
92 // then remove it from one location at a time and verify that
93 // QFINDTESTDATA correctly falls back each time.
94
95 // 1. relative to test binary.
96 QString app_path = QCoreApplication::applicationDirPath();
97 QString testfile_path1 = app_path + "/" TESTFILE;
98 QVERIFY(touch(testfile_path1));
99
100 // 2. at the test install path (faked via qt.conf)
101 QString testfile_path2 = app_path
102#ifdef Q_OS_MAC
103 + "/Contents"
104#endif
105 + "/tests/findtestdata/" TESTFILE;
106 QVERIFY(touch(testfile_path2));
107
108 // 3. at the source path (which we will fake later on)
109 QString testfile_path3 = app_path + "/fakesrc/" TESTFILE;
110 QVERIFY(touch(testfile_path3));
111
112 // OK, all testdata created. Verify that they are found in correct priority order.
113
114 QCOMPARE(QFINDTESTDATA(TESTFILE), testfile_path1);
115 QVERIFY(QFile(testfile_path1).remove());
116
117 QCOMPARE(QFINDTESTDATA(TESTFILE), testfile_path2);
118 QVERIFY(QFile(testfile_path2).remove());
119
120 // We cannot reasonably redefine __FILE__, so we call the underlying function instead.
121 // __FILE__ may be absolute or relative path; test both.
122
123 // absolute:
124#if defined(Q_OS_WIN)
125 QCOMPARE(QTest::qFindTestData(TESTFILE, qPrintable(app_path + "/fakesrc/fakefile.cpp"), __LINE__).toLower(), testfile_path3.toLower());
126#else
127 QCOMPARE(QTest::qFindTestData(TESTFILE, qPrintable(app_path + "/fakesrc/fakefile.cpp"), __LINE__), testfile_path3);
128#endif
129 // relative: (pretend that we were compiled within fakebuild directory, pointing at files in ../fakesrc)
130#if defined(Q_OS_WIN)
131 QCOMPARE(QTest::qFindTestData(TESTFILE, "../fakesrc/fakefile.cpp", __LINE__, qPrintable(app_path + "/fakebuild")).toLower(), testfile_path3.toLower());
132#else
133 QCOMPARE(QTest::qFindTestData(TESTFILE, "../fakesrc/fakefile.cpp", __LINE__, qPrintable(app_path + "/fakebuild")), testfile_path3);
134#endif
135 QVERIFY(QFile(testfile_path3).remove());
136
137#if !defined(Q_OS_WIN)
138 struct ChdirOnReturn
139 {
140 ~ChdirOnReturn() { QDir::setCurrent(dir); }
141 QString dir;
142 };
143
144 // When cross-compiling from Windows to a *nix system the __FILE__ path's canonical path is an
145 // empty string, which, when used as a prefix, would cause QFINDTESTDATA to look for files in
146 // root ('/') when trying to look for files relative to the test source.
147 QString usrPath = app_path + "/temp/usr/";
148 QVERIFY(QDir().mkpath(usrPath));
149 {
150 ChdirOnReturn chdirObject{.dir: QDir::currentPath()};
151 QDir::setCurrent(app_path + "/temp");
152 QCOMPARE(QTest::qFindTestData("usr/",
153 "C:\\path\\to\\source\\source.cpp",
154 __LINE__,
155 "C:\\path\\to\\build\\").toLower(),
156 usrPath.toLower());
157 }
158 QVERIFY(QDir().rmpath(usrPath));
159#endif
160
161 // Note, this is expected to generate a warning.
162 // We can't use ignoreMessage, because the warning comes from testlib,
163 // not via a "normal" qWarning. But it's OK, our caller (tst_selftests)
164 // will verify that the warning is printed.
165 QCOMPARE(QFINDTESTDATA(TESTFILE), QString());
166}
167
168QTEST_MAIN(FindTestData)
169#include "findtestdata.moc"
170

source code of qtbase/tests/auto/testlib/selftests/findtestdata/findtestdata.cpp