mirror of
https://github.com/CachyOS/New-Cli-Installer.git
synced 2025-01-23 22:42:31 +08:00
add new test
This commit is contained in:
parent
65e9f3d35d
commit
256018f25b
@ -13,7 +13,7 @@ list(APPEND test_SOURCES
|
||||
|
||||
add_library(test_libreq STATIC ${test_SOURCES})
|
||||
target_include_directories(test_libreq PRIVATE ${CMAKE_BINARY_DIR}/include ${CMAKE_SOURCE_DIR}/include ${CMAKE_CURRENT_DIR})
|
||||
target_link_libraries(test_libreq PRIVATE project_warnings project_options spdlog::spdlog fmt::fmt ftxui::component cpr::cpr range-v3::range-v3)
|
||||
target_link_libraries(test_libreq PRIVATE project_warnings project_options spdlog::spdlog fmt::fmt ftxui::component cpr::cpr range-v3::range-v3 ctre::ctre)
|
||||
|
||||
#############################################################################
|
||||
# one executable for each unit test file
|
||||
@ -31,5 +31,5 @@ foreach(file ${files})
|
||||
$<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations>
|
||||
)
|
||||
target_include_directories(${testcase} PRIVATE ${CMAKE_BINARY_DIR}/include ${CMAKE_SOURCE_DIR}/include ${CMAKE_CURRENT_DIR})
|
||||
target_link_libraries(${testcase} PRIVATE project_warnings project_options test_libreq spdlog::spdlog fmt::fmt ftxui::component cpr::cpr range-v3::range-v3)
|
||||
target_link_libraries(${testcase} PRIVATE project_warnings project_options test_libreq spdlog::spdlog fmt::fmt ftxui::component cpr::cpr range-v3::range-v3 ctre::ctre)
|
||||
endforeach()
|
||||
|
136
tests/unit-initcpio.cpp
Normal file
136
tests/unit-initcpio.cpp
Normal file
@ -0,0 +1,136 @@
|
||||
#include "utils.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
#include <fmt/core.h>
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wold-style-cast"
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wnull-dereference"
|
||||
#pragma GCC diagnostic ignored "-Wuseless-cast"
|
||||
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
||||
#endif
|
||||
|
||||
#include <range/v3/algorithm/find.hpp>
|
||||
#include <range/v3/range/conversion.hpp>
|
||||
#include <range/v3/view/getlines.hpp>
|
||||
#include <range/v3/view/join.hpp>
|
||||
#include <range/v3/view/split.hpp>
|
||||
#include <range/v3/view/transform.hpp>
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
static constexpr auto MKINITCPIO_STR = R"(
|
||||
# MODULES
|
||||
MODULES=()
|
||||
|
||||
# BINARIES
|
||||
BINARIES=()
|
||||
|
||||
# FILES
|
||||
FILES=()
|
||||
|
||||
# HOOKS
|
||||
HOOKS=(base udev autodetect modconf block filesystems keyboard fsck)
|
||||
)";
|
||||
|
||||
static constexpr auto MKINITCPIO_TEST = R"(
|
||||
# MODULES
|
||||
MODULES=(crc32c-intel)
|
||||
|
||||
# BINARIES
|
||||
BINARIES=()
|
||||
|
||||
# FILES
|
||||
FILES=()
|
||||
|
||||
# HOOKS
|
||||
HOOKS=(base usr lvm2 zfs)
|
||||
)";
|
||||
|
||||
class Initcpio {
|
||||
public:
|
||||
Initcpio(const std::string_view& file_path) noexcept : m_file_path(file_path) {}
|
||||
|
||||
bool write() const noexcept {
|
||||
auto&& file_content = utils::read_whole_file(m_file_path);
|
||||
std::string&& result = file_content | ranges::views::split('\n')
|
||||
| ranges::views::transform([&](auto&& rng) {
|
||||
auto&& line = std::string_view(&*rng.begin(), static_cast<size_t>(ranges::distance(rng)));
|
||||
if (line.starts_with("MODULES")) {
|
||||
auto&& formatted_modules = modules | ranges::views::join(' ')
|
||||
| ranges::to<std::string>();
|
||||
return fmt::format("MODULES=({})", formatted_modules);
|
||||
} else if (line.starts_with("FILES")) {
|
||||
auto&& formatted_files = files | ranges::views::join(' ')
|
||||
| ranges::to<std::string>();
|
||||
return fmt::format("FILES=({})", formatted_files);
|
||||
} else if (line.starts_with("HOOKS")) {
|
||||
auto&& formatted_hooks = hooks | ranges::views::join(' ')
|
||||
| ranges::to<std::string>();
|
||||
return fmt::format("HOOKS=({})", formatted_hooks);
|
||||
}
|
||||
return std::string{line.data(), line.size()};
|
||||
})
|
||||
| ranges::views::join('\n')
|
||||
| ranges::to<std::string>();
|
||||
result += '\n';
|
||||
|
||||
/* clang-format off */
|
||||
std::ofstream mhinitcpio_file{m_file_path.data()};
|
||||
if (!mhinitcpio_file.is_open()) { return false; }
|
||||
/* clang-format on */
|
||||
mhinitcpio_file << result;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<std::string> modules{};
|
||||
std::vector<std::string> files{};
|
||||
std::vector<std::string> hooks{};
|
||||
|
||||
private:
|
||||
std::string_view m_file_path{};
|
||||
};
|
||||
|
||||
int main() {
|
||||
static constexpr std::string_view filename{"/tmp/mkinitcpio.conf"};
|
||||
|
||||
// Open mkinitcpio file for writing.
|
||||
std::ofstream mhinitcpio_file{filename.data()};
|
||||
assert(mhinitcpio_file.is_open());
|
||||
|
||||
// Setup mkinitcpio file.
|
||||
mhinitcpio_file << MKINITCPIO_STR;
|
||||
mhinitcpio_file.close();
|
||||
|
||||
auto initcpio = Initcpio{filename};
|
||||
|
||||
// Insert data.
|
||||
initcpio.modules.insert(initcpio.modules.end(), {"crc32c-intel"});
|
||||
initcpio.hooks.insert(initcpio.hooks.end(), {"base", "usr", "lvm2", "zfs"});
|
||||
|
||||
// Write data.
|
||||
assert(initcpio.write());
|
||||
|
||||
// Check if file is equal to test data.
|
||||
// "\n# MODULES\nMODULES=(crc32c-intel)\n\n# BINARIES\nBINARIES=()\n\n# FILES\nFILES=()\n\n# HOOKS\nHOOKS=(base usr lvm2 zfs)"\n
|
||||
const auto&& file_content = utils::read_whole_file(filename);
|
||||
assert(file_content == MKINITCPIO_TEST);
|
||||
|
||||
// Cleanup.
|
||||
fs::remove(filename);
|
||||
}
|
Loading…
Reference in New Issue
Block a user