1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtCore>
#include <QtTest>
bool g_testDirectoryBuild = false; // toggle to keep build output for debugging.
QTemporaryDir *g_temporaryDirectory;
QString g_macdeployqtBinary;
QString g_qmakeBinary;
QString g_makeBinary;
QString g_installNameToolBinary;
#if QT_CONFIG(process)
static const QString msgProcessError(const QProcess &process, const QString &what)
{
QString result;
QTextStream(&result) << what << ": \"" << process.program() << ' '
<< process.arguments().join(QLatin1Char(' ')) << "\": " << process.errorString();
return result;
}
static bool runProcess(const QString &binary,
const QStringList &arguments,
QString *errorMessage,
const QString &workingDir = QString(),
const QProcessEnvironment &env = QProcessEnvironment(),
int timeOut = 10000,
QByteArray *stdOut = nullptr, QByteArray *stdErr = nullptr)
{
QProcess process;
if (!env.isEmpty())
process.setProcessEnvironment(env);
if (!workingDir.isEmpty())
process.setWorkingDirectory(workingDir);
process.start(binary, arguments, QIODevice::ReadOnly);
if (!process.waitForStarted()) {
*errorMessage = msgProcessError(process, "Failed to start");
return false;
}
if (!process.waitForFinished(timeOut)) {
*errorMessage = msgProcessError(process, "Timed out");
process.terminate();
if (!process.waitForFinished(300))
process.kill();
return false;
}
if (stdOut)
*stdOut = process.readAllStandardOutput();
if (stdErr)
*stdErr= process.readAllStandardError();
if (process.exitStatus() != QProcess::NormalExit) {
*errorMessage = msgProcessError(process, "Crashed");
return false;
}
if (process.exitCode() != QProcess::NormalExit) {
*errorMessage = msgProcessError(process, "Exit code " + QString::number(process.exitCode()));
return false;
}
return true;
}
#else
static bool runProcess(const QString &binary,
const QStringList &arguments,
QString *arguments,
const QString &workingDir = QString(),
const QProcessEnvironment &env = QProcessEnvironment(),
int timeOut = 5000,
QByteArray *stdOut = Q_NULLPTR, QByteArray *stdErr = Q_NULLPTR)
{
Q_UNUSED(binary);
Q_UNUSED(arguments);
Q_UNUSED(arguments);
Q_UNUSED(workingDir);
Q_UNUSED(env);
Q_UNUSED(timeOut);
Q_UNUSED(stdOut);
Q_UNUSED(stdErr);
return false;
}
#endif
QString sourcePath(const QString &name)
{
return "source_" + name;
}
QString buildPath(const QString &name)
{
if (g_testDirectoryBuild)
return "build_" + name;
return g_temporaryDirectory->path() + "/build_" + name;
}
bool qmake(const QString &source, const QString &destination, QString *errorMessage)
{
QStringList args = QStringList() << source;
return runProcess(g_qmakeBinary, args, errorMessage, destination);
}
bool make(const QString &destination, QString *errorMessage)
{
QStringList args;
return runProcess(g_makeBinary, args, errorMessage, destination);
}
void build(const QString &name)
{
// Build the app or framework according to the convention used
// by this test:
// source_name (source code)
// build_name (build artifacts)
QString source = sourcePath(name);
QString build = buildPath(name);
QString profile = name + ".pro";
QString sourcePath = QFINDTESTDATA(source);
QVERIFY(!sourcePath.isEmpty());
// Clear/set up build dir
QString buildPath = build;
QVERIFY(QDir(buildPath).removeRecursively());
QVERIFY(QDir().mkdir(buildPath));
QVERIFY(QDir(buildPath).exists());
// Build application
QString sourceProFile = QDir(sourcePath).canonicalPath() + '/' + profile;
QString errorMessage;
QVERIFY2(qmake(sourceProFile, buildPath, &errorMessage), qPrintable(errorMessage));
QVERIFY2(make(buildPath, &errorMessage), qPrintable(errorMessage));
}
bool changeInstallName(const QString &path, const QString &binary, const QString &from, const QString &to)
{
QStringList args = QStringList() << binary << "-change" << from << to;
QString errorMessage;
return runProcess(g_installNameToolBinary, args, &errorMessage, path);
}
bool deploy(const QString &name, const QStringList &options, QString *errorMessage)
{
QString bundle = name + ".app";
QString path = buildPath(name);
QStringList args = QStringList() << bundle << options;
return runProcess(g_macdeployqtBinary, args, errorMessage, path);
}
bool debugDeploy(const QString &name, const QStringList &options, QString *errorMessage)
{
QString bundle = name + ".app";
QString path = buildPath(name);
QStringList args = QStringList() << bundle << options << "-verbose=3";
QByteArray stdOut;
QByteArray stdErr;
bool exitOK = runProcess(g_macdeployqtBinary, args, errorMessage, path, QProcessEnvironment(),
10000, &stdOut, &stdErr);
qDebug() << "macdeployqt exit OK" << exitOK;
qDebug() << qPrintable(stdOut);
qDebug() << qPrintable(stdErr);
return exitOK;
}
bool run(const QString &name, QString *errorMessage)
{
QString path = buildPath(name);
QStringList args;
QString binary = name + ".app/Contents/MacOS/" + name;
return runProcess(binary, args, errorMessage, path);
}
bool runPrintLibraries(const QString &name, QString *errorMessage, QByteArray *stdErr)
{
QString binary = name + ".app/Contents/MacOS/" + name;
QString path = buildPath(name);
QStringList args;
QProcessEnvironment env = QProcessEnvironment();
env.insert("DYLD_PRINT_LIBRARIES", "true");
QByteArray stdOut;
return runProcess(binary, args, errorMessage, path, env, 5000, &stdOut, stdErr);
}
void runVerifyDeployment(const QString &name)
{
QString errorMessage;
// Verify that the application runs after deployment and that it loads binaries from
// the application bundle only.
QByteArray libraries;
QVERIFY2(runPrintLibraries(name, &errorMessage, &libraries), qPrintable(errorMessage));
const QList<QString> parts = QString::fromLocal8Bit(libraries).split("dyld: loaded:");
const QString qtPath = QLibraryInfo::location(QLibraryInfo::PrefixPath);
// Let assume Qt is not installed in system
foreach (QString part, parts) {
part = part.trimmed();
if (part.isEmpty())
continue;
QVERIFY(!parts.startsWith(qtPath));
}
}
class tst_macdeployqt : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void cleanupTestCase();
void basicapp();
};
void tst_macdeployqt::initTestCase()
{
#ifdef QT_NO_PROCESS
QSKIP("This test requires QProcess support");
#endif
// Set up test-global unique temporary directory
g_temporaryDirectory = new QTemporaryDir();
QVERIFY(g_temporaryDirectory->isValid());
// Locate build and deployment tools
g_macdeployqtBinary = QStringLiteral(TESTBINDIR "/macdeployqt");
QVERIFY(!g_macdeployqtBinary.isEmpty());
g_qmakeBinary = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmake";
QVERIFY(!g_qmakeBinary.isEmpty());
g_makeBinary = QStandardPaths::findExecutable("make");
QVERIFY(!g_makeBinary.isEmpty());
g_installNameToolBinary = QStandardPaths::findExecutable("install_name_tool");
QVERIFY(!g_installNameToolBinary.isEmpty());
}
void tst_macdeployqt::cleanupTestCase()
{
delete g_temporaryDirectory;
}
// Verify that deployment of a basic Qt Gui application works
void tst_macdeployqt::basicapp()
{
#ifdef QT_NO_PROCESS
QSKIP("This test requires QProcess support");
#endif
QString errorMessage;
QString name = "basicapp";
// Build and verify that the application runs before deployment
build(name);
QVERIFY2(run(name, &errorMessage), qPrintable(errorMessage));
// Deploy application
QVERIFY2(deploy(name, QStringList(), &errorMessage), qPrintable(errorMessage));
// Verify deployment
runVerifyDeployment(name);
}
QTEST_MAIN(tst_macdeployqt)
#include "tst_macdeployqt.moc"
|