diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..cfb04c9 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,70 @@ +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: + 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: + # 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.24 + 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 + 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.24 + with: + key: ubuntu-${{ 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 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/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/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 ) 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"); +}