1 # SPDX-License-Identifier: 0BSD
3 #############################################################################
5 # Optional file to be included by the top-level CMakeLists.txt to run tests
7 # The CMake rules for the tests are in this separate optional file so
8 # that it's trivial to just delete the whole "tests" directory and still
9 # get an otherwise normal CMake-based build. This way it's easy to ensure
10 # that nothing in the "tests" directory can affect the build process.
12 # Author: Lasse Collin
14 #############################################################################
19 ################################
20 # Windows Application Manifest #
21 ################################
23 # On Windows (but not on Cygwin or MSYS2) we want to add the
24 # application manifest to the test programs so that they are
25 # run in the same configuration as the programs that are installed.
26 # The same object file can be used for all test programs.
27 add_library(tests_w32res OBJECT)
29 # CMake requires that even an object library must have at least once
30 # source file. Give it a header file that results in no output files.
31 target_sources(tests_w32res PRIVATE tests/tests.h)
33 # The Ninja Generator requires setting the linker language since it
34 # cannot guess the programming language of a header file.
35 set_target_properties(tests_w32res PROPERTIES LINKER_LANGUAGE C)
37 target_include_directories(tests_w32res PRIVATE src/common
43 target_sources(tests_w32res PRIVATE tests/tests_w32res.rc)
44 set_source_files_properties(tests/tests_w32res.rc PROPERTIES
45 OBJECT_DEPENDS "${W32RES_DEPENDENCIES}"
68 # MicroLZMA encoder is needed for both encoder and decoder tests.
69 # If MicroLZMA decoder is not configured but LZMA1 decoder is, then
70 # test_microlzma will fail to compile because this configuration is
71 # not possible in the Autotools build, so the test was not made to
72 # support it since it would have required additional changes.
73 if (XZ_MICROLZMA_ENCODER AND (XZ_MICROLZMA_DECODER
74 OR NOT "lzma1" IN_LIST DECODERS))
75 list(APPEND LIBLZMA_TESTS test_microlzma)
78 foreach(TEST IN LISTS LIBLZMA_TESTS)
79 add_executable("${TEST}" "tests/${TEST}.c")
81 target_include_directories("${TEST}" PRIVATE
87 target_link_libraries("${TEST}" PRIVATE liblzma tests_w32res)
89 # Put the test programs into their own subdirectory so they don't
90 # pollute the top-level dir which might contain xz and xzdec.
91 set_target_properties("${TEST}" PROPERTIES
92 RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
95 add_test(NAME "${TEST}"
96 COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
99 # Set srcdir environment variable so that the tests find their
100 # input files from the source tree.
102 # Set the return code for skipped tests to match Automake convention.
103 set_tests_properties("${TEST}" PROPERTIES
104 ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
110 ###########################
111 # Command line tool tests #
112 ###########################
114 # Since the CMake-based build doesn't use config.h, the test scripts
115 # cannot grep the contents of config.h to know which features have
116 # been disabled. When config.h is missing, they assume that all
117 # features are enabled. Thus, check if certain groups of features have
118 # been disabled and then possibly skip some of the tests entirely instead
119 # of letting them fail.
120 set(SUPPORTED_FILTERS_SORTED "${SUPPORTED_FILTERS}")
121 list(SORT SUPPORTED_FILTERS_SORTED)
123 set(ENCODERS_SORTED "${XZ_ENCODERS}")
124 list(SORT ENCODERS_SORTED)
126 if("${ENCODERS_SORTED}" STREQUAL "${SUPPORTED_FILTERS_SORTED}")
127 set(HAVE_ALL_ENCODERS ON)
129 set(HAVE_ALL_ENCODERS OFF)
132 set(DECODERS_SORTED "${XZ_DECODERS}")
133 list(SORT DECODERS_SORTED)
135 if("${DECODERS_SORTED}" STREQUAL "${SUPPORTED_FILTERS_SORTED}")
136 set(HAVE_ALL_DECODERS ON)
138 set(HAVE_ALL_DECODERS OFF)
141 set(SUPPORTED_CHECKS_SORTED "${SUPPORTED_CHECKS}")
142 list(SORT SUPPORTED_CHECKS_SORTED)
144 set(XZ_CHECKS_SORTED "${XZ_CHECKS}")
145 list(SORT XZ_CHECKS_SORTED)
147 if("${SUPPORTED_CHECKS_SORTED}" STREQUAL "${XZ_CHECKS_SORTED}")
148 set(HAVE_ALL_CHECKS ON)
150 set(HAVE_ALL_CHECKS OFF)
153 # test_scripts.sh only needs LZMA2 decoder and CRC32.
155 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_scripts")
157 add_test(NAME test_scripts.sh
158 COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_scripts.sh" ".."
159 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_scripts"
162 set_tests_properties(test_scripts.sh PROPERTIES
163 ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
168 # test_suffix.sh only needs LZMA2 encoder and decoder.
169 if(UNIX AND HAVE_ENCODERS AND HAVE_DECODERS)
170 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_suffix")
172 add_test(NAME test_suffix.sh
173 COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_suffix.sh" ".."
174 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_suffix"
177 set_tests_properties(test_suffix.sh PROPERTIES
182 # The test_compress.sh based tests compress and decompress using different
183 # filters so run it only if all encoders and decoders have been enabled.
184 if(UNIX AND HAVE_ALL_ENCODERS AND HAVE_ALL_DECODERS)
185 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_compress")
187 add_executable(create_compress_files tests/create_compress_files.c)
188 target_link_libraries(create_compress_files PRIVATE tests_w32res)
189 target_include_directories(create_compress_files PRIVATE src/common)
190 set_target_properties(create_compress_files PROPERTIES
191 RUNTIME_OUTPUT_DIRECTORY test_compress)
193 foreach(T compress_generated_abc
194 compress_generated_text
195 compress_generated_random)
196 add_test(NAME "test_${T}"
197 COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_compress.sh"
199 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_compress"
202 set_tests_properties("test_${T}" PROPERTIES
203 ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
209 # test_files.sh decompresses files that use different filters and
210 # check types so run it only if support for all of them has been enabled.
211 if(UNIX AND HAVE_ALL_DECODERS AND HAVE_ALL_CHECKS AND XZ_LZIP_DECODER)
212 # test_files.sh doesn't make any temporary files but it
213 # must not be run at the top-level build directory because
214 # it checks if ../config.h exists. We don't want to read
215 # files outside the build directory!
216 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_files")
218 add_test(NAME test_files.sh
219 COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_files.sh" ".."
220 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_files"
223 set_tests_properties(test_files.sh PROPERTIES
224 ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"