- Make homedir work on windows

- add test cases for homedir
This commit is contained in:
Sebastian Messmer 2018-05-16 21:34:01 -07:00
parent c1bb081861
commit 2888ea6efd
5 changed files with 165 additions and 35 deletions

View File

@ -1,27 +1,14 @@
#include "homedir.h" #include "homedir.h"
#include <sys/types.h> #include <sys/types.h>
#include <pwd.h>
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
using std::string; using std::string;
namespace cpputils { #if !defined(_MSC_VER)
namespace system {
HomeDirectory::HomeDirectory() #include <pwd.h>
:_home_directory(_get_home_directory()) { namespace {
} bf::path _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() {
struct passwd* pwd = getpwuid(getuid()); struct passwd* pwd = getpwuid(getuid());
string homedir; string homedir;
if (pwd) { if (pwd) {
@ -36,23 +23,87 @@ namespace cpputils {
return homedir; return homedir;
} }
bf::path HomeDirectory::getXDGDataDir() { bf::path _get_appdata_directory() {
const char* xdg_data_dir = std::getenv("XDG_DATA_HOME"); const char* xdg_data_dir = std::getenv("XDG_DATA_HOME");
if (xdg_data_dir != nullptr) { if (xdg_data_dir != nullptr) {
return xdg_data_dir; 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) bf::path _get_home_directory() {
:_oldHomeDirectory(HomeDirectory::singleton()._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()._home_directory = fakeHomeDirectory;
HomeDirectory::singleton()._appdata_directory = fakeAppdataDirectory;
} }
FakeHomeDirectoryRAII::~FakeHomeDirectoryRAII() { FakeHomeDirectoryRAII::~FakeHomeDirectoryRAII() {
// Reset to old (non-fake) value // Reset to old (non-fake) value
HomeDirectory::singleton()._home_directory = _oldHomeDirectory; HomeDirectory::singleton()._home_directory = _oldHomeDirectory;
HomeDirectory::singleton()._appdata_directory = _oldAppdataDirectory;
} }
FakeTempHomeDirectoryRAII::FakeTempHomeDirectoryRAII()
: _tempDir(), _fakeHome(_tempDir.path() / "home", _tempDir.path() / "appdata") {}
} }
} }

View File

@ -5,6 +5,7 @@
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>
#include "../macros.h" #include "../macros.h"
#include <cpp-utils/pointer/unique_ref.h> #include <cpp-utils/pointer/unique_ref.h>
#include "../tempfile/TempDir.h"
namespace cpputils { namespace cpputils {
namespace system { namespace system {
@ -15,14 +16,14 @@ namespace cpputils {
public: public:
static const boost::filesystem::path &get(); static const boost::filesystem::path &get();
static boost::filesystem::path getXDGDataDir(); static const boost::filesystem::path &getXDGDataDir();
private: private:
boost::filesystem::path _home_directory; boost::filesystem::path _home_directory;
boost::filesystem::path _appdata_directory;
HomeDirectory(); HomeDirectory();
static HomeDirectory &singleton(); static HomeDirectory &singleton();
boost::filesystem::path _get_home_directory();
friend class FakeHomeDirectoryRAII; friend class FakeHomeDirectoryRAII;
@ -32,15 +33,27 @@ namespace cpputils {
class FakeHomeDirectoryRAII final { class FakeHomeDirectoryRAII final {
public: public:
FakeHomeDirectoryRAII(const boost::filesystem::path &fakeHomeDirectory); FakeHomeDirectoryRAII(const boost::filesystem::path &fakeHomeDirectory, const boost::filesystem::path &fakeAppdataDirectory);
~FakeHomeDirectoryRAII(); ~FakeHomeDirectoryRAII();
private: private:
boost::filesystem::path _oldHomeDirectory; boost::filesystem::path _oldHomeDirectory;
boost::filesystem::path _oldAppdataDirectory;
DISALLOW_COPY_AND_ASSIGN(FakeHomeDirectoryRAII); DISALLOW_COPY_AND_ASSIGN(FakeHomeDirectoryRAII);
}; };
class FakeTempHomeDirectoryRAII final {
public:
FakeTempHomeDirectoryRAII();
private:
TempDir _tempDir;
FakeHomeDirectoryRAII _fakeHome;
DISALLOW_COPY_AND_ASSIGN(FakeTempHomeDirectoryRAII);
};
} }
} }

View File

@ -46,6 +46,7 @@ set(SOURCES
assert/assert_include_test.cpp assert/assert_include_test.cpp
assert/assert_debug_test.cpp assert/assert_debug_test.cpp
system/MemoryTest.cpp system/MemoryTest.cpp
system/HomedirTest.cpp
) )
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} ${SOURCES})

View 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());
}

View File

@ -6,14 +6,8 @@
#include <cpp-utils/system/homedir.h> #include <cpp-utils/system/homedir.h>
class TestWithFakeHomeDirectory { class TestWithFakeHomeDirectory {
public:
TestWithFakeHomeDirectory()
:homedir(), fakeHomeDirRAII(homedir.path()) {
}
private: private:
cpputils::TempDir homedir; cpputils::system::FakeTempHomeDirectoryRAII fakeHomeDirRAII;
cpputils::system::FakeHomeDirectoryRAII fakeHomeDirRAII;
}; };
#endif #endif