<commit_before>###
# Create config.ini file for tests
###
if("fuzzer" IN_LIST ENABLE_SANITIZERS)
	set(ENABLE_FUZZ ON)
else()
	set(ENABLE_FUZZ OFF)
endif()

# Create build ini file
configure_file(config.ini.cmake.in config.ini)

###
# Setup symlinks for testing
###
include(SanitizeHelper)
function(make_link file)
	set(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
	set(dest "${CMAKE_CURRENT_BINARY_DIR}/${file}")

	# Create the target directory and parents if needed.
	get_filename_component(dest_dir "${dest}" DIRECTORY)
	file(MAKE_DIRECTORY "${dest_dir}")

	add_custom_command(
		OUTPUT "${dest}"
		COMMAND ${CMAKE_COMMAND} -E create_symlink "${src}" "${dest}"
		COMMENT "link ${file}"
		MAIN_DEPENDENCY "${src}"
	)
	# Add a phony target to make sure the files are linked by default.
	sanitize_target_name("link-" "${file}" NAME)
	add_custom_target(${NAME} ALL DEPENDS "${dest}")

	foreach(PARENT_TARGET ${ARGN})
		if(TARGET ${PARENT_TARGET})
			add_dependencies(${PARENT_TARGET} ${NAME})
		endif()
	endforeach()
endfunction()

make_link(functional/test_runner.py)
make_link(util/bitcoin-util-test.py)
make_link(util/rpcauth-test.py)

get_property(FUZZ_TARGETS GLOBAL PROPERTY FUZZ_TARGETS)
make_link(fuzz/test_runner.py ${FUZZ_TARGETS})

include(Coverage)
include(TestSuite)

set(_TEST_TARGET_DEPENDS "")
if(BUILD_BITCOIN_CLI)
	list(APPEND _TEST_TARGET_DEPENDS bitcoin-cli)
endif()
if(BUILD_BITCOIN_WALLET)
	list(APPEND _TEST_TARGET_DEPENDS bitcoin-wallet)
endif()

function(add_functional_test_check TARGET DESCRIPTION)
	if(ENABLE_JUNIT_REPORT)
		string(REGEX REPLACE " " "_" JUNIT_FILE "${DESCRIPTION}.xml")
		set(JUNIT_OUTPUT "--junitoutput=${JUNIT_REPORT_DIRECTORY}/${JUNIT_FILE}")
	endif()

	add_test_custom_target(${TARGET}
		TEST_COMMAND
			"${Python_EXECUTABLE}"
			./functional/test_runner.py
			"--testsuitename=Bitcoin ABC ${DESCRIPTION}"
			${JUNIT_OUTPUT}
			${ARGN}
		CUSTOM_TARGET_ARGS
			COMMENT "Running ${DESCRIPTION}"
			DEPENDS<commit_msg>[cmake] Double the functional test timeout when a sanitizer is enabled<commit_after>###
# Create config.ini file for tests
###
if("fuzzer" IN_LIST ENABLE_SANITIZERS)
	set(ENABLE_FUZZ ON)
else()
	set(ENABLE_FUZZ OFF)
endif()

# Create build ini file
configure_file(config.ini.cmake.in config.ini)

###
# Setup symlinks for testing
###
include(SanitizeHelper)
function(make_link file)
	set(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
	set(dest "${CMAKE_CURRENT_BINARY_DIR}/${file}")

	# Create the target directory and parents if needed.
	get_filename_component(dest_dir "${dest}" DIRECTORY)
	file(MAKE_DIRECTORY "${dest_dir}")

	add_custom_command(
		OUTPUT "${dest}"
		COMMAND ${CMAKE_COMMAND} -E create_symlink "${src}" "${dest}"
		COMMENT "link ${file}"
		MAIN_DEPENDENCY "${src}"
	)
	# Add a phony target to make sure the files are linked by default.
	sanitize_target_name("link-" "${file}" NAME)
	add_custom_target(${NAME} ALL DEPENDS "${dest}")

	foreach(PARENT_TARGET ${ARGN})
		if(TARGET ${PARENT_TARGET})
			add_dependencies(${PARENT_TARGET} ${NAME})
		endif()
	endforeach()
endfunction()

make_link(functional/test_runner.py)
make_link(util/bitcoin-util-test.py)
make_link(util/rpcauth-test.py)

get_property(FUZZ_TARGETS GLOBAL PROPERTY FUZZ_TARGETS)
make_link(fuzz/test_runner.py ${FUZZ_TARGETS})

include(Coverage)
include(TestSuite)

set(_TEST_TARGET_DEPENDS "")
if(BUILD_BITCOIN_CLI)
	list(APPEND _TEST_TARGET_DEPENDS bitcoin-cli)
endif()
if(BUILD_BITCOIN_WALLET)
	list(APPEND _TEST_TARGET_DEPENDS bitcoin-wallet)
endif()

function(add_functional_test_check TARGET DESCRIPTION)
	if(ENABLE_JUNIT_REPORT)
		string(REGEX REPLACE " " "_" JUNIT_FILE "${DESCRIPTION}.xml")
		set(JUNIT_OUTPUT "--junitoutput=${JUNIT_REPORT_DIRECTORY}/${JUNIT_FILE}")
	endif()

	# Sanitizers can have a lot of overhead, so double the timeout to prevent
	# false positives.
	if(ENABLE_SANITIZERS)
		set(EXTENDED_TIMEOUT "--timeout-factor=2")
	endif()

	add_test_custom_target(${TARGET}
		TEST_COMMAND
			"${Python_EXECUTABLE}"
			./functional/test_runner.py
			"--testsuitename=Bitcoin ABC ${DESCRIPTION}"
			${JUNIT_OUTPUT}
			${EXTENDED_TIMEOUT}
			${ARGN}
		CUSTOM_TARGET_ARGS
			COMMENT "Running ${DESCRIPTION}"
			DEPENDS
{'fraction_non_alphanumeric': 0.13251857177090823, 'fraction_numerical': 0.00023963575365444525, 'mean_word_length': 6.493716337522442, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5174968', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1523, 'n_words': 299}