From 42ba3b4af9f761108500721f73f35efbae1bce5f Mon Sep 17 00:00:00 2001 From: Staaaszek Date: Thu, 29 Oct 2026 11:24:30 +0100 Subject: [PATCH 1/5] Build all sources except main.cpp as the email_client_core library The tests can then link against the same code as the client. Also adds the BUILD_TESTS option and turns off libxml2's own tests. --- CMakeLists.txt | 33 +++++++++++++++++++++++++-------- src/backend/CMakeLists.txt | 4 ++-- src/frontend/CMakeLists.txt | 2 +- src/logging/CMakeLists.txt | 2 +- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f25029..4956436 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ set(PKGVER_VMIME 2cfa8185f9c11fc38a56e90a8bd048759b45585c) set(PKGVER_TVISION 85aaeca0999c8b53006c556ccda063f143d259ca) option(ENABLE_LOG "Enable logging" OFF) +option(BUILD_TESTS "Build the unit tests (fetches GoogleTest)" OFF) option(EXTERNAL_VMIME "If OFF, then build VMime and link to it for development" OFF) option(EXTERNAL_SQLITE "If OFF, then build SQLite3 and link to it for development" OFF) @@ -80,6 +81,8 @@ FetchContent_Declare(libxml2 EXCLUDE_FROM_ALL ) if (NOT EXTERNAL_LIBXML2) + # libxml2's own tests are not built, so keep them out of ctest + set(LIBXML2_WITH_TESTS OFF CACHE BOOL "" FORCE) FetchContent_MakeAvailable(libxml2) else() find_package(libxml2 REQUIRED) @@ -148,9 +151,9 @@ endif() # == END FETCH LIBRARIES =============================================== -# Define the email_client executable -add_executable(email_client - src/main.cpp +# All sources except main.cpp go into a static library, so the tests can link +# against the same code as the email_client executable +add_library(email_client_core STATIC src/app.cpp ) message("libs ${_deferred_link_libraries}") @@ -160,16 +163,30 @@ message("incdirs ${_deferred_include_directories}") if(NOT EXTERNAL_VMIME) # VMime is an ExternalProject, so CMake doesn't know our sources need its headers # and library; without this, parallel builds compile them before VMime is installed - add_dependencies(email_client vmime) + add_dependencies(email_client_core vmime) endif() # Include headers and subdirectories -target_include_directories(email_client PUBLIC src) -target_include_directories(email_client PRIVATE ${_deferred_include_directories}) +target_include_directories(email_client_core PUBLIC src) +target_include_directories(email_client_core PUBLIC ${_deferred_include_directories}) add_subdirectory(src/backend) add_subdirectory(src/frontend) add_subdirectory(src/logging) -target_link_libraries(email_client PRIVATE ${_deferred_link_libraries}) -target_link_directories(email_client PRIVATE ${_deferred_link_directories}) +# Dependencies are PUBLIC, so everything linking email_client_core (the executable +# and the tests) gets their headers and libraries too +list(REMOVE_ITEM _deferred_link_libraries PRIVATE) +target_link_libraries(email_client_core PUBLIC ${_deferred_link_libraries}) +target_link_directories(email_client_core PUBLIC ${_deferred_link_directories}) + +# Define the email_client executable +add_executable(email_client + src/main.cpp +) +target_link_libraries(email_client PRIVATE email_client_core) + +if(BUILD_TESTS) + enable_testing() + add_subdirectory(tests) +endif() diff --git a/src/backend/CMakeLists.txt b/src/backend/CMakeLists.txt index 2d6dd31..ef0e36f 100644 --- a/src/backend/CMakeLists.txt +++ b/src/backend/CMakeLists.txt @@ -1,5 +1,5 @@ -# Add source files from backend to the email_client target -target_sources(email_client +# Add source files from backend to the email_client_core library +target_sources(email_client_core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/mailbox.cpp ${CMAKE_CURRENT_SOURCE_DIR}/html_parser.cpp diff --git a/src/frontend/CMakeLists.txt b/src/frontend/CMakeLists.txt index f341a8e..862cafc 100644 --- a/src/frontend/CMakeLists.txt +++ b/src/frontend/CMakeLists.txt @@ -1,4 +1,4 @@ -target_sources(email_client +target_sources(email_client_core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/app_frontend.cpp PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/email_window.cpp PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/login_dialog.cpp diff --git a/src/logging/CMakeLists.txt b/src/logging/CMakeLists.txt index a6c4905..2f1c623 100644 --- a/src/logging/CMakeLists.txt +++ b/src/logging/CMakeLists.txt @@ -1,4 +1,4 @@ -target_sources(email_client +target_sources(email_client_core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/logging.cpp ) From a749d0247350a550c205c5284ddede976685e5b5 Mon Sep 17 00:00:00 2001 From: Staaaszek Date: Thu, 29 Oct 2026 11:24:30 +0100 Subject: [PATCH 2/5] Add GoogleTest with an example test --- Justfile | 6 ++++++ README.md | 30 ++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 27 +++++++++++++++++++++++++++ tests/backend/html_parser_test.cpp | 13 +++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/backend/html_parser_test.cpp diff --git a/Justfile b/Justfile index 979fdc5..a2dc4e0 100644 --- a/Justfile +++ b/Justfile @@ -10,6 +10,12 @@ buildrun: run: ./build/email_client +# Build with the unit tests and run them; extra args go to ctest, e.g. `just test -R HtmlParser` +test *args: + cmake -S . -B build -DBUILD_TESTS=ON + cmake --build build --parallel {{num_cpus()}} + ctest --test-dir build/tests --output-on-failure {{args}} + clean: rm -rf build diff --git a/README.md b/README.md index 1aabba8..8124f77 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,36 @@ Run `dev/mail-server.sh` without arguments to see all commands. With `just`: - GreenMail keeps mail in memory only, so stopping the server clears all mailboxes. +### Tests + +Unit tests use [GoogleTest](https://google.github.io/googletest/) (with GMock). +They are off by default, so a normal build does not download GoogleTest. + +```sh +cmake -S . -B build -DBUILD_TESTS=ON +cmake --build build --parallel "$(nproc)" +ctest --test-dir build/tests --output-on-failure +``` + +Or `just test`. Extra arguments go to `ctest`, e.g. `just test -R HtmlParser` +runs only the tests whose name matches `HtmlParser`. + +All sources except `src/main.cpp` are built into the `email_client_core` +library, which both the client and the tests link against. CI (GitHub +Actions) builds the project and runs the tests on every pull request and on +every push to `main`. + +Conventions (see `tests/backend/html_parser_test.cpp` for an example): + +- Test files mirror `src/`: tests for `src/backend/html_parser.cpp` go in + `tests/backend/html_parser_test.cpp`. Add new files to `tests/CMakeLists.txt`. +- Suite name: the tested class or file + `Test`, e.g. `HtmlParserTest`. +- Test name: what the code should do, e.g. `DecodeQuotedPrintableDecodesHexEscapes`. +- Use `EXPECT_*` by default. Use `ASSERT_*` only when the rest of the test + makes no sense after a failure, e.g. before using an element of a vector + whose size was just checked. +- Unit tests must not use the network or the user's config. + ## License This project is licensed under the GNU GPL version 3 (or later). See the diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..d21b7e9 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,27 @@ +set(PKGVER_GOOGLETEST v1.17.0) + +# GoogleTest is only linked into the test binary, so build it statically +# (the top-level CMakeLists.txt turns BUILD_SHARED_LIBS on) +set(BUILD_SHARED_LIBS OFF) +set(INSTALL_GTEST OFF CACHE BOOL "" FORCE) + +FetchContent_Declare(googletest + URL https://github.com/google/googletest/archive/refs/tags/${PKGVER_GOOGLETEST}.tar.gz + EXCLUDE_FROM_ALL +) +FetchContent_MakeAvailable(googletest) + +include(GoogleTest) + +# One test binary for the whole project. Test files mirror src/, +# e.g. src/backend/html_parser.cpp is tested in tests/backend/html_parser_test.cpp +add_executable(email_client_tests + backend/html_parser_test.cpp +) +target_link_libraries(email_client_tests PRIVATE + email_client_core + GTest::gmock_main +) +target_compile_options(email_client_tests PRIVATE -Wall -Wextra -Wpedantic) + +gtest_discover_tests(email_client_tests) diff --git a/tests/backend/html_parser_test.cpp b/tests/backend/html_parser_test.cpp new file mode 100644 index 0000000..73fe53f --- /dev/null +++ b/tests/backend/html_parser_test.cpp @@ -0,0 +1,13 @@ +#include "backend/html_parser.h" + +#include + +// Test suite name: the tested class or file + "Test". +// Test name: what the code should do, written as a sentence. +TEST(HtmlParserTest, DecodeQuotedPrintableDecodesHexEscapes) { + // EXPECT_* reports a failure and continues, ASSERT_* stops the test. + // Use ASSERT_* only when the rest of the test makes no sense after a failure. + EXPECT_EQ(HtmlParser::decode_quoted_printable("caf=C3=A9"), "caf\xC3\xA9"); + EXPECT_EQ(HtmlParser::decode_quoted_printable("1+1=3D2"), "1+1=2"); + EXPECT_EQ(HtmlParser::decode_quoted_printable("no escapes"), "no escapes"); +} From d7b9ca9c0314c8739a05bbd87fddae465aedad15 Mon Sep 17 00:00:00 2001 From: Staaaszek Date: Thu, 29 Oct 2026 11:24:30 +0100 Subject: [PATCH 3/5] Add CI that builds the project and runs the tests --- .github/workflows/ci.yml | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..21a8dd6 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,35 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + +jobs: + build-and-test: + runs-on: ubuntu-24.04 + env: + # Also picked up by the VMime ExternalProject, so its build is cached too + CMAKE_C_COMPILER_LAUNCHER: ccache + CMAKE_CXX_COMPILER_LAUNCHER: ccache + steps: + - uses: actions/checkout@v5 + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y libgnutls28-dev libicu-dev libncurses-dev libgpm-dev + + - name: Set up ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: ${{ runner.os }}-${{ hashFiles('CMakeLists.txt') }} + + - name: Configure + run: cmake -S . -B build -DBUILD_TESTS=ON + + - name: Build + run: cmake --build build --parallel "$(nproc)" + + - name: Test + run: ctest --test-dir build/tests --output-on-failure From f2da06b77bdcc7b5caffbfd80583766a04757455 Mon Sep 17 00:00:00 2001 From: Staaaszek Date: Thu, 29 Oct 2026 14:09:38 +0100 Subject: [PATCH 4/5] Run CI on Arch Linux and on every push Arch is the target platform (AUR); the Ubuntu job stays as a stable baseline. Outdated runs on the same branch are cancelled, except on main. --- .github/workflows/ci.yml | 49 ++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 21a8dd6..d1ce38c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,17 +1,52 @@ name: CI +# Every push to any branch, plus pull requests (needed for PRs from forks, +# whose pushes do not run workflows in this repository) on: push: - branches: [main] pull_request: +# A newer push to the same branch or PR cancels the older, now outdated run; +# on main every commit is tested to the end +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + +env: + # Also picked up by the VMime ExternalProject, so its build is cached too + CMAKE_C_COMPILER_LAUNCHER: ccache + CMAKE_CXX_COMPILER_LAUNCHER: ccache + jobs: - build-and-test: + # The target platform (the client is packaged for the AUR). Arch is a rolling + # release, so this job can start failing after a package update with no change + # on our side; the Ubuntu job then shows whether our code or the update is at fault. + arch: + runs-on: ubuntu-24.04 + container: archlinux:latest + steps: + # Before checkout: actions/checkout needs git, and FetchContent clones with it + - name: Install dependencies + run: pacman -Syu --noconfirm --needed base-devel cmake git gnutls icu ncurses gpm ccache + + - uses: actions/checkout@v5 + + - name: Set up ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: arch-${{ hashFiles('CMakeLists.txt') }} + + - name: Configure + run: cmake -S . -B build -DBUILD_TESTS=ON + + - name: Build + run: cmake --build build --parallel "$(nproc)" + + - name: Test + run: ctest --test-dir build/tests --output-on-failure + + ubuntu: runs-on: ubuntu-24.04 - env: - # Also picked up by the VMime ExternalProject, so its build is cached too - CMAKE_C_COMPILER_LAUNCHER: ccache - CMAKE_CXX_COMPILER_LAUNCHER: ccache steps: - uses: actions/checkout@v5 @@ -23,7 +58,7 @@ jobs: - name: Set up ccache uses: hendrikmuhs/ccache-action@v1.2 with: - key: ${{ runner.os }}-${{ hashFiles('CMakeLists.txt') }} + key: ubuntu-${{ hashFiles('CMakeLists.txt') }} - name: Configure run: cmake -S . -B build -DBUILD_TESTS=ON From b719e50a5dffef8e0eace61140afffa334fdb109 Mon Sep 17 00:00:00 2001 From: Staaaszek Date: Thu, 29 Oct 2026 16:35:58 +0100 Subject: [PATCH 5/5] Update ccache-action to v1.2.24, which runs on Node.js 24 The v1.2 tag still points to a release that targets the deprecated Node.js 20. --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d1ce38c..cfb04c9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,7 +32,7 @@ jobs: - uses: actions/checkout@v5 - name: Set up ccache - uses: hendrikmuhs/ccache-action@v1.2 + uses: hendrikmuhs/ccache-action@v1.2.24 with: key: arch-${{ hashFiles('CMakeLists.txt') }} @@ -56,7 +56,7 @@ jobs: sudo apt-get install -y libgnutls28-dev libicu-dev libncurses-dev libgpm-dev - name: Set up ccache - uses: hendrikmuhs/ccache-action@v1.2 + uses: hendrikmuhs/ccache-action@v1.2.24 with: key: ubuntu-${{ hashFiles('CMakeLists.txt') }}