tests/example/: Make search for template.lua explicit
Previously, while recursively scanning the directory tree, the code in here also scanned for template.lua files and remembered the latest one it found. This commit adds a function which finds the right template.lua for a given file name, making this search explicit and easier to understand. Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
parent
d4dc579105
commit
5fb6ca8194
|
@ -106,13 +106,26 @@ function(escape_code path escaped_content pre_header post_header)
|
||||||
set(${post_header} ${example_post_header} PARENT_SCOPE)
|
set(${post_header} ${example_post_header} PARENT_SCOPE)
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
# Execute a lua file.
|
# Find the template.lua that is closest to the given file. For example, if a
|
||||||
function(run_test test_path namespace template escaped_content)
|
# template.lua is present in the same directory, its path will be returned. If
|
||||||
|
# one is present in the parent directory, that path is returned etc.
|
||||||
|
function(find_template result_variable file)
|
||||||
|
get_filename_component(path "${file}" DIRECTORY)
|
||||||
|
|
||||||
# A template is required to know how to handle the output.
|
while(NOT EXISTS "${path}/template.lua")
|
||||||
if (template STREQUAL " ")
|
set(last_path "${path}")
|
||||||
message(FATAL_ERROR "No template found for " ${test_path} ", bye")
|
get_filename_component(path "${path}" DIRECTORY)
|
||||||
endif()
|
if(last_path STREQUAL path)
|
||||||
|
message(FATAL_ERROR "Failed to find template.lua for ${file}")
|
||||||
|
endif()
|
||||||
|
endwhile()
|
||||||
|
|
||||||
|
set(${result_variable} "${path}/template.lua" PARENT_SCOPE)
|
||||||
|
endfunction()
|
||||||
|
|
||||||
|
# Execute a lua file.
|
||||||
|
function(run_test test_path namespace escaped_content)
|
||||||
|
find_template(template "${test_path}")
|
||||||
|
|
||||||
# Get the file name without the extension
|
# Get the file name without the extension
|
||||||
get_filename_component(${test_path} TEST_FILE_NAME NAME)
|
get_filename_component(${test_path} TEST_FILE_NAME NAME)
|
||||||
|
@ -199,14 +212,7 @@ endfunction()
|
||||||
|
|
||||||
# Recursive helper function to avoid adding CMakeLists.txt and add_subdirectory
|
# Recursive helper function to avoid adding CMakeLists.txt and add_subdirectory
|
||||||
# in every sub-directories.
|
# in every sub-directories.
|
||||||
function(digg path namespace template)
|
function(digg path namespace)
|
||||||
|
|
||||||
# Check if there is a template for this directory, else use the
|
|
||||||
# last known one.
|
|
||||||
if(EXISTS ${path}/template.lua)
|
|
||||||
message(STATUS "Testing code based on ${namespace}")
|
|
||||||
set(template ${path}/template.lua)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Get the directory content
|
# Get the directory content
|
||||||
file(GLOB ex_files RELATIVE "${path}"
|
file(GLOB ex_files RELATIVE "${path}"
|
||||||
|
@ -216,7 +222,7 @@ function(digg path namespace template)
|
||||||
if(IS_DIRECTORY ${path}/${ex_file_name}
|
if(IS_DIRECTORY ${path}/${ex_file_name}
|
||||||
AND (NOT ${ex_file_name} STREQUAL "shims"))
|
AND (NOT ${ex_file_name} STREQUAL "shims"))
|
||||||
|
|
||||||
digg("${path}/${ex_file_name}" "${namespace}_${ex_file_name}" ${template})
|
digg("${path}/${ex_file_name}" "${namespace}_${ex_file_name}")
|
||||||
|
|
||||||
elseif(${ex_file_name} MATCHES ".lua"
|
elseif(${ex_file_name} MATCHES ".lua"
|
||||||
AND NOT ${ex_file_name} MATCHES "template.lua")
|
AND NOT ${ex_file_name} MATCHES "template.lua")
|
||||||
|
@ -224,7 +230,7 @@ function(digg path namespace template)
|
||||||
# Get the file name without the extension
|
# Get the file name without the extension
|
||||||
string(REGEX REPLACE "\\.lua" "" TEST_FILE_NAME ${ex_file_name})
|
string(REGEX REPLACE "\\.lua" "" TEST_FILE_NAME ${ex_file_name})
|
||||||
|
|
||||||
run_test("${path}/${ex_file_name}" "${namespace}" ${template} ESCAPED_CODE_EXAMPLE)
|
run_test("${path}/${ex_file_name}" "${namespace}" ESCAPED_CODE_EXAMPLE)
|
||||||
|
|
||||||
# Set the test name
|
# Set the test name
|
||||||
set(TEST_NAME DOC${namespace}_${TEST_FILE_NAME}_EXAMPLE)
|
set(TEST_NAME DOC${namespace}_${TEST_FILE_NAME}_EXAMPLE)
|
||||||
|
@ -245,7 +251,7 @@ endfunction()
|
||||||
# the test. In parallel, build a namespace for the global variables. Those
|
# the test. In parallel, build a namespace for the global variables. Those
|
||||||
# variables will be inserted into the lua source code itself once the examples
|
# variables will be inserted into the lua source code itself once the examples
|
||||||
# are validated.
|
# are validated.
|
||||||
digg("${SOURCE_DIR}/tests/examples" "" " ")
|
digg("${SOURCE_DIR}/tests/examples" "")
|
||||||
|
|
||||||
# This is ugly, but CMake variable scope system totally ignore 50 years of
|
# This is ugly, but CMake variable scope system totally ignore 50 years of
|
||||||
# computer science evolution and only support function local variables.
|
# computer science evolution and only support function local variables.
|
||||||
|
|
Loading…
Reference in New Issue