I'm writing tests for some code that interacts with the filesystem, and I want to assert that it writes the expected data to disk. My problem is that some of the code I want to test includes hardcoded paths and due to my requirements I cannot avoid these without adding test-specific code paths, which I would like to avoid. As such, it seems to me that some sort of filesystem mocking is the only way to avoid polluting the filesystem with real files and potentially also messing with user configuration—the program configures other programs on the system, like a settings app.
I read through the Testing Framework section of the GLib docs, but it doesn't mention anything about this. I also searched for "test" and scanned through the results but I couldn't find anything relevant there either.
Does GLib's GTest testing framework provide any filesystem mocking functionality?
This won't work for every use-case, but for mine I was able to use the G_TEST_OPTION_ISOLATE_DIRS
flag for g_test_init ()
like so:
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
This only affects XDG directories, so if your program doesn't respect those or operates outside of them this will do nothing. But for my use-case this is perfect.