Skip to content

Commit ec7bade

Browse files
committed
Add two tests: with/without running test suite in static initializer
1 parent 287806b commit ec7bade

File tree

3 files changed

+93
-23
lines changed

3 files changed

+93
-23
lines changed

CMakeLists.txt

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -304,19 +304,46 @@ if(BUILD_TESTING)
304304
)
305305

306306
#
307-
# tests_PythonQtDynamicLoading: This test checks that a dynamically loaded library (itself linked against
308-
# PythonQt) properly initializes PythonQt.
307+
# PythonQtDynamicLoading tests
309308
#
309+
# These tests check that a dynamically loaded library (itself linked against PythonQt) properly
310+
# initializes PythonQt and allow the PythonQt testing suites to complete.
311+
#
312+
# Infrastructure:
313+
#
314+
# * PythonQtDynamicLoaderSharedLibrary:
315+
# - Shared library linked against PythonQt
316+
# - export two "C compatible" symbols:
317+
# 'this_function_returns_42': function that is expected to return the integer 42
318+
# 'run_pythonqt_tests' : function running python test suite if RUN_TESTSUITE_IN_STATIC_INITIALIZER is 0
319+
# - contain a statically initialized structure named 'StaticInitializer':
320+
# - always call 'PythonQt::init()'
321+
# - running python test suite if RUN_TESTSUITE_IN_STATIC_INITIALIZER is 1
322+
#
323+
#
324+
# * PythonQtDynamicLoader: Executable loading the 'PythonQtDynamicLoaderSharedLibrary' using QLibrary
325+
# - resolve symbols 'this_function_returns_42' and 'run_pythonqt_tests' and call the corresponding function.
326+
#
327+
#
328+
# To summarize, two cases are considered:
329+
#
330+
# (a) RUN_TESTSUITE_IN_STATIC_INITIALIZER = 0
331+
#
332+
# - PythonQt::init(...) called in StaticInitializer
333+
# - Python test suite executed in function 'run_pythonqt_tests'
334+
#
335+
# Name of test: tests_PythonQtDynamicLoading_RUN_TESTSUITE_IN_STATIC_INITIALIZER_0
336+
#
337+
#
338+
# (b) RUN_TESTSUITE_IN_STATIC_INITIALIZER = 1
339+
#
340+
# - PythonQt::init(...) called in 'StaticInitializer'
341+
# - Python test suite executed in 'StaticInitializer'
342+
#
343+
# Name of test: tests_PythonQtDynamicLoading_RUN_TESTSUITE_IN_STATIC_INITIALIZER_1
310344

311-
add_library(PythonQtDynamicLoaderSharedLibrary SHARED
312-
tests/PythonQtDynamicLoaderSharedLibrary.cpp
313-
${PythonQtTests_sources}
314-
)
315-
target_link_libraries(PythonQtDynamicLoaderSharedLibrary PythonQt)
316-
317-
add_executable(PythonQtDynamicLoader tests/PythonQtDynamicLoader.cpp)
318-
target_link_libraries(PythonQtDynamicLoader ${QT_LIBRARIES})
319-
345+
# Configure 'test_wrapper' common to all 'PythonQtDynamicLoading' tests
346+
#
320347
# Since setting the ENVIRONMENT property value with semicolon (e.g. PATH) is not supported, we
321348
# generate a wrapper script to update PATH and LD_LIBRARY_PATH.
322349
set(test_wrapper "${CMAKE_CURRENT_BINARY_DIR}/test_wrapper.cmake")
@@ -332,10 +359,42 @@ if(NOT result EQUAL 0)
332359
endif()
333360
")
334361

335-
add_test(
336-
NAME tests_PythonQtDynamicLoading
337-
COMMAND ${CMAKE_COMMAND} -DTARGET_FILE=$<TARGET_FILE:PythonQtDynamicLoader> -P ${test_wrapper}
338-
)
362+
function(_add_dynamic_loading_test run_testsuite_in_static_initializer)
363+
364+
set(suffix 0)
365+
if(run_testsuite_in_static_initializer)
366+
set(suffix 1)
367+
endif()
368+
369+
add_library(PythonQtDynamicLoaderSharedLibrary_${suffix} SHARED
370+
tests/PythonQtDynamicLoaderSharedLibrary.cpp
371+
${PythonQtTests_sources}
372+
)
373+
target_link_libraries(PythonQtDynamicLoaderSharedLibrary_${suffix} PythonQt)
374+
375+
add_executable(PythonQtDynamicLoader_${suffix} tests/PythonQtDynamicLoader.cpp)
376+
target_link_libraries(PythonQtDynamicLoader_${suffix} ${QT_LIBRARIES})
377+
378+
if(run_testsuite_in_static_initializer)
379+
set_target_properties(
380+
PythonQtDynamicLoaderSharedLibrary_${suffix}
381+
PythonQtDynamicLoader_${suffix}
382+
PROPERTIES COMPILE_DEFINITIONS "RUN_TESTSUITE_IN_STATIC_INITIALIZER"
383+
)
384+
endif()
385+
386+
add_test(
387+
NAME tests_PythonQtDynamicLoading_RUN_TESTSUITE_IN_STATIC_INITIALIZER_${suffix}
388+
COMMAND ${CMAKE_COMMAND} -DTARGET_FILE=$<TARGET_FILE:PythonQtDynamicLoader_${suffix}> -P ${test_wrapper}
389+
)
390+
391+
endfunction()
392+
393+
# RUN_TESTSUITE_IN_STATIC_INITIALIZER = 0
394+
_add_dynamic_loading_test(0)
395+
396+
# RUN_TESTSUITE_IN_STATIC_INITIALIZER = 1
397+
_add_dynamic_loading_test(1)
339398

340399
endif()
341400

tests/PythonQtDynamicLoader.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#include <cstdlib>
77
#include <iostream>
88

9+
#ifndef RUN_TESTSUITE_IN_STATIC_INITIALIZER
10+
# define DYNAMIC_LOADER_SHARED_LIBRARY_NAME "PythonQtDynamicLoaderSharedLibrary_0"
11+
#else
12+
# define DYNAMIC_LOADER_SHARED_LIBRARY_NAME "PythonQtDynamicLoaderSharedLibrary_1"
13+
#endif
14+
915
int main(int argc, char* argv[])
1016
{
1117
QApplication qapp(argc, argv);
@@ -17,10 +23,11 @@ int main(int argc, char* argv[])
1723
#endif
1824

1925

20-
QLibrary library("PythonQtDynamicLoaderSharedLibrary");
26+
QLibrary library(DYNAMIC_LOADER_SHARED_LIBRARY_NAME);
27+
std::cout << "Loading '" << DYNAMIC_LOADER_SHARED_LIBRARY_NAME << "'" << std::endl;
2128
if (!library.load())
2229
{
23-
std::cerr << "Failed to load 'PythonQtDynamicLoaderSharedLibrary': "
30+
std::cerr << "Failed to load '" << DYNAMIC_LOADER_SHARED_LIBRARY_NAME << "': "
2431
<< qPrintable(library.errorString()) << std::endl;
2532
return EXIT_FAILURE;
2633
}

tests/PythonQtDynamicLoaderSharedLibrary.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#define MY_EXPORT
1010
#endif
1111

12-
#define RUN_TEST_IN_STATIC_INITIALIZER
13-
1412
extern "C"
1513
{
1614

@@ -21,6 +19,8 @@ extern "C"
2119

2220
int _run_pythonqt_tests(int argc, char* argv[])
2321
{
22+
std::cout << " running test suite" << std::endl;
23+
2424
// Copied from PythonQtTestMain.cpp
2525
int failCount = 0;
2626
PythonQtTestApi api;
@@ -42,11 +42,13 @@ extern "C"
4242

4343
int MY_EXPORT run_pythonqt_tests(int argc, char* argv[])
4444
{
45-
#ifndef RUN_TEST_IN_STATIC_INITIALIZER
45+
std::cout << "run_pythonqt_tests:" << std::endl;
46+
#ifndef RUN_TESTSUITE_IN_STATIC_INITIALIZER
4647
return _run_pythonqt_tests(argc, argv);
4748
#else
4849
Q_UNUSED(argc);
4950
Q_UNUSED(argv);
51+
std::cout << " no test suite" << std::endl;
5052
return 0;
5153
#endif
5254
}
@@ -57,13 +59,15 @@ struct StaticInitializer
5759
{
5860
StaticInitializer()
5961
{
60-
std::cout << "PythonQtDynamicLoaderSharedLibrary::StaticInitializer" << std::endl;
62+
std::cout << "StaticInitializer:" << std::endl;
6163
PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
6264

63-
#ifdef RUN_TEST_IN_STATIC_INITIALIZER
65+
#ifdef RUN_TESTSUITE_IN_STATIC_INITIALIZER
6466
int argc = 1;
65-
char * argv [] = {"RunTestInStaticInitializer"};
67+
char * argv [] = {"RunTestSuiteInStaticInitializer"};
6668
_run_pythonqt_tests(argc, argv);
69+
#else
70+
std::cout << " no test suite" << std::endl;
6771
#endif
6872
}
6973
};

0 commit comments

Comments
 (0)