Skip to content

Commit

Permalink
Make most fcitx-utils test pass on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Feb 4, 2025
1 parent c96635e commit 113704d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ set(FCITX_UTILS_TEST
testcolor
testi18nstring
testevent
testcustomeventloop
testlist
testfs
testlibrary
Expand All @@ -41,6 +40,7 @@ set(testeventdispatcher_LIBS Pthread::Pthread)
set(testevent_LIBS Pthread::Pthread eventlooptests)

if (NOT WIN32)
list(APPEND FCITX_UTILS_TEST testcustomeventloop)
set(testcustomeventloop_LIBS Pthread::Pthread eventlooptests)
endif()

Expand Down
12 changes: 9 additions & 3 deletions test/testfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ using namespace fcitx;
UniqueCPtr<char> cdUp(const char *path) {
return makeUniqueCPtr(
strdup(cleanPath(stringutils::joinPath(path, "..")).data()));
return makeUniqueCPtr(
realpath(stringutils::joinPath(path, "..").data(), nullptr));
}

#define TEST_PATH(PATHSTR, EXPECT) \
Expand All @@ -34,7 +32,7 @@ UniqueCPtr<char> cdUp(const char *path) {
char pathstr[] = PATHSTR; \
auto cleanStr = dirName(pathstr); \
const char *r = dirname(pathstr); \
FCITX_ASSERT(cleanStr == r); \
FCITX_ASSERT(cleanStr == r) << cleanStr << " " << r; \
} while (0);

#define TEST_BASENAME(PATHSTR) \
Expand Down Expand Up @@ -75,21 +73,29 @@ int main() {
TEST_PATH("/usr/share/", "/usr/share");

TEST_DIRNAME("/usr/lib");
#ifndef _WIN32
TEST_DIRNAME("/usr/");
#endif
TEST_DIRNAME("usr");
#ifndef _WIN32
TEST_DIRNAME("/");
#endif
TEST_DIRNAME(".");
TEST_DIRNAME("..");
TEST_DIRNAME("a///b");
TEST_DIRNAME("a//b///");
#ifndef _WIN32
TEST_DIRNAME("///a/b");
#endif
TEST_DIRNAME("/a/b/");
TEST_DIRNAME("/a/b///");

TEST_BASENAME("/usr/lib");
TEST_BASENAME("/usr/");
TEST_BASENAME("usr");
#ifndef _WIN32
TEST_BASENAME("/");
#endif
TEST_BASENAME(".");
TEST_BASENAME("..");
TEST_BASENAME("a///b");
Expand Down
22 changes: 11 additions & 11 deletions test/teststandardpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#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"
Expand All @@ -20,11 +21,10 @@ using namespace fcitx;
#define TEST_ADDON_DIR FCITX5_SOURCE_DIR "/test/addon"

void test_basic() {
FCITX_ASSERT(setenv("XDG_CONFIG_HOME", "/TEST/PATH", 1) == 0);
FCITX_ASSERT(setenv("XDG_CONFIG_DIRS",
"/TEST/PATH1/:/TEST/PATH2:/TEST/PATH2/:/TEST/PATH1",
1) == 0);
FCITX_ASSERT(setenv("XDG_DATA_DIRS", TEST_ADDON_DIR, 1) == 0);
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) ==
Expand Down Expand Up @@ -87,9 +87,9 @@ void test_basic() {
}

void test_nouser() {
FCITX_ASSERT(setenv("XDG_CONFIG_HOME", "/TEST/PATH", 1) == 0);
FCITX_ASSERT(setenv("XDG_CONFIG_DIRS", "/TEST/PATH1:/TEST/PATH2", 1) == 0);
FCITX_ASSERT(setenv("XDG_DATA_DIRS", TEST_ADDON_DIR, 1) == 0);
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(
Expand Down Expand Up @@ -137,9 +137,9 @@ void test_nouser() {
}

void test_custom() {
FCITX_ASSERT(setenv("XDG_CONFIG_HOME", "/TEST/PATH", 1) == 0);
FCITX_ASSERT(setenv("XDG_CONFIG_DIRS", "/TEST/PATH1:/TEST/PATH2", 1) == 0);
FCITX_ASSERT(setenv("XDG_DATA_DIRS", TEST_ADDON_DIR, 1) == 0);
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",
Expand Down
13 changes: 12 additions & 1 deletion test/testunixfd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@
#include "fcitx-utils/log.h"
#include "fcitx-utils/unixfd.h"

#ifdef _WIN32
#include <io.h>
#endif

using namespace fcitx;

bool fd_is_valid(int fd) { return fcntl(fd, F_GETFD) != -1 || errno != EBADF; }
bool fd_is_valid(int fd) {

#ifdef _WIN32
return _get_osfhandle(fd) != -1 || errno != EBADF;
#else
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
#endif
}

int main() {
char fname[] = "XXXXXX";
Expand Down

0 comments on commit 113704d

Please sign in to comment.