From 16947b93e26ab921c0c5be0a91691617ae04c7db Mon Sep 17 00:00:00 2001 From: Riyyi Date: Tue, 8 Feb 2022 23:39:12 +0100 Subject: [PATCH] Manager+Test: Move exclude paths and system directories to config --- src/config.h | 2 ++ src/dotfile.cpp | 16 ++++++++-------- src/dotfile.h | 17 ----------------- src/main.cpp | 15 --------------- test/unit/testdotfile.cpp | 18 +++++++++--------- 5 files changed, 19 insertions(+), 49 deletions(-) diff --git a/src/config.h b/src/config.h index 6e8f90d..b487778 100644 --- a/src/config.h +++ b/src/config.h @@ -37,6 +37,8 @@ public: Config(s); virtual ~Config(); + void setSystemDirectories(const std::vector& systemDirectories) { m_settings.systemDirectories = systemDirectories; } + void setExcludePaths(const std::map& excludePaths) { m_settings.excludePaths = excludePaths; } void setVerbose(bool verbose) { m_verbose = verbose; } const std::map& excludePaths() const { return m_settings.excludePaths; } diff --git a/src/dotfile.cpp b/src/dotfile.cpp index a340eb3..f5030d4 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -353,19 +353,19 @@ void Dotfile::forEachDotfile(const std::vector& targets, const std: bool Dotfile::filter(const std::filesystem::path& path) { - for (auto& excludePath : m_excludePaths) { - if (excludePath.type == ExcludeType::File) { - if (path.string() == Config::the().workingDirectory() / excludePath.path) { + for (auto& excludePath : Config::the().excludePaths()) { + if (excludePath.second == "file") { + if (path.string() == Config::the().workingDirectory() / excludePath.first) { return true; } } - else if (excludePath.type == ExcludeType::Directory) { - if (path.string().find(Config::the().workingDirectory() / excludePath.path) == 0) { + else if (excludePath.second == "directory") { + if (path.string().find(Config::the().workingDirectory() / excludePath.first) == 0) { return true; } } - else if (excludePath.type == ExcludeType::EndsWith) { - if (path.string().find(excludePath.path) == path.string().size() - excludePath.path.size()) { + else if (excludePath.second == "endsWith") { + if (path.string().find(excludePath.first) == path.string().size() - excludePath.first.size()) { return true; } } @@ -387,7 +387,7 @@ bool Dotfile::include(const std::filesystem::path& path, const std::vector& targets = {}); void list(const std::vector& targets = {}); void pull(const std::vector& targets = {}); void push(const std::vector& targets = {}); - void setSystemDirectories(const std::vector& systemDirectories) { m_systemDirectories = systemDirectories; } - void setExcludePaths(const std::vector& excludePaths) { m_excludePaths = excludePaths; } - private: void pullOrPush(SyncType type, const std::vector& targets = {}); void sync(SyncType type, @@ -56,9 +42,6 @@ private: bool filter(const std::filesystem::path& path); bool include(const std::filesystem::path& path, const std::vector& targets); bool isSystemTarget(const std::string& target); - - std::vector m_excludePaths; - std::vector m_systemDirectories; }; #endif // DOTFILE_H diff --git a/src/main.cpp b/src/main.cpp index 81a9c31..893d90d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,21 +54,6 @@ int main(int argc, const char* argv[]) Config::the().setVerbose(verbose); if (fileOperation) { - Dotfile::the().setExcludePaths({ - { Dotfile::ExcludeType::File, "dotfiles.sh" }, - { Dotfile::ExcludeType::File, "packages" }, - { Dotfile::ExcludeType::EndsWith, ".md" }, - { Dotfile::ExcludeType::EndsWith, "README.org" }, - { Dotfile::ExcludeType::Directory, ".git" }, - { Dotfile::ExcludeType::File, "screenshot.png" }, - - { Dotfile::ExcludeType::Directory, ".cache" }, - { Dotfile::ExcludeType::Directory, "CMakeFiles" }, - { Dotfile::ExcludeType::Directory, "cppcheck-cppcheck-build-dir" }, - }); - - Dotfile::the().setSystemDirectories({ "/boot", "/etc", "/usr/share" }); - if (addOrAur) { Dotfile::the().add(targets); } diff --git a/test/unit/testdotfile.cpp b/test/unit/testdotfile.cpp index df8318d..27abb5e 100644 --- a/test/unit/testdotfile.cpp +++ b/test/unit/testdotfile.cpp @@ -209,13 +209,13 @@ TEST_CASE(PushDotfilesWithExcludePath) createTestDotfiles(fileNames, { "", "", "", "" }); - Dotfile::the().setExcludePaths({ - { Dotfile::ExcludeType::File, "__test-file-1" }, - { Dotfile::ExcludeType::Directory, "__subdir" }, - { Dotfile::ExcludeType::EndsWith, ".test" }, + Config::the().setExcludePaths({ + { "__test-file-1", "file" }, + { "__subdir", "directory" }, + { ".test", "endsWith" }, }); Dotfile::the().push(fileNames); - Dotfile::the().setExcludePaths({}); + Config::the().setExcludePaths({}); for (const auto& file : fileNames) { EXPECT(!std::filesystem::exists(homeDirectory / file)); @@ -323,9 +323,9 @@ TEST_CASE(AddSystemDotfiles) { VERIFY(geteuid() == 0, return); - Dotfile::the().setSystemDirectories({ "/etc", "/usr/lib" }); + Config::the().setSystemDirectories({ "/etc", "/usr/lib" }); Dotfile::the().add({ "/etc/group", "/usr/lib/os-release" }); - Dotfile::the().setSystemDirectories({}); + Config::the().setSystemDirectories({}); EXPECT(std::filesystem::exists("etc/group")); EXPECT(std::filesystem::exists("usr/lib/os-release")); @@ -340,9 +340,9 @@ TEST_CASE(PullSystemDotfiles) createTestDotfiles({ "etc/group" }, { "" }, true); - Dotfile::the().setSystemDirectories({ "/etc" }); + Config::the().setSystemDirectories({ "/etc" }); Dotfile::the().pull({ "etc/group" }); - Dotfile::the().setSystemDirectories({}); + Config::the().setSystemDirectories({}); Util::File lhs("/etc/group"); Util::File rhs(Config::the().workingDirectory() / "etc/group");