summaryrefslogtreecommitdiffstats
path: root/tests/tests.cmake
blob: 62c546ced3e65c7ca62465ad9278a29833845b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# SPDX-License-Identifier: 0BSD

#############################################################################
#
# Optional file to be included by the top-level CMakeLists.txt to run tests
#
# The CMake rules for the tests are in this separate optional file so
# that it's trivial to just delete the whole "tests" directory and still
# get an otherwise normal CMake-based build. This way it's easy to ensure
# that nothing in the "tests" directory can affect the build process.
#
# Author: Lasse Collin
#
#############################################################################

include(CTest)

if(BUILD_TESTING)
    #################
    # liblzma tests #
    #################

    set(LIBLZMA_TESTS
        test_bcj_exact_size
        test_block_header
        test_check
        test_filter_flags
        test_filter_str
        test_hardware
        test_index
        test_index_hash
        test_lzip_decoder
        test_memlimit
        test_stream_flags
        test_vli
    )

    # MicroLZMA encoder is needed for both encoder and decoder tests.
    # If MicroLZMA decoder is not configured but LZMA1 decoder is, then
    # test_microlzma will fail to compile because this configuration is
    # not possible in the Autotools build, so the test was not made to
    # support it since it would have required additional changes.
    if (MICROLZMA_ENCODER AND (MICROLZMA_DECODER
            OR NOT "lzma1" IN_LIST DECODERS))
        list(APPEND LIBLZMA_TESTS test_microlzma)
    endif()

    foreach(TEST IN LISTS LIBLZMA_TESTS)
        add_executable("${TEST}" "tests/${TEST}.c")

        target_include_directories("${TEST}" PRIVATE
            src/common
            src/liblzma/api
            src/liblzma
        )

        target_link_libraries("${TEST}" PRIVATE liblzma)

        # Put the test programs into their own subdirectory so they don't
        # pollute the top-level dir which might contain xz and xzdec.
        set_target_properties("${TEST}" PROPERTIES
            RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
        )

        add_test(NAME "${TEST}"
                 COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
        )

        # Set srcdir environment variable so that the tests find their
        # input files from the source tree.
        #
        # Set the return code for skipped tests to match Automake convention.
        set_tests_properties("${TEST}" PROPERTIES
            ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
            SKIP_RETURN_CODE 77
        )
    endforeach()


    ###########################
    # Command line tool tests #
    ###########################

    # Since the CMake-based build doesn't use config.h, the test scripts
    # cannot grep the contents of config.h to know which features have
    # been disabled. When config.h is missing, they assume that all
    # features are enabled. Thus, check if certain groups of features have
    # been disabled and then possibly skip some of the tests entirely instead
    # of letting them fail.
    set(SUPPORTED_FILTERS_SORTED "${SUPPORTED_FILTERS}")
    list(SORT SUPPORTED_FILTERS_SORTED)

    set(ENCODERS_SORTED "${ENCODERS}")
    list(SORT ENCODERS_SORTED)

    if("${ENCODERS_SORTED}" STREQUAL "${SUPPORTED_FILTERS_SORTED}")
        set(HAVE_ALL_ENCODERS ON)
    else()
        set(HAVE_ALL_ENCODERS OFF)
    endif()

    set(DECODERS_SORTED "${DECODERS}")
    list(SORT DECODERS_SORTED)

    if("${DECODERS_SORTED}" STREQUAL "${SUPPORTED_FILTERS_SORTED}")
        set(HAVE_ALL_DECODERS ON)
    else()
        set(HAVE_ALL_DECODERS OFF)
    endif()

    set(ADDITIONAL_SUPPORTED_CHECKS_SORTED "${ADDITIONAL_SUPPORTED_CHECKS}")
    list(SORT ADDITIONAL_SUPPORTED_CHECKS_SORTED)

    set(ADDITIONAL_CHECK_TYPES_SORTED "${ADDITIONAL_CHECK_TYPES}")
    list(SORT ADDITIONAL_CHECK_TYPES_SORTED)

    if("${ADDITIONAL_SUPPORTED_CHECKS_SORTED}" STREQUAL
        "${ADDITIONAL_CHECK_TYPES_SORTED}")
        set(HAVE_ALL_CHECK_TYPES ON)
    else()
        set(HAVE_ALL_CHECK_TYPES OFF)
    endif()

    # test_scripts.sh only needs LZMA2 decoder and CRC32.
    if(UNIX AND HAVE_DECODERS)
        file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_scripts")

        add_test(NAME test_scripts.sh
            COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_scripts.sh" ".."
            WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_scripts"
        )

        set_tests_properties(test_scripts.sh PROPERTIES
            ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
            SKIP_RETURN_CODE 77
        )
    endif()

    # test_suffix.sh only needs LZMA2 encoder and decoder.
    if(UNIX AND HAVE_ENCODERS AND HAVE_DECODERS)
        file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_suffix")

        add_test(NAME test_suffix.sh
            COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_suffix.sh" ".."
            WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_suffix"
        )

        set_tests_properties(test_suffix.sh PROPERTIES
            SKIP_RETURN_CODE 77
        )
    endif()

    # The test_compress.sh based tests compress and decompress using different
    # filters so run it only if all encoders and decoders have been enabled.
    if(UNIX AND HAVE_ALL_ENCODERS AND HAVE_ALL_DECODERS)
        file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_compress")

        add_executable(create_compress_files tests/create_compress_files.c)
        target_include_directories(create_compress_files PRIVATE src/common)
        set_target_properties(create_compress_files PROPERTIES
                              RUNTIME_OUTPUT_DIRECTORY test_compress)

        foreach(T compress_generated_abc
                  compress_generated_text
                  compress_generated_random)
            add_test(NAME "test_${T}"
                COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_compress.sh"
                        "${T}" ".."
                WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_compress"
            )

            set_tests_properties("test_${T}" PROPERTIES
                ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
                SKIP_RETURN_CODE 77
            )
        endforeach()
    endif()

    # test_files.sh decompresses files that use different filters and
    # check types so run it only if support for all of them has been enabled.
    if(UNIX AND HAVE_ALL_DECODERS AND HAVE_ALL_CHECK_TYPES AND LZIP_DECODER)
        # test_files.sh doesn't make any temporary files but it
        # must not be run at the top-level build directory because
        # it checks if ../config.h exists. We don't want to read
        # files outside the build directory!
        file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_files")

        add_test(NAME test_files.sh
            COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_files.sh" ".."
            WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_files"
        )

        set_tests_properties(test_files.sh PROPERTIES
            ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
            SKIP_RETURN_CODE 77
        )
    endif()
endif()