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
|
/*
* SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
*/
#include <fcntl.h>
#include <cstdlib>
#include <set>
#include <string>
#include <vector>
#include "fcitx-utils/environ.h"
#include "fcitx-utils/fs.h"
#include "fcitx-utils/log.h"
#include "fcitx-utils/standardpath.h"
#include "testdir.h"
using namespace fcitx;
#define TEST_ADDON_DIR FCITX5_SOURCE_DIR "/test/addon"
void test_basic() {
setEnvironment("XDG_CONFIG_HOME", "/TEST/PATH");
setEnvironment("XDG_CONFIG_DIRS",
"/TEST/PATH1/:/TEST/PATH2:/TEST/PATH2/:/TEST/PATH1");
setEnvironment("XDG_DATA_DIRS", TEST_ADDON_DIR);
StandardPath standardPath(true);
FCITX_ASSERT(standardPath.userDirectory(StandardPath::Type::Config) ==
"/TEST/PATH");
// The order to the path should be kept for their first appearance.
FCITX_ASSERT(standardPath.directories(StandardPath::Type::Config) ==
std::vector<std::string>({"/TEST/PATH1", "/TEST/PATH2"}));
{
auto result = standardPath.multiOpen(
StandardPath::Type::PkgData, "addon", O_RDONLY,
filter::Not(filter::User()), filter::Suffix(".conf"));
std::set<std::string> names;
std::set<std::string> expect_names = {"testim.conf",
"testfrontend.conf"};
for (auto &p : result) {
names.insert(p.first);
FCITX_ASSERT(p.second.fd() >= 0);
}
FCITX_ASSERT(names == expect_names);
}
{
auto result = standardPath.locate(StandardPath::Type::PkgData, "addon",
filter::Not(filter::User()),
filter::Suffix(".conf"));
std::set<std::string> names;
std::set<std::string> expect_names = {"testim.conf",
"testfrontend.conf"};
for (auto &p : result) {
names.insert(p.first);
}
FCITX_ASSERT(names == expect_names);
}
{
auto result = standardPath.multiOpen(
StandardPath::Type::PkgData, "addon", O_RDONLY,
filter::Not(filter::User()), filter::Suffix("im.conf"));
std::set<std::string> names;
std::set<std::string> expect_names = {"testim.conf"};
for (auto &p : result) {
names.insert(p.first);
FCITX_ASSERT(p.second.fd() >= 0);
}
FCITX_ASSERT(names == expect_names);
}
auto file = standardPath.open(StandardPath::Type::PkgData,
"addon/testim.conf", O_RDONLY);
FCITX_ASSERT(file.path() ==
fs::cleanPath(TEST_ADDON_DIR "/fcitx5/addon/testim.conf"));
auto file2 = standardPath.open(StandardPath::Type::Data,
"fcitx5/addon/testim2.conf", O_RDONLY);
FCITX_ASSERT(file2.fd() == -1);
}
void test_nouser() {
setEnvironment("XDG_CONFIG_HOME", "/TEST/PATH");
setEnvironment("XDG_CONFIG_DIRS", "/TEST/PATH1:/TEST/PATH2");
setEnvironment("XDG_DATA_DIRS", TEST_ADDON_DIR);
StandardPath standardPath(true, true);
FCITX_ASSERT(
standardPath.userDirectory(StandardPath::Type::Config).empty());
FCITX_ASSERT(standardPath.directories(StandardPath::Type::Config) ==
std::vector<std::string>({"/TEST/PATH1", "/TEST/PATH2"}));
{
auto result = standardPath.multiOpen(
StandardPath::Type::PkgData, "addon", O_RDONLY,
filter::Not(filter::User()), filter::Suffix(".conf"));
std::set<std::string> names;
std::set<std::string> expect_names = {"testim.conf",
"testfrontend.conf"};
for (auto &p : result) {
names.insert(p.first);
FCITX_ASSERT(p.second.fd() >= 0);
}
FCITX_ASSERT(names == expect_names);
}
{
auto result = standardPath.multiOpen(
StandardPath::Type::PkgData, "addon", O_RDONLY,
filter::Not(filter::User()), filter::Suffix("im.conf"));
std::set<std::string> names;
std::set<std::string> expect_names = {"testim.conf"};
for (auto &p : result) {
names.insert(p.first);
FCITX_ASSERT(p.second.fd() >= 0);
}
FCITX_ASSERT(names == expect_names);
}
auto file = standardPath.open(StandardPath::Type::PkgData,
"addon/testim.conf", O_RDONLY);
FCITX_ASSERT(file.path() ==
fs::cleanPath(TEST_ADDON_DIR "/fcitx5/addon/testim.conf"));
auto file2 = standardPath.open(StandardPath::Type::Data,
"fcitx5/addon/testim2.conf", O_RDONLY);
FCITX_ASSERT(file2.fd() == -1);
}
void test_custom() {
setEnvironment("XDG_CONFIG_HOME", "/TEST/PATH");
setEnvironment("XDG_CONFIG_DIRS", "/TEST/PATH1:/TEST/PATH2");
setEnvironment("XDG_DATA_DIRS", TEST_ADDON_DIR);
StandardPath path("mypackage", {{"datadir", "/TEST/PATH3"}}, false, false);
FCITX_ASSERT(path.directories(fcitx::StandardPath::Type::PkgConfig) ==
std::vector<std::string>{"/TEST/PATH1/mypackage",
"/TEST/PATH2/mypackage"});
FCITX_ASSERT(path.directories(fcitx::StandardPath::Type::Data) ==
std::vector<std::string>{TEST_ADDON_DIR, "/TEST/PATH3"})
<< path.directories(fcitx::StandardPath::Type::Data);
}
int main() {
test_basic();
test_nouser();
test_custom();
return 0;
}
|