From 5748e0a496342883a6db9a62d924315533462dc1 Mon Sep 17 00:00:00 2001 From: light7734 Date: Tue, 15 Jul 2025 16:49:34 +0330 Subject: [PATCH 1/8] ci: add unit tests Dockerfile --- tools/ci/images/unit_tests/Dockerfile | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tools/ci/images/unit_tests/Dockerfile diff --git a/tools/ci/images/unit_tests/Dockerfile b/tools/ci/images/unit_tests/Dockerfile new file mode 100644 index 0000000..453253f --- /dev/null +++ b/tools/ci/images/unit_tests/Dockerfile @@ -0,0 +1,37 @@ +FROM alpine:latest + +RUN apk add --no-cache \ + bash \ + clang \ + llvm \ + cmake \ + git \ + make \ + g++ \ + python3 \ + py3-pip \ + mesa-dev \ + mesa-gl \ + pkgconf + + +RUN pip install --no-cache-dir --break-system-packages conan gitpython \ + && conan profile detect + +RUN clang --version \ + && conan --version \ + && pip --version \ + && cmake --version \ + && clang --version + + +RUN git clone 'https://git.light7734.com/light7734/light.git' --recursive \ + && cd light \ + && conan install . \ + -s build_type=Debug \ + -c tools.system.package_manager:mode=install \ + --build=missing \ + && conan install . \ + -s build_type=Release \ + -c tools.system.package_manager:mode=install \ + --build=missing -- 2.45.3 From 5bcb4cfdd318a923fef7bf1478dd02edd44af8d3 Mon Sep 17 00:00:00 2001 From: light7734 Date: Wed, 16 Jul 2025 11:03:42 +0330 Subject: [PATCH 2/8] ci: add unit testing to drone-ci --- .drone.yml | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/.drone.yml b/.drone.yml index 8474519..c98ed16 100644 --- a/.drone.yml +++ b/.drone.yml @@ -32,8 +32,43 @@ steps: fi exit ${has_fomatting_issues} ---- +--- +kind: pipeline +type: docker +name: unit tests +clone: + recursive: true + submodule_update_remote: true + +trigger: + branch: + - main + +steps: +- name: unit tests + image: unit_tests:latest + pull: if-not-exists + commands: + - | + git submodule update --init --recursive + + conan build . \ + -c tools.system.package_manager:mode=install \ + -s build_type=Release \ + -o enable_static_analysis=False \ + -o enable_tests=True \ + --build=missing + + find ./build \ + -type f \ + -name '*_tests' \ + -executable \ + -exec sh -c \ + '{}' \ + \; + +--- kind: pipeline type: docker name: static analysis @@ -51,5 +86,12 @@ steps: pull: if-not-exists privileged: true commands: - - git submodule update --init --recursive - - conan build . -s build_type=Release -o enable_static_analysis=True --build=missing + - | + git submodule update --init --recursive + + conan build . \ + -c tools.system.package_manager:mode=install \ + -s build_type=Release \ + -o enable_static_analysis=True \ + -o enable_tests=True \ + --build=missing -- 2.45.3 From 60944b9d494a4ed68aeaa39a0fa9e9168ac58a9c Mon Sep 17 00:00:00 2001 From: light7734 Date: Wed, 16 Jul 2025 11:37:02 +0330 Subject: [PATCH 3/8] fix: build error on gcc --- modules/window/src/linux/window.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/window/src/linux/window.cpp b/modules/window/src/linux/window.cpp index 91834de..0b9cb27 100644 --- a/modules/window/src/linux/window.cpp +++ b/modules/window/src/linux/window.cpp @@ -55,7 +55,7 @@ void lWindow::on_event(const Event &event) on_window_resize(dynamic_cast(event)); break; - default: + default: break; } } -- 2.45.3 From 5f1c65d72d23d3da321feae9c1b1b1818defa196 Mon Sep 17 00:00:00 2001 From: light7734 Date: Wed, 16 Jul 2025 11:41:16 +0330 Subject: [PATCH 4/8] fix: conflicting declaration build error from gcc --- modules/ecs/src/uuid.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/ecs/src/uuid.cpp b/modules/ecs/src/uuid.cpp index c84dd39..09a55cc 100644 --- a/modules/ecs/src/uuid.cpp +++ b/modules/ecs/src/uuid.cpp @@ -2,8 +2,10 @@ namespace lt { -auto UUID::s_engine = std::mt19937_64(std::random_device()()); -auto UUID::s_distribution = std::uniform_int_distribution {}; +std::mt19937_64 UUID::s_engine = std::mt19937_64(std::random_device()()); + +std::uniform_int_distribution + UUID::s_distribution = std::uniform_int_distribution {}; UUID::UUID(uint64_t uuid /* = -1 */): m_uuid(uuid == -1 ? s_distribution(s_engine) : uuid) { -- 2.45.3 From c76d6e8019a907a39237bc2575fbdba6c2cc3c6c Mon Sep 17 00:00:00 2001 From: light7734 Date: Wed, 16 Jul 2025 12:44:58 +0330 Subject: [PATCH 5/8] feat: test executables will exit with failing code if any tests fails --- modules/test/include/test/test.hpp | 93 ++++++++++++++++++------------ modules/test/src/entrypoint.cpp | 2 +- 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/modules/test/include/test/test.hpp b/modules/test/include/test/test.hpp index 57d3988..e0f5f11 100644 --- a/modules/test/include/test/test.hpp +++ b/modules/test/include/test/test.hpp @@ -26,6 +26,62 @@ concept test = requires(T test) { } // namespace concepts +namespace details { + + +class Registry +{ +public: + using Suite = void (*)(); + + static void register_suite(Suite suite) + { + instance().m_suites.emplace_back(suite); + } + + static auto run_all() -> int32_t + { + for (auto &test : instance().m_suites) + { + test(); + } + + std::cout << "_________________________[TEST RESULTS]_________________________"; + std::cout << "Ran " << instance().m_failed_count + instance().m_pasesed_count << " tests:\n" + << "Passed: " << instance().m_pasesed_count << '\n' + << "Failed: " << instance().m_failed_count << '\n'; + + return instance().m_failed_count; + } + + static void increment_passed_count() + { + ++instance().m_pasesed_count; + } + + static void increment_failed_count() + { + ++instance().m_failed_count; + } + +private: + Registry() = default; + + [[nodiscard]] static auto instance() -> Registry & + { + static auto registry = Registry {}; + return registry; + } + + std::vector m_suites; + + int32_t m_pasesed_count {}; + int32_t m_failed_count {}; +}; + + +} // namespace details + struct Case { auto operator=(std::invocable auto test) -> void // NOLINT @@ -40,6 +96,7 @@ struct Case { std::cout << " --> FAIL !" << '\n'; std::cout << exp.what() << "\n\n"; + details::Registry::increment_failed_count(); return; // TODO(Light): Should we run the remaining tests after a failure? } @@ -49,42 +106,6 @@ struct Case std::string_view name; }; -namespace details { - - -class Registry -{ -public: - using Suite = void (*)(); - - static void register_suite(Suite suite) - { - instance().m_suites.emplace_back(suite); - } - - static void run_all() - { - for (auto &test : instance().m_suites) - { - test(); - } - } - -private: - Registry() = default; - - [[nodiscard]] static auto instance() -> Registry & - { - static auto registry = Registry {}; - return registry; - } - - std::vector m_suites; -}; - - -} // namespace details - struct TestSuite { template diff --git a/modules/test/src/entrypoint.cpp b/modules/test/src/entrypoint.cpp index fc2a861..5404f4a 100644 --- a/modules/test/src/entrypoint.cpp +++ b/modules/test/src/entrypoint.cpp @@ -6,7 +6,7 @@ try using namespace ::lt::test; using namespace ::lt::test::details; - Registry::run_all(); + return Registry::run_all(); } catch (const std::exception &exp) { -- 2.45.3 From 61c898f47fec7278c75348d628d2e3cc71105a03 Mon Sep 17 00:00:00 2001 From: light7734 Date: Wed, 16 Jul 2025 12:50:37 +0330 Subject: [PATCH 6/8] ci: fix ci step not failing on test failure --- .drone.yml | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/.drone.yml b/.drone.yml index c98ed16..8707deb 100644 --- a/.drone.yml +++ b/.drone.yml @@ -51,8 +51,9 @@ steps: pull: if-not-exists commands: - | - git submodule update --init --recursive + set -e + git submodule update --init --recursive conan build . \ -c tools.system.package_manager:mode=install \ -s build_type=Release \ @@ -60,13 +61,10 @@ steps: -o enable_tests=True \ --build=missing - find ./build \ - -type f \ - -name '*_tests' \ - -executable \ - -exec sh -c \ - '{}' \ - \; + for test in $(find ./build -type f -name '*_tests' -executable); do + echo "Running $test" + "$test" + done --- kind: pipeline -- 2.45.3 From f457e5ae1981b5548b46a25725df4b233907cfa2 Mon Sep 17 00:00:00 2001 From: light7734 Date: Wed, 16 Jul 2025 13:18:08 +0330 Subject: [PATCH 7/8] refactor: test result output issues --- modules/test/include/test/test.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/test/include/test/test.hpp b/modules/test/include/test/test.hpp index e0f5f11..7282202 100644 --- a/modules/test/include/test/test.hpp +++ b/modules/test/include/test/test.hpp @@ -46,10 +46,10 @@ public: test(); } - std::cout << "_________________________[TEST RESULTS]_________________________"; + std::cout << "_________________________[TEST RESULTS]_________________________\n"; std::cout << "Ran " << instance().m_failed_count + instance().m_pasesed_count << " tests:\n" - << "Passed: " << instance().m_pasesed_count << '\n' - << "Failed: " << instance().m_failed_count << '\n'; + << "\tpassed: " << instance().m_pasesed_count << '\n' + << "\tfailed: " << instance().m_failed_count << '\n'; return instance().m_failed_count; } @@ -100,6 +100,7 @@ struct Case return; // TODO(Light): Should we run the remaining tests after a failure? } + details::Registry::increment_passed_count(); std::cout << " --> SUCCESS :D" << "\n"; } -- 2.45.3 From c4b9bd835904fb08b1d4c5d0cd38a0c982fce43e Mon Sep 17 00:00:00 2001 From: light7734 Date: Wed, 16 Jul 2025 13:20:36 +0330 Subject: [PATCH 8/8] fix: failing tests in test.tests.cpp --- modules/test/src/test.tests.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/modules/test/src/test.tests.cpp b/modules/test/src/test.tests.cpp index 95fd8c6..6661af3 100644 --- a/modules/test/src/test.tests.cpp +++ b/modules/test/src/test.tests.cpp @@ -2,6 +2,7 @@ lt::test::Suite meta = []() { using lt::test::expect_eq; + using lt::test::expect_true; lt::test::Case { "test_1" } = [] { expect_eq(5, 5); @@ -12,7 +13,18 @@ lt::test::Suite meta = []() { }; lt::test::Case { "test_3" } = [] { - expect_eq(true, false); + auto exception_thrown = false; + + try + { + expect_eq(true, false); + } + catch (const std::exception &exp) + { + exception_thrown = true; + } + + expect_true(exception_thrown); }; lt::test::Case { "test_4" } = [] { @@ -20,6 +32,5 @@ lt::test::Suite meta = []() { }; lt::test::Case { "test_5" } = [] { - throw std::runtime_error("Uncaught std exception!"); }; }; -- 2.45.3