Compare commits

..
3 Commits
2 changed files with 68 additions and 5 deletions
+11 -3
View File
@@ -173,7 +173,6 @@ void Dotfile::sync(SyncType type,
}
auto printError = [](const std::filesystem::path& path, const std::error_code& error) -> void {
// std::filesystem::copy doesnt respect 'overwrite_existing' for symlinks
if (error.value() && error.message() != "File exists") {
fprintf(stderr, "\033[31;1mDotfile:\033[0m '%s': %c%s\n",
path.c_str(),
@@ -210,8 +209,17 @@ void Dotfile::sync(SyncType type,
if (Config::the().verbose()) {
printf("'%s' -> '%s'\n", from.c_str(), to.c_str());
}
std::filesystem::copy(from, to, copyOptions, error);
printError(to, error);
if (std::filesystem::is_symlink(from)) {
// NOTE: std::filesystem::copy doesnt respect 'overwrite_existing' for symlinks
std::filesystem::remove(to, error);
printError(to, error);
std::filesystem::copy_symlink(from, to, error);
printError(from, error);
}
else {
std::filesystem::copy(from, to, copyOptions, error);
printError(from, error);
}
if (homePath && root) {
seteuid(0);
+57 -2
View File
@@ -64,13 +64,13 @@ void removeTestDotfiles(const std::vector<std::string>& files)
file = file.substr(0, file.find_first_of('/'));
// Delete recursively in working directory
if (std::filesystem::exists(file)) {
if (std::filesystem::exists(file) || std::filesystem::is_symlink(file)) {
std::filesystem::remove_all(file);
}
// Delete recursively in home directory
file = homeDirectory / file;
if (std::filesystem::exists(file)) {
if (std::filesystem::exists(file) || std::filesystem::is_symlink(file)) {
std::filesystem::remove_all(file);
}
}
@@ -621,3 +621,58 @@ TEST_CASE(PullSystemDotfiles)
std::filesystem::remove_all(Config::the().workingDirectory() / "etc");
}
TEST_CASE(AddSymlinkDotfiles)
{
std::filesystem::path fileInHome = homeDirectory / "__the-add-file";
std::filesystem::path symlinkFileName = "__the-add-symlink";
std::filesystem::path symlinkInHome = homeDirectory / symlinkFileName;
createTestDotfiles({ fileInHome }, { "the file contents" });
std::filesystem::create_symlink(fileInHome, symlinkInHome);
Dotfile::the().add({ symlinkInHome });
EXPECT(std::filesystem::is_symlink(symlinkFileName));
EXPECT_EQ(std::filesystem::read_symlink(symlinkFileName).string(), fileInHome);
EXPECT_EQ(Util::File(symlinkFileName).data(), "the file contents");
removeTestDotfiles({ symlinkInHome, fileInHome });
}
TEST_CASE(PullSymlinkDotfiles)
{
std::filesystem::path fileInHome = homeDirectory / "__the-pull-file";
std::filesystem::path symlinkFileName = "__the-pull-symlink";
std::filesystem::path symlinkInHome = homeDirectory / symlinkFileName;
createTestDotfiles({ fileInHome }, { "the file contents" });
std::filesystem::create_symlink(fileInHome, symlinkInHome);
std::filesystem::create_symlink("doesnt-exist", symlinkFileName);
Dotfile::the().pull({ symlinkFileName });
EXPECT(std::filesystem::is_symlink(symlinkFileName));
EXPECT_EQ(std::filesystem::read_symlink(symlinkFileName).string(), fileInHome);
EXPECT_EQ(Util::File(symlinkFileName).data(), "the file contents");
removeTestDotfiles({ symlinkInHome, fileInHome });
}
TEST_CASE(PushSymlinkDotfiles)
{
std::filesystem::path fileInHome = homeDirectory / "__the-push-file";
std::filesystem::path symlinkFileName = "__the-push-symlink";
std::filesystem::path symlinkInHome = homeDirectory / symlinkFileName;
createTestDotfiles({ fileInHome }, { "the file contents" });
std::filesystem::create_symlink(fileInHome, symlinkFileName);
Dotfile::the().push({ symlinkFileName });
EXPECT(std::filesystem::is_symlink(symlinkInHome));
EXPECT_EQ(std::filesystem::read_symlink(symlinkInHome).string(), fileInHome);
EXPECT_EQ(Util::File(symlinkInHome).data(), "the file contents");
removeTestDotfiles({ symlinkInHome, fileInHome });
}