Unable to configure working directory in CMake/Catch
- Dominant language
- C++
- Stars
- 21.5k
- Forks
- 3.5k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 2
Description
**Describe the bug**
Configuring the working directory does not do anything to a CMake/Catch invocation. Data files that are used in tests are always retrieved from the directory from with the test binary was invoked. The programmer must always invoke the test binary from the directory that contains the data needed by the test.
**Expected behavior**
The configuration of the working directory to allow tests to access data files from a predictable directory, without requiring the programmer to invoke the test binary from a specific directory.
**Reproduction steps**
Create a CMake/Catch test and set the working directory to a directory where tests should be able to find their data files:
```cmake
enable_testing()
include(CTest)
include(Catch)
configure_file(
${CMAKE_SOURCE_DIR}/data/example.txt
${CMAKE_CURRENT_BINARY_DIR}/data/example.txt
COPYONLY)
add_executable(mytest)
target_link_libraries(mytest
PRIVATE CONAN_PKG::catch2)
target_sources(mytest
PRIVATE init.cpp example.cpp)
catch_discover_tests(mytest
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/data)
```
Write a simple test file called `example.cpp` that tries to use the data file `example.txt`:
```cpp
#include
#include
#include
TEST_CASE("my test case") {
SECTION("my section") {
const std::filesystem::path file{"example.txt"};
std::clog << std::filesystem::absolute(file) << '\n';
}
}
```
Notice that specifying the working directory does not allow the test to find the data file it needs; the working directory is always the directory from which the binary is invoked:
```sh
$ ./bin/mytest
"/home/wouter/someDir/Debug/test.txt"
$ cd bin/
$ ./mytest
"/home/wouter/someDir/Debug/bin/test.txt"
```
**Platform information:**
- OS: **Ubuntu 21.04**
- Compiler+version: **GCC v11.1.0**
- Catch version: **v2.13.6**
Contributor guide
Assessment
This issue has not been assessed yet.