👷 add new function to control pacmanconf repos

This commit is contained in:
Vladislav Nepogodin 2023-01-21 17:17:04 +04:00
parent d1d5059463
commit 80cec17ef5
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
7 changed files with 181 additions and 2 deletions

View File

@ -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)

View File

@ -20,6 +20,7 @@
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/split.hpp>
#include <range/v3/view/filter.hpp>
#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<std::string> {
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<size_t>(ranges::distance(rng)));
return !(line.empty() || line.starts_with("#") || !line.starts_with("[") || line.starts_with("[options]"));
})
| ranges::to<std::vector<std::string>>();
return result;
}
} // namespace detail::pacmanconf

View File

@ -1,10 +1,13 @@
#ifndef PACMANCONF_REPO_HPP
#define PACMANCONF_REPO_HPP
#include <string> // for string
#include <string_view> // for string_view
#include <vector> // 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<std::string>;
} // namespace detail::pacmanconf
#endif // PACMANCONF_REPO_HPP

View File

@ -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

View File

@ -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)

View File

@ -8,8 +8,6 @@
#include <vector>
#include <filesystem>
#include <fmt/core.h>
namespace fs = std::filesystem;
static constexpr auto MKINITCPIO_STR = R"(

151
tests/unit-pacmanconf.cpp Normal file
View File

@ -0,0 +1,151 @@
#include "utils.hpp"
#include "pacmanconf_repo.hpp"
#include <cassert>
#include <fstream>
#include <string>
#include <string_view>
#include <vector>
#include <filesystem>
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<std::string>{"[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<std::string>{"[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);
}