Skip to content

Commit

Permalink
👷 gucc: use doctest instead of raw C asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Aug 9, 2024
1 parent deaf0e8 commit b3929ef
Show file tree
Hide file tree
Showing 21 changed files with 530 additions and 378 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions gucc/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
#############################################################################
Expand All @@ -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} $<TARGET_OBJECTS:doctest_main> ${file})
target_compile_definitions(${testcase} PRIVATE DOCTEST_CONFIG_SUPER_FAST_ASSERTS)
target_compile_options(${testcase} PRIVATE
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated;-Wno-float-equal>
$<$<CXX_COMPILER_ID:GNU>:-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()
32 changes: 32 additions & 0 deletions gucc/tests/doctest_compatibility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef DOCTEST_COMPATIBILITY_H

Check notice on line 1 in gucc/tests/doctest_compatibility.h

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on gucc/tests/doctest_compatibility.h

File gucc/tests/doctest_compatibility.h does not conform to Custom style guidelines. (lines 5, 17, 21, 22, 23, 24, 26)
#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 <doctest/doctest.h>

Check failure on line 6 in gucc/tests/doctest_compatibility.h

View workflow job for this annotation

GitHub Actions / cpp-linter

gucc/tests/doctest_compatibility.h:6:10 [clang-diagnostic-error]

'doctest/doctest.h' file not found

// 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 <string>...)
#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
60 changes: 34 additions & 26 deletions gucc/tests/meson.build
Original file line number Diff line number Diff line change
@@ -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)
59 changes: 30 additions & 29 deletions gucc/tests/unit-btrfs.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "doctest_compatibility.h"

Check notice on line 1 in gucc/tests/unit-btrfs.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on gucc/tests/unit-btrfs.cpp

File gucc/tests/unit-btrfs.cpp does not conform to Custom style guidelines. (lines 16, 32, 48, 64, 80, 94, 109, 126)

#include "gucc/btrfs.hpp"
#include "gucc/logger.hpp"

#include <cassert>

#include <string>
#include <string_view>
#include <vector>
Expand All @@ -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<spdlog::sinks::callback_sink_mt>([](const spdlog::details::log_msg&) {
// noop
});
Expand All @@ -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<gucc::fs::Partition> 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},
Expand All @@ -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<gucc::fs::Partition> 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},
Expand All @@ -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<gucc::fs::Partition> 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},
Expand All @@ -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<gucc::fs::Partition> 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},
Expand All @@ -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<gucc::fs::Partition> 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},
Expand All @@ -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<gucc::fs::Partition> 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},
Expand All @@ -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<gucc::fs::Partition> 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},
Expand All @@ -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);
}
}
Loading

0 comments on commit b3929ef

Please sign in to comment.