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 (MICROLZMA_ENCODER AND (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 "${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 "${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(ADDITIONAL_SUPPORTED_CHECKS_SORTED "${ADDITIONAL_SUPPORTED_CHECKS}")
142 list(SORT ADDITIONAL_SUPPORTED_CHECKS_SORTED)
144 set(ADDITIONAL_CHECK_TYPES_SORTED "${ADDITIONAL_CHECK_TYPES}")
145 list(SORT ADDITIONAL_CHECK_TYPES_SORTED)
147 if("${ADDITIONAL_SUPPORTED_CHECKS_SORTED}" STREQUAL
148 "${ADDITIONAL_CHECK_TYPES_SORTED}")
149 set(HAVE_ALL_CHECK_TYPES ON)
151 set(HAVE_ALL_CHECK_TYPES OFF)
154 # test_scripts.sh only needs LZMA2 decoder and CRC32.
155 if(UNIX AND HAVE_DECODERS)
156 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_scripts")
158 add_test(NAME test_scripts.sh
159 COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_scripts.sh" ".."
160 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_scripts"
163 set_tests_properties(test_scripts.sh PROPERTIES
164 ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
169 # test_suffix.sh only needs LZMA2 encoder and decoder.
170 if(UNIX AND HAVE_ENCODERS AND HAVE_DECODERS)
171 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_suffix")
173 add_test(NAME test_suffix.sh
174 COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_suffix.sh" ".."
175 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_suffix"
178 set_tests_properties(test_suffix.sh PROPERTIES
183 # The test_compress.sh based tests compress and decompress using different
184 # filters so run it only if all encoders and decoders have been enabled.
185 if(UNIX AND HAVE_ALL_ENCODERS AND HAVE_ALL_DECODERS)
186 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_compress")
188 add_executable(create_compress_files tests/create_compress_files.c)
189 target_link_libraries(create_compress_files PRIVATE tests_w32res)
190 target_include_directories(create_compress_files PRIVATE src/common)
191 set_target_properties(create_compress_files PROPERTIES
192 RUNTIME_OUTPUT_DIRECTORY test_compress)
194 foreach(T compress_generated_abc
195 compress_generated_text
196 compress_generated_random)
197 add_test(NAME "test_${T}"
198 COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_compress.sh"
200 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_compress"
203 set_tests_properties("test_${T}" PROPERTIES
204 ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
210 # test_files.sh decompresses files that use different filters and
211 # check types so run it only if support for all of them has been enabled.
212 if(UNIX AND HAVE_ALL_DECODERS AND HAVE_ALL_CHECK_TYPES AND LZIP_DECODER)
213 # test_files.sh doesn't make any temporary files but it
214 # must not be run at the top-level build directory because
215 # it checks if ../config.h exists. We don't want to read
216 # files outside the build directory!
217 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_files")
219 add_test(NAME test_files.sh
220 COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_files.sh" ".."
221 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_files"
224 set_tests_properties(test_files.sh PROPERTIES
225 ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"