add_test: Handling of spaces in path in WORKING_DIRECTORY

Currently banging my head against the following.

Am on Windows and have the following working directory:

C:\code\project - windows\build

I then configured a test within the CMakeLists.txt:

add_test(NAME MyTest
  COMMAND "${CMAKE_CURRENT_SOURCE_DIR}\tests.cmd"
  WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)

I cannot get this test to work when the working directory has a space in it. If I change the location the code is checked out to i.e:

C:\code\project_windows\build

It works without issue. How can I get CTest to handle a WORKING_DIRECTORY with a space in it’s path on Windows?

The error I get with WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" is:

1. 'C:/code/project' is not a recognized as an internal or external command'

I then thought it was a simple case of escaping i.e WORKING_DIRECTORY "\"${CMAKE_CURRENT_SOURCE_DIR}\"", but this then fails as well. It complains about not being able to change into the now escaped working directory i.e "C:\code\project_windows\build"

FWIW I think the error is coming down to this part of the codebase:

COMMAND "${CMAKE_CURRENT_SOURCE_DIR}\tests.cmd"

Use a forward slash. Otherwise \t is treated as an escaped TAB character.

In general spaces in the path are known to work well. Both CI and nightly testing jobs use spaces in many of the build. With this code:

add_test(NAME MyTest
  COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests.cmd"
  WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)

if the test still doesn’t work, please post more details about what goes wrong.

Also please be sure the error is coming from ctest and not code in tests.cmd.

Hi Brad. Thanks for the response. No joy I’m afraid, tried a few things as well as renaming the script to not begin with a t.

The issue is around the the fact “C:\code\project - windows\build” gets turned into “C:\code\project” somewhere which is obviously not a directory I have.

Let me try and get a standalone version that I can share via github.

Thanks again

Richard.