# Test suite that has been moved from Tarantool repository in
# scope of https://github.com/tarantool/tarantool/issues/4478.

add_custom_target(tarantool-tests-libs
  DEPENDS libluajit
)

macro(BuildTestCLib lib sources)
  AddTestLib(${lib} ${sources})
  add_dependencies(tarantool-tests-libs ${lib})
  # Add the directory where the lib is built to the list with
  # entries for LUA_CPATH environment variable, so LuaJIT can find
  # and load it. See the comment about extending the list in the
  # parent scope few lines above.
  set(LUA_CPATHS "${CMAKE_CURRENT_BINARY_DIR}/?${CMAKE_SHARED_LIBRARY_SUFFIX};${LUA_CPATHS}" PARENT_SCOPE)
  # Also add this directory to LD_LIBRARY_PATH environment
  # variable, so FFI machinery can find and load it.
  set(LD_LIBRARY_PATH "${CMAKE_CURRENT_BINARY_DIR}:${LD_LIBRARY_PATH}" PARENT_SCOPE)
endmacro()

add_subdirectory(ffi-ccall)
add_subdirectory(fix-bit-shift-generation)
add_subdirectory(gh-4427-ffi-sandwich)
add_subdirectory(gh-5813-resolving-of-c-symbols/both)
add_subdirectory(gh-5813-resolving-of-c-symbols/hash)
add_subdirectory(gh-5813-resolving-of-c-symbols/gnuhash)
add_subdirectory(gh-5813-resolving-of-c-symbols/stripped)
add_subdirectory(gh-6098-fix-side-exit-patching-on-arm64)
add_subdirectory(gh-6189-cur_L)
add_subdirectory(lj-416-xor-before-jcc)
add_subdirectory(lj-549-bytecode-loader)
add_subdirectory(lj-551-bytecode-c-broken-macro)
add_subdirectory(lj-601-fix-gc-finderrfunc)
add_subdirectory(lj-727-lightuserdata-itern)
add_subdirectory(lj-802-panic-at-mcode-protfail)
add_subdirectory(lj-flush-on-trace)
add_subdirectory(lj-1066-fix-cur_L-after-coroutine-resume)
add_subdirectory(lj-1166-error-stitch)

# The part of the memory profiler toolchain is located in tools
# directory, jit, profiler, and bytecode toolchains are located
# in src/ directory, jit/vmdef.lua is autogenerated file also
# located in src/ directory, but in the scope of the binary
# artefacts tree and auxiliary tests-related modules are located
# in the current directory (but tests are run in the binary
# directory), so LUA_PATH need to be updated.
make_lua_path(LUA_PATH
  PATHS
    ${CMAKE_CURRENT_SOURCE_DIR}/?.lua
    ${CMAKE_CURRENT_SOURCE_DIR}/?/init.lua
    ${PROJECT_SOURCE_DIR}/tools/?.lua
    ${LUAJIT_SOURCE_DIR}/?.lua
    ${LUAJIT_BINARY_DIR}/?.lua
)

# Update LUA_CPATH with the library paths collected within
# <BuildTestLib> macro.
make_lua_path(LUA_CPATH PATHS ${LUA_CPATHS})

set(LUA_TEST_SUFFIX .test.lua)

# XXX: Since the auxiliary libraries are built as a dynamically
# loaded modules on MacOS instead of shared libraries as it is
# done on Linux and BSD, another environment variable should be
# used to guide <ffi.load> while searching the extension.
# XXX: Be noticed that we shouldn't use `"` here to wrap
# the variable's content. If we do this, the variable value will
# contain `"` at the beginning and the end, so this `"` at the
# beginning will be treated as the directory for the first entry
# (the last subdirectory added).
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  # XXX: Apple tries their best to "protect their users from
  # malware". As a result SIP (see the link[1] below) has been
  # designed and released. Now, Apple developers are so protected,
  # that they can load nothing being not installed in the system,
  # since some programs sanitize the environment before they start
  # child processes. Specifically, environment variables starting
  # with DYLD_ and LD_ are unset for child process started by
  # other programs (like `ctest` using for launching tests).
  # For more info, see the docs[2] below.
  #
  # These environment variables are used by FFI machinery to find
  # the proper shared library, hence we can still tweak testing
  # environment before calling <ffi.load>. However, the value
  # can't be passed via the standard environment variable, so we
  # use ENVIRONMENT property in `set_tests_properties` to get
  # around SIP magic tricks.
  #
  # [1]: https://support.apple.com/en-us/HT204899
  # [2]: https://developer.apple.com/library/archive/documentation/Security/Conceptual/System_Integrity_Protection_Guide/RuntimeProtections/RuntimeProtections.html
  list(APPEND LUA_TEST_ENV_MORE DYLD_LIBRARY_PATH=${LD_LIBRARY_PATH})
else()
  list(APPEND LUA_TEST_ENV_MORE LD_LIBRARY_PATH=${LD_LIBRARY_PATH})
endif()

# Some tests use `LD_PRELOAD` to mock system calls (like
# <lj-802-panic-at-mcode-protfail.test.lua> overwrites
# `mprotect()`. When compiling with ASan support under GCC, it is
# required that the ASan library go first in the `LD_PRELOAD`
# list. Set it manually. The test will append it to the executed
# process.
if(LUAJIT_USE_ASAN AND CMAKE_C_COMPILER_ID STREQUAL "GNU")
  # FIXME: We should set this environment variable only
  # for the corresponding tests to avoid warnings from
  # the GNU libc and other libc implementations.
  # See https://github.com/tarantool/tarantool/issues/9898.
  LibRealPath(LIB_ASAN libasan.so)
  list(APPEND LUA_TEST_ENV_MORE LD_PRELOAD=${LIB_ASAN})
endif()

set(TEST_SUITE_NAME "tarantool-tests")

# XXX: The call produces both test and target
# <tarantool-tests-deps> as a side effect.
add_test_suite_target(tarantool-tests
  LABELS ${TEST_SUITE_NAME}
  DEPENDS ${LUAJIT_TEST_DEPS} tarantool-tests-libs
)

file(GLOB_RECURSE tests ${CMAKE_CURRENT_SOURCE_DIR} "*${LUA_TEST_SUFFIX}")
foreach(test_path ${tests})
  get_filename_component(test_name ${test_path} NAME)
  set(test_title "test/${TEST_SUITE_NAME}/${test_name}")
  add_test(NAME ${test_title}
    COMMAND ${LUAJIT_TEST_COMMAND} ${test_path}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  )
  set_tests_properties(${test_title} PROPERTIES
    # LUA_CPATH and LD_LIBRARY_PATH variables and also
    # dependencies list with libraries are set in scope of
    # BuildTestLib macro.
    ENVIRONMENT "LUA_PATH=${LUA_PATH};LUA_CPATH=${LUA_CPATH};${LUA_TEST_ENV_MORE}"
    LABELS ${TEST_SUITE_NAME}
    DEPENDS tarantool-tests-deps
  )
endforeach()
