I'm trying to set the working directory for a CMake test, but I can't seem to get it to work. Here's the really simple test I'm trying to use:
CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
set (CMAKE_CXX_STANDARD 17)
project(working_directory_error LANGUAGES CXX)
enable_testing()
add_executable(main main.cpp)
add_test(main_test main WORKING_DIRECTORY "C:/")
main.cpp
#include <filesystem>
#include <iostream>
int main()
{
std::filesystem::path workingDirectory = std::filesystem::current_path();
bool workingDirectoryIsC = workingDirectory == std::filesystem::path("C:/");
std::cout << "Working directory is " << workingDirectory << " which is "
<< (workingDirectoryIsC ? "right" : "wrong") << std::endl;
return workingDirectoryIsC ? 0 : 1;
}
console
C:\dev\repros\working_directory_error>cmake -B out -G Ninja
-- The CXX compiler identification is MSVC 19.37.32825.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.37.32822/bin/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (1.3s)
-- Generating done (0.0s)
-- Build files have been written to: C:/dev/repros/working_directory_error/out
C:\dev\repros\working_directory_error>cmake --build out
[2/2] Linking CXX executable main.exe
C:\dev\repros\working_directory_error>ctest --test-dir out --output-on-failure
Internal ctest changing into directory: C:/dev/repros/working_directory_error/out
Test project C:/dev/repros/working_directory_error/out
Start 1: main_test
1/1 Test #1: main_test ........................***Failed 0.03 sec
Working directory is "C:\\dev\\repros\\working_directory_error\\out" which is wrong
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 0.03 sec
The following tests FAILED:
1 - main_test (Failed)
Errors while running CTest
I've tried both MSVC and Ninja generators on Windows, and Ninja on Linux using WSL. I've tried running from the out
folder in case --test-dir
was somehow overriding the working directory. Nothing seems to work.
Is there something obvious I'm missing?
Try the following:
add_test(NAME main_test COMMAND main WORKING_DIRECTORY "C:/")
I believe the issue is that you are using the older, less flexible form of add_test
.
Unlike the above NAME signature, target names are not supported in the command-line.