11# =============================================================================
22# tests/CMakeLists.txt
3- #
4- # Builds and registers the self-contained docx_comment_parser test suite.
5- # Invoked automatically by the parent CMakeLists when BUILD_TESTS=ON (default).
6- #
7- # Usage:
8- # cmake -B build -DBUILD_TESTS=ON
9- # cmake --build build
10- # ctest --test-dir build --output-on-failure
113# =============================================================================
124
135cmake_minimum_required (VERSION 3.15 )
146
15- # ─── zlib is needed by the test's own ZIP builder (uses crc32/inflate) ────────
16- # On MSVC the vendored zlib.h is used (same as zip_reader.cpp).
17- # On Linux/macOS/MinGW we link the system libz.
7+ # ─── zlib ────────────────────────────────────────────────────────────────────
8+ # On MSVC: crc32 body is compiled into the test TU via VENDOR_ZLIB_IMPLEMENTATION;
9+ # no library link needed.
10+ # On Linux / macOS / MinGW: link the system libz.
1811if (NOT MSVC )
1912 find_package (ZLIB REQUIRED )
2013endif ()
@@ -24,7 +17,6 @@ add_executable(test_docx_parser
2417 test_docx_parser.cpp
2518)
2619
27- # Inherit C++17 from parent; make it explicit here as a safety net
2820set_target_properties (test_docx_parser PROPERTIES
2921 CXX_STANDARD 17
3022 CXX_STANDARD_REQUIRED ON
@@ -33,52 +25,85 @@ set_target_properties(test_docx_parser PROPERTIES
3325
3426target_include_directories (test_docx_parser
3527 PRIVATE
36- # Public headers of the library under test
3728 ${CMAKE_SOURCE_DIR} /include
38- # Vendor dir: test_docx_parser.cpp includes vendor/zlib/zlib.h on MSVC
3929 $<$<CXX_COMPILER_ID :MSVC >:${CMAKE_SOURCE_DIR} /vendor >
4030)
4131
4232target_link_libraries (test_docx_parser
4333 PRIVATE
44- # The shared library being tested (provides docx:: API)
4534 docx_comment_parser
46- # zlib — crc32() is called by the in-memory ZIP builder in the test.
47- # On MSVC the vendored header is used; no link step is needed there.
4835 $<$<NOT :$<CXX_COMPILER_ID :MSVC >>:ZLIB ::ZLIB >
4936)
5037
5138target_compile_options (test_docx_parser PRIVATE
5239 $<$<CXX_COMPILER_ID :GNU ,Clang >:-Wall -Wextra -Wpedantic >
53- # Debug build: keep symbols for readable stack traces
54- $<$<CONFIG :Debug >:-g -O0 >
55- # Release build: full optimisation, suppress debug info
56- $<$<CONFIG :Release >:-O2 -DNDEBUG >
40+ $<$<AND :$<CONFIG :Debug >,$<CXX_COMPILER_ID :GNU ,Clang >>:-g -O0 >
41+ $<$<AND :$<CONFIG :Release >,$<CXX_COMPILER_ID :GNU ,Clang >>:-O2 -DNDEBUG >
5742)
5843
59- # ─── CTest registration ───────────────────────────────────────────────────────
60- # One test entry per logical group so CTest can report them individually.
61- # All groups are driven by the single binary; the binary's own main() returns
62- # non-zero on any failure, which CTest treats as a test failure.
44+ # ─── Windows DLL placement ────────────────────────────────────────────────────
45+ # The runtime loader searches the exe's own directory first. Copy our DLL
46+ # there so the test runs without any extra PATH setup.
47+ if (WIN32 )
48+ add_custom_command (TARGET test_docx_parser POST_BUILD
49+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
50+ $<TARGET_FILE :docx_comment_parser >
51+ $<TARGET_FILE_DIR :test_docx_parser >
52+ COMMENT "Copying docx_comment_parser DLL next to test executable"
53+ VERBATIM
54+ )
55+
56+ # On MinGW: also copy zlib1.dll (runtime dependency of our DLL) and
57+ # pre-resolve the compiler bin dir for use in ENVIRONMENT_MODIFICATION below.
58+ if (MINGW)
59+ get_filename_component (_mingw_bin "${CMAKE_CXX_COMPILER} " DIRECTORY )
60+
61+ find_file (_zlib1_dll
62+ NAMES zlib1.dll
63+ HINTS "${_mingw_bin} "
64+ NO_DEFAULT_PATH
65+ )
66+ if (_zlib1_dll)
67+ add_custom_command (TARGET test_docx_parser POST_BUILD
68+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
69+ "${_zlib1_dll} "
70+ $<TARGET_FILE_DIR :test_docx_parser >
71+ COMMENT "Copying zlib1.dll next to test executable"
72+ VERBATIM
73+ )
74+ endif ()
75+ endif ()
76+ endif ()
6377
78+ # ─── CTest registration ───────────────────────────────────────────────────────
6479add_test (
6580 NAME DocxParser.BasicParsing
6681 COMMAND test_docx_parser
6782)
6883
69- # Working directory: project root so that relative paths (e.g. sample .docx
70- # files placed there in future) resolve correctly.
7184set_tests_properties (DocxParser.BasicParsing PROPERTIES
7285 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR} "
73- # Fail the test if it takes more than 30 seconds (catches infinite loops)
7486 TIMEOUT 30
75- # Pass these back to CTest's output for easier diagnosis
7687 LABELS "unit;docx;parser"
7788)
7889
90+ # Prepend the MinGW compiler bin dir to PATH so CTest finds libgcc_s_seh-1.dll,
91+ # libstdc++-6.dll, libwinpthread-1.dll even when MSYS2 is not on the system PATH.
92+ # Must be set AFTER add_test().
93+ # ENVIRONMENT_MODIFICATION (CMake 3.22+) correctly handles the OS path separator.
94+ if (WIN32 AND MINGW)
95+ if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.22" )
96+ set_tests_properties (DocxParser.BasicParsing PROPERTIES
97+ ENVIRONMENT_MODIFICATION
98+ "PATH=path_list_prepend:${_mingw_bin} "
99+ )
100+ endif ()
101+ # For CMake < 3.22 the DLL copies above are the fallback: zlib1.dll is
102+ # copied next to the exe; libgcc / libstdc++ / libwinpthread are assumed
103+ # to be reachable through the existing system PATH (standard MSYS2 setup).
104+ endif ()
105+
79106# ─── Optional: valgrind memory-check target ───────────────────────────────────
80- # Creates a second CTest entry that runs the binary under valgrind when it is
81- # available. Skipped silently on systems without valgrind.
82107find_program (VALGRIND_EXECUTABLE valgrind )
83108if (VALGRIND_EXECUTABLE)
84109 add_test (
0 commit comments