Fcitx
testing.cpp
1 /*
2  * SPDX-FileCopyrightText: 2020~2020 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #include "testing.h"
8 #include <filesystem>
9 #include <memory>
10 #include <string>
11 #include <unordered_map>
12 #include <vector>
13 #include <ranges>
14 #include "environ.h"
15 #include "standardpaths.h"
16 #include "standardpaths_p.h"
17 #include "stringutils.h"
18 
19 namespace fcitx {
20 
21 void setupTestingEnvironment(const std::string &testBinaryDir,
22  const std::vector<std::string> &addonDirs,
23  const std::vector<std::string> &dataDirs) {
24  std::vector<std::filesystem::path> addonDirsPath;
25  addonDirsPath.assign(addonDirs.begin(), addonDirs.end());
26  std::vector<std::filesystem::path> dataDirsPath;
27  dataDirsPath.assign(dataDirs.begin(), dataDirs.end());
28  setupTestingEnvironmentPath(std::filesystem::path(testBinaryDir),
29  addonDirsPath, dataDirsPath);
30 }
31 
32 void setupTestingEnvironmentPath(
33  const std::filesystem::path &testBinaryDir,
34  const std::vector<std::filesystem::path> &addonDirs,
35  const std::vector<std::filesystem::path> &dataDirs) {
36 
37  // Path to addon library
38  std::vector<std::filesystem::path> fullAddonDirs;
39  for (const auto &addonDir : addonDirs) {
40  if (addonDir.empty()) {
41  continue;
42  }
43  if (addonDir.is_absolute()) {
44  fullAddonDirs.push_back(addonDir);
45  } else {
46  fullAddonDirs.push_back(testBinaryDir / addonDir);
47  }
48  }
49  // Add built-in path for testing addons.
50  fullAddonDirs.push_back(StandardPaths::fcitxPath("addondir"));
51  // Make sure we can find addon files.
52  // Path to addon library
53  std::vector<std::filesystem::path> fullDataDirs;
54  for (const auto &dataDir : dataDirs) {
55  if (dataDir.empty()) {
56  continue;
57  }
58  if (dataDir.is_absolute()) {
59  fullDataDirs.push_back(dataDir);
60  } else {
61  fullDataDirs.push_back(testBinaryDir / dataDir);
62  }
63  }
64  // Include the three testing only addons.
65  fullDataDirs.push_back(StandardPaths::fcitxPath("pkgdatadir", "testing"));
66 
67 #ifndef _WIN32
68  // Skip resolution with fcitxPath
69  setEnvironment("SKIP_FCITX_PATH", "1");
70  setEnvironment("SKIP_FCITX_USER_PATH", "1");
71 
72  setEnvironment(
73  "FCITX_ADDON_DIRS",
74  stringutils::join(fullAddonDirs |
75  std::views::transform([](const auto &path) {
76  return path.string();
77  }),
78  ":")
79  .data());
80  // Make sure we don't write to user data.
81  setEnvironment("FCITX_DATA_HOME", "/Invalid/Path");
82  // Make sure we don't write to user data.
83  setEnvironment("FCITX_CONFIG_HOME", "/Invalid/Path");
84  setEnvironment(
85  "FCITX_DATA_DIRS",
86  stringutils::join(fullDataDirs |
87  std::views::transform([](const auto &path) {
88  return path.string();
89  }),
90  ":")
91  .data());
92 #endif
93 
94  StandardPathsPrivate::setGlobal(std::make_unique<StandardPaths>(
95  "fcitx5",
96  std::unordered_map<std::string, std::vector<std::filesystem::path>>{
97  {"pkgdatadir", fullDataDirs},
98  {"addondir", fullAddonDirs},
99  },
101  StandardPathsOption::SkipUserPath,
102  StandardPathsOption::SkipSystemPath,
103  }));
104 }
105 
106 } // namespace fcitx
Definition: action.cpp:17
New Utility classes to handle application specific path.
void setupTestingEnvironment(const std::string &testBinaryDir, const std::vector< std::string > &addonDirs, const std::vector< std::string > &dataDirs)
Set corresponding environment variable to make sure fcitx can be run properly for testing...
Definition: testing.cpp:21
Utility functions for testing.
Class provides bit flag support for Enum.
Definition: flags.h:33
String handle utilities.
static std::filesystem::path fcitxPath(const char *path, const std::filesystem::path &subPath={})
Return fcitx specific path defined at compile time.
std::string join(Iter start, Iter end, T &&delim)
Join a range of string with delim.
Definition: stringutils.h:108