tags:

views:

285

answers:

2

I want to use CMake to run some tests. One of the test should call a validator script on all files matching "fixtures/*.ext". How can transform the following pseudo-CMake into real CMake?

i=0
for file in fixtures/*.ext; do
  ADD_TEST(validate_${i}, "validator", $file)
  let "i=i+1"
done
+2  A: 

Like this:

file(GLOB files "fixtures/*.ext")
foreach(file ${files})
  ... calculate ${i} to get the test name
  add_test(validate_${i}, "validator", ${file})
endforeach()

But this does not calculate i for you. Is it important that you have an integer suffix to the test? Otherwise you could use file(...) to extract the filename (without extension) to use.

JesperE
+1  A: 

You need enable_testing to activate CMake's test machinery. The add_test function needs the name of the test and the command to run and the syntax is as below.

To add a counter, you can use the math() function. The following will let you also run out of source builds by specifying the complete path to the inputs.

cmake_minimum_required(VERSION 2.6)
enable_testing()

file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/fixtures/*.ext")
set(validator ${CMAKE_CURRENT_SOURCE_DIR}/validator)
set(i 0)
foreach(filename ${files})
    add_test(NAME "validate_${i}"
       COMMAND "${validator}" ${filename})
    math(EXPR i "${i} + 1")
endforeach()
rq