- Make homedir work on windows
- add test cases for homedir
This commit is contained in:
parent
c1bb081861
commit
2888ea6efd
@ -1,27 +1,14 @@
|
||||
#include "homedir.h"
|
||||
#include <sys/types.h>
|
||||
#include <pwd.h>
|
||||
|
||||
namespace bf = boost::filesystem;
|
||||
using std::string;
|
||||
|
||||
namespace cpputils {
|
||||
namespace system {
|
||||
#if !defined(_MSC_VER)
|
||||
|
||||
HomeDirectory::HomeDirectory()
|
||||
:_home_directory(_get_home_directory()) {
|
||||
}
|
||||
|
||||
HomeDirectory &HomeDirectory::singleton() {
|
||||
static HomeDirectory _singleton;
|
||||
return _singleton;
|
||||
}
|
||||
|
||||
const bf::path &HomeDirectory::get() {
|
||||
return singleton()._home_directory;
|
||||
}
|
||||
|
||||
bf::path HomeDirectory::_get_home_directory() {
|
||||
#include <pwd.h>
|
||||
namespace {
|
||||
bf::path _get_home_directory() {
|
||||
struct passwd* pwd = getpwuid(getuid());
|
||||
string homedir;
|
||||
if (pwd) {
|
||||
@ -36,23 +23,87 @@ namespace cpputils {
|
||||
return homedir;
|
||||
}
|
||||
|
||||
bf::path HomeDirectory::getXDGDataDir() {
|
||||
bf::path _get_appdata_directory() {
|
||||
const char* xdg_data_dir = std::getenv("XDG_DATA_HOME");
|
||||
if (xdg_data_dir != nullptr) {
|
||||
return xdg_data_dir;
|
||||
}
|
||||
|
||||
return cpputils::system::HomeDirectory::get() / ".local" / "share";
|
||||
return _get_home_directory() / ".local" / "share";
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <Shlobj.h>
|
||||
namespace {
|
||||
struct PathBuffer final {
|
||||
PWSTR path = nullptr;
|
||||
|
||||
~PathBuffer() {
|
||||
CoTaskMemFree(path);
|
||||
}
|
||||
};
|
||||
|
||||
bf::path _get_known_path(KNOWNFOLDERID folderId) {
|
||||
PathBuffer path;
|
||||
HRESULT result_code = ::SHGetKnownFolderPath(folderId, 0, nullptr, &path.path);
|
||||
if (S_OK != result_code) {
|
||||
throw std::runtime_error("Failed getting user home directory. Hresult: " + std::to_string(result_code));
|
||||
}
|
||||
bf::path result(path.path);
|
||||
return result;
|
||||
}
|
||||
|
||||
FakeHomeDirectoryRAII::FakeHomeDirectoryRAII(const boost::filesystem::path &fakeHomeDirectory)
|
||||
:_oldHomeDirectory(HomeDirectory::singleton()._home_directory) {
|
||||
bf::path _get_home_directory() {
|
||||
return _get_known_path(FOLDERID_Profile);
|
||||
}
|
||||
|
||||
bf::path _get_appdata_directory() {
|
||||
return _get_known_path(FOLDERID_LocalAppData);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
namespace bf = boost::filesystem;
|
||||
using std::string;
|
||||
|
||||
namespace cpputils {
|
||||
namespace system {
|
||||
|
||||
HomeDirectory::HomeDirectory()
|
||||
: _home_directory(::_get_home_directory())
|
||||
, _appdata_directory(::_get_appdata_directory()) {
|
||||
}
|
||||
|
||||
HomeDirectory &HomeDirectory::singleton() {
|
||||
static HomeDirectory _singleton;
|
||||
return _singleton;
|
||||
}
|
||||
|
||||
const bf::path &HomeDirectory::get() {
|
||||
return singleton()._home_directory;
|
||||
}
|
||||
|
||||
const bf::path &HomeDirectory::getXDGDataDir() {
|
||||
return singleton()._appdata_directory;
|
||||
}
|
||||
|
||||
FakeHomeDirectoryRAII::FakeHomeDirectoryRAII(const bf::path& fakeHomeDirectory, const bf::path& fakeAppdataDirectory)
|
||||
: _oldHomeDirectory(HomeDirectory::singleton()._home_directory)
|
||||
, _oldAppdataDirectory(HomeDirectory::singleton()._appdata_directory) {
|
||||
HomeDirectory::singleton()._home_directory = fakeHomeDirectory;
|
||||
HomeDirectory::singleton()._appdata_directory = fakeAppdataDirectory;
|
||||
}
|
||||
|
||||
FakeHomeDirectoryRAII::~FakeHomeDirectoryRAII() {
|
||||
// Reset to old (non-fake) value
|
||||
HomeDirectory::singleton()._home_directory = _oldHomeDirectory;
|
||||
HomeDirectory::singleton()._appdata_directory = _oldAppdataDirectory;
|
||||
}
|
||||
|
||||
FakeTempHomeDirectoryRAII::FakeTempHomeDirectoryRAII()
|
||||
: _tempDir(), _fakeHome(_tempDir.path() / "home", _tempDir.path() / "appdata") {}
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <boost/filesystem/path.hpp>
|
||||
#include "../macros.h"
|
||||
#include <cpp-utils/pointer/unique_ref.h>
|
||||
#include "../tempfile/TempDir.h"
|
||||
|
||||
namespace cpputils {
|
||||
namespace system {
|
||||
@ -15,14 +16,14 @@ namespace cpputils {
|
||||
public:
|
||||
static const boost::filesystem::path &get();
|
||||
|
||||
static boost::filesystem::path getXDGDataDir();
|
||||
static const boost::filesystem::path &getXDGDataDir();
|
||||
|
||||
private:
|
||||
boost::filesystem::path _home_directory;
|
||||
boost::filesystem::path _appdata_directory;
|
||||
|
||||
HomeDirectory();
|
||||
static HomeDirectory &singleton();
|
||||
boost::filesystem::path _get_home_directory();
|
||||
|
||||
friend class FakeHomeDirectoryRAII;
|
||||
|
||||
@ -32,15 +33,27 @@ namespace cpputils {
|
||||
|
||||
class FakeHomeDirectoryRAII final {
|
||||
public:
|
||||
FakeHomeDirectoryRAII(const boost::filesystem::path &fakeHomeDirectory);
|
||||
FakeHomeDirectoryRAII(const boost::filesystem::path &fakeHomeDirectory, const boost::filesystem::path &fakeAppdataDirectory);
|
||||
~FakeHomeDirectoryRAII();
|
||||
|
||||
private:
|
||||
boost::filesystem::path _oldHomeDirectory;
|
||||
boost::filesystem::path _oldAppdataDirectory;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(FakeHomeDirectoryRAII);
|
||||
};
|
||||
|
||||
class FakeTempHomeDirectoryRAII final {
|
||||
public:
|
||||
FakeTempHomeDirectoryRAII();
|
||||
|
||||
private:
|
||||
TempDir _tempDir;
|
||||
FakeHomeDirectoryRAII _fakeHome;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(FakeTempHomeDirectoryRAII);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,6 +46,7 @@ set(SOURCES
|
||||
assert/assert_include_test.cpp
|
||||
assert/assert_debug_test.cpp
|
||||
system/MemoryTest.cpp
|
||||
system/HomedirTest.cpp
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||
|
71
test/cpp-utils/system/HomedirTest.cpp
Normal file
71
test/cpp-utils/system/HomedirTest.cpp
Normal file
@ -0,0 +1,71 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <cpp-utils/system/homedir.h>
|
||||
#include <cpp-utils/tempfile/TempDir.h>
|
||||
|
||||
using cpputils::system::HomeDirectory;
|
||||
using cpputils::system::FakeHomeDirectoryRAII;
|
||||
using cpputils::system::FakeTempHomeDirectoryRAII;
|
||||
using cpputils::TempDir;
|
||||
|
||||
namespace bf = boost::filesystem;
|
||||
|
||||
TEST(HomedirTest, HomedirExists) {
|
||||
EXPECT_TRUE(bf::exists(HomeDirectory::get()));
|
||||
}
|
||||
|
||||
TEST(HomedirTest, AppDataDirExists) {
|
||||
EXPECT_TRUE(bf::exists(HomeDirectory::getXDGDataDir()));
|
||||
}
|
||||
|
||||
TEST(HomedirTest, FakeHomeDirectorySetsHomedirCorrectly) {
|
||||
TempDir fakeHomeDir, fakeAppDataDir;
|
||||
FakeHomeDirectoryRAII a(fakeHomeDir.path(), fakeAppDataDir.path());
|
||||
|
||||
EXPECT_EQ(fakeHomeDir.path(), HomeDirectory::get());
|
||||
EXPECT_EQ(fakeAppDataDir.path(), HomeDirectory::getXDGDataDir());
|
||||
}
|
||||
|
||||
TEST(HomedirTest, FakeHomeDirectoryResetsHomedirCorrectly) {
|
||||
bf::path actualHomeDir = HomeDirectory::get();
|
||||
bf::path actualAppDataDir = HomeDirectory::getXDGDataDir();
|
||||
|
||||
{
|
||||
TempDir fakeHomeDir, fakeAppDataDir;
|
||||
FakeHomeDirectoryRAII a(fakeHomeDir.path(), fakeAppDataDir.path());
|
||||
|
||||
EXPECT_NE(actualHomeDir, HomeDirectory::get());
|
||||
EXPECT_NE(actualAppDataDir, HomeDirectory::getXDGDataDir());
|
||||
}
|
||||
EXPECT_EQ(actualHomeDir, HomeDirectory::get());
|
||||
EXPECT_EQ(actualAppDataDir, HomeDirectory::getXDGDataDir());
|
||||
}
|
||||
|
||||
TEST(HomedirTest, FakeTempHomeDirectorySetsHomedirCorrectly) {
|
||||
bf::path actualHomeDir = HomeDirectory::get();
|
||||
bf::path actualAppDataDir = HomeDirectory::getXDGDataDir();
|
||||
|
||||
FakeTempHomeDirectoryRAII a;
|
||||
|
||||
EXPECT_NE(actualHomeDir, HomeDirectory::get());
|
||||
EXPECT_NE(actualAppDataDir, HomeDirectory::getXDGDataDir());
|
||||
}
|
||||
|
||||
TEST(HomedirTest, FakeTempHomeDirectoryResetsHomedirCorrectly) {
|
||||
bf::path actualHomeDir = HomeDirectory::get();
|
||||
bf::path actualAppDataDir = HomeDirectory::getXDGDataDir();
|
||||
|
||||
{
|
||||
FakeTempHomeDirectoryRAII a;
|
||||
|
||||
EXPECT_NE(actualHomeDir, HomeDirectory::get());
|
||||
EXPECT_NE(actualAppDataDir, HomeDirectory::getXDGDataDir());
|
||||
}
|
||||
EXPECT_EQ(actualHomeDir, HomeDirectory::get());
|
||||
EXPECT_EQ(actualAppDataDir, HomeDirectory::getXDGDataDir());
|
||||
}
|
||||
|
||||
TEST(HomedirTest, FakeTempHomeDirectoryUsesDifferentDirsForHomedirAndAppdataDir) {
|
||||
FakeTempHomeDirectoryRAII a;
|
||||
|
||||
EXPECT_NE(HomeDirectory::get(), HomeDirectory::getXDGDataDir());
|
||||
}
|
@ -6,14 +6,8 @@
|
||||
#include <cpp-utils/system/homedir.h>
|
||||
|
||||
class TestWithFakeHomeDirectory {
|
||||
public:
|
||||
TestWithFakeHomeDirectory()
|
||||
:homedir(), fakeHomeDirRAII(homedir.path()) {
|
||||
}
|
||||
|
||||
private:
|
||||
cpputils::TempDir homedir;
|
||||
cpputils::system::FakeHomeDirectoryRAII fakeHomeDirRAII;
|
||||
cpputils::system::FakeTempHomeDirectoryRAII fakeHomeDirRAII;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user