Debian Codegraph @

tests/ut_dstandardpaths.cpp

raw
1 // SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include <gtest/gtest.h>
6 #include <QDir>
7 #include <QList>
8 #include <QProcessEnvironment>
9 #include <QDebug>
10
11 #include "test_helper.hpp"
12 #include "filesystem/dstandardpaths.h"
13
14 DCORE_USE_NAMESPACE
15
16 class ut_DStandardPaths : public testing::Test
17 {
18 protected:
19 void SetUp() override;
20 void TearDown() override;
21 };
22
23 void ut_DStandardPaths::SetUp()
24 {
25 QDir dir("/tmp/etc/");
26 if (!dir.exists())
27 dir.mkdir("/tmp/etc/");
28 DStandardPaths::setMode(DStandardPaths::Snap);
29 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
30 if (env.value("SNAP_USER_COMMON").isEmpty())
31 qputenv("SNAP_USER_COMMON", "/tmp/etc");
32 }
33
34 void ut_DStandardPaths::TearDown()
35 {
36 QDir dir("/tmp/etc/");
37 if (dir.exists())
38 dir.remove("/tmp/etc/");
39 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
40 if (env.value("SNAP_USER_COMMON") == "/tmp/etc")
41 qputenv("SNAP_USER_COMMON", "");
42 DStandardPaths::setMode(DStandardPaths::Auto);
43 }
44
45 TEST_F(ut_DStandardPaths, testDStandardPathsWritableLocation)
46 {
47 QString dir = DStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
48 ASSERT_TRUE(dir == "/tmp/etc");
49
50 DStandardPaths::setMode(DStandardPaths::Test);
51 dir = DStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
52 ASSERT_TRUE(dir == QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
53 }
54
55 TEST_F(ut_DStandardPaths, testDStandardPathsStandardLocations)
56 {
57 QStringList dirs = DStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
58 ASSERT_TRUE(dirs.contains("/tmp/etc"));
59
60 DStandardPaths::setMode(DStandardPaths::Test);
61 dirs = DStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
62 ASSERT_TRUE(dirs == QStandardPaths::standardLocations(QStandardPaths::DesktopLocation));
63 }
64
65 TEST_F(ut_DStandardPaths, testD_Q)
66 {
67 ASSERT_EQ(DStandardPaths::locate(QStandardPaths::ApplicationsLocation, "sh"),
68 QStandardPaths::locate(QStandardPaths::ApplicationsLocation, "sh"));
69
70 ASSERT_EQ(DStandardPaths::locateAll(QStandardPaths::ApplicationsLocation, "sh"),
71 QStandardPaths::locateAll(QStandardPaths::ApplicationsLocation, "sh"));
72
73 ASSERT_EQ(DStandardPaths::findExecutable("sh"), QStandardPaths::findExecutable("sh"));
74 }
75
76 TEST_F(ut_DStandardPaths, homepath)
77 {
78 EnvGuard homeGuard;
79 homeGuard.unset("HOME");
80 auto homePath = DStandardPaths::homePath();
81 ASSERT_EQ(DStandardPaths::homePath(getuid()), homePath);
82
83 homeGuard.set("HOME", "/tmp/home", false);
84 homePath = DStandardPaths::homePath();
85 ASSERT_EQ("/tmp/home", homePath);
86 homeGuard.restore();
87 }
88
89 TEST_F(ut_DStandardPaths, xdgpath)
90 {
91 auto testFunc = [](DStandardPaths::XDG type, const QLatin1String &env, const QByteArray &sufix,
92 const QString &prefix = DStandardPaths::homePath()) {
93 EnvGuard guard;
94 guard.unset(env.data());
95 QString path = DStandardPaths::path(type);
96 const QString &localpath = prefix + sufix;
97 ASSERT_EQ(path, localpath);
98
99 QByteArray custom = QByteArray("/tmp/home") + sufix;
100 guard.set(env.data(), custom, false);
101 qputenv(env.data(), custom.data());
102 path = DStandardPaths::path(type);
103 ASSERT_EQ(path, custom);
104 guard.restore();
105 };
106
107 testFunc(DStandardPaths::XDG::DataHome, QLatin1String("XDG_DATA_HOME"), "/.local/share");
108 testFunc(DStandardPaths::XDG::CacheHome, QLatin1String("XDG_CACHE_HOME"), "/.cache");
109 testFunc(DStandardPaths::XDG::ConfigHome, QLatin1String("XDG_CONFIG_HOME"), "/.config");
110 testFunc(DStandardPaths::XDG::RuntimeDir, QLatin1String("XDG_RUNTIME_DIR"), QByteArray::number(getuid()), "/run/user/");
111 }
112
113 TEST_F(ut_DStandardPaths, dsgpath)
114 {
115 {
116 EnvGuard guard;
117 guard.unset("DSG_APP_DATA");
118 QString path = DStandardPaths::path(DStandardPaths::DSG::AppData);
119 ASSERT_EQ(path, QString());
120
121 guard.set("DSG_APP_DATA", "/tmp/dsg");
122 path = DStandardPaths::path(DStandardPaths::DSG::AppData);
123 ASSERT_EQ(path, "/tmp/dsg");
124 guard.restore();
125 }
126 {
127 EnvGuard guard;
128 guard.unset("DSG_DATA_DIRS");
129 QString path = DStandardPaths::path(DStandardPaths::DSG::DataDir);
130 ASSERT_EQ(path, QString(PREFIX"/share/dsg"));
131
132 guard.set("DSG_DATA_DIRS", "/tmp/dsg");
133 path = DStandardPaths::path(DStandardPaths::DSG::DataDir);
134 ASSERT_EQ(path, "/tmp/dsg");
135 guard.restore();
136 }
137 }
138
139 TEST_F(ut_DStandardPaths, filepath)
140 {
141 {
142 EnvGuard guard;
143 guard.unset("DSG_APP_DATA");
144 QString path = DStandardPaths::filePath(DStandardPaths::DSG::AppData, "filename");
145 ASSERT_EQ(path, QString());
146
147 guard.set("DSG_APP_DATA", "/tmp/dsg");
148 path = DStandardPaths::filePath(DStandardPaths::DSG::AppData, "filename");
149 ASSERT_EQ(path, "/tmp/dsg/filename");
150 guard.restore();
151 }
152
153 QString path = DStandardPaths::filePath(DStandardPaths::XDG::CacheHome, "filename");
154 ASSERT_EQ(path, DStandardPaths::path(DStandardPaths::XDG::CacheHome).append("/filename"));
155 }