summaryrefslogtreecommitdiffstats
path: root/tests/auto/installer/createoffline/tst_createoffline.cpp
blob: 3a343144260ca8529f033b75ef5768245ade3ae0 (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
/**************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
**
** $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 "../shared/packagemanager.h"

#include <packagemanagercore.h>
#include <errors.h>

#include <QFile>
#include <QTest>

using namespace QInstaller;

class tst_CreateOffline : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase()
    {
        // Need to provide a replacement base binary as we are not running actual installer
#ifdef Q_OS_WIN
        m_installerBase = "../../../../bin/installerbase.exe";
#else
        m_installerBase = "../../../../bin/installerbase";
#endif
        if (!QFile(m_installerBase).exists()) {
            QSKIP("No \"installerbase\" binary found in source tree. This can be "
                  "the case if this is an out of sources build or the binaries are "
                  "installed to a location with a different path prefix.");
        }
    }

    void init()
    {
        // Get new target directory for each test function
        m_targetDir = QInstaller::generateTemporaryFileName();
    }

    void cleanup()
    {
        QDir dir(m_targetDir);
        QVERIFY(dir.removeRecursively());
    }

    void testCreateOfflineInstaller_data()
    {
        QTest::addColumn<QString>("repository");
        QTest::addColumn<QString>("component");
        QTest::addColumn<PackageManagerCore::Status>("expectedStatus");
        QTest::newRow("Both metaformats | License")
            << ":///data/repository-bothmeta-license" << "org.qtproject.ifw.example.licenseagreement"
            << PackageManagerCore::Success;
        QTest::newRow("Both metaformats | UserInterface")
            << ":///data/repository-bothmeta-userinteface" << "or.qtproject.ifw.example.openreadme"
            << PackageManagerCore::Success;
        QTest::newRow("Component metaformat | Script")
            << ":///data/repository-componentmeta-script" << "org.qtproject.ifw.example"
            << PackageManagerCore::Success;
        QTest::newRow("Unified metaformat | Script")
            << ":///data/repository-unifiedmeta-script" << "org.qtproject.ifw.example"
            << PackageManagerCore::Success;
        QTest::newRow("Component metaformat | Empty component meta-archive")
            << ":///data/repository-componentmeta-emptymetafile" << "A"
            << PackageManagerCore::Success;
        QTest::newRow("Non-existing component")
            << ":///data/repository-unifiedmeta-script" << "a.dummy.component"
            << PackageManagerCore::Canceled;
        QTest::newRow("Invalid repository")
            << ":///data/repository-invalid" << "a.dummy.component"
            << PackageManagerCore::Failure;
    }

    void testCreateOfflineInstaller()
    {
        QFETCH(QString, repository);
        QFETCH(QString, component);
        QFETCH(PackageManagerCore::Status, expectedStatus);

        PackageManagerCore *core = PackageManager::getPackageManagerWithInit(m_targetDir, repository);
        core->setCommandLineInstance(true);
        core->setOfflineBaseBinary(m_installerBase);
        core->setOfflineBinaryName("ifw_test_offline");
        core->setAutoAcceptLicenses();

        // Replace the custom message handler installed in QInstaller::init() to suppress all output
        qInstallMessageHandler(silentTestMessageHandler);

        QCOMPARE(expectedStatus, core->createOfflineInstaller(QStringList() << component));

        if (expectedStatus == PackageManagerCore::Success) {
#ifdef Q_OS_LINUX
            QVERIFY(QFile::exists(m_targetDir + QDir::separator() + "ifw_test_offline"));
#elif defined Q_OS_MACOS
            QVERIFY(QFile::exists(m_targetDir + QDir::separator() + "ifw_test_offline.dmg"));
#elif defined Q_OS_WIN
            QVERIFY(QFile::exists(m_targetDir + QDir::separator() + "ifw_test_offline.exe"));
#endif
        }
    }

    void testCreateOfflineWithUnstableComponent_data()
    {
        QTest::addColumn<QString>("repository");
        QTest::addColumn<QString>("component");
        QTest::addColumn<bool>("allowUnstable");
        QTest::addColumn<PackageManagerCore::Status>("expectedStatus");
        QTest::newRow("Allow unstable | Missing dependency with selected component")
            << ":///data/repository-missingdependency" << "example.with.unstable.dependency"
            << true << PackageManagerCore::Canceled;
        QTest::newRow("Disallow unstable | Missing dependency with selected component")
            << ":///data/repository-missingdependency" << "example.with.unstable.dependency"
            << false << PackageManagerCore::Failure;
        QTest::newRow("Allow unstable | Missing dependency with other component")
            << ":///data/repository-missingdependency" << "example.without.unstable.dependency"
            << true << PackageManagerCore::Success;
        QTest::newRow("Disallow unstable | Missing dependency with other component")
            << ":///data/repository-missingdependency" << "example.without.unstable.dependency"
            << false << PackageManagerCore::Failure;
    }

    void testCreateOfflineWithUnstableComponent()
    {
        QFETCH(QString, repository);
        QFETCH(QString, component);
        QFETCH(bool, allowUnstable);
        QFETCH(PackageManagerCore::Status, expectedStatus);

        PackageManagerCore *core = PackageManager::getPackageManagerWithInit(m_targetDir, repository);
        core->setCommandLineInstance(true);
        core->setOfflineBaseBinary(m_installerBase);
        core->setOfflineBinaryName("ifw_test_offline");
        core->settings().setAllowUnstableComponents(allowUnstable);
        core->autoAcceptMessageBoxes();

        // Replace the custom message handler installed in QInstaller::init() to suppress all output
        qInstallMessageHandler(silentTestMessageHandler);

        QCOMPARE(expectedStatus, core->createOfflineInstaller(QStringList() << component));
    }

private:
    QString m_targetDir;
    QString m_installerBase;
};

QTEST_GUILESS_MAIN(tst_CreateOffline)

#include "tst_createoffline.moc"