Tests/Windows: Add the application manifest to the test programs
[xz/debian.git] / tests / tests.cmake
blob9174251dab9df5ca1a00523dedd2cfce74236ca6
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 (MICROLZMA_ENCODER AND (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 "${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 "${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(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)
150     else()
151         set(HAVE_ALL_CHECK_TYPES OFF)
152     endif()
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"
161         )
163         set_tests_properties(test_scripts.sh PROPERTIES
164             ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
165             SKIP_RETURN_CODE 77
166         )
167     endif()
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"
176         )
178         set_tests_properties(test_suffix.sh PROPERTIES
179             SKIP_RETURN_CODE 77
180         )
181     endif()
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"
199                         "${T}" ".."
200                 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_compress"
201             )
203             set_tests_properties("test_${T}" PROPERTIES
204                 ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
205                 SKIP_RETURN_CODE 77
206             )
207         endforeach()
208     endif()
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"
222         )
224         set_tests_properties(test_files.sh PROPERTIES
225             ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
226             SKIP_RETURN_CODE 77
227         )
228     endif()
229 endif()