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);
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; }
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)
{
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<std::
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) {
return true;

17
src/dotfile.h

@ -25,25 +25,11 @@ public:
Push,
};
enum class ExcludeType {
File,
Directory,
EndsWith,
};
struct ExcludePath {
ExcludeType type;
std::string path;
};
void add(const std::vector<std::string>& targets = {});
void list(const std::vector<std::string>& targets = {});
void pull(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:
void pullOrPush(SyncType type, const std::vector<std::string>& 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<std::string>& targets);
bool isSystemTarget(const std::string& target);
std::vector<ExcludePath> m_excludePaths;
std::vector<std::filesystem::path> m_systemDirectories;
};
#endif // DOTFILE_H

15
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);
}

18
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");

Loading…
Cancel
Save