File: teststandardpaths_win.cpp

package info (click to toggle)
fcitx5 5.1.18-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 32,228 kB
  • sloc: cpp: 110,377; java: 3,125; javascript: 2,137; sh: 1,912; xml: 1,552; python: 1,312; ansic: 843; makefile: 11
file content (107 lines) | stat: -rw-r--r-- 3,960 bytes parent folder | download | duplicates (3)
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
/*
 * SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 */

#include <fcntl.h>
#include <algorithm>
#include <filesystem>
#include <set>
#include <string>
#include <vector>
#include "fcitx-utils/environ.h"
#include "fcitx-utils/log.h"
#include "fcitx-utils/standardpaths.h"
#include "testdir.h"

using namespace fcitx;

#define TEST_ADDON_DIR FCITX5_SOURCE_DIR "/test/addon"

void test_basic() {
    StandardPaths standardPaths("fcitx5", {},
                                StandardPathsOption::SkipSystemPath);

    FCITX_ASSERT(standardPaths.userDirectory(StandardPathsType::Config)
                     .string()
                     .ends_with("/Fcitx5/config"));
    // The order to the path should be kept for their first appearance.
    FCITX_ASSERT(standardPaths.directories(StandardPathsType::Config).front() ==
                 standardPaths.userDirectory(StandardPathsType::Config));
    FCITX_ASSERT(standardPaths.directories(StandardPathsType::Config).back() ==
                 FCITX5_BINARY_DIR "/config");
    FCITX_ASSERT(standardPaths.directories(StandardPathsType::PkgData).back() ==
                 std::filesystem::path(FCITX5_BINARY_DIR) / "data/fcitx5");
}

void test_override() {
    StandardPaths standardPaths("fcitx5",
                                {{"pkgdatadir", {TEST_ADDON_DIR "/fcitx5"}}},
                                StandardPathsOption::SkipSystemPath);
    {
        auto result = standardPaths.locate(StandardPathsType::PkgData, "addon",
                                           pathfilter::extension(".conf"));
        std::set<std::filesystem::path> names;
        std::set<std::filesystem::path> expect_names = {"testim.conf",
                                                        "testfrontend.conf"};
        for (auto &p : result) {
            names.insert(p.first);
        }

        FCITX_ASSERT(names == expect_names) << names;
    }

    {
        auto result = standardPaths.locate(StandardPathsType::PkgData, "addon",
                                           pathfilter::extension(".conf"),
                                           StandardPathsMode::System);
        std::set<std::filesystem::path> names;
        std::set<std::filesystem::path> expect_names = {"testim.conf",
                                                        "testfrontend.conf"};
        for (auto &p : result) {
            names.insert(p.first);
        }

        FCITX_ASSERT(names == expect_names);
    }

    {
        std::filesystem::path filePath;
        auto file =
            standardPaths.open(StandardPathsType::PkgData, "addon/testim.conf",
                               StandardPathsMode::Default, &filePath);
        FCITX_ASSERT(filePath == std::filesystem::path(
                                     TEST_ADDON_DIR "/fcitx5/addon/testim.conf")
                                     .lexically_normal());
    }

    {
        auto file2 = standardPaths.open(StandardPathsType::Data,
                                        "fcitx5/addon/testim2.conf");
        FCITX_ASSERT(!file2.isValid());
    }

    {
        std::vector<std::filesystem::path> filePaths;
        auto files = standardPaths.openAll(
            StandardPathsType::PkgData, "addon/testim.conf",
            StandardPathsMode::Default, &filePaths);
        FCITX_ASSERT(filePaths ==
                     std::vector<std::filesystem::path>{
                         std::filesystem::path(TEST_ADDON_DIR
                                               "/fcitx5/addon/testim.conf")
                             .lexically_normal()});
        auto files2 = standardPaths.locateAll(StandardPathsType::PkgData,
                                              "addon/testim.conf",
                                              StandardPathsMode::Default);
        FCITX_ASSERT(filePaths == files2);
    }
}

int main() {
    test_basic();
    test_override();
    return 0;
}