1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-29 02:56:11 +00:00
transmission/tests/libtransmission/platform-test.cc
Charles Kerr 445aad56a0
test: add platform tests (#3514)
* test: add tr_getDefaultDownloadDir() tests

this also indirectly tests xdg and homedir

* test: add PlatformTest.defaultConfigDirEnv

* test: add PlatformTest.defaultConfigDirXdgConfig

test: add PlatformTest.defaultConfigDirXdgConfigHome

* test: add PlatformTest.webClientDirEnvClutch

test: add PlatformTest.webClientDirEnvTr

test: add PlatformTest.webClientDirXdgDataHome

* fixup! test: add PlatformTest.webClientDirEnvClutch

fix: win32 breakage
2022-07-22 20:10:02 -05:00

122 lines
3.2 KiB
C++

// This file Copyright (C) 2022 Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <cstdlib>
#include <string_view>
#include "transmission.h"
#include "file.h"
#include "tr-strbuf.h"
#include "test-fixtures.h"
using namespace std::literals;
using PlatformTest = ::libtransmission::test::SessionTest;
using ::libtransmission::test::makeString;
#ifdef _WIN32
#include <windows.h>
#define setenv(key, value, unused) SetEnvironmentVariableA(key, value)
#define unsetenv(key) SetEnvironmentVariableA(key, nullptr)
#endif
TEST_F(PlatformTest, defaultDownloadDirXdg)
{
setenv("HOME", sandboxDir().c_str(), 1);
setenv("XDG_CONFIG_HOME", LIBTRANSMISSION_TEST_ASSETS_DIR, 1);
auto actual = makeString(tr_getDefaultDownloadDir());
auto expected = fmt::format("{:s}/UserDirsDownloads"sv, sandboxDir());
EXPECT_EQ(expected, actual);
unsetenv("XDG_CONFIG_HOME");
unsetenv("HOME");
}
#if !defined(_WIN32) && !defined(__HAIKU__)
TEST_F(PlatformTest, defaultDownloadDir)
{
setenv("HOME", sandboxDir().c_str(), 1);
auto expected = fmt::format("{:s}/Downloads"sv, sandboxDir());
auto actual = makeString(tr_getDefaultDownloadDir());
EXPECT_EQ(expected, actual);
unsetenv("HOME");
}
#endif
TEST_F(PlatformTest, defaultConfigDirEnv)
{
setenv("TRANSMISSION_HOME", sandboxDir().c_str(), 1);
auto actual = makeString(tr_getDefaultConfigDir("appname"));
auto expected = sandboxDir();
EXPECT_EQ(expected, actual);
unsetenv("TRANSMISSION_HOME");
}
#if !defined(__APPLE__) && !defined(_WIN32) && !defined(__HAIKU__)
TEST_F(PlatformTest, defaultConfigDirXdgConfig)
{
setenv("XDG_CONFIG_HOME", sandboxDir().c_str(), 1);
auto expected = fmt::format("{:s}/appname", sandboxDir());
auto actual = makeString(tr_getDefaultConfigDir("appname"));
EXPECT_EQ(expected, actual);
unsetenv("XDG_CONFIG_HOME");
}
TEST_F(PlatformTest, defaultConfigDirXdgConfigHome)
{
auto const home = tr_pathbuf{ sandboxDir(), "/home/user" };
setenv("HOME", home, 1);
auto expected = fmt::format("{:s}/.config/appname", home.sv());
auto actual = makeString(tr_getDefaultConfigDir("appname"));
EXPECT_EQ(expected, actual);
unsetenv("HOME");
}
#endif
TEST_F(PlatformTest, webClientDirEnvClutch)
{
setenv("CLUTCH_HOME", sandboxDir().c_str(), 1);
EXPECT_EQ(sandboxDir(), tr_getWebClientDir(session_));
unsetenv("CLUTCH_HOME");
}
TEST_F(PlatformTest, webClientDirEnvTr)
{
setenv("TRANSMISSION_WEB_HOME", sandboxDir().c_str(), 1);
EXPECT_EQ(sandboxDir(), tr_getWebClientDir(session_));
unsetenv("TRANSMISSION_WEB_HOME");
}
#if !defined(BUILD_MAC_CLIENT) && !defined(_WIN32)
TEST_F(PlatformTest, webClientDirXdgDataHome)
{
setenv("XDG_DATA_HOME", sandboxDir().c_str(), 1);
auto const expected = tr_pathbuf{ sandboxDir(), "/transmission/public_html"sv };
auto const index_html = tr_pathbuf{ expected, "/index.html"sv };
EXPECT_TRUE(tr_sys_dir_create(expected, TR_SYS_DIR_CREATE_PARENTS, 0777));
EXPECT_TRUE(tr_saveFile(index_html, "<html></html>"sv));
EXPECT_EQ(expected, tr_getWebClientDir(session_));
unsetenv("XDG_DATA_HOME");
}
#endif