Debian Codegraph @

cmake-proxies/cmake-modules/AudacityTesting.cmake

raw
1 if( ${_OPT}has_tests )
2
3 set( TESTS_DIR "${CMAKE_BINARY_DIR}/tests" )
4
5 # Setup default CTest arguments when running from IDE
6
7 set(CMAKE_CTEST_ARGUMENTS "--output-on-failure;--verbose;${CMAKE_CTEST_ARGUMENTS}")
8
9 enable_testing()
10
11 #[[
12 add_unit_test(NAME name [MOCK_PREFS] [MOCK_AUDIO] SOURCES file1 ... LIBRARIES lib1 ...)
13
14 If MOCK_PREFS is specified, a test can instantiate a mocked Prefs object.
15 If MOCK_AUDIO is specified, a test will initialize PortAudio.
16
17 Audio mocking is a subject to change when Audio I/O is refactored.
18
19 Creates an executable called ${name}-test from the source files ${file1}, ... and linked
20 to libraries ${lib1}, ... Catch2 is linked implicitly.
21
22 Create a CTest test called ${name}, that expects 0 code on success
23 ]]
24 function( add_unit_test )
25 cmake_parse_arguments(
26 ADD_UNIT_TEST # Prefix
27 "MOCK_PREFS;MOCK_AUDIO;WAV_FILE_IO" # Options
28 "NAME" # One value keywords
29 "SOURCES;LIBRARIES"
30 ${ARGN}
31 )
32
33 if( NOT ADD_UNIT_TEST_NAME )
34 message( FATAL_ERROR "Missing required NAME parameter for the add_unit_test")
35 endif()
36
37 set( test_executable_name "${ADD_UNIT_TEST_NAME}-test" )
38
39 # Create test executable
40
41 add_executable( ${test_executable_name} ${ADD_UNIT_TEST_SOURCES} "${CMAKE_SOURCE_DIR}/tests/Catch2Main.cpp")
42 target_link_libraries( ${test_executable_name} PRIVATE ${ADD_UNIT_TEST_LIBRARIES} Catch2::Catch2 )
43
44 if (ADD_UNIT_TEST_MOCK_PREFS)
45 target_compile_definitions( ${test_executable_name} PRIVATE MOCK_PREFS )
46 target_sources( ${test_executable_name} PRIVATE "${CMAKE_SOURCE_DIR}/tests/MockedPrefs.cpp" "${CMAKE_SOURCE_DIR}/tests/MockedPrefs.h" )
47 target_include_directories( ${test_executable_name} PRIVATE "${CMAKE_SOURCE_DIR}/tests" )
48 target_link_libraries( ${test_executable_name} PRIVATE lib-preferences-interface )
49 endif()
50
51 if (ADD_UNIT_TEST_MOCK_AUDIO)
52 target_compile_definitions( ${test_executable_name} PRIVATE MOCK_AUDIO )
53 target_sources( ${test_executable_name} PRIVATE "${CMAKE_SOURCE_DIR}/tests/MockedAudio.cpp" "${CMAKE_SOURCE_DIR}/tests/MockedAudio.h" )
54 endif()
55
56 if (ADD_UNIT_TEST_WAV_FILE_IO)
57 target_sources( ${test_executable_name} PRIVATE
58 "${CMAKE_SOURCE_DIR}/tests/AudioFileInfo.h"
59 "${CMAKE_SOURCE_DIR}/tests/AudioFileIO.cpp"
60 "${CMAKE_SOURCE_DIR}/tests/AudioFileIO.h"
61 "${CMAKE_SOURCE_DIR}/tests/Mp3FileReader.cpp"
62 "${CMAKE_SOURCE_DIR}/tests/Mp3FileReader.h"
63 "${CMAKE_SOURCE_DIR}/tests/WavFileIO.cpp"
64 "${CMAKE_SOURCE_DIR}/tests/WavFileIO.h"
65 )
66 target_include_directories( ${test_executable_name} PRIVATE "${CMAKE_SOURCE_DIR}/tests" )
67 target_link_libraries( ${test_executable_name} PRIVATE
68 SndFile::sndfile
69 mpg123::libmpg123
70 )
71 endif()
72
73 set( OPTIONS )
74 audacity_append_common_compiler_options( OPTIONS NO )
75 target_compile_options( ${test_executable_name} ${OPTIONS} )
76
77 set_target_properties(
78 ${test_executable_name}
79 PROPERTIES
80 FOLDER "tests" # for IDE organization
81 RUNTIME_OUTPUT_DIRECTORY "${TESTS_DIR}"
82 BUILD_RPATH "${_DESTDIR}/${_PKGLIB}"
83 # Allow running tests from Visual Studio by setting up the proper PATH
84 VS_DEBUGGER_ENVIRONMENT "PATH=${_DESTDIR}/${_PKGLIB};%PATH%"
85 )
86
87 # Register unit test with CTest
88
89 add_test(
90 NAME
91 ${ADD_UNIT_TEST_NAME}
92 COMMAND
93 ${test_executable_name}
94 WORKING_DIRECTORY
95 ${CMAKE_SOURCE_DIR}
96 )
97
98 set_tests_properties(
99 ${ADD_UNIT_TEST_NAME}
100 PROPERTIES
101 LABELS "unit_tests"
102 )
103
104 if( WIN32 )
105 # On Windows, set the PATH so it points to the DLL location
106
107 # CTest expects that ENVIRONMENT is a CMake list.
108 # Escape ';' so PATH is handled correctly
109 string(REPLACE ";" "\\;" escaped_path "$ENV{PATH}")
110
111 set_tests_properties(
112 ${ADD_UNIT_TEST_NAME}
113 PROPERTIES
114 ENVIRONMENT "PATH=$<SHELL_PATH:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIG>>\\;${escaped_path}"
115 )
116 elseif( APPLE )
117 # We target an old version of macOS, disable std::uncaught_exceptions
118 target_compile_definitions( ${test_executable_name} PRIVATE CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS )
119 # Similar to Windows, but uses DYLD_FALLBACK_LIBRARY_PATH instead of PATH
120 set_tests_properties(
121 ${ADD_UNIT_TEST_NAME}
122 PROPERTIES
123 ENVIRONMENT "DYLD_FALLBACK_LIBRARY_PATH=$<SHELL_PATH:${CMAKE_BINARY_DIR}/$<CONFIG>/${_APPDIR}/Frameworks>"
124 )
125 endif()
126 endfunction()
127
128 set( JOURNAL_TEST_TIMEOUT_SECONDS 180 )
129
130 #[[
131 add_journal_test(journal_file)
132
133 Adds a test, that runs Audacity with the journal ${journal_file}.
134
135 Test name is based on the name component of the ${journal_file}
136 ]]
137 function( add_journal_test journal_file )
138 get_filename_component(test_name ${journal_file} NAME_WE)
139
140 if( APPLE )
141 # On macOS CMake will generate a placeholder that CTest fails to handle correctly,
142 # so we have to setup the path manually
143 set( audacity_target "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIG>/Audacity.app/Contents/MacOS/Audacity" )
144 elseif (WIN32)
145 set( audacity_target
146 powershell
147 -ExecutionPolicy Bypass
148 -File "${CMAKE_SOURCE_DIR}/tests/journals/test_runner.ps1"
149 "$<TARGET_FILE:Audacity>"
150 --timeout ${JOURNAL_TEST_TIMEOUT_SECONDS}
151 )
152 else()
153 set( audacity_target "$<TARGET_FILE:Audacity>" )
154 endif()
155
156 # Adds a test that invokes Audacity with the given journal file
157
158 add_test(
159 NAME
160 ${test_name}
161 COMMAND
162 ${audacity_target} --journal ${journal_file}
163 )
164
165 set_tests_properties(
166 ${test_name}
167 PROPERTIES
168 LABELS "journal_tests"
169 TIMEOUT ${JOURNAL_TEST_TIMEOUT_SECONDS}
170 )
171 endfunction()
172 else()
173 # Just a placeholder for the cases unit testing is disabled
174 function(add_unit_test)
175 endfunction()
176
177 function( add_journal_test journal_file )
178 endfunction()
179 endif()