summaryrefslogtreecommitdiffstats
path: root/tests/auto/qtchooser/tst_qtchooser.cpp
blob: 8864130048ab77345db3de5a918b34f043a17ff3 (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/****************************************************************************
**
** Copyright (C) 2012 Intel Corporation.
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt tool chooser of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest>

#ifdef Q_OS_WIN
#  include <process.h>
#  define getpid _getpid
#else
#  include <unistd.h>
#endif

#ifdef Q_OS_WIN
#  define LIST_SEP ";"
#  define EXE_SUFFIX ".exe"
#else
#  define LIST_SEP ":"
#  define EXE_SUFFIX ""
#endif

#define VERIFY_NORMAL_EXIT(proc) \
    if (!(proc)) return; \
    QCOMPARE((proc)->readAllStandardError().constData(), ""); \
    QCOMPARE((proc)->exitCode(), 0)

class tst_ToolChooser : public QObject
{
    Q_OBJECT

public:
    enum {
        Copy = 0x1,
        Symlink = 0x2,
        Environment = 0x4,
        CommandLine = 0x8
    };
    QProcessEnvironment testModeEnvironment;
    QString testData;
    QString toolPath;
    QString pathsWithDefault;
    QString tempFileName;
    QString tempFileBaseName;

    tst_ToolChooser();
    inline QProcess *execute(const QStringList &arguments)
    { return execute(arguments, testModeEnvironment); }
    inline QProcess *execute(const QStringList &arguments, const QProcessEnvironment &env)
    { return execute(toolPath, arguments, env); }
    QProcess *execute(const QString &program, const QStringList &arguments, const QProcessEnvironment &env);


public Q_SLOTS:
    void initTestCase();
    void cleanup();

private Q_SLOTS:
    void list();
    void selectTool_data();
    void selectTool();
    void selectQt_data();
    void selectQt();
    void defaultQt_data();
    void defaultQt();
    void passArgs_data();
    void passArgs();
    void install_data();
    void install();
    void install2();
};

tst_ToolChooser::tst_ToolChooser()
    : testModeEnvironment(QProcessEnvironment::systemEnvironment())
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
    testData = QFINDTESTDATA("testdata");
#else
    testData = SRCDIR "testdata";
#endif
    pathsWithDefault = testData + "/config1" LIST_SEP +
            testData + "/config2";
    testModeEnvironment.insert("XDG_CONFIG_HOME", "/dev/null");
    testModeEnvironment.insert("XDG_CONFIG_DIRS", pathsWithDefault);
    testModeEnvironment.insert("QT_SELECT", "4.8");

    pathsWithDefault.prepend(testData + "/default" LIST_SEP);

    toolPath = QCoreApplication::applicationDirPath() + "/../../../src/qtchooser/test/qtchooser" EXE_SUFFIX;

    tempFileBaseName = "tool-" + QString::number(getpid()) + ".exe";
    tempFileName = QDir::currentPath() + "/" + tempFileBaseName;
}

QProcess *tst_ToolChooser::execute(const QString &program, const QStringList &arguments, const QProcessEnvironment &env)
{
    QProcess *proc = new QProcess;
    proc->setProcessEnvironment(env);
    proc->start(program, arguments, QIODevice::ReadOnly | QIODevice::Text);
    if (!proc->waitForFinished()) {
        QTest::qFail("Executing '" + program.toLocal8Bit()
                     + "' failed: " + proc->errorString().toLocal8Bit(),
                     __FILE__, __LINE__);
        delete proc;
        return 0;
    }
    return proc;
}

void tst_ToolChooser::initTestCase()
{
    QVERIFY(QFile::exists(toolPath));
    QVERIFY(!QFile::exists(tempFileName));
}

void tst_ToolChooser::cleanup()
{
    QFile::remove(tempFileName);
}

void tst_ToolChooser::list()
{
    QScopedPointer<QProcess> proc(execute(QStringList() << "-list-versions", testModeEnvironment));
    VERIFY_NORMAL_EXIT(proc);

    QStringList foundVersions;
    while (!proc->atEnd()) {
        QByteArray line = proc->readLine().trimmed();
        QVERIFY(!line.isEmpty());
        QVERIFY(!foundVersions.contains(line));
        foundVersions << line;
    }

    qDebug() << foundVersions;
    QVERIFY(foundVersions.contains("4.8"));
    QVERIFY(foundVersions.contains("5"));
}

void tst_ToolChooser::selectTool_data()
{
    QTest::addColumn<int>("mode");
    QTest::addColumn<QString>("expected");

    QString tempExpected = "/" + tempFileBaseName;
    QTest::newRow("copy") << int(Copy) << tempExpected;
#ifdef Q_OS_UNIX
    QTest::newRow("symlink") << int(Symlink) << tempExpected;
#endif
    QTest::newRow("env") << int(Environment) << "/environ";
    QTest::newRow("cmdline") << int(CommandLine) << "/cmdline";

    // argv[0] overrides everything:
    QTest::newRow("copy+env") << int(Copy | Environment) << tempExpected;
    QTest::newRow("copy+cmdline") << int(Copy | CommandLine) << tempExpected;
    QTest::newRow("copy+env+cmdline") << int(Copy | Environment | CommandLine) << tempExpected;

    // the environment overrides the command-line:
    QTest::newRow("env+cmdline") << int(Environment | CommandLine) << "/environ";
}

void tst_ToolChooser::selectTool()
{
    QFETCH(int, mode);
    QProcessEnvironment env = testModeEnvironment;
    QStringList args;

    QFile source(toolPath);
    if (mode & Symlink) {
#ifndef Q_OS_UNIX
        qFatal("Impossible, cannot happen, you've broken the test!!");
#else
        // even though QTemporaryFile has the file opened, we'll overwrite it with a symlink
        // on Unix, we're allowed to do that
        QVERIFY(source.link(tempFileName));
#endif
    } else if (mode & Copy) {
        QFile tmp(tempFileName);
        QVERIFY(tmp.open(QIODevice::ReadWrite));
        QVERIFY(source.open(QIODevice::ReadOnly));
        tmp.write(source.readAll());
        tmp.setPermissions(QFile::ExeOwner | QFile::ReadOwner | QFile::WriteOwner);
        source.close();
    }

    QString exe = toolPath;
    if (mode & (Copy | Symlink)) {
        QVERIFY(QFile::exists(tempFileName));
        exe = tempFileName;
    }

    if (mode & Environment)
        env.insert("QTCHOOSER_RUNTOOL", "environ");
    if (mode & CommandLine)
        args << "-run-tool=cmdline";

    QScopedPointer<QProcess> proc(execute(exe, args, env));
    VERIFY_NORMAL_EXIT(proc);

    QFETCH(QString, expected);
    QByteArray procstdout = proc->readAllStandardOutput().trimmed();
    QVERIFY2(procstdout.contains(expected.toLatin1()), procstdout);
}

void tst_ToolChooser::selectQt_data()
{
    QTest::addColumn<bool>("useEnv");
    QTest::addColumn<QString>("select");
    QTest::addColumn<QString>("expected");

    QTest::newRow("cmdline-4.8") << false << "4.8" << "correct-4.8";
    QTest::newRow("env-4.8") << true << "4.8" << "correct-4.8";
    QTest::newRow("cmdline-5") << false << "5" << "qt5";
    QTest::newRow("env-5") << true << "5" << "qt5";

    QTest::newRow("cmdline-invalid") << false << "invalid" << QString();
    QTest::newRow("env-invalid") << true << "invalid" << QString();
}

void tst_ToolChooser::selectQt()
{
    QFETCH(bool, useEnv);
    QFETCH(QString, select);
    QFETCH(QString, expected);

    QProcessEnvironment env = testModeEnvironment;
    QStringList args;

    env.remove("QT_SELECT");
    if (useEnv)
        env.insert("QT_SELECT", select);
    else
        args << "-qt=" + select;
    args << "-print-env";

    QScopedPointer<QProcess> proc(execute(args, env));
    QVERIFY(proc);
    if (expected.isEmpty()) {
        // it is supposed to fail
        QCOMPARE(proc->readAllStandardOutput().constData(), "");
        QVERIFY(proc->exitCode() != 0);
        QVERIFY(!proc->readAllStandardError().isEmpty());
    } else {
        QCOMPARE(proc->readAllStandardError().constData(), "");
        QCOMPARE(proc->exitCode(), 0);

        // The first line is QT_SELECT= again
        QByteArray line = proc->readLine().trimmed();
        QVERIFY2(line.startsWith("QT_SELECT="), line);

        // The second line is QTTOOLDIR=
        line = proc->readLine().trimmed();
        QVERIFY2(line.startsWith("QTTOOLDIR="), line);
        QVERIFY2(line.contains(expected.toLatin1()), line);
        QVERIFY2(line.endsWith("tooldir\""), line);

        // The third line is QTLIBDIR=
        line = proc->readLine().trimmed();
        QVERIFY2(line.startsWith("QTLIBDIR="), line);
        QVERIFY2(line.contains(expected.toLatin1()), line);
        QVERIFY2(line.endsWith("libdir\""), line);
    }
}

void tst_ToolChooser::defaultQt_data()
{
    QTest::addColumn<bool>("withDefault");
    QTest::addColumn<QString>("expected");

    QTest::newRow("no-default") << false << QString();
    QTest::newRow("with-default") << true << "default-qt";
}

void tst_ToolChooser::defaultQt()
{
    QFETCH(bool, withDefault);
    QFETCH(QString, expected);

    QProcessEnvironment env = testModeEnvironment;
    env.remove("QT_SELECT");
    if (withDefault)
        env.insert("XDG_CONFIG_DIRS", pathsWithDefault);

    QScopedPointer<QProcess> proc(execute(QStringList() << "-run-tool=qmake", env));
    QVERIFY(proc);
    if (withDefault) {
        QCOMPARE(proc->readAllStandardError().constData(), "");
        QCOMPARE(proc->exitCode(), 0);

        QByteArray procstdout = proc->readAllStandardOutput().trimmed();
        QVERIFY2(procstdout.contains(expected.toLatin1()), procstdout);
    } else {
        // no default, the tool fails
        QVERIFY(proc->exitCode() != 0);
        QVERIFY(!proc->readAllStandardError().isEmpty());
        QByteArray procstdout = proc->readAllStandardOutput().trimmed();
        QVERIFY2(procstdout.isEmpty(), procstdout.constData());
    }
}

void tst_ToolChooser::passArgs_data()
{
    QTest::addColumn<QStringList>("args");
    QTest::addColumn<QStringList>("expected");

    QTest::newRow("empty") << QStringList() << QStringList();
    QTest::newRow("drop1") << (QStringList() << "-qt5") << QStringList();
    QTest::newRow("drop2") << (QStringList() << "-qt5" << "-qt5") << QStringList();

    QTest::newRow("dash-dash") << (QStringList() << "--") << QStringList();
    QTest::newRow("dash-dash-qt5") << (QStringList() << "--" << "-qt5") << (QStringList() << "-qt5");
    QTest::newRow("dash-dash-dash-dash") << (QStringList() << "--" << "--") << (QStringList() << "--");

    QTest::newRow("unknown-opt") << (QStringList() << "-query") << (QStringList() << "-query");
    QTest::newRow("unknown-opt-qt5") << (QStringList() << "-query" << "-qt5") << (QStringList() << "-query" << "-qt5");
    QTest::newRow("non-opt") << (QStringList() << ".") << (QStringList() << ".");
    QTest::newRow("non-opt-qt5") << (QStringList() << "." << "-qt5") << (QStringList() << "." << "-qt5");

    // Since we're running a tool with QTCHOOSER_RUNTOOL set, it should not
    // swallow the -print-env, -list-versions and -run-tool arguments either.
    QTest::newRow("list-versions") << (QStringList() << "-list-versions") << (QStringList() << "-list-versions");
    QTest::newRow("qt5-list-versions") << (QStringList() << "-qt5" << "-list-versions") << (QStringList() << "-list-versions");
    QTest::newRow("print-env") << (QStringList() << "-print-env") << (QStringList() << "-print-env");
    QTest::newRow("qt5-print-env") << (QStringList() << "-qt5" << "-print-env") << (QStringList() << "-print-env");
    QTest::newRow("run-tool") << (QStringList() << "-run-tool=foobar") << (QStringList() << "-run-tool=foobar");
    QTest::newRow("qt5-run-tool") << (QStringList() << "-qt5" << "-run-tool=foobar") << (QStringList() << "-run-tool=foobar");
}

void tst_ToolChooser::passArgs()
{
    QFETCH(QStringList, args);
    QFETCH(QStringList, expected);

    QProcessEnvironment env = testModeEnvironment;
    env.insert("QTCHOOSER_RUNTOOL", "testtool");
    QScopedPointer<QProcess> proc(execute(args, env));
    VERIFY_NORMAL_EXIT(proc);

    // skip the first line of procstdout, as it contains the tool name
    proc->readLine();

    QByteArray procstdout = proc->readAll().trimmed();
    QCOMPARE(QString::fromLocal8Bit(procstdout), expected.join("\n"));
}

void tst_ToolChooser::install_data()
{
    QTest::addColumn<QStringList>("args");
    QTest::addColumn<QString>("expectedName");

    QTest::newRow("missing-name") << QStringList() << QString();
    QTest::newRow("missing-qmake") << (QStringList() << "sdk") << QString();

    QString qmake = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmake";
    QVERIFY(QFile::exists(qmake));

    QStringList baseArgs;
    baseArgs << "5" << qmake;

    QTest::newRow("global-wouldoverwrite") << baseArgs << QString();
    QTest::newRow("local-wouldoverwrite") << (QStringList() << "-local" << baseArgs) << QString();

    baseArgs.prepend("-f");
    QTest::newRow("global-overwrite") << baseArgs << testData + "/config2/qtchooser/5.conf";
    QTest::newRow("local-overwrite") << (QStringList() << "-local" << baseArgs) << "/dev/null/qtchooser/5.conf";

    baseArgs.clear();
    baseArgs << "newname" << qmake;
    QTest::newRow("global-newname") << baseArgs << testData + "/config2/qtchooser/newname.conf";
    QTest::newRow("local-newname") << (QStringList() << "-local" << baseArgs) << "/dev/null/qtchooser/newname.conf";

    // ensure that we find an SDK in a later path, even if we could install on an earlier one
    QTest::newRow("global-wouldoverwrite-later") << (QStringList() << "later" << qmake) << QString();
}

void tst_ToolChooser::install()
{
    QFETCH(QStringList, args);
    QFETCH(QString, expectedName);

    QProcessEnvironment env = testModeEnvironment;
    QScopedPointer<QProcess> proc(execute((QStringList() << "-install") + args, env));
    QVERIFY(!!proc);
    if (expectedName.isEmpty()) {
        QByteArray err = proc->readAllStandardError();
        QVERIFY(!err.isEmpty());
        QVERIFY(proc->exitCode() != 0);
        qDebug() << err.trimmed();
    } else {
        VERIFY_NORMAL_EXIT(proc);

        QByteArray out = proc->readLine();
        QVERIFY(!out.isEmpty());
        QCOMPARE(QString(out).trimmed(), expectedName);

        out = proc->readLine();
        QCOMPARE(QString(out).trimmed(), QLibraryInfo::location(QLibraryInfo::BinariesPath));

        out = proc->readLine();
        QCOMPARE(QString(out).trimmed(), QLibraryInfo::location(QLibraryInfo::LibrariesPath));
    }
}

void tst_ToolChooser::install2()
{
    // verify that the root is not writable by the current user
    QString root = QDir::rootPath();
    {
        QFile f(root + "qtchooser");
        QVERIFY(!f.exists());
        QVERIFY(!f.open(QIODevice::ReadWrite));
    }

    QTemporaryDir tempdir;
    QDir dir(tempdir.path());
    dir.mkdir("/global");
    dir.mkdir("/home");

    QString realToolPath = QCoreApplication::applicationDirPath() + "/../../../src/qtchooser/qtchooser" EXE_SUFFIX;
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    env.remove("XDG_CONFIG_HOME");
    env.insert("XDG_CONFIG_DIRS", tempdir.path() + "/global/etc/xdg" LIST_SEP "/");
    env.insert("HOME", tempdir.path() + "/home");

    QProcess proc;
    proc.setProcessEnvironment(env);
    QString qmake = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmake";
    QString expectedContents = QLibraryInfo::location(QLibraryInfo::BinariesPath) + '\n' +
                               QLibraryInfo::location(QLibraryInfo::LibrariesPath) + '\n';

    // test 1: check that it installs into $HOME and recursively mkdirs
    proc.setProgram(realToolPath);
    proc.setArguments(QStringList() << "-install" << "-local" << "test" << qmake);
    proc.start();
    QVERIFY(proc.waitForFinished());
    VERIFY_NORMAL_EXIT(&proc);

    // find the file it must've created
    {
        QFile f(tempdir.path() + "/home/.config/qtchooser/test.conf");
        QVERIFY2(f.open(QIODevice::ReadOnly), qPrintable(f.errorString()));
        QCOMPARE(f.readAll(), expectedContents.toLocal8Bit());
    }
    QVERIFY(!QFile::exists(tempdir.path() + "/global/etc/xdg/qtchooser/test.conf"));

    // test 2: check that it can create a global override
    proc.setArguments(QStringList() << "-install" << "-f" << "test" << qmake);
    proc.start();
    QVERIFY(proc.waitForFinished());
    VERIFY_NORMAL_EXIT(&proc);

    // find the global file
    {
        QFile f(tempdir.path() + "/global/etc/xdg/qtchooser/test.conf");
        QVERIFY2(f.open(QIODevice::ReadOnly), qPrintable(f.errorString()));
        QCOMPARE(f.readAll(), expectedContents.toLocal8Bit());
    }
}

QTEST_MAIN(tst_ToolChooser)

#include "tst_qtchooser.moc"