File: tst_xdgdirs.cpp

package info (click to toggle)
libqtxdg 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 672 kB
  • sloc: cpp: 5,811; xml: 21; makefile: 11
file content (296 lines) | stat: -rw-r--r-- 10,673 bytes parent folder | download
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
/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * LXQt - a lightweight, Qt based, desktop toolset
 * https://lxqt.org
 *
 * Copyright: 2015 LXQt team
 * Authors:
 *   Luís Pereira <luis.artur.pereira@gmail.com>
 *
 * This program or library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */

#include <QObject>

#include "xdgdirs.h"

#include <QDir>
#include <QFileInfo>
#include <QRandomGenerator>
#include <QTest>
#include <QTemporaryDir>

using namespace Qt::Literals::StringLiterals;

QString randomString(int length)
{
    static const QString characterPool("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"_L1);
    if (length < 0)
        return QString();

    QString randomString;
    for (int i = 0; i < length; ++i) {
        const int index = QRandomGenerator::global()->generate() % characterPool.size();
        randomString.append(characterPool.at(index));
    }
    return randomString;
}

class tst_xdgdirs : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();

    void testDataHome();
    void testConfigHome();
    void testDataDirs();
    void testConfigDirs();
    void testCacheHome();
    void testAutostartHome();
    void testAutostartDirs();
    void testNonWritableLocations();

private:
    void setDefaultLocations();
    void setCustomLocations();
    void setNonWritableLocations();

    QString noSlashPostfix;
    QString postfix;

    QString m_configHome;
    QTemporaryDir m_configHomeTemp;
    QString m_configDirs;
    QTemporaryDir m_configDirsTemp;
    QString m_dataHome;
    QTemporaryDir m_dataHomeTemp;
    QString m_dataDirs;
    QTemporaryDir m_dataDirsTemp;
    QString m_cacheHome;
    QTemporaryDir m_cacheHomeTemp;

    QDir m_nonWritableRoot;
    QString m_configHomeNonWritable;
};

void tst_xdgdirs::initTestCase()
{
    QCoreApplication::instance()->setOrganizationName(QStringLiteral("QtXdg"));
    QCoreApplication::instance()->setApplicationName(QStringLiteral("tst_xdgdirs"));

    noSlashPostfix = QCoreApplication::applicationName();
    postfix = u'/' + QCoreApplication::applicationName();
}

void tst_xdgdirs::cleanupTestCase()
{
    QCoreApplication::instance()->setOrganizationName(QString());
    QCoreApplication::instance()->setApplicationName(QString());

    QFile f(m_nonWritableRoot.path());
    QVERIFY(f.setPermissions(QFile::WriteUser | QFile::WriteOwner));
    QVERIFY(m_nonWritableRoot.removeRecursively());
}

void tst_xdgdirs::setDefaultLocations()
{
    qputenv("XDG_CONFIG_HOME", QByteArray());
    qputenv("XDG_CONFIG_DIRS", QByteArray());
    qputenv("XDG_DATA_HOME", QByteArray());
    qputenv("XDG_DATA_DIRS", QByteArray());
    qputenv("XDG_CACHE_HOME", QByteArray());
}

void tst_xdgdirs::setCustomLocations()
{
    m_configHome = m_configHomeTemp.path();
    m_configDirs = m_configDirsTemp.path();
    m_dataHome = m_dataHomeTemp.path();
    m_dataDirs = m_dataDirsTemp.path();
    m_cacheHome = m_cacheHomeTemp.path();
    qputenv("XDG_CONFIG_HOME", QFile::encodeName(m_configHome));
    qputenv("XDG_CONFIG_DIRS", QFile::encodeName(m_configDirs));
    qputenv("XDG_DATA_HOME", QFile::encodeName(m_dataHome));
    qputenv("XDG_DATA_DIRS", QFile::encodeName(m_dataDirs));
    qputenv("XDG_CACHE_HOME", QFile::encodeName(m_cacheHome));

}

void tst_xdgdirs::setNonWritableLocations()
{
    const QString randomDir = randomString(8);
    m_nonWritableRoot = QDir::temp();

    QVERIFY(m_nonWritableRoot.mkdir(randomDir, QFileDevice::ReadUser));
    m_nonWritableRoot.cd(randomDir);
    m_configHomeNonWritable = m_nonWritableRoot.path() + u'/' + "nonwritabledir"_L1;

    qputenv("XDG_CONFIG_HOME", QFile::encodeName(m_configHomeNonWritable));
    qputenv("XDG_DATA_HOME", QFile::encodeName(m_configHomeNonWritable));
    qputenv("XDG_CACHE_HOME", QFile::encodeName(m_configHomeNonWritable));
}

void tst_xdgdirs::testDataHome()
{
    setDefaultLocations();
    const QString expectedDataHome = QDir::homePath() + QString::fromLatin1("/.local/share");
    QCOMPARE(XdgDirs::dataHome(), expectedDataHome);
    QCOMPARE(XdgDirs::dataHome(false), expectedDataHome);
    QCOMPARE(XdgDirs::dataHome(true), expectedDataHome);

    setCustomLocations();
    QCOMPARE(XdgDirs::dataHome(), m_dataHome);
    QCOMPARE(XdgDirs::dataHome(false), m_dataHome);
}

void tst_xdgdirs::testConfigHome()
{
    setDefaultLocations();
    const QString expectedConfigHome = QDir::homePath() + QString::fromLatin1("/.config");
    QCOMPARE(XdgDirs::configHome(), expectedConfigHome);
    QCOMPARE(XdgDirs::configHome(false), expectedConfigHome);

    setCustomLocations();
    QCOMPARE(XdgDirs::configHome(), m_configHome);
    QCOMPARE(XdgDirs::configHome(false), m_configHome);
}

void tst_xdgdirs::testDataDirs()
{
    setDefaultLocations();
    const QStringList dataDirs = XdgDirs::dataDirs();
    QCOMPARE(dataDirs.count(), 2);
    QCOMPARE(dataDirs.at(0), QString::fromLatin1("/usr/local/share"));
    QCOMPARE(dataDirs.at(1), QString::fromLatin1("/usr/share"));

    const QStringList dataDirsWithPostfix = XdgDirs::dataDirs(postfix);
    QCOMPARE(dataDirsWithPostfix.count(), 2);
    QCOMPARE(dataDirsWithPostfix.at(0), QString::fromLatin1("/usr/local/share") + postfix);
    QCOMPARE(dataDirsWithPostfix.at(1), QString::fromLatin1("/usr/share") + postfix);

    const QStringList dataDirsWithNoSlashPostfix = XdgDirs::dataDirs(noSlashPostfix);
    QCOMPARE(dataDirsWithNoSlashPostfix.count(), 2);
    QCOMPARE(dataDirsWithNoSlashPostfix.at(0), "/usr/local/share"_L1 + u'/' + noSlashPostfix);
    QCOMPARE(dataDirsWithNoSlashPostfix.at(1), "/usr/share"_L1 + u'/' + noSlashPostfix);

    setCustomLocations();
    const QStringList dataDirsCustom = XdgDirs::dataDirs();
    QCOMPARE(dataDirsCustom.count(), 1);
    QCOMPARE(dataDirsCustom.at(0), m_dataDirs);

    const QStringList dataDirsCustomWithPostfix = XdgDirs::dataDirs(postfix);
    QCOMPARE(dataDirsCustomWithPostfix.count(), 1);
    QCOMPARE(dataDirsCustomWithPostfix.at(0), m_dataDirs + postfix);

    const QStringList dataDirsCustomWithNoSlashPostfix;
    QCOMPARE(dataDirsCustomWithPostfix.count(), 1);
    QCOMPARE(dataDirsCustomWithPostfix.at(0), m_dataDirs + u'/' + noSlashPostfix);
}

void tst_xdgdirs::testConfigDirs()
{
    setDefaultLocations();

    const QStringList configDirs = XdgDirs::configDirs();
    QCOMPARE(configDirs.count(), 1);
    QCOMPARE(configDirs.at(0), QString::fromLatin1("/etc/xdg"));

    const QStringList configDirsWithPostfix = XdgDirs::configDirs(postfix);
    QCOMPARE(configDirsWithPostfix.count(), 1);
    QCOMPARE(configDirsWithPostfix.at(0), QString::fromLatin1("/etc/xdg") + postfix);

    setCustomLocations();
    const QStringList configDirsCustom = XdgDirs::configDirs();
    QCOMPARE(configDirsCustom.count(), 1);
    QCOMPARE(configDirsCustom.at(0), m_configDirs);

    const QStringList configDirsCustomWithPostfix = XdgDirs::configDirs(postfix);
    QCOMPARE(configDirsCustomWithPostfix.count(), 1);
    QCOMPARE(configDirsCustomWithPostfix.at(0), m_configDirs + postfix);
}

void tst_xdgdirs::testCacheHome()
{
    setDefaultLocations();
    const QString expectedCacheHome = QDir::homePath() + QStringLiteral("/.cache");
    QCOMPARE(XdgDirs::cacheHome(), expectedCacheHome);
    QCOMPARE(XdgDirs::cacheHome(false), expectedCacheHome);

    setCustomLocations();
    const QString expectedCacheHomeCustom = XdgDirs::cacheHome();
    QCOMPARE(XdgDirs::cacheHome(), expectedCacheHomeCustom);
    QCOMPARE(XdgDirs::cacheHome(false), expectedCacheHomeCustom);

}

void tst_xdgdirs::testAutostartHome()
{
    setDefaultLocations();
    const QString expectedAutoStartHome = QDir::homePath() + QString::fromLatin1("/.config/autostart");
    QCOMPARE(XdgDirs::autostartHome(), expectedAutoStartHome);
    QCOMPARE(XdgDirs::autostartHome(false), expectedAutoStartHome);

    setCustomLocations();
    const QString expectedAutostartHomeCustom = XdgDirs::configHome() + QString::fromLatin1("/autostart");
    QCOMPARE(XdgDirs::autostartHome(), expectedAutostartHomeCustom);
    QCOMPARE(XdgDirs::autostartHome(false), expectedAutostartHomeCustom);
}

void tst_xdgdirs::testAutostartDirs()
{
    setDefaultLocations();
    const QStringList autostartDirs = XdgDirs::autostartDirs();
    QCOMPARE(autostartDirs.count(), 1);
    QCOMPARE(autostartDirs.at(0), QString::fromLatin1("/etc/xdg/autostart"));

    const QStringList autostartDirsWithPostfix = XdgDirs::autostartDirs(postfix);
    QCOMPARE(autostartDirsWithPostfix.count(), 1);
    QCOMPARE(autostartDirsWithPostfix.at(0), QString::fromLatin1("/etc/xdg/autostart") + postfix);

    const QStringList autostartDirsWithNoSlashPostfix = XdgDirs::autostartDirs(noSlashPostfix);
    QCOMPARE(autostartDirsWithNoSlashPostfix.count(), 1);
    QCOMPARE(autostartDirsWithNoSlashPostfix.at(0), "/etc/xdg/autostart"_L1 + u'/' + noSlashPostfix);

    setCustomLocations();
    const QStringList autostartDirsCustom = XdgDirs::autostartDirs();
    QCOMPARE(autostartDirsCustom.count(), 1);
    QCOMPARE(autostartDirsCustom.at(0), m_configDirs + QString::fromLatin1("/autostart"));

    const QStringList autostartDirsCustomWithPostfix = XdgDirs::autostartDirs(postfix);
    QCOMPARE(autostartDirsCustomWithPostfix.count(), 1);
    QCOMPARE(autostartDirsCustomWithPostfix.at(0), m_configDirs + QString::fromLatin1("/autostart") + postfix);

    const QStringList autostartDirsCustomWithNoSlashPostfix = XdgDirs::autostartDirs(noSlashPostfix);
    QCOMPARE(autostartDirsCustomWithNoSlashPostfix.count(), 1);
    QCOMPARE(autostartDirsCustomWithNoSlashPostfix.at(0), m_configDirs + "/autostart"_L1 + u'/' + noSlashPostfix);
}

void tst_xdgdirs::testNonWritableLocations()
{
    setNonWritableLocations();
    QCOMPARE(XdgDirs::configHome(true), QString());
    QCOMPARE(XdgDirs::dataHome(true), QString());
    QCOMPARE(XdgDirs::cacheHome(true), QString());
    QCOMPARE(XdgDirs::autostartHome(true), QString());
}

QTEST_APPLESS_MAIN(tst_xdgdirs)
#include "tst_xdgdirs.moc"