
include_directories("${CMAKE_HOME_DIRECTORY}/src")

aux_source_directory(. SRCS)

if (WIN32)
    #message(STATUS ${SRCS})
    list(REMOVE_ITEM SRCS ./test_press_syslog.c)
    list(REMOVE_ITEM SRCS ./test_syslog.c)
    list(REMOVE_ITEM SRCS ./test_press_write.c)
    list(REMOVE_ITEM SRCS ./test_press_write2.c)
    list(REMOVE_ITEM SRCS ./test_press_zlog.c)
    list(REMOVE_ITEM SRCS ./test_press_zlog2.c)
    #message(STATUS ${SRCS})
endif ()

set(not_auto_add_test
        test_hello
        test_bitmap
        test_hex
        test_leak
        test_press_write
        test_press_write2
        test_press_zlog
        test_press_zlog2
        test_press_syslog
        test_syslog
        test_longlog
        test_multithread
        test_dzlog_conf
        fifo_test
        test_pipe
)

if (CMAKE_BUILD_TYPE STREQUAL "Release")
    list(REMOVE_ITEM SRCS ./test_buf.c) # release does not export buf functions
    list(REMOVE_ITEM SRCS ./fifo_test.c) # release does not export buf functions
endif()

# single command test
foreach (test_src ${SRCS})
    string(REGEX MATCH "^.*/([^/]+)[.]c$" test_name ${test_src})
    set(test_name ${CMAKE_MATCH_1})

    message(STATUS "${test_name} ${test_src}")

    add_executable("${test_name}" "${test_src}")
    target_link_libraries(${test_name} zlog)

    list(FIND not_auto_add_test ${test_name} not_auto_test)
    if (not_auto_test EQUAL -1)
        # the target form, not a path: it is what lets ctest put
        # CMAKE_CROSSCOMPILING_EMULATOR in front when these are built for
        # another architecture
        add_test(NAME ${test_name} COMMAND ${test_name})
    endif ()
endforeach (test_src)

# test with args
add_test(NAME test_hello COMMAND test_hello hello_output 3)
add_test(NAME test_longlog COMMAND test_longlog 2222)
add_test(NAME test_bitmap COMMAND test_bitmap 0xaa55 0x66)

file(GLOB CONF_FILES . *.conf)
file(COPY
        ${CONF_FILES} hello_output
        DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
)

if (UNIX)
find_program(BASH NAMES bash REQUIRED)
find_program(MD5_EXECUTABLE NAMES md5sum md5 REQUIRED)

# Determine where test executables are placed
if (CMAKE_RUNTIME_OUTPUT_DIRECTORY)
    set(TEST_BIN_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
else ()
    set(TEST_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR})
endif ()

# tests defined in scripts
set(script_test
    consumer_static_file_single
    varify_static_file_single
    fifo
    test_multi_thread_reload
    test_multi_thread_ftrue
)

if (CMAKE_BUILD_TYPE STREQUAL "Release")
    list(REMOVE_ITEM script_test fifo)
endif()

foreach (case ${script_test})
    set(TEST_WORKING_DIR "${CMAKE_BINARY_DIR}/Testing/Temporary/${case}")
    file(MAKE_DIRECTORY ${TEST_WORKING_DIR})

    add_test(NAME ${case}
        COMMAND ${BASH} ${CMAKE_SOURCE_DIR}/scripts/test.sh -t ${case}
        WORKING_DIRECTORY ${TEST_WORKING_DIR}
    )
    # scripts/test.sh runs the binaries itself, so it needs the emulator as
    # well; a space joined string, as ENVIRONMENT is itself a ; separated list
    string(JOIN " " ZLOG_TEST_EMULATOR ${CMAKE_CROSSCOMPILING_EMULATOR})
    set_property(TEST ${case} PROPERTY ENVIRONMENT
        "bin_dir=${TEST_BIN_DIR};conf_dir=${CMAKE_CURRENT_BINARY_DIR};emu=${ZLOG_TEST_EMULATOR}")
endforeach(case)

endif (UNIX)
