New-Cli-Installer/tests/unit-initcpio.cpp

81 lines
1.8 KiB
C++
Raw Normal View History

2022-08-11 07:55:09 +08:00
#include "utils.hpp"
2022-08-11 08:07:16 +08:00
#include "initcpio.hpp"
2022-08-11 07:55:09 +08:00
#include <cassert>
#include <fstream>
#include <string>
#include <string_view>
#include <vector>
#include <filesystem>
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
2022-08-12 02:33:43 +08:00
MODULES=(radeon crc32c-intel)
2022-08-11 07:55:09 +08:00
# BINARIES
BINARIES=()
# FILES
FILES=()
# HOOKS
2022-08-12 02:33:43 +08:00
HOOKS=(base udev autodetect modconf block filesystems keyboard fsck btrfs usr lvm2 zfs)
2022-08-11 07:55:09 +08:00
)";
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();
2022-08-11 08:07:16 +08:00
auto initcpio = detail::Initcpio{filename};
2022-08-11 07:55:09 +08:00
// Insert data.
2022-08-12 02:33:43 +08:00
initcpio.append_module("radeon");
initcpio.append_hook("btrfs");
2022-08-12 07:41:04 +08:00
initcpio.append_module("crc32c-intel");
initcpio.append_hooks({"usr", "lvm2", "zfs"});
2022-08-11 07:55:09 +08:00
// Write data.
assert(initcpio.write());
2022-08-12 07:41:04 +08:00
// Checking insertion of equal items
assert(!initcpio.append_module("radeon"));
assert(!initcpio.append_hook("btrfs"));
assert(!initcpio.append_module("crc32c-intel"));
assert(!initcpio.insert_hook("btrfs", {"usr", "lvm2", "zfs"}));
2022-08-12 07:41:04 +08:00
// Write data.
assert(initcpio.write());
2022-08-11 07:55:09 +08:00
// 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
2022-08-12 02:33:43 +08:00
const auto& file_content = utils::read_whole_file(filename);
2022-08-11 07:55:09 +08:00
assert(file_content == MKINITCPIO_TEST);
// Cleanup.
fs::remove(filename);
}