From c279605abf4feb9bb1f8176a004b88c9fcdbe2a4 Mon Sep 17 00:00:00 2001 From: "Schaich, Alonso" Date: Sat, 28 Jun 2025 01:24:04 +0900 Subject: [PATCH] Add an option to have ctest apply the required test migration Generate cmake targets that invoke `testdata_tom` to apply the test schema. Use cmake here in order to be able to invoke the binary independend of debug suffices or binary paths. A Generator expression could also have emitted $ in oder to determine the filename of the emitted binaries, however, `file(GENERATE OUTPUT)` does not support target dependend generator expression. Rather then generating additional test cases for the `testdata_tom` binary and adding them to a ctest testsuite in a fixture where the migration is a FIXTURE_SETUP to the other tests to ensure it gets executed prior to the other unit tests, generate a CTestCustom.cmake script which, unlike the content in CMakeLists.txt, gets executed at test-time, so a variation of the environment variables is detected. Add a new environment variable named `DB_AUTOMIGRATE` to enable schema migration prior to the test suite invokation. CTestCustom.cmake is way less capable to process cmake options then CMakeLists.txt, due to CMP0012 causing "ON" to be interpreted as false until cmake-4.0 which removes legacy support for CMP0012. Therefore, keep the ctest definition simple for the time being Fixes silverqx/TinyORM#67 . --- CMakeLists.txt | 2 ++ CTestCustom.cmake.in | 14 ++++++++++++++ tests/testdata_tom/CMakeLists.txt | 9 +++++++++ 3 files changed, 25 insertions(+) create mode 100644 CTestCustom.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ef0ec0ed..da2e1dab5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -467,6 +467,8 @@ if(BUILD_TESTS) find_package(Qt${QT_VERSION_MAJOR} ${minReqQtVersion} REQUIRED COMPONENTS Test) add_subdirectory(tests) + + configure_file(CTestCustom.cmake.in CTestCustom.cmake @ONLY) endif() # Build examples diff --git a/CTestCustom.cmake.in b/CTestCustom.cmake.in new file mode 100644 index 000000000..421bd4b60 --- /dev/null +++ b/CTestCustom.cmake.in @@ -0,0 +1,14 @@ +if(DEFINED ENV{DB_AUTOMIGRATE}) + if(DEFINED ENV{DB_MYSQL_DATABASE}) + execute_process(COMMAND @CMAKE_COMMAND@ --build . --target tom_testdata_migrate_mysql COMMAND_ECHO STDOUT) + endif() + if(DEFINED ENV{DB_POSTGRES_DATABASE}) + execute_process(COMMAND @CMAKE_COMMAND@ --build . --target tom_testdata_migrate_postgres COMMAND_ECHO STDOUT) + endif() + if(DEFINED ENV{DB_SQLITE_DATABASE}) + execute_process(COMMAND @CMAKE_COMMAND@ --build . --target tom_testdata_migrate_sqlite COMMAND_ECHO STDOUT) + endif() +else() + message("DB_AUTOMIGRATE not set, skipping automigration") +endif() + diff --git a/tests/testdata_tom/CMakeLists.txt b/tests/testdata_tom/CMakeLists.txt index f8c68aafe..d9945aaad 100644 --- a/tests/testdata_tom/CMakeLists.txt +++ b/tests/testdata_tom/CMakeLists.txt @@ -91,3 +91,12 @@ if(NOT STRICT_MODE) endif() target_link_libraries(${TomTestData_target} PRIVATE ${TinyOrm_ns}::${TinyOrm_target}) + + +# All unit tests are functional only if the test databases are migrated prior to test execution. +# +# These targets are provided in order to simplify test setup +# --- +add_custom_target(tom_testdata_migrate_mysql COMMAND tom_testdata migrate:fresh --database=tinyorm_testdata_tom_mysql --seed --no-ansi DEPENDS ${TomTestData_target}) +add_custom_target(tom_testdata_migrate_postgres COMMAND tom_testdata migrate:fresh --database=tinyorm_testdata_tom_postgres --seed --no-ansi DEPENDS ${TomTestData_target}) +add_custom_target(tom_testdata_migrate_sqlite COMMAND tom_testdata migrate:fresh --database=tinyorm_testdata_tom_sqlite --seed --no-ansi DEPENDS ${TomTestData_target})