Generating input files for a test case


I am writing a test case for which I need several input files. All of these files are fairly trivial, they just need to be there so that CMake actually generates something; so I think checking them in would not be a great idea.

Currently I am doing this in RunCMakeTest.cmake:

set(test_bindir ${RunCMake_BINARY_DIR}/MyTestName-build)
file(WRITE ${test_bindir}/foo.cpp "int foo(int a) { return a*a; }")
file(WRITE ${test_bindir}/ "1")
file(WRITE ${test_bindir}/CMakeLists.txt
  "cmake_minimum_required(VERSION 3.18)\n"
  "add_library(foo ${test_bindir}/foo.cpp)\n"
  "configure_file(${test_bindir}/ ${test_bindir}/samplefile.txt.tmp)\n"
  "file(GENERATE OUTPUT ${test_bindir}/samplefile.txt INPUT ${test_bindir}/samplefile.txt.tmp)\n")

@brad.king suggested to use the file-GENERATE.cmake facility for this, but I cannot find any documentation or examples. I read Tests/RunCMake/README.rst but didn’t find anything in that regard.

What is the clean way to generate input files for a test case using the CMake test infrastructure?

For reference, this comes from review here. What I meant is that you should just create file-GENERATE.cmake in an editor and put it in the source tree. This is documented in Tests/RunCMake/README.rst:

  • Create a <Test>/<SubTest>.cmake file

@brad.king Thanks for this clarification! Makes sense.

I am currently quite busy, but hope to find some time to do this on this weekend.


In this particular case, I’ve already re-opened your merge request and added the needed tests.