Suppose I have a function that reads the contents of a file into some buffer e.g.
template <class T>
static std::streamsize ReadContents(const std::filesystem::path& path, T& buff, size_t max)
{
...
std::ifstream input(path.c_str(), std::ios::in | std::ios_base::binary);
auto bytesRead = input.readsome(reinterpret_cast<char *>(buff.data()), static_cast<std::streamsize>(max));
...
return bytsRead
}
Normally, this would be simple to unit test as I could provide some known files into the function so can test all return paths, size, buffer contents, etc.
If I wanted to test this function with files from /proc e.g. /proc/meminfo it worked ok in the unit test too because I created a fake virtual filesystem with known files and simply pointed the code at it.
When using the code in a real environment it failed however because readsome will not read files of zero sizes in the virtual file system. The fix of course for that is to replace readsome with read and gcount combo to get the same functionality but it made me think about how it would be possible to have accurate unit testing of code where you need to test reading files in the virtual filesystem without actually using the real files on the host.
I'm using c++ and gtest. Any idea of a pattern to solving this problem; pipes maybe?