From b3929ef98ed0d2f620cab73c8dd3a89403f7e383 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Wed, 7 Aug 2024 02:26:31 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20gucc:=20use=20doctest=20instead?= =?UTF-8?q?=20of=20raw=20C=20asserts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 6 + gucc/tests/CMakeLists.txt | 19 +- gucc/tests/doctest_compatibility.h | 32 +++ gucc/tests/meson.build | 60 +++--- gucc/tests/unit-btrfs.cpp | 59 +++--- gucc/tests/unit-crypttab_gen.cpp | 31 +-- gucc/tests/unit-fetch_file.cpp | 28 +-- gucc/tests/unit-fstab_gen.cpp | 27 +-- gucc/tests/unit-grub_config_gen.cpp | 15 +- gucc/tests/unit-initcpio.cpp | 200 +++++++++++------- gucc/tests/unit-kernel_params.cpp | 75 +++---- gucc/tests/unit-locale.cpp | 43 ++-- gucc/tests/unit-mtab.cpp | 91 ++++---- gucc/tests/unit-package_profiles.cpp | 51 ++--- gucc/tests/unit-pacmanconf.cpp | 50 +++-- gucc/tests/unit-refind_config_gen.cpp | 35 +-- gucc/tests/unit-refind_extra_kern_strings.cpp | 59 +++--- gucc/tests/unit.cpp | 2 + meson.build | 1 + subprojects/doctest.wrap | 12 ++ .../doctest/0001-fix-include-doctest.patch | 12 ++ 21 files changed, 530 insertions(+), 378 deletions(-) create mode 100644 gucc/tests/doctest_compatibility.h create mode 100644 gucc/tests/unit.cpp create mode 100644 subprojects/doctest.wrap create mode 100644 subprojects/packagefiles/doctest/0001-fix-include-doctest.patch diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e9a6b2..05dd8f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,12 @@ CPMAddPackage( GIT_TAG v3.9.0 EXCLUDE_FROM_ALL YES ) +CPMAddPackage( + NAME doctest + GITHUB_REPOSITORY doctest/doctest + GIT_TAG v2.4.11 + EXCLUDE_FROM_ALL YES +) ## ## CONFIGURATION diff --git a/gucc/tests/CMakeLists.txt b/gucc/tests/CMakeLists.txt index c960d52..a058b5b 100644 --- a/gucc/tests/CMakeLists.txt +++ b/gucc/tests/CMakeLists.txt @@ -1,3 +1,12 @@ +############################################################################# +# doctest library with the main function to speed up build +############################################################################# + +add_library(doctest_main OBJECT unit.cpp) +target_compile_features(doctest_main PUBLIC cxx_std_23) +target_include_directories(doctest_main PRIVATE ${CMAKE_BINARY_DIR}/include ${CMAKE_CURRENT_DIR}) +target_link_libraries(doctest_main PRIVATE doctest::doctest) + ############################################################################# # one executable for each unit test file ############################################################################# @@ -11,12 +20,18 @@ foreach(file ${files}) get_filename_component(file_basename ${file} NAME_WE) string(REGEX REPLACE "unit-([^$]+)" "test-\\1" testcase ${file_basename}) - add_executable(${testcase} ${file}) + add_executable(${testcase} $ ${file}) + target_compile_definitions(${testcase} PRIVATE DOCTEST_CONFIG_SUPER_FAST_ASSERTS) target_compile_options(${testcase} PRIVATE $<$>:-Wno-deprecated;-Wno-float-equal> $<$:-Wno-deprecated-declarations> ) add_definitions(-DGUCC_TEST_DIR="${GUCC_TEST_DIR}") add_definitions(-DGUCC_TOP_DIR="${GUCC_TOP_DIR}") - target_link_libraries(${testcase} PRIVATE project_warnings project_options gucc::gucc) + target_link_libraries(${testcase} PRIVATE project_warnings project_options gucc::gucc doctest::doctest) + + add_test(NAME "${testcase}" + COMMAND ${testcase} ${DOCTEST_TEST_FILTER} --no-skip + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + ) endforeach() diff --git a/gucc/tests/doctest_compatibility.h b/gucc/tests/doctest_compatibility.h new file mode 100644 index 0000000..3e84db7 --- /dev/null +++ b/gucc/tests/doctest_compatibility.h @@ -0,0 +1,32 @@ +#ifndef DOCTEST_COMPATIBILITY_H +#define DOCTEST_COMPATIBILITY_H + +#define DOCTEST_CONFIG_VOID_CAST_EXPRESSIONS +#define DOCTEST_THREAD_LOCAL // enable single-threaded builds on XCode 6/7 - https://github.com/onqtam/doctest/issues/172 +#include + +// Catch doesn't require a semicolon after CAPTURE but doctest does +#undef CAPTURE +#define CAPTURE(x) DOCTEST_CAPTURE(x); + +// Sections from Catch are called Subcases in doctest and don't work with std::string by default +#undef SUBCASE +#define SECTION(x) DOCTEST_SUBCASE(x) + +// convenience macro around INFO since it doesn't support temporaries (it is optimized to avoid allocations for runtime speed) +#define INFO_WITH_TEMP_IMPL(x, var_name) const auto var_name = x; INFO(var_name) // lvalue! +#define INFO_WITH_TEMP(x) INFO_WITH_TEMP_IMPL(x, DOCTEST_ANONYMOUS(DOCTEST_STD_STRING_)) + +// doctest doesn't support THROWS_WITH for std::string out of the box (has to include ...) +#define CHECK_THROWS_WITH_STD_STR_IMPL(expr, str, var_name) \ + do { \ + std::string var_name = str; \ + CHECK_THROWS_WITH(expr, var_name.c_str()); \ + } while (false) +#define CHECK_THROWS_WITH_STD_STR(expr, str) \ + CHECK_THROWS_WITH_STD_STR_IMPL(expr, str, DOCTEST_ANONYMOUS(DOCTEST_STD_STRING_)) + +// Catch does this by default +using doctest::Approx; + +#endif diff --git a/gucc/tests/meson.build b/gucc/tests/meson.build index cad39fb..2e1ea56 100644 --- a/gucc/tests/meson.build +++ b/gucc/tests/meson.build @@ -1,103 +1,111 @@ +doctest_main_lib = static_library('doctest_main', + sources : [ + 'unit.cpp', + ], + include_directories : [include_directories('.')], + dependencies: doctest +) + executable( 'test-initcpio', files('unit-initcpio.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) executable( 'test-pacmanconf', files('unit-pacmanconf.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) executable( 'test-fstab_gen', files('unit-fstab_gen.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) executable( 'test-crypttab_gen', files('unit-crypttab_gen.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) executable( 'test-grub_config_gen', files('unit-grub_config_gen.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) executable( 'test-mtab', files('unit-mtab.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) executable( 'test-locale', files('unit-locale.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) executable( 'test-package_profiles', files('unit-package_profiles.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) executable( 'test-fetch_file', files('unit-fetch_file.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) executable( 'test-kernel_params', files('unit-kernel_params.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) executable( 'test-btrfs', files('unit-btrfs.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) executable( 'test-refind_config_gen', files('unit-refind_config_gen.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) executable( 'test-refind_extra_kern_strings', files('unit-refind_extra_kern_strings.cpp'), - dependencies: deps, - link_with: [gucc_lib], + dependencies: [deps, doctest], + link_with: [gucc_lib, doctest_main_lib], include_directories: [include_directories('../include')], install: false) diff --git a/gucc/tests/unit-btrfs.cpp b/gucc/tests/unit-btrfs.cpp index 746eecf..d737f45 100644 --- a/gucc/tests/unit-btrfs.cpp +++ b/gucc/tests/unit-btrfs.cpp @@ -1,8 +1,8 @@ +#include "doctest_compatibility.h" + #include "gucc/btrfs.hpp" #include "gucc/logger.hpp" -#include - #include #include #include @@ -13,7 +13,8 @@ using namespace std::string_literals; using namespace std::string_view_literals; -int main() { +TEST_CASE("BTRFS test") +{ auto callback_sink = std::make_shared([](const spdlog::details::log_msg&) { // noop }); @@ -28,7 +29,7 @@ int main() { // gucc::fs::BtrfsSubvolume{.subvolume = "/@snapshots"sv, .mountpoint = "/.snapshots"sv}, }; - // btrfs with subvolumes + SECTION("btrfs with subvolumes") { const std::vector expected_partitions{ gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s, .subvolume = "/@"s}, @@ -40,11 +41,11 @@ int main() { gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s}, gucc::fs::Partition{.fstype = "fat32"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; - assert(gucc::fs::btrfs_append_subvolumes(partitions, subvolumes)); - assert(partitions.size() == 4); - assert(partitions == expected_partitions); + REQUIRE(gucc::fs::btrfs_append_subvolumes(partitions, subvolumes)); + REQUIRE(partitions.size() == 4); + REQUIRE(partitions == expected_partitions); } - // invalid btrfs with subvolumes + SECTION("invalid btrfs with subvolumes") { const std::vector expected_partitions{ gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/home"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s, .subvolume = "/@home"s}, @@ -56,11 +57,11 @@ int main() { gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/var/cache"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s, .subvolume = "/@cache"s}, gucc::fs::Partition{.fstype = "fat32"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; - assert(!gucc::fs::btrfs_append_subvolumes(partitions, subvolumes)); - assert(partitions.size() == 3); - assert(partitions == expected_partitions); + REQUIRE(!gucc::fs::btrfs_append_subvolumes(partitions, subvolumes)); + REQUIRE(partitions.size() == 3); + REQUIRE(partitions == expected_partitions); } - // invalid (without root part) btrfs with subvolumes + SECTION("invalid (without root part) btrfs with subvolumes") { const std::vector expected_partitions{ gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/home"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s, .subvolume = "/@home"s}, @@ -72,11 +73,11 @@ int main() { gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/var/cache"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s, .subvolume = "/@cache"s}, gucc::fs::Partition{.fstype = "fat32"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; - assert(!gucc::fs::btrfs_append_subvolumes(partitions, subvolumes)); - assert(partitions.size() == 3); - assert(partitions == expected_partitions); + REQUIRE(!gucc::fs::btrfs_append_subvolumes(partitions, subvolumes)); + REQUIRE(partitions.size() == 3); + REQUIRE(partitions == expected_partitions); } - // btrfs without subvolumes + SECTION("btrfs without subvolumes") { const std::vector expected_partitions{ gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s}, @@ -86,11 +87,11 @@ int main() { gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s}, gucc::fs::Partition{.fstype = "fat32"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; - assert(gucc::fs::btrfs_append_subvolumes(partitions, {})); - assert(partitions.size() == 2); - assert(partitions == expected_partitions); + REQUIRE(gucc::fs::btrfs_append_subvolumes(partitions, {})); + REQUIRE(partitions.size() == 2); + REQUIRE(partitions == expected_partitions); } - // luks xfs + SECTION("luks xfs") { const std::vector expected_partitions{ gucc::fs::Partition{.fstype = "xfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,lazytime,noatime,attr2,inode64,logbsize=256k,noquota"s, .luks_mapper_name = "luks_device"s, .luks_uuid = "00e1b836-81b6-433f-83ca-0fd373e3cd50"s}, @@ -102,10 +103,10 @@ int main() { gucc::fs::Partition{.fstype = "linuxswap"s, .mountpoint = ""s, .uuid_str = ""s, .device = "/dev/nvme0n1p3"s, .mount_opts = "defaults,noatime"s}, gucc::fs::Partition{.fstype = "vfat"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; - assert(!gucc::fs::btrfs_append_subvolumes(partitions, subvolumes)); - assert(partitions == expected_partitions); + REQUIRE(!gucc::fs::btrfs_append_subvolumes(partitions, subvolumes)); + REQUIRE(partitions == expected_partitions); } - // valid zfs + SECTION("valid zfs") { const std::vector expected_partitions{ gucc::fs::Partition{.fstype = "zfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s}, @@ -119,10 +120,10 @@ int main() { gucc::fs::Partition{.fstype = "zfs"s, .mountpoint = "/var/cache"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s}, gucc::fs::Partition{.fstype = "vfat"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; - assert(!gucc::fs::btrfs_append_subvolumes(partitions, subvolumes)); - assert(partitions == expected_partitions); + REQUIRE(!gucc::fs::btrfs_append_subvolumes(partitions, subvolumes)); + REQUIRE(partitions == expected_partitions); } - // luks btrfs with subvolumes + SECTION("luks btrfs with subvolumes") { const std::vector expected_partitions{ gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s, .subvolume = "/@"s, .luks_mapper_name = "luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .luks_uuid = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s}, @@ -134,8 +135,8 @@ int main() { gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s, .subvolume = "/@"s, .luks_mapper_name = "luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .luks_uuid = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s}, gucc::fs::Partition{.fstype = "fat32"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; - assert(gucc::fs::btrfs_append_subvolumes(partitions, subvolumes)); - assert(partitions.size() == 4); - assert(partitions == expected_partitions); + REQUIRE(gucc::fs::btrfs_append_subvolumes(partitions, subvolumes)); + REQUIRE(partitions.size() == 4); + REQUIRE(partitions == expected_partitions); } } diff --git a/gucc/tests/unit-crypttab_gen.cpp b/gucc/tests/unit-crypttab_gen.cpp index dc8b439..86e2871 100644 --- a/gucc/tests/unit-crypttab_gen.cpp +++ b/gucc/tests/unit-crypttab_gen.cpp @@ -1,8 +1,8 @@ +#include "doctest_compatibility.h" + #include "gucc/crypttab.hpp" #include "gucc/logger.hpp" -#include - #include #include #include @@ -32,7 +32,8 @@ static constexpr auto CRYPTTAB_UNENCR_BOOT_TEST = R"(# Configuration for encrypt luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f none )"sv; -int main() { +TEST_CASE("crypttab gen test") +{ auto callback_sink = std::make_shared([](const spdlog::details::log_msg&) { // noop }); @@ -44,7 +45,7 @@ int main() { const auto& btrfs_mountopts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s; const auto& xfs_mountopts = "defaults,lazytime,noatime,attr2,inode64,logbsize=256k,noquota"s; - // btrfs with subvolumes + SECTION("btrfs with subvolumes") { const std::vector partitions{ gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/"s, .uuid_str = uuid_str, .device = "/dev/nvme0n1p1"s, .mount_opts = btrfs_mountopts, .subvolume = "/@"s}, @@ -60,18 +61,18 @@ int main() { gucc::fs::Partition{.fstype = "fat32"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s, .luks_mapper_name = "", .luks_uuid = ""}, }; const auto& crypttab_content = gucc::fs::generate_crypttab_content(partitions, "luks"sv); - assert(crypttab_content == CRYPTTAB_EMPTY_TEST); + REQUIRE(crypttab_content == CRYPTTAB_EMPTY_TEST); } - // basic xfs + SECTION("basic xfs") { const std::vector partitions{ gucc::fs::Partition{.fstype = "xfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = xfs_mountopts, .luks_mapper_name = ""}, gucc::fs::Partition{.fstype = "fat16"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s, .luks_uuid = ""}, }; const auto& crypttab_content = gucc::fs::generate_crypttab_content(partitions, "luks"sv); - assert(crypttab_content == CRYPTTAB_EMPTY_TEST); + REQUIRE(crypttab_content == CRYPTTAB_EMPTY_TEST); } - // luks xfs + SECTION("luks xfs") { const std::vector partitions{ gucc::fs::Partition{.fstype = "xfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = xfs_mountopts, .luks_mapper_name = "luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .luks_uuid = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s}, @@ -79,9 +80,9 @@ int main() { gucc::fs::Partition{.fstype = "vfat"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& crypttab_content = gucc::fs::generate_crypttab_content(partitions, "luks"sv); - assert(crypttab_content == CRYPTTAB_UNENCR_BOOT_TEST); + REQUIRE(crypttab_content == CRYPTTAB_UNENCR_BOOT_TEST); } - // zfs + SECTION("zfs") { const std::vector partitions{ gucc::fs::Partition{.fstype = "zfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s}, @@ -90,9 +91,9 @@ int main() { gucc::fs::Partition{.fstype = "vfat"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& crypttab_content = gucc::fs::generate_crypttab_content(partitions, "luks"sv); - assert(crypttab_content == CRYPTTAB_EMPTY_TEST); + REQUIRE(crypttab_content == CRYPTTAB_EMPTY_TEST); } - // luks btrfs with subvolumes + SECTION("luks btrfs with subvolumes") { const std::vector partitions{ gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/"s, .device = "/dev/nvme0n1p1"s, .mount_opts = btrfs_mountopts, .subvolume = "/@"s, .luks_mapper_name = "luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .luks_uuid = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s}, @@ -101,9 +102,9 @@ int main() { gucc::fs::Partition{.fstype = "fat32"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& crypttab_content = gucc::fs::generate_crypttab_content(partitions, "luks"sv); - assert(crypttab_content == CRYPTTAB_UNENCR_BOOT_TEST); + REQUIRE(crypttab_content == CRYPTTAB_UNENCR_BOOT_TEST); } - // luks btrfs with subvolumes {shuffled} + SECTION("luks btrfs with subvolumes {shuffled}") { const std::vector partitions{ gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/home"s, .device = "/dev/nvme0n1p1"s, .mount_opts = btrfs_mountopts, .subvolume = "/@home"s, .luks_mapper_name = "luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .luks_uuid = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s}, @@ -112,7 +113,7 @@ int main() { gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/var/cache"s, .device = "/dev/nvme0n1p1"s, .mount_opts = btrfs_mountopts, .subvolume = "/@cache"s, .luks_mapper_name = "luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .luks_uuid = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s}, }; const auto& crypttab_content = gucc::fs::generate_crypttab_content(partitions, "luks"sv); - assert(crypttab_content == CRYPTTAB_UNENCR_BOOT_TEST); + REQUIRE(crypttab_content == CRYPTTAB_UNENCR_BOOT_TEST); } /* diff --git a/gucc/tests/unit-fetch_file.cpp b/gucc/tests/unit-fetch_file.cpp index 5b92953..8ceadad 100644 --- a/gucc/tests/unit-fetch_file.cpp +++ b/gucc/tests/unit-fetch_file.cpp @@ -1,8 +1,8 @@ +#include "doctest_compatibility.h" + #include "gucc/fetch_file.hpp" #include "gucc/file_utils.hpp" -#include - #include #include @@ -11,35 +11,37 @@ namespace fs = std::filesystem; using namespace std::string_view_literals; -int main() { - // existing remote url, non existent fallback url +TEST_CASE("fetch file test") +{ static constexpr std::string_view LICENSE_PATH = GUCC_TOP_DIR "/LICENSE"; + + SECTION("existing remote url, non existent fallback url") { static constexpr auto remote_url = "https://github.com/CachyOS/New-Cli-Installer/raw/master/LICENSE"sv; const auto& file_content = gucc::fetch::fetch_file_from_url(remote_url, "file:///ter-testunit"); - assert(file_content); - assert(!file_content->empty()); + REQUIRE(file_content); + REQUIRE(!file_content->empty()); const auto& expected_file_content = gucc::file_utils::read_whole_file(LICENSE_PATH); - assert(file_content == expected_file_content); + REQUIRE(file_content == expected_file_content); } - // non existent remote url, existing fallback url + SECTION("non existent remote url, existing fallback url") { static constexpr auto remote_url = "https://github.com/CachyOS/New-Cli-Installer/raw/master/LCNS"sv; const auto& file_content = gucc::fetch::fetch_file_from_url(remote_url, fmt::format("file://{}", LICENSE_PATH)); - assert(file_content); - assert(!file_content->empty()); + REQUIRE(file_content); + REQUIRE(!file_content->empty()); const auto& expected_file_content = gucc::file_utils::read_whole_file(LICENSE_PATH); - assert(file_content == expected_file_content); + REQUIRE(file_content == expected_file_content); } - // non existent remote url, non existent fallback url + SECTION("non existent remote url, non existent fallback url") { static constexpr auto remote_url = "https://github.com/CachyOS/New-Cli-Installer/raw/master/LCNS"sv; const auto& file_content = gucc::fetch::fetch_file_from_url(remote_url, "file:///ter-testunit"); - assert(!file_content); + REQUIRE(!file_content); } } diff --git a/gucc/tests/unit-fstab_gen.cpp b/gucc/tests/unit-fstab_gen.cpp index facf3f3..bda03f3 100644 --- a/gucc/tests/unit-fstab_gen.cpp +++ b/gucc/tests/unit-fstab_gen.cpp @@ -1,8 +1,8 @@ +#include "doctest_compatibility.h" + #include "gucc/fstab.hpp" #include "gucc/logger.hpp" -#include - #include #include #include @@ -82,7 +82,8 @@ UUID=8EFB-4B84 /boot vfat defaults,noatim )"sv; -int main() { +TEST_CASE("fstab gen test") +{ auto callback_sink = std::make_shared([](const spdlog::details::log_msg&) { // noop }); @@ -90,7 +91,7 @@ int main() { spdlog::set_default_logger(logger); gucc::logger::set_logger(logger); - // btrfs with subvolumes + SECTION("btrfs with subvolumes") { const std::vector partitions{ gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s, .subvolume = "/@"s}, @@ -99,18 +100,18 @@ int main() { gucc::fs::Partition{.fstype = "fat32"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& fstab_content = gucc::fs::generate_fstab_content(partitions); - assert(fstab_content == FSTAB_BTRFS_TEST); + REQUIRE(fstab_content == FSTAB_BTRFS_TEST); } - // basic xfs + SECTION("basic xfs") { const std::vector partitions{ gucc::fs::Partition{.fstype = "xfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,lazytime,noatime,attr2,inode64,logbsize=256k,noquota"s}, gucc::fs::Partition{.fstype = "fat16"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& fstab_content = gucc::fs::generate_fstab_content(partitions); - assert(fstab_content == FSTAB_XFS_TEST); + REQUIRE(fstab_content == FSTAB_XFS_TEST); } - // luks xfs + SECTION("luks xfs") { const std::vector partitions{ gucc::fs::Partition{.fstype = "xfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,lazytime,noatime,attr2,inode64,logbsize=256k,noquota"s, .luks_mapper_name = "luks_device"s, .luks_uuid = "00e1b836-81b6-433f-83ca-0fd373e3cd50"s}, @@ -118,9 +119,9 @@ int main() { gucc::fs::Partition{.fstype = "vfat"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& fstab_content = gucc::fs::generate_fstab_content(partitions); - assert(fstab_content == FSTAB_LUKS_XFS_TEST); + REQUIRE(fstab_content == FSTAB_LUKS_XFS_TEST); } - // zfs + SECTION("zfs") { const std::vector partitions{ gucc::fs::Partition{.fstype = "zfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s}, @@ -129,9 +130,9 @@ int main() { gucc::fs::Partition{.fstype = "vfat"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& fstab_content = gucc::fs::generate_fstab_content(partitions); - assert(fstab_content == FSTAB_ZFS_TEST); + REQUIRE(fstab_content == FSTAB_ZFS_TEST); } - // luks btrfs with subvolumes + SECTION("luks btrfs with subvolumes") { const std::vector partitions{ gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s, .subvolume = "/@"s, .luks_mapper_name = "luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .luks_uuid = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s}, @@ -140,6 +141,6 @@ int main() { gucc::fs::Partition{.fstype = "fat32"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& fstab_content = gucc::fs::generate_fstab_content(partitions); - assert(fstab_content == FSTAB_LUKS_BTRFS_TEST); + REQUIRE(fstab_content == FSTAB_LUKS_BTRFS_TEST); } } diff --git a/gucc/tests/unit-grub_config_gen.cpp b/gucc/tests/unit-grub_config_gen.cpp index 415b3e0..854078f 100644 --- a/gucc/tests/unit-grub_config_gen.cpp +++ b/gucc/tests/unit-grub_config_gen.cpp @@ -1,8 +1,8 @@ +#include "doctest_compatibility.h" + #include "gucc/bootloader.hpp" #include "gucc/logger.hpp" -#include - #include #include #include @@ -114,7 +114,8 @@ inline auto filtered_res(std::string_view content) noexcept -> std::string { return result + '\n'; } -int main() { +TEST_CASE("grub config gen test") +{ auto callback_sink = std::make_shared([](const spdlog::details::log_msg&) { // noop }); @@ -122,13 +123,13 @@ int main() { spdlog::set_default_logger(logger); gucc::logger::set_logger(logger); - // default config + SECTION("default config") { const gucc::bootloader::GrubConfig grub_config{}; const auto& grub_config_content = gucc::bootloader::gen_grub_config(grub_config); - assert(grub_config_content == GRUB_DEFAULTS_TEST); + REQUIRE(grub_config_content == GRUB_DEFAULTS_TEST); } - // optionals set + SECTION("optionals set") { const gucc::bootloader::GrubConfig grub_config{ .default_entry = "saved"s, @@ -155,6 +156,6 @@ int main() { .disable_os_prober = false, }; const auto& grub_config_content = filtered_res(gucc::bootloader::gen_grub_config(grub_config)); - assert(grub_config_content == GRUB_OPTIONALS_TEST); + REQUIRE(grub_config_content == GRUB_OPTIONALS_TEST); } } diff --git a/gucc/tests/unit-initcpio.cpp b/gucc/tests/unit-initcpio.cpp index ce25d42..35cbff6 100644 --- a/gucc/tests/unit-initcpio.cpp +++ b/gucc/tests/unit-initcpio.cpp @@ -1,8 +1,9 @@ +#include "doctest_compatibility.h" + #include "gucc/file_utils.hpp" #include "gucc/initcpio.hpp" #include "gucc/logger.hpp" -#include #include #include #include @@ -28,6 +29,20 @@ FILES=() HOOKS=(base udev autodetect modconf block filesystems keyboard fsck) )"; +static constexpr auto MKINITCPIO_MODIFY_STR = R"( +# MODULES +MODULES=(radeon crc32c-intel) + +# BINARIES +BINARIES=() + +# FILES +FILES=() + +# HOOKS +HOOKS=(base udev autodetect modconf block filesystems keyboard fsck btrfs usr lvm2 zfs) +)"; + static constexpr auto MKINITCPIO_INVALID_TEST = R"( # MODULES MODULES=() @@ -58,7 +73,8 @@ FILES=() HOOKS=(base udev autodetect modconf block filesystems keyboard fsck btrfs usr lvm2 zfs) )"; -int main() { +TEST_CASE("initcpio test") +{ auto callback_sink = std::make_shared([](const spdlog::details::log_msg&) { // noop }); @@ -70,80 +86,108 @@ int main() { static constexpr std::string_view filename{"/tmp/mkinitcpio.conf"}; - // Open mkinitcpio file for writing. - std::ofstream mkinitcpio_file{filename.data()}; - assert(mkinitcpio_file.is_open()); - - // Setup mkinitcpio file. - mkinitcpio_file << MKINITCPIO_STR; - mkinitcpio_file.close(); - - auto initcpio = detail::Initcpio{filename}; - - // Insert data. - initcpio.append_module("radeon"); - initcpio.append_hook("btrfs"); - initcpio.append_module("crc32c-intel"); - initcpio.append_hooks({"usr", "lvm2", "zfs"}); - - // Write data. - assert(initcpio.write()); - - // 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"})); - - // 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 = file_utils::read_whole_file(filename); - assert(file_content == MKINITCPIO_TEST); - - // Cleanup. - fs::remove(filename); - - // check invalid file - mkinitcpio_file = std::ofstream{filename.data()}; - assert(mkinitcpio_file.is_open()); - - mkinitcpio_file << MKINITCPIO_INVALID_TEST; - mkinitcpio_file.close(); - - initcpio = detail::Initcpio{filename}; - assert(initcpio.parse_file()); - - assert(initcpio.modules.empty()); - assert(initcpio.files.empty()); - assert(initcpio.hooks.empty()); - - // Cleanup. - fs::remove(filename); - - // check empty file - mkinitcpio_file = std::ofstream{filename.data()}; - assert(mkinitcpio_file.is_open()); - - mkinitcpio_file << ""; - mkinitcpio_file.close(); - - initcpio = detail::Initcpio{filename}; - assert(!initcpio.parse_file()); - assert(!initcpio.write()); - - // Cleanup. - fs::remove(filename); - - // check write to nonexistent file - initcpio = detail::Initcpio{"/path/to/non/exist_file.conf"}; - assert(!initcpio.parse_file()); - assert(!initcpio.write()); - - // check write to root file - initcpio = detail::Initcpio{"/etc/mtab"}; - assert(!initcpio.parse_file()); - assert(!initcpio.write()); + SECTION("parse file") + { + // setup data. + REQUIRE(file_utils::create_file_for_overwrite(filename, MKINITCPIO_STR)); + + auto initcpio = detail::Initcpio{filename}; + REQUIRE(initcpio.parse_file()); + + const std::vector default_hooks{ + "base", "udev", "autodetect", "modconf", "block", "filesystems", "keyboard", "fsck"}; + + REQUIRE_EQ(initcpio.hooks.size(), default_hooks.size()); + REQUIRE_EQ(initcpio.hooks, default_hooks); + REQUIRE(initcpio.modules.empty()); + REQUIRE(initcpio.files.empty()); + } + SECTION("insert data") + { + // setup data. + REQUIRE(file_utils::create_file_for_overwrite(filename, MKINITCPIO_STR)); + + auto initcpio = detail::Initcpio{filename}; + REQUIRE(initcpio.append_module("radeon")); + REQUIRE(initcpio.append_hook("btrfs")); + REQUIRE(initcpio.append_module("crc32c-intel")); + REQUIRE(initcpio.append_hooks({"usr", "lvm2", "zfs"})); + + const std::vector expected_hooks{ + "base", "udev", "autodetect", "modconf", "block", "filesystems", "keyboard", "fsck", "btrfs", "usr", "lvm2", "zfs"}; + + const std::vector expected_modules{ + "radeon", "crc32c-intel"}; + + REQUIRE_EQ(initcpio.hooks.size(), expected_hooks.size()); + REQUIRE_EQ(initcpio.hooks, expected_hooks); + REQUIRE_EQ(initcpio.modules.size(), expected_modules.size()); + REQUIRE_EQ(initcpio.modules, expected_modules); + REQUIRE(initcpio.files.empty()); + + // Write data. + REQUIRE(initcpio.write()); + } + SECTION("checking insertion of equal items") + { + // setup data. + REQUIRE(file_utils::create_file_for_overwrite(filename, MKINITCPIO_MODIFY_STR)); + + auto initcpio = detail::Initcpio{filename}; + REQUIRE(initcpio.parse_file()); + + REQUIRE(!initcpio.append_module("radeon")); + REQUIRE(!initcpio.append_hook("btrfs")); + REQUIRE(!initcpio.append_module("crc32c-intel")); + REQUIRE(!initcpio.insert_hook("btrfs", {"usr", "lvm2", "zfs"})); + + // Write data. + REQUIRE(initcpio.write()); + } + SECTION("check if file is equal to test data") + { + // setup data. + REQUIRE(file_utils::create_file_for_overwrite(filename, MKINITCPIO_MODIFY_STR)); + + // "\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 = file_utils::read_whole_file(filename); + REQUIRE_EQ(file_content, MKINITCPIO_TEST); + } + SECTION("check invalid file") + { + REQUIRE(file_utils::create_file_for_overwrite(filename, MKINITCPIO_INVALID_TEST)); + + auto initcpio = detail::Initcpio{filename}; + REQUIRE(initcpio.parse_file()); + + REQUIRE(initcpio.modules.empty()); + REQUIRE(initcpio.files.empty()); + REQUIRE(initcpio.hooks.empty()); + + // Cleanup. + fs::remove(filename); + } + SECTION("check empty file") + { + REQUIRE(file_utils::create_file_for_overwrite(filename, "")); + + auto initcpio = detail::Initcpio{filename}; + REQUIRE(!initcpio.parse_file()); + REQUIRE(!initcpio.write()); + + // Cleanup. + fs::remove(filename); + } + SECTION("check write to nonexistent file") + { + auto initcpio = detail::Initcpio{"/path/to/non/exist_file.conf"}; + REQUIRE(!initcpio.parse_file()); + REQUIRE(!initcpio.write()); + } + SECTION("check write to root file") + { + auto initcpio = detail::Initcpio{"/etc/mtab"}; + REQUIRE(!initcpio.parse_file()); + REQUIRE(!initcpio.write()); + } } diff --git a/gucc/tests/unit-kernel_params.cpp b/gucc/tests/unit-kernel_params.cpp index 17d30e3..e3c2311 100644 --- a/gucc/tests/unit-kernel_params.cpp +++ b/gucc/tests/unit-kernel_params.cpp @@ -1,8 +1,8 @@ +#include "doctest_compatibility.h" + #include "gucc/kernel_params.hpp" #include "gucc/logger.hpp" -#include - #include #include #include @@ -13,7 +13,8 @@ using namespace std::string_literals; using namespace std::string_view_literals; -int main() { +TEST_CASE("kernel params get test") +{ auto callback_sink = std::make_shared([](const spdlog::details::log_msg&) { // noop }); @@ -23,7 +24,7 @@ int main() { static constexpr auto DEFAULT_KERNEL_PARAMS = "quiet splash"sv; - // btrfs with subvolumes + SECTION("btrfs with subvolumes") { const std::vector partitions{ gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s, .subvolume = "/@"s}, @@ -32,40 +33,40 @@ int main() { gucc::fs::Partition{.fstype = "fat32"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& kernel_params = gucc::fs::get_kernel_params(partitions, DEFAULT_KERNEL_PARAMS); - assert(kernel_params.has_value()); - assert(kernel_params->size() == 5); - assert((*kernel_params == std::vector{"quiet", "splash", "rw", "rootflags=subvol=/@", "root=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"})); + REQUIRE(kernel_params.has_value()); + REQUIRE(kernel_params->size() == 5); + REQUIRE((*kernel_params == std::vector{"quiet", "splash", "rw", "rootflags=subvol=/@", "root=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"})); } - // invalid xfs (empty uuid) + SECTION("invalid xfs (empty uuid)") { const std::vector partitions{ gucc::fs::Partition{.fstype = "xfs"s, .mountpoint = "/"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,lazytime,noatime,attr2,inode64,logbsize=256k,noquota"s}, gucc::fs::Partition{.fstype = "fat16"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& kernel_params = gucc::fs::get_kernel_params(partitions, DEFAULT_KERNEL_PARAMS); - assert(!kernel_params.has_value()); + REQUIRE(!kernel_params.has_value()); } - // invalid xfs (without root partition) + SECTION("invalid xfs (without root partition)") { const std::vector partitions{ gucc::fs::Partition{.fstype = "xfs"s, .mountpoint = "/home"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,lazytime,noatime,attr2,inode64,logbsize=256k,noquota"s}, gucc::fs::Partition{.fstype = "fat16"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& kernel_params = gucc::fs::get_kernel_params(partitions, DEFAULT_KERNEL_PARAMS); - assert(!kernel_params.has_value()); + REQUIRE(!kernel_params.has_value()); } - // basic xfs + SECTION("basic xfs") { const std::vector partitions{ gucc::fs::Partition{.fstype = "xfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,lazytime,noatime,attr2,inode64,logbsize=256k,noquota"s}, gucc::fs::Partition{.fstype = "fat16"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& kernel_params = gucc::fs::get_kernel_params(partitions, DEFAULT_KERNEL_PARAMS); - assert(kernel_params.has_value()); - assert(kernel_params->size() == 4); - assert((*kernel_params == std::vector{"quiet", "splash", "rw", "root=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"})); + REQUIRE(kernel_params.has_value()); + REQUIRE(kernel_params->size() == 4); + REQUIRE((*kernel_params == std::vector{"quiet", "splash", "rw", "root=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"})); } - // swap xfs + SECTION("swap xfs") { const std::vector partitions{ gucc::fs::Partition{.fstype = "xfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,lazytime,noatime,attr2,inode64,logbsize=256k,noquota"s}, @@ -73,11 +74,11 @@ int main() { gucc::fs::Partition{.fstype = "vfat"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& kernel_params = gucc::fs::get_kernel_params(partitions, DEFAULT_KERNEL_PARAMS); - assert(kernel_params.has_value()); - assert(kernel_params->size() == 5); - assert((*kernel_params == std::vector{"quiet", "splash", "rw", "root=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f", "resume=UUID=59848b1b-c6be-48f4-b3e1-48179ea72dec"})); + REQUIRE(kernel_params.has_value()); + REQUIRE(kernel_params->size() == 5); + REQUIRE((*kernel_params == std::vector{"quiet", "splash", "rw", "root=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f", "resume=UUID=59848b1b-c6be-48f4-b3e1-48179ea72dec"})); } - // luks xfs + SECTION("luks xfs") { const std::vector partitions{ gucc::fs::Partition{.fstype = "xfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,lazytime,noatime,attr2,inode64,logbsize=256k,noquota"s, .luks_mapper_name = "luks_device"s, .luks_uuid = "00e1b836-81b6-433f-83ca-0fd373e3cd50"s}, @@ -85,11 +86,11 @@ int main() { gucc::fs::Partition{.fstype = "vfat"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& kernel_params = gucc::fs::get_kernel_params(partitions, DEFAULT_KERNEL_PARAMS); - assert(kernel_params.has_value()); - assert(kernel_params->size() == 5); - assert((*kernel_params == std::vector{"quiet", "splash", "rw", "cryptdevice=UUID=00e1b836-81b6-433f-83ca-0fd373e3cd50:luks_device", "root=/dev/mapper/luks_device"})); + REQUIRE(kernel_params.has_value()); + REQUIRE(kernel_params->size() == 5); + REQUIRE((*kernel_params == std::vector{"quiet", "splash", "rw", "cryptdevice=UUID=00e1b836-81b6-433f-83ca-0fd373e3cd50:luks_device", "root=/dev/mapper/luks_device"})); } - // luks swap xfs + SECTION("luks swap xfs") { const std::vector partitions{ gucc::fs::Partition{.fstype = "xfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,lazytime,noatime,attr2,inode64,logbsize=256k,noquota"s, .luks_mapper_name = "luks_device"s, .luks_uuid = "00e1b836-81b6-433f-83ca-0fd373e3cd50"s}, @@ -97,11 +98,11 @@ int main() { gucc::fs::Partition{.fstype = "vfat"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& kernel_params = gucc::fs::get_kernel_params(partitions, DEFAULT_KERNEL_PARAMS); - assert(kernel_params.has_value()); - assert(kernel_params->size() == 6); - assert((*kernel_params == std::vector{"quiet", "splash", "rw", "cryptdevice=UUID=00e1b836-81b6-433f-83ca-0fd373e3cd50:luks_device", "root=/dev/mapper/luks_device", "resume=/dev/mapper/luks_swap_device"})); + REQUIRE(kernel_params.has_value()); + REQUIRE(kernel_params->size() == 6); + REQUIRE((*kernel_params == std::vector{"quiet", "splash", "rw", "cryptdevice=UUID=00e1b836-81b6-433f-83ca-0fd373e3cd50:luks_device", "root=/dev/mapper/luks_device", "resume=/dev/mapper/luks_swap_device"})); } - // invalid zfs + SECTION("invalid zfs") { const std::vector partitions{ gucc::fs::Partition{.fstype = "zfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s}, @@ -110,9 +111,9 @@ int main() { gucc::fs::Partition{.fstype = "vfat"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& kernel_params = gucc::fs::get_kernel_params(partitions, DEFAULT_KERNEL_PARAMS); - assert(!kernel_params.has_value()); + REQUIRE(!kernel_params.has_value()); } - // valid zfs + SECTION("valid zfs") { const std::vector partitions{ gucc::fs::Partition{.fstype = "zfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s}, @@ -121,11 +122,11 @@ int main() { gucc::fs::Partition{.fstype = "vfat"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& kernel_params = gucc::fs::get_kernel_params(partitions, DEFAULT_KERNEL_PARAMS, "zpcachyos/ROOT"); - assert(kernel_params.has_value()); - assert(kernel_params->size() == 5); - assert((*kernel_params == std::vector{"quiet", "splash", "rw", "root=ZFS=zpcachyos/ROOT", "root=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"})); + REQUIRE(kernel_params.has_value()); + REQUIRE(kernel_params->size() == 5); + REQUIRE((*kernel_params == std::vector{"quiet", "splash", "rw", "root=ZFS=zpcachyos/ROOT", "root=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"})); } - // luks btrfs with subvolumes + SECTION("luks btrfs with subvolumes") { const std::vector partitions{ gucc::fs::Partition{.fstype = "btrfs"s, .mountpoint = "/"s, .uuid_str = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .device = "/dev/nvme0n1p1"s, .mount_opts = "defaults,noatime,compress=zstd,space_cache=v2,commit=120"s, .subvolume = "/@"s, .luks_mapper_name = "luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s, .luks_uuid = "6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"s}, @@ -134,8 +135,8 @@ int main() { gucc::fs::Partition{.fstype = "fat32"s, .mountpoint = "/boot"s, .uuid_str = "8EFB-4B84"s, .device = "/dev/nvme0n1p2"s, .mount_opts = "defaults,noatime"s}, }; const auto& kernel_params = gucc::fs::get_kernel_params(partitions, DEFAULT_KERNEL_PARAMS); - assert(kernel_params.has_value()); - assert(kernel_params->size() == 6); - assert((*kernel_params == std::vector{"quiet", "splash", "rw", "rootflags=subvol=/@", "cryptdevice=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f:luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f", "root=/dev/mapper/luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"})); + REQUIRE(kernel_params.has_value()); + REQUIRE(kernel_params->size() == 6); + REQUIRE((*kernel_params == std::vector{"quiet", "splash", "rw", "rootflags=subvol=/@", "cryptdevice=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f:luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f", "root=/dev/mapper/luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"})); } } diff --git a/gucc/tests/unit-locale.cpp b/gucc/tests/unit-locale.cpp index ca8f86a..642c158 100644 --- a/gucc/tests/unit-locale.cpp +++ b/gucc/tests/unit-locale.cpp @@ -1,9 +1,9 @@ +#include "doctest_compatibility.h" + #include "gucc/file_utils.hpp" #include "gucc/locale.hpp" #include "gucc/logger.hpp" -#include - #include #include #include @@ -42,7 +42,8 @@ inline auto filtered_res(std::string_view content) noexcept -> std::string { return result + '\n'; } -int main() { +TEST_CASE("locale test") +{ auto callback_sink = std::make_shared([](const spdlog::details::log_msg&) { // noop }); @@ -56,21 +57,23 @@ int main() { static constexpr std::string_view dest_locale_gen{"/tmp/test-locale-unittest/etc/locale.gen"}; static constexpr std::string_view dest_locale_conf{"/tmp/test-locale-unittest/etc/locale.conf"}; - fs::create_directories(folder_path); - - fs::copy_file(GUCC_TEST_DIR "/files/locale.gen", dest_locale_gen, fs::copy_options::overwrite_existing); - - // set test locale. - assert(gucc::locale::prepare_locale_set("ru_RU.UTF-8"sv, folder_testpath)); - - auto locale_conf_content = gucc::file_utils::read_whole_file(dest_locale_conf); - assert(locale_conf_content == LOCALE_CONF_TEST); - - auto locale_gen_content = filtered_res(gucc::file_utils::read_whole_file(dest_locale_gen)); - assert(locale_gen_content == LOCALE_GEN_TEST); - - // Cleanup. - fs::remove_all(folder_testpath); - - assert(!gucc::locale::prepare_locale_set("ru_RU.UTF-8"sv, folder_testpath)); + SECTION("set test locale") + { + fs::create_directories(folder_path); + fs::copy_file(GUCC_TEST_DIR "/files/locale.gen", dest_locale_gen, fs::copy_options::overwrite_existing); + + REQUIRE(gucc::locale::prepare_locale_set("ru_RU.UTF-8"sv, folder_testpath)); + auto locale_conf_content = gucc::file_utils::read_whole_file(dest_locale_conf); + REQUIRE_EQ(locale_conf_content, LOCALE_CONF_TEST); + + auto locale_gen_content = filtered_res(gucc::file_utils::read_whole_file(dest_locale_gen)); + REQUIRE_EQ(locale_gen_content, LOCALE_GEN_TEST); + + // Cleanup. + fs::remove_all(folder_testpath); + } + SECTION("set locale at invalid file path") + { + REQUIRE(!gucc::locale::prepare_locale_set("ru_RU.UTF-8"sv, folder_testpath)); + } } diff --git a/gucc/tests/unit-mtab.cpp b/gucc/tests/unit-mtab.cpp index a87afbb..96d70d2 100644 --- a/gucc/tests/unit-mtab.cpp +++ b/gucc/tests/unit-mtab.cpp @@ -1,6 +1,6 @@ -#include "gucc/mtab.hpp" +#include "doctest_compatibility.h" -#include +#include "gucc/mtab.hpp" #include #include @@ -107,78 +107,79 @@ sys /tmp/calamares-root-q_z5rdlx/sys sysfs rw,noatime 0 0 efivarfs /tmp/calamares-root-q_z5rdlx/sys/firmware/efi/efivars efivarfs rw,noatime 0 0 )"sv; -int main() { - // running system +TEST_CASE("mtab test") +{ + SECTION("running system") { const auto& mtab_entries = gucc::mtab::parse_mtab_content(MTAB_RUNNING_SYSTEM_TEST, "/mnt"sv); - assert(mtab_entries.size() == 2); - assert(mtab_entries[0].device == "/dev/nvme0n1p3"); - assert(mtab_entries[0].mountpoint == "/mnt"); - assert(mtab_entries[1].device == "/dev/nvme0n1p1"); - assert(mtab_entries[1].mountpoint == "/mnt/boot"); + REQUIRE(mtab_entries.size() == 2); + REQUIRE(mtab_entries[0].device == "/dev/nvme0n1p3"); + REQUIRE(mtab_entries[0].mountpoint == "/mnt"); + REQUIRE(mtab_entries[1].device == "/dev/nvme0n1p1"); + REQUIRE(mtab_entries[1].mountpoint == "/mnt/boot"); } - // live iso system + SECTION("live iso system") { static constexpr std::string_view filename{"/tmp/mtab.conf"}; // Open mtab file for writing. std::ofstream mtab_file{filename.data()}; - assert(mtab_file.is_open()); + REQUIRE(mtab_file.is_open()); // Setup mtab file. mtab_file << MTAB_LIVE_ISO_TEST; mtab_file.close(); const auto& mtab_entries = gucc::mtab::parse_mtab("/tmp/calamares-root-q_z5rdlx"sv, filename); - assert(mtab_entries.has_value()); + REQUIRE(mtab_entries.has_value()); // Cleanup. fs::remove(filename); const auto& entries = *mtab_entries; - assert(entries.size() == 14); - assert(entries[0].device == "/dev/sda2"); - assert(entries[0].mountpoint == "/tmp/calamares-root-q_z5rdlx"); - assert(entries[1].device == "/dev/sda2"); - assert(entries[1].mountpoint == "/tmp/calamares-root-q_z5rdlx/home"); - assert(entries[2].device == "/dev/sda2"); - assert(entries[2].mountpoint == "/tmp/calamares-root-q_z5rdlx/root"); - assert(entries[3].device == "/dev/sda2"); - assert(entries[3].mountpoint == "/tmp/calamares-root-q_z5rdlx/srv"); - assert(entries[4].device == "/dev/sda2"); - assert(entries[4].mountpoint == "/tmp/calamares-root-q_z5rdlx/var/cache"); - assert(entries[5].device == "/dev/sda2"); - assert(entries[5].mountpoint == "/tmp/calamares-root-q_z5rdlx/var/tmp"); - assert(entries[6].device == "/dev/sda2"); - assert(entries[6].mountpoint == "/tmp/calamares-root-q_z5rdlx/var/log"); - assert(entries[7].device == "/dev/sda1"); - assert(entries[7].mountpoint == "/tmp/calamares-root-q_z5rdlx/boot"); - - assert(entries[8].device == "dev"); - assert(entries[8].mountpoint == "/tmp/calamares-root-q_z5rdlx/dev"); - assert(entries[9].device == "proc"); - assert(entries[9].mountpoint == "/tmp/calamares-root-q_z5rdlx/proc"); - assert(entries[10].device == "tmpfs"); - assert(entries[10].mountpoint == "/tmp/calamares-root-q_z5rdlx/run"); - assert(entries[11].device == "run"); - assert(entries[11].mountpoint == "/tmp/calamares-root-q_z5rdlx/run/udev"); - assert(entries[12].device == "sys"); - assert(entries[12].mountpoint == "/tmp/calamares-root-q_z5rdlx/sys"); - assert(entries[13].device == "efivarfs"); - assert(entries[13].mountpoint == "/tmp/calamares-root-q_z5rdlx/sys/firmware/efi/efivars"); + REQUIRE(entries.size() == 14); + REQUIRE(entries[0].device == "/dev/sda2"); + REQUIRE(entries[0].mountpoint == "/tmp/calamares-root-q_z5rdlx"); + REQUIRE(entries[1].device == "/dev/sda2"); + REQUIRE(entries[1].mountpoint == "/tmp/calamares-root-q_z5rdlx/home"); + REQUIRE(entries[2].device == "/dev/sda2"); + REQUIRE(entries[2].mountpoint == "/tmp/calamares-root-q_z5rdlx/root"); + REQUIRE(entries[3].device == "/dev/sda2"); + REQUIRE(entries[3].mountpoint == "/tmp/calamares-root-q_z5rdlx/srv"); + REQUIRE(entries[4].device == "/dev/sda2"); + REQUIRE(entries[4].mountpoint == "/tmp/calamares-root-q_z5rdlx/var/cache"); + REQUIRE(entries[5].device == "/dev/sda2"); + REQUIRE(entries[5].mountpoint == "/tmp/calamares-root-q_z5rdlx/var/tmp"); + REQUIRE(entries[6].device == "/dev/sda2"); + REQUIRE(entries[6].mountpoint == "/tmp/calamares-root-q_z5rdlx/var/log"); + REQUIRE(entries[7].device == "/dev/sda1"); + REQUIRE(entries[7].mountpoint == "/tmp/calamares-root-q_z5rdlx/boot"); + + REQUIRE(entries[8].device == "dev"); + REQUIRE(entries[8].mountpoint == "/tmp/calamares-root-q_z5rdlx/dev"); + REQUIRE(entries[9].device == "proc"); + REQUIRE(entries[9].mountpoint == "/tmp/calamares-root-q_z5rdlx/proc"); + REQUIRE(entries[10].device == "tmpfs"); + REQUIRE(entries[10].mountpoint == "/tmp/calamares-root-q_z5rdlx/run"); + REQUIRE(entries[11].device == "run"); + REQUIRE(entries[11].mountpoint == "/tmp/calamares-root-q_z5rdlx/run/udev"); + REQUIRE(entries[12].device == "sys"); + REQUIRE(entries[12].mountpoint == "/tmp/calamares-root-q_z5rdlx/sys"); + REQUIRE(entries[13].device == "efivarfs"); + REQUIRE(entries[13].mountpoint == "/tmp/calamares-root-q_z5rdlx/sys/firmware/efi/efivars"); } - // empty file + SECTION("empty file") { static constexpr std::string_view filename{"/tmp/mtab.conf"}; // Open mtab file for writing. std::ofstream mtab_file{filename.data()}; - assert(mtab_file.is_open()); + REQUIRE(mtab_file.is_open()); // Setup mtab file. mtab_file << ""; mtab_file.close(); const auto& mtab_entries = gucc::mtab::parse_mtab("/tmp/calamares-root-q_z5rdlx"sv, filename); - assert(!mtab_entries.has_value()); + REQUIRE(!mtab_entries.has_value()); // Cleanup. fs::remove(filename); diff --git a/gucc/tests/unit-package_profiles.cpp b/gucc/tests/unit-package_profiles.cpp index 8234f91..43c5ef9 100644 --- a/gucc/tests/unit-package_profiles.cpp +++ b/gucc/tests/unit-package_profiles.cpp @@ -1,9 +1,9 @@ +#include "doctest_compatibility.h" + #include "gucc/file_utils.hpp" #include "gucc/logger.hpp" #include "gucc/package_profiles.hpp" -#include - #include #include #include @@ -36,7 +36,8 @@ pacages = ["ca,"da","fa" packaes = ["cb","db",fb"] )"sv; -int main() { +TEST_CASE("package profiles test") +{ auto callback_sink = std::make_shared([](const spdlog::details::log_msg&) { // noop }); @@ -44,40 +45,40 @@ int main() { spdlog::set_default_logger(logger); gucc::logger::set_logger(logger); - // valid profile + SECTION("valid profile") { auto base_profs = gucc::profile::parse_base_profiles(VALID_PROFILE_TEST); - assert(base_profs); - assert((base_profs->base_packages == std::vector{"a", "b"})); - assert((base_profs->base_desktop_packages == std::vector{"c", "d", "f"})); + REQUIRE(base_profs); + REQUIRE((base_profs->base_packages == std::vector{"a", "b"})); + REQUIRE((base_profs->base_desktop_packages == std::vector{"c", "d", "f"})); auto base_desktop_profs = gucc::profile::parse_desktop_profiles(VALID_PROFILE_TEST); - assert(base_desktop_profs); - assert(base_desktop_profs->size() == 2); - assert(((*base_desktop_profs)[0].profile_name == "someprofile-1")); - assert(((*base_desktop_profs)[0].packages == std::vector{"ca", "da", "fa"})); - assert(((*base_desktop_profs)[1].profile_name == "someprofile-2")); - assert(((*base_desktop_profs)[1].packages == std::vector{"cb", "db", "fb"})); + REQUIRE(base_desktop_profs); + REQUIRE(base_desktop_profs->size() == 2); + REQUIRE(((*base_desktop_profs)[0].profile_name == "someprofile-1")); + REQUIRE(((*base_desktop_profs)[0].packages == std::vector{"ca", "da", "fa"})); + REQUIRE(((*base_desktop_profs)[1].profile_name == "someprofile-2")); + REQUIRE(((*base_desktop_profs)[1].packages == std::vector{"cb", "db", "fb"})); auto net_profs = gucc::profile::parse_net_profiles(VALID_PROFILE_TEST); - assert(net_profs); - assert((net_profs->base_profiles.base_packages == std::vector{"a", "b"})); - assert((net_profs->base_profiles.base_desktop_packages == std::vector{"c", "d", "f"})); - assert(net_profs->desktop_profiles.size() == 2); - assert((net_profs->desktop_profiles[0].profile_name == "someprofile-1")); - assert((net_profs->desktop_profiles[0].packages == std::vector{"ca", "da", "fa"})); - assert((net_profs->desktop_profiles[1].profile_name == "someprofile-2")); - assert((net_profs->desktop_profiles[1].packages == std::vector{"cb", "db", "fb"})); + REQUIRE(net_profs); + REQUIRE((net_profs->base_profiles.base_packages == std::vector{"a", "b"})); + REQUIRE((net_profs->base_profiles.base_desktop_packages == std::vector{"c", "d", "f"})); + REQUIRE(net_profs->desktop_profiles.size() == 2); + REQUIRE((net_profs->desktop_profiles[0].profile_name == "someprofile-1")); + REQUIRE((net_profs->desktop_profiles[0].packages == std::vector{"ca", "da", "fa"})); + REQUIRE((net_profs->desktop_profiles[1].profile_name == "someprofile-2")); + REQUIRE((net_profs->desktop_profiles[1].packages == std::vector{"cb", "db", "fb"})); } - // invalid profile + SECTION("invalid profile") { auto base_profs = gucc::profile::parse_base_profiles(INVALID_PROFILE_TEST); - assert(!base_profs); + REQUIRE(!base_profs); auto base_desktop_profs = gucc::profile::parse_desktop_profiles(INVALID_PROFILE_TEST); - assert(!base_desktop_profs); + REQUIRE(!base_desktop_profs); auto net_profs = gucc::profile::parse_net_profiles(INVALID_PROFILE_TEST); - assert(!net_profs); + REQUIRE(!net_profs); } } diff --git a/gucc/tests/unit-pacmanconf.cpp b/gucc/tests/unit-pacmanconf.cpp index bc20d39..26ad6f5 100644 --- a/gucc/tests/unit-pacmanconf.cpp +++ b/gucc/tests/unit-pacmanconf.cpp @@ -1,7 +1,8 @@ +#include "doctest_compatibility.h" + #include "gucc/file_utils.hpp" #include "gucc/pacmanconf_repo.hpp" -#include #include #include #include @@ -117,36 +118,39 @@ Include = /etc/pacman.d/mirrorlist Include = /etc/pacman.d/mirrorlist )"; -int main() { +TEST_CASE("pacman conf test") +{ using namespace gucc; 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()); + SECTION("get current repos") + { + REQUIRE(file_utils::create_file_for_overwrite(filename, PACMANCONF_STR)); - // Setup pacmanconf file. - pacmanconf_file << PACMANCONF_STR; - pacmanconf_file.close(); + auto repo_list = detail::pacmanconf::get_repo_list(filename); + REQUIRE(!repo_list.empty()); + REQUIRE((repo_list == std::vector{"[core]", "[extra]", "[community]", "[multilib]"})); - // 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]"})); + // Cleanup. + fs::remove(filename); + } + SECTION("push repo") + { + REQUIRE(file_utils::create_file_for_overwrite(filename, PACMANCONF_STR)); - // Push repo. - assert(detail::pacmanconf::push_repos_front(filename, "[cachyos]\nInclude = /etc/pacman.d/cachyos-mirrorlist")); + REQUIRE(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 repo list after pushing repo + auto repo_list = detail::pacmanconf::get_repo_list(filename); + REQUIRE(!repo_list.empty()); + REQUIRE((repo_list == std::vector{"[cachyos]", "[core]", "[extra]", "[community]", "[multilib]"})); - // Check if file is equal to test data. - const auto& file_content = file_utils::read_whole_file(filename); - assert(file_content == PACMANCONF_TEST); + // check if file is equal to test data + const auto& file_content = file_utils::read_whole_file(filename); + REQUIRE(file_content == PACMANCONF_TEST); - // Cleanup. - fs::remove(filename); + // Cleanup. + fs::remove(filename); + } } diff --git a/gucc/tests/unit-refind_config_gen.cpp b/gucc/tests/unit-refind_config_gen.cpp index a45c843..8207e74 100644 --- a/gucc/tests/unit-refind_config_gen.cpp +++ b/gucc/tests/unit-refind_config_gen.cpp @@ -1,9 +1,9 @@ +#include "doctest_compatibility.h" + #include "gucc/bootloader.hpp" #include "gucc/kernel_params.hpp" #include "gucc/logger.hpp" -#include - #include #include #include @@ -42,7 +42,8 @@ static constexpr auto REFIND_CONFIG_LUKS_SUBVOL_TEST = R"("Boot with standard op "Boot to single-user mode" "quiet splash rw rootflags=subvol=/@ cryptdevice=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f:luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f root=/dev/mapper/luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f" single )"sv; -int main() { +TEST_CASE("refind config gen test") +{ auto callback_sink = std::make_shared([](const spdlog::details::log_msg&) { // noop }); @@ -50,54 +51,54 @@ int main() { spdlog::set_default_logger(logger); gucc::logger::set_logger(logger); - // btrfs with subvolumes + SECTION("btrfs with subvolumes") { const std::vector kernel_params{ "quiet", "splash", "rw", "rootflags=subvol=/@", "root=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"}; const auto& refind_config_text = gucc::bootloader::gen_refind_config(kernel_params); - assert(refind_config_text == REFIND_CONFIG_SUBVOL_TEST); + REQUIRE(refind_config_text == REFIND_CONFIG_SUBVOL_TEST); } - // basic xfs + SECTION("basic xfs") { const std::vector kernel_params{ "quiet", "splash", "rw", "root=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"}; const auto& refind_config_text = gucc::bootloader::gen_refind_config(kernel_params); - assert(refind_config_text == REFIND_CONFIG_TEST); + REQUIRE(refind_config_text == REFIND_CONFIG_TEST); } - // swap xfs + SECTION("swap xfs") { const std::vector kernel_params{ "quiet", "splash", "rw", "root=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f", "resume=UUID=59848b1b-c6be-48f4-b3e1-48179ea72dec"}; const auto& refind_config_text = gucc::bootloader::gen_refind_config(kernel_params); - assert(refind_config_text == REFIND_CONFIG_SWAP_TEST); + REQUIRE(refind_config_text == REFIND_CONFIG_SWAP_TEST); } - // luks xfs + SECTION("luks xfs") { const std::vector kernel_params{ "quiet", "splash", "rw", "cryptdevice=UUID=00e1b836-81b6-433f-83ca-0fd373e3cd50:luks_device", "root=/dev/mapper/luks_device"}; const auto& refind_config_text = gucc::bootloader::gen_refind_config(kernel_params); - assert(refind_config_text == REFIND_CONFIG_LUKS_TEST); + REQUIRE(refind_config_text == REFIND_CONFIG_LUKS_TEST); } - // luks swap xfs + SECTION("luks swap xfs") { const std::vector kernel_params{ "quiet", "splash", "rw", "cryptdevice=UUID=00e1b836-81b6-433f-83ca-0fd373e3cd50:luks_device", "root=/dev/mapper/luks_device", "resume=/dev/mapper/luks_swap_device"}; const auto& refind_config_text = gucc::bootloader::gen_refind_config(kernel_params); - assert(refind_config_text == REFIND_CONFIG_LUKS_SWAP_TEST); + REQUIRE(refind_config_text == REFIND_CONFIG_LUKS_SWAP_TEST); } - // valid zfs + SECTION("valid zfs") { const std::vector kernel_params{ "quiet", "splash", "rw", "root=ZFS=zpcachyos/ROOT", "root=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"}; const auto& refind_config_text = gucc::bootloader::gen_refind_config(kernel_params); - assert(refind_config_text == REFIND_CONFIG_ZFS_TEST); + REQUIRE(refind_config_text == REFIND_CONFIG_ZFS_TEST); } - // luks btrfs with subvolumes + SECTION("luks btrfs with subvolumes") { const std::vector kernel_params{ "quiet", "splash", "rw", "rootflags=subvol=/@", "cryptdevice=UUID=6bdb3301-8efb-4b84-b0b7-4caeef26fd6f:luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f", "root=/dev/mapper/luks-6bdb3301-8efb-4b84-b0b7-4caeef26fd6f"}; const auto& refind_config_text = gucc::bootloader::gen_refind_config(kernel_params); - assert(refind_config_text == REFIND_CONFIG_LUKS_SUBVOL_TEST); + REQUIRE(refind_config_text == REFIND_CONFIG_LUKS_SUBVOL_TEST); } } diff --git a/gucc/tests/unit-refind_extra_kern_strings.cpp b/gucc/tests/unit-refind_extra_kern_strings.cpp index b629f3b..ff57ced 100644 --- a/gucc/tests/unit-refind_extra_kern_strings.cpp +++ b/gucc/tests/unit-refind_extra_kern_strings.cpp @@ -1,9 +1,9 @@ +#include "doctest_compatibility.h" + #include "gucc/bootloader.hpp" #include "gucc/file_utils.hpp" #include "gucc/logger.hpp" -#include - #include #include #include @@ -29,7 +29,8 @@ inline auto filtered_res(std::string_view content) noexcept -> std::string { return result + '\n'; } -int main() { +TEST_CASE("refind extra kern strings test") +{ auto callback_sink = std::make_shared([](const spdlog::details::log_msg&) { // noop }); @@ -40,40 +41,42 @@ int main() { // prepare test data static constexpr std::string_view file_testpath{"/tmp/test-extrakernverstr-refind.conf"}; static constexpr std::string_view file_sample_path{GUCC_TEST_DIR "/files/refind.conf-sample"}; - fs::copy_file(file_sample_path, file_testpath, fs::copy_options::overwrite_existing); - // test valid extkernstr with valid file. const std::vector extra_kernel_strings{"linux-lts", "linux", "linux-zen"}; - assert(gucc::bootloader::refind_write_extra_kern_strings(file_testpath, extra_kernel_strings)); - auto refind_conf_content = gucc::file_utils::read_whole_file(file_testpath); - auto filtered_content = filtered_res(refind_conf_content); + SECTION("test valid extkernstr with valid file") + { + fs::copy_file(file_sample_path, file_testpath, fs::copy_options::overwrite_existing); - // Cleanup. - fs::remove(file_testpath); + REQUIRE(gucc::bootloader::refind_write_extra_kern_strings(file_testpath, extra_kernel_strings)); - assert(filtered_content == REFIND_CONF_TEST); + auto refind_conf_content = gucc::file_utils::read_whole_file(file_testpath); + auto filtered_content = filtered_res(refind_conf_content); - // test empty kernel strings with valid file. - fs::copy_file(file_sample_path, file_testpath, fs::copy_options::overwrite_existing); - assert(!gucc::bootloader::refind_write_extra_kern_strings(file_testpath, {})); - auto sample_conf_content = gucc::file_utils::read_whole_file(file_testpath); - refind_conf_content = gucc::file_utils::read_whole_file(file_testpath); - assert(refind_conf_content == sample_conf_content); + // Cleanup. + fs::remove(file_testpath); - // Cleanup. - fs::remove(file_testpath); + REQUIRE_EQ(filtered_content, REFIND_CONF_TEST); + } + SECTION("test empty kernel strings with valid file") + { + fs::copy_file(file_sample_path, file_testpath, fs::copy_options::overwrite_existing); - // test empty file - std::ofstream test_file{file_testpath.data()}; - assert(test_file.is_open()); + REQUIRE(!gucc::bootloader::refind_write_extra_kern_strings(file_testpath, {})); + auto sample_conf_content = gucc::file_utils::read_whole_file(file_testpath); + auto refind_conf_content = gucc::file_utils::read_whole_file(file_testpath); + REQUIRE_EQ(refind_conf_content, sample_conf_content); - // setup file - test_file << ""; - test_file.close(); + // Cleanup. + fs::remove(file_testpath); + } + SECTION("test empty file") + { + REQUIRE(gucc::file_utils::create_file_for_overwrite(file_testpath, "")); - assert(!gucc::bootloader::refind_write_extra_kern_strings(file_testpath, extra_kernel_strings)); + REQUIRE(!gucc::bootloader::refind_write_extra_kern_strings(file_testpath, extra_kernel_strings)); - // Cleanup. - fs::remove(file_testpath); + // Cleanup. + fs::remove(file_testpath); + } } diff --git a/gucc/tests/unit.cpp b/gucc/tests/unit.cpp new file mode 100644 index 0000000..bacd335 --- /dev/null +++ b/gucc/tests/unit.cpp @@ -0,0 +1,2 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest_compatibility.h" diff --git a/meson.build b/meson.build index fce65d6..ce1d3f4 100644 --- a/meson.build +++ b/meson.build @@ -67,6 +67,7 @@ ranges = dependency('range-v3', version : ['>=0.11.0'], fallback : ['range-v3', tomlplusplus = dependency('tomlplusplus', version : ['>=3.4.0'], fallback : ['tomlplusplus', 'tomlplusplus_dep'], default_options: ['compile_library=false']) #glibmm = dependency('glibmm-2.4', version : ['>=2.56.0']) cpr = dependency('cpr', version : ['>=1.10.0'], fallback : ['cpr', 'cpr_dep']) +doctest = dependency('doctest', version : ['>=2.4.11'], fallback : ['doctest', 'doctest_dep']) src_files = files( 'src/view.hpp', diff --git a/subprojects/doctest.wrap b/subprojects/doctest.wrap new file mode 100644 index 0000000..a473de9 --- /dev/null +++ b/subprojects/doctest.wrap @@ -0,0 +1,12 @@ +[wrap-file] +directory = doctest-2.4.11 +source_url = https://github.com/doctest/doctest/archive/refs/tags/v2.4.11.tar.gz +source_filename = doctest-2.4.11.tar.gz +source_hash = 632ed2c05a7f53fa961381497bf8069093f0d6628c5f26286161fbd32a560186 +source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/doctest_2.4.11-1/doctest-2.4.11.tar.gz +wrapdb_version = 2.4.11-1 + +diff_files = doctest/0001-fix-include-doctest.patch + +[provide] +dependency_names = doctest diff --git a/subprojects/packagefiles/doctest/0001-fix-include-doctest.patch b/subprojects/packagefiles/doctest/0001-fix-include-doctest.patch new file mode 100644 index 0000000..33ada46 --- /dev/null +++ b/subprojects/packagefiles/doctest/0001-fix-include-doctest.patch @@ -0,0 +1,12 @@ +diff --git a/meson.build b/meson.build +index 585dd55..f2f193b 100644 +--- a/meson.build ++++ b/meson.build +@@ -1,6 +1,6 @@ + project('doctest', ['cpp'], version: '2.4.11') + +-doctest_dep = declare_dependency(include_directories: include_directories('doctest')) ++doctest_dep = declare_dependency(include_directories: include_directories('.')) + + if meson.version().version_compare('>=0.54.0') + meson.override_dependency('doctest', doctest_dep)