From 80cec17ef51ba392c49ba01c4887008b85963139 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Sat, 21 Jan 2023 17:17:04 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20add=20new=20function=20to=20cont?= =?UTF-8?q?rol=20pacmanconf=20repos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 1 + src/pacmanconf_repo.cpp | 16 ++++ src/pacmanconf_repo.hpp | 3 + tests/CMakeLists.txt | 1 + tests/meson.build | 9 +++ tests/unit-initcpio.cpp | 2 - tests/unit-pacmanconf.cpp | 151 ++++++++++++++++++++++++++++++++++++ 7 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 tests/unit-pacmanconf.cpp diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 71b8f60..a93626e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -37,6 +37,7 @@ jobs: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} cd ${{github.workspace}}/build ./tests/test-initcpio + ./tests/test-pacmanconf shell: bash build_withoutdev: name: Build (DEVENV OFF) diff --git a/src/pacmanconf_repo.cpp b/src/pacmanconf_repo.cpp index 16196a6..04d772d 100644 --- a/src/pacmanconf_repo.cpp +++ b/src/pacmanconf_repo.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #if defined(__clang__) #pragma clang diagnostic pop @@ -61,4 +62,19 @@ bool push_repos_front(const std::string_view& file_path, const std::string_view& return true; } +auto get_repo_list(const std::string_view& file_path) noexcept -> std::vector { + auto&& file_content = utils::read_whole_file(file_path); + if (file_content.empty()) { + spdlog::error("[PACMANCONFREPO] '{}' error occurred!", file_path); + return {}; + } + auto&& result = file_content | ranges::views::split('\n') + | ranges::views::filter([](auto&& rng) { + auto&& line = std::string_view(&*rng.begin(), static_cast(ranges::distance(rng))); + return !(line.empty() || line.starts_with("#") || !line.starts_with("[") || line.starts_with("[options]")); + }) + | ranges::to>(); + return result; +} + } // namespace detail::pacmanconf diff --git a/src/pacmanconf_repo.hpp b/src/pacmanconf_repo.hpp index d0f5bb7..2135b7b 100644 --- a/src/pacmanconf_repo.hpp +++ b/src/pacmanconf_repo.hpp @@ -1,10 +1,13 @@ #ifndef PACMANCONF_REPO_HPP #define PACMANCONF_REPO_HPP +#include // for string #include // for string_view +#include // for vector namespace detail::pacmanconf { bool push_repos_front(const std::string_view& file_path, const std::string_view& value) noexcept; +auto get_repo_list(const std::string_view& file_path) noexcept -> std::vector; } // namespace detail::pacmanconf #endif // PACMANCONF_REPO_HPP diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e66f8a9..0d97218 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,6 +2,7 @@ list(APPEND test_SOURCES ${CMAKE_SOURCE_DIR}/src/config.cpp ${CMAKE_SOURCE_DIR}/src/utils.cpp ${CMAKE_SOURCE_DIR}/src/cpu.cpp + ${CMAKE_SOURCE_DIR}/src/pacmanconf_repo.cpp ${CMAKE_SOURCE_DIR}/src/initcpio.cpp ${CMAKE_SOURCE_DIR}/src/disk.cpp ${CMAKE_SOURCE_DIR}/src/drivers.cpp diff --git a/tests/meson.build b/tests/meson.build index afaab00..a9c4265 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -5,6 +5,7 @@ test_libreq = shared_library('test_libreq', source_path + 'config.cpp', source_path + 'utils.cpp', source_path + 'cpu.cpp', + source_path + 'pacmanconf_repo.cpp', source_path + 'disk.cpp', source_path + 'drivers.cpp', source_path + 'widgets.cpp', @@ -42,3 +43,11 @@ executable( link_with: [test_libreq], include_directories: [include_directories(source_path)], install: false) + +executable( + 'test-pacmanconf', + files('unit-pacmanconf.cpp'), + dependencies: deps, + link_with: [test_libreq], + include_directories: [include_directories(source_path)], + install: false) diff --git a/tests/unit-initcpio.cpp b/tests/unit-initcpio.cpp index e10e095..fa508a5 100644 --- a/tests/unit-initcpio.cpp +++ b/tests/unit-initcpio.cpp @@ -8,8 +8,6 @@ #include #include -#include - namespace fs = std::filesystem; static constexpr auto MKINITCPIO_STR = R"( diff --git a/tests/unit-pacmanconf.cpp b/tests/unit-pacmanconf.cpp new file mode 100644 index 0000000..2d65ee5 --- /dev/null +++ b/tests/unit-pacmanconf.cpp @@ -0,0 +1,151 @@ +#include "utils.hpp" +#include "pacmanconf_repo.hpp" + +#include +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; + +static constexpr auto PACMANCONF_STR = R"( +[options] +HoldPkg = pacman glibc +Architecture = auto + +# Misc options +Color +CheckSpace +VerbosePkgLists +ParallelDownloads = 10 + +# Repository entries are of the format: +# [repo-name] +# Server = ServerName +# Include = IncludePath +# +# The header [repo-name] is crucial - it must be present and +# uncommented to enable the repo. +# + +# The testing repositories are disabled by default. To enable, uncomment the +# repo name header and Include lines. You can add preferred servers immediately +# after the header, and they will be used before the default mirrors. + +#[testing] +#Include = /etc/pacman.d/mirrorlist + +#[core-debug] +#Include = /etc/pacman.d/mirrorlist + +#[extra-debug] +#Include = /etc/pacman.d/mirrorlist + +[core] +Include = /etc/pacman.d/mirrorlist + +[extra] +Include = /etc/pacman.d/mirrorlist + +#[community-testing] +#Include = /etc/pacman.d/mirrorlist + +[community] +Include = /etc/pacman.d/mirrorlist + +#[multilib-testing] +#Include = /etc/pacman.d/mirrorlist + +[multilib] +Include = /etc/pacman.d/mirrorlist +)"; + +static constexpr auto PACMANCONF_TEST = R"( +[options] +HoldPkg = pacman glibc +Architecture = auto + +# Misc options +Color +CheckSpace +VerbosePkgLists +ParallelDownloads = 10 + +# Repository entries are of the format: +# [repo-name] +# Server = ServerName +# Include = IncludePath +# +# The header [repo-name] is crucial - it must be present and +# uncommented to enable the repo. +# + +# The testing repositories are disabled by default. To enable, uncomment the +# repo name header and Include lines. You can add preferred servers immediately +# after the header, and they will be used before the default mirrors. + +#[testing] +#Include = /etc/pacman.d/mirrorlist + +#[core-debug] +#Include = /etc/pacman.d/mirrorlist + +#[extra-debug] +#Include = /etc/pacman.d/mirrorlist + +[cachyos] +Include = /etc/pacman.d/cachyos-mirrorlist + + +[core] +Include = /etc/pacman.d/mirrorlist + +[extra] +Include = /etc/pacman.d/mirrorlist + +#[community-testing] +#Include = /etc/pacman.d/mirrorlist + +[community] +Include = /etc/pacman.d/mirrorlist + +#[multilib-testing] +#Include = /etc/pacman.d/mirrorlist + +[multilib] +Include = /etc/pacman.d/mirrorlist)"; + + +int main() { + static constexpr std::string_view filename{"/tmp/pacman.conf"}; + + // Open pacmanconf file for writing. + std::ofstream pacmanconf_file{filename.data()}; + assert(pacmanconf_file.is_open()); + + // Setup pacmanconf file. + pacmanconf_file << PACMANCONF_STR; + pacmanconf_file.close(); + + // Get current repos. + auto repo_list = detail::pacmanconf::get_repo_list(filename); + assert(!repo_list.empty()); + assert((repo_list == std::vector{"[core]", "[extra]", "[community]", "[multilib]"})); + + // Push repo. + assert(detail::pacmanconf::push_repos_front(filename, "[cachyos]\nInclude = /etc/pacman.d/cachyos-mirrorlist")); + + // Check repo list after pushing repo. + repo_list = detail::pacmanconf::get_repo_list(filename); + assert(!repo_list.empty()); + assert((repo_list == std::vector{"[cachyos]", "[core]", "[extra]", "[community]", "[multilib]"})); + + // Check if file is equal to test data. + const auto& file_content = utils::read_whole_file(filename); + assert(file_content == PACMANCONF_TEST); + + // Cleanup. + fs::remove(filename); +}