# add\_test: Handling of spaces in path in WORKING\_DIRECTORY

**URL:** <https://discourse.cmake.org/t/add-test-handling-of-spaces-in-path-in-working-directory/9265>\
**Category:** Code\
**Created:** [October 24, 2023, 2:29pm UTC](https://discourse.cmake.org/t/add-test-handling-of-spaces-in-path-in-working-directory/9265 "2023-10-24T14:29:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![imrichardcole](https://discourse.cmake.org/user_avatar/discourse.cmake.org/imrichardcole/32/3940_2.png) [@imrichardcole](https://discourse.cmake.org/u/imrichardcole)\
**Post date:** [October 24, 2023, 2:29pm UTC](https://discourse.cmake.org/t/add-test-handling-of-spaces-in-path-in-working-directory/9265/1 "2023-10-24T14:29:52Z")

</div>

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`:

```auto
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:

```auto
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:

> <https://github.com/Kitware/CMake/blob/master/Source/kwsys/SystemTools.cxx#L3373>

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [October 24, 2023, 2:56pm UTC](https://discourse.cmake.org/t/add-test-handling-of-spaces-in-path-in-working-directory/9265/2 "2023-10-24T14:56:47Z")

</div>

> `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:

```cmake
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`.

---

<div class="post-metadata">

**Author:** ![imrichardcole](https://discourse.cmake.org/user_avatar/discourse.cmake.org/imrichardcole/32/3940_2.png) [@imrichardcole](https://discourse.cmake.org/u/imrichardcole)\
**Post date:** [October 24, 2023, 3:40pm UTC](https://discourse.cmake.org/t/add-test-handling-of-spaces-in-path-in-working-directory/9265/3 "2023-10-24T15:40:26Z")

</div>

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.
