Browse Source

Manager+Test: Move exclude paths and system directories to config

master
Riyyi 3 years ago
parent
commit
16947b93e2
  1. 2
      src/config.h
  2. 16
      src/dotfile.cpp
  3. 17
      src/dotfile.h
  4. 15
      src/main.cpp
  5. 18
      test/unit/testdotfile.cpp

2
src/config.h

@ -37,6 +37,8 @@ public:
Config(s); Config(s);
virtual ~Config(); virtual ~Config();
void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { m_settings.systemDirectories = systemDirectories; }
void setExcludePaths(const std::map<std::string, std::string>& excludePaths) { m_settings.excludePaths = excludePaths; }
void setVerbose(bool verbose) { m_verbose = verbose; } void setVerbose(bool verbose) { m_verbose = verbose; }
const std::map<std::string, std::string>& excludePaths() const { return m_settings.excludePaths; } const std::map<std::string, std::string>& excludePaths() const { return m_settings.excludePaths; }

16
src/dotfile.cpp

@ -353,19 +353,19 @@ void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std:
bool Dotfile::filter(const std::filesystem::path& path) bool Dotfile::filter(const std::filesystem::path& path)
{ {
for (auto& excludePath : m_excludePaths) { for (auto& excludePath : Config::the().excludePaths()) {
if (excludePath.type == ExcludeType::File) { if (excludePath.second == "file") {
if (path.string() == Config::the().workingDirectory() / excludePath.path) { if (path.string() == Config::the().workingDirectory() / excludePath.first) {
return true; return true;
} }
} }
else if (excludePath.type == ExcludeType::Directory) { else if (excludePath.second == "directory") {
if (path.string().find(Config::the().workingDirectory() / excludePath.path) == 0) { if (path.string().find(Config::the().workingDirectory() / excludePath.first) == 0) {
return true; return true;
} }
} }
else if (excludePath.type == ExcludeType::EndsWith) { else if (excludePath.second == "endsWith") {
if (path.string().find(excludePath.path) == path.string().size() - excludePath.path.size()) { if (path.string().find(excludePath.first) == path.string().size() - excludePath.first.size()) {
return true; return true;
} }
} }
@ -387,7 +387,7 @@ bool Dotfile::include(const std::filesystem::path& path, const std::vector<std::
bool Dotfile::isSystemTarget(const std::string& target) bool Dotfile::isSystemTarget(const std::string& target)
{ {
for (const auto& systemDirectory : m_systemDirectories) { for (const auto& systemDirectory : Config::the().systemDirectories()) {
if (target.find(systemDirectory) == 0) { if (target.find(systemDirectory) == 0) {
return true; return true;

17
src/dotfile.h

@ -25,25 +25,11 @@ public:
Push, Push,
}; };
enum class ExcludeType {
File,
Directory,
EndsWith,
};
struct ExcludePath {
ExcludeType type;
std::string path;
};
void add(const std::vector<std::string>& targets = {}); void add(const std::vector<std::string>& targets = {});
void list(const std::vector<std::string>& targets = {}); void list(const std::vector<std::string>& targets = {});
void pull(const std::vector<std::string>& targets = {}); void pull(const std::vector<std::string>& targets = {});
void push(const std::vector<std::string>& targets = {}); void push(const std::vector<std::string>& targets = {});
void setSystemDirectories(const std::vector<std::filesystem::path>& systemDirectories) { m_systemDirectories = systemDirectories; }
void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { m_excludePaths = excludePaths; }
private: private:
void pullOrPush(SyncType type, const std::vector<std::string>& targets = {}); void pullOrPush(SyncType type, const std::vector<std::string>& targets = {});
void sync(SyncType type, void sync(SyncType type,
@ -56,9 +42,6 @@ private:
bool filter(const std::filesystem::path& path); bool filter(const std::filesystem::path& path);
bool include(const std::filesystem::path& path, const std::vector<std::string>& targets); bool include(const std::filesystem::path& path, const std::vector<std::string>& targets);
bool isSystemTarget(const std::string& target); bool isSystemTarget(const std::string& target);
std::vector<ExcludePath> m_excludePaths;
std::vector<std::filesystem::path> m_systemDirectories;
}; };
#endif // DOTFILE_H #endif // DOTFILE_H

15
src/main.cpp

@ -54,21 +54,6 @@ int main(int argc, const char* argv[])
Config::the().setVerbose(verbose); Config::the().setVerbose(verbose);
if (fileOperation) { 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) { if (addOrAur) {
Dotfile::the().add(targets); Dotfile::the().add(targets);
} }

18
test/unit/testdotfile.cpp

@ -209,13 +209,13 @@ TEST_CASE(PushDotfilesWithExcludePath)
createTestDotfiles(fileNames, { "", "", "", "" }); createTestDotfiles(fileNames, { "", "", "", "" });
Dotfile::the().setExcludePaths({ Config::the().setExcludePaths({
{ Dotfile::ExcludeType::File, "__test-file-1" }, { "__test-file-1", "file" },
{ Dotfile::ExcludeType::Directory, "__subdir" }, { "__subdir", "directory" },
{ Dotfile::ExcludeType::EndsWith, ".test" }, { ".test", "endsWith" },
}); });
Dotfile::the().push(fileNames); Dotfile::the().push(fileNames);
Dotfile::the().setExcludePaths({}); Config::the().setExcludePaths({});
for (const auto& file : fileNames) { for (const auto& file : fileNames) {
EXPECT(!std::filesystem::exists(homeDirectory / file)); EXPECT(!std::filesystem::exists(homeDirectory / file));
@ -323,9 +323,9 @@ TEST_CASE(AddSystemDotfiles)
{ {
VERIFY(geteuid() == 0, return); 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().add({ "/etc/group", "/usr/lib/os-release" });
Dotfile::the().setSystemDirectories({}); Config::the().setSystemDirectories({});
EXPECT(std::filesystem::exists("etc/group")); EXPECT(std::filesystem::exists("etc/group"));
EXPECT(std::filesystem::exists("usr/lib/os-release")); EXPECT(std::filesystem::exists("usr/lib/os-release"));
@ -340,9 +340,9 @@ TEST_CASE(PullSystemDotfiles)
createTestDotfiles({ "etc/group" }, { "" }, true); createTestDotfiles({ "etc/group" }, { "" }, true);
Dotfile::the().setSystemDirectories({ "/etc" }); Config::the().setSystemDirectories({ "/etc" });
Dotfile::the().pull({ "etc/group" }); Dotfile::the().pull({ "etc/group" });
Dotfile::the().setSystemDirectories({}); Config::the().setSystemDirectories({});
Util::File lhs("/etc/group"); Util::File lhs("/etc/group");
Util::File rhs(Config::the().workingDirectory() / "etc/group"); Util::File rhs(Config::the().workingDirectory() / "etc/group");

Loading…
Cancel
Save