I have a project built with CMake that uses Catch2 for unit tests. Some of the unit tests exercise code that loads data from a file like this:
std::string resource_dir = "TEST_CWD/resources/";
std::ifstream infile{resource_dir + "datafile.txt"}
The question is how to properly get the value of TEST_CWD
.
The directory structure is simple (and not set in stone):
my_project/
test/
resources/datafile.txt
loader_test.cpp
Leaving TEST_CWD
blank sometimes works, but breaks when running tests through an IDE. Setting an environment variable with the absolute path also works, but will break on others' machines. Similarly forcing all users to manually set environment variables is user unfriendly.
What is a good way to specify relative file paths in CMake projects?