I have a project directory with multiple sub-projects, each with their own CMake setups. I’m using add_test()
in the CMakeLists.txt
of a library to add bash scripts for testing. I’m also making use of add_subdirectory()
to add the library to the setups of other sub-projects with their own CMake setups. To avoid path related confusion, I would prefer it if all bash scripts were run as if they were executed from a shared ‘base’, the project directory.
Inside ~/my-project/my-lib/CMakeLists.txt
I’m using the following lines to add a test script with a specific working directory:
set(GIT_WORKING_DIR ${CMAKE_CURRENT_LIST_DIR}/..) # should be '/home/user/my-project/my-lib/..'
message("GIT_WORKING_DIR=${GIT_WORKING_DIR}") # outputs "GIT_WORKING_DIR=/home/user/my-project/my-lib/.." as expected
...
add_test("test-name" "${GIT_WORKING_DIR}/my-lib/scripts/test-script.sh" WORKING_DIRECTORY ${GIT_WORKING_DIR})
Let’s assume that the test script does nothing but call pwd
to see if the working directory was set as expected.
I’m building the library and running the tests like this:
mkdir ~/build
cmake -B ~/my-project/build -S ~/my-project/my-lib
cmake --build ~/my-project/build --target all test
Note that CMake itself seams to run without issues and as expected, it is only the test that ‘fails’ because the current working directory doesn’t match the expectations.
I was expecting that the test script would run with ~/my-project/my-lib
as the current working directory. But the output of pwd
shows it is run from the build directory ~/my-project/build
instead, which would be the default option if nothing other is specified in the call of add_test()
. Other commands also fail, e.g. there is no folder ./source
to scan for files.
I did specify a working directory for the test? Why doesn’t it have the expected effect? What am I doing wrong here?