lzmainfo: Use tuklib_mbstr_wrap for --help text
[xz.git] / tests / tests.cmake
blob9302a5e42e81649bd8fd7d9d8a6f3b6323dcc6f4
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 #############################################################################
16 include(CTest)
18 if(BUILD_TESTING)
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
38         src/common
39         src/liblzma/api
40     )
42     if(WIN32)
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}"
46         )
47     endif()
49     #################
50     # liblzma tests #
51     #################
53     set(LIBLZMA_TESTS
54         test_bcj_exact_size
55         test_block_header
56         test_check
57         test_filter_flags
58         test_filter_str
59         test_hardware
60         test_index
61         test_index_hash
62         test_lzip_decoder
63         test_memlimit
64         test_stream_flags
65         test_vli
66     )
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)
76     endif()
78     foreach(TEST IN LISTS LIBLZMA_TESTS)
79         add_executable("${TEST}" "tests/${TEST}.c")
81         target_include_directories("${TEST}" PRIVATE
82             src/common
83             src/liblzma/api
84             src/liblzma
85         )
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"
93         )
95         add_test(NAME "${TEST}"
96                  COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
97         )
99         # Set srcdir environment variable so that the tests find their
100         # input files from the source tree.
101         #
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"
105             SKIP_RETURN_CODE 77
106         )
107     endforeach()
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)
128     else()
129         set(HAVE_ALL_ENCODERS OFF)
130     endif()
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)
137     else()
138         set(HAVE_ALL_DECODERS OFF)
139     endif()
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)
149     else()
150         set(HAVE_ALL_CHECKS OFF)
151     endif()
153     # test_scripts.sh only needs LZMA2 decoder and CRC32.
154     if(ENABLE_SCRIPTS)
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"
160         )
162         set_tests_properties(test_scripts.sh PROPERTIES
163             ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
164             SKIP_RETURN_CODE 77
165         )
166     endif()
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"
175         )
177         set_tests_properties(test_suffix.sh PROPERTIES
178             SKIP_RETURN_CODE 77
179         )
180     endif()
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"
198                         "${T}" ".."
199                 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_compress"
200             )
202             set_tests_properties("test_${T}" PROPERTIES
203                 ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
204                 SKIP_RETURN_CODE 77
205             )
206         endforeach()
207     endif()
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"
221         )
223         set_tests_properties(test_files.sh PROPERTIES
224             ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
225             SKIP_RETURN_CODE 77
226         )
227     endif()
228 endif()