File: ut_dstandardpaths.cpp

package info (click to toggle)
dtkcore 5.7.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,728 kB
  • sloc: cpp: 22,021; ansic: 183; python: 68; xml: 58; makefile: 27; sh: 15
file content (155 lines) | stat: -rw-r--r-- 5,263 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
// SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <gtest/gtest.h>
#include <QDir>
#include <QList>
#include <QProcessEnvironment>
#include <QDebug>

#include "test_helper.hpp"
#include "filesystem/dstandardpaths.h"

DCORE_USE_NAMESPACE

class ut_DStandardPaths : public testing::Test
{
protected:
    void SetUp() override;
    void TearDown() override;
};

void ut_DStandardPaths::SetUp()
{
    QDir dir("/tmp/etc/");
    if (!dir.exists())
        dir.mkdir("/tmp/etc/");
    DStandardPaths::setMode(DStandardPaths::Snap);
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    if (env.value("SNAP_USER_COMMON").isEmpty())
        qputenv("SNAP_USER_COMMON", "/tmp/etc");
}

void ut_DStandardPaths::TearDown()
{
    QDir dir("/tmp/etc/");
    if (dir.exists())
        dir.remove("/tmp/etc/");
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    if (env.value("SNAP_USER_COMMON") == "/tmp/etc")
        qputenv("SNAP_USER_COMMON", "");
    DStandardPaths::setMode(DStandardPaths::Auto);
}

TEST_F(ut_DStandardPaths, testDStandardPathsWritableLocation)
{
    QString dir = DStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    ASSERT_TRUE(dir == "/tmp/etc");

    DStandardPaths::setMode(DStandardPaths::Test);
    dir = DStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    ASSERT_TRUE(dir == QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
}

TEST_F(ut_DStandardPaths, testDStandardPathsStandardLocations)
{
    QStringList dirs = DStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
    ASSERT_TRUE(dirs.contains("/tmp/etc"));

    DStandardPaths::setMode(DStandardPaths::Test);
    dirs = DStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
    ASSERT_TRUE(dirs == QStandardPaths::standardLocations(QStandardPaths::DesktopLocation));
}

TEST_F(ut_DStandardPaths, testD_Q)
{
    ASSERT_EQ(DStandardPaths::locate(QStandardPaths::ApplicationsLocation, "sh"),
                QStandardPaths::locate(QStandardPaths::ApplicationsLocation, "sh"));

    ASSERT_EQ(DStandardPaths::locateAll(QStandardPaths::ApplicationsLocation, "sh"),
                QStandardPaths::locateAll(QStandardPaths::ApplicationsLocation, "sh"));

    ASSERT_EQ(DStandardPaths::findExecutable("sh"), QStandardPaths::findExecutable("sh"));
}

TEST_F(ut_DStandardPaths, homepath)
{
    EnvGuard homeGuard;
    homeGuard.unset("HOME");
    auto homePath = DStandardPaths::homePath();
    ASSERT_EQ(DStandardPaths::homePath(getuid()), homePath);

    homeGuard.set("HOME", "/tmp/home", false);
    homePath = DStandardPaths::homePath();
    ASSERT_EQ("/tmp/home", homePath);
    homeGuard.restore();
}

TEST_F(ut_DStandardPaths, xdgpath)
{
    auto testFunc = [](DStandardPaths::XDG type, const QLatin1String &env, const QByteArray &sufix,
            const QString &prefix = DStandardPaths::homePath()) {
        EnvGuard guard;
        guard.unset(env.data());
        QString path = DStandardPaths::path(type);
        const QString &localpath = prefix + sufix;
        ASSERT_EQ(path, localpath);

        QByteArray custom = QByteArray("/tmp/home") + sufix;
        guard.set(env.data(), custom, false);
        qputenv(env.data(), custom.data());
        path = DStandardPaths::path(type);
        ASSERT_EQ(path, custom);
        guard.restore();
    };

    testFunc(DStandardPaths::XDG::DataHome, QLatin1String("XDG_DATA_HOME"), "/.local/share");
    testFunc(DStandardPaths::XDG::CacheHome, QLatin1String("XDG_CACHE_HOME"), "/.cache");
    testFunc(DStandardPaths::XDG::ConfigHome, QLatin1String("XDG_CONFIG_HOME"), "/.config");
    testFunc(DStandardPaths::XDG::RuntimeDir, QLatin1String("XDG_RUNTIME_DIR"), QByteArray::number(getuid()), "/run/user/");
}

TEST_F(ut_DStandardPaths, dsgpath)
{
    {
        EnvGuard guard;
        guard.unset("DSG_APP_DATA");
        QString path = DStandardPaths::path(DStandardPaths::DSG::AppData);
        ASSERT_EQ(path, QString());

        guard.set("DSG_APP_DATA", "/tmp/dsg");
        path = DStandardPaths::path(DStandardPaths::DSG::AppData);
        ASSERT_EQ(path, "/tmp/dsg");
        guard.restore();
    }
    {
        EnvGuard guard;
        guard.unset("DSG_DATA_DIRS");
        QString path = DStandardPaths::path(DStandardPaths::DSG::DataDir);
        ASSERT_EQ(path, QString(PREFIX"/share/dsg"));

        guard.set("DSG_DATA_DIRS", "/tmp/dsg");
        path = DStandardPaths::path(DStandardPaths::DSG::DataDir);
        ASSERT_EQ(path, "/tmp/dsg");
        guard.restore();
    }
}

TEST_F(ut_DStandardPaths, filepath)
{
    {
        EnvGuard guard;
        guard.unset("DSG_APP_DATA");
        QString path = DStandardPaths::filePath(DStandardPaths::DSG::AppData, "filename");
        ASSERT_EQ(path, QString());

        guard.set("DSG_APP_DATA", "/tmp/dsg");
        path = DStandardPaths::filePath(DStandardPaths::DSG::AppData, "filename");
        ASSERT_EQ(path, "/tmp/dsg/filename");
        guard.restore();
    }

    QString path = DStandardPaths::filePath(DStandardPaths::XDG::CacheHome, "filename");
    ASSERT_EQ(path, DStandardPaths::path(DStandardPaths::XDG::CacheHome).append("/filename"));
}