GNOME Bugzilla – Bug 674197
Add helpers to redirect XDG_*_HOME to a temp dir for unit tests
Last modified: 2018-05-24 14:01:29 UTC
Some unit tests may want to write in ~/.cache but does not want to mess up with user's files. In that case the unit test needs to set XDG_CACHE_HOME to /tmp/something. This gets tricky to cleanup the directory in the case the unit test crash for example. We could profit of the watcher process we fork in gtestdbus.c (see bug #672985) and make that process also cleanup the temp dirs.
My git branch doing this: http://cgit.collabora.com/git/user/xclaesse/glib.git/log/?h=xdg-test
Created attachment 212136 [details] [review] Add helper functions to set XDG dirs to a temporary directory This is useful for unit tests not wanting to mess up user's files
This commit is a WIP, it already got some critics in bug #672985, that's why I'm proposing it in a separate bug to continue discussion separately.
Created attachment 212354 [details] [review] Add helper functions to set XDG dirs to a temporary directory This is useful for unit tests not wanting to mess up user's files
If this is just about testing then I think with the changes to g_get_home_dir() to support HOME, this bug is obsolete?
marking as such, then
Not sure that's enough. What my patch does is also cleanup the tmp directory when the test is done, or even if it crash. Also, I think we need a way to make g_get_user_cache_dir() and friends reload their value, so each unit test can use a new directory to be sure they don't get data of previous test. Something like g_reload_user_special_dirs_cache().
I think this belongs in a framework outside of GLib...
(In reply to comment #7) > Not sure that's enough. What my patch does is also cleanup the tmp directory > when the test is done, or even if it crash. Also, I think we need a way to make > g_get_user_cache_dir() and friends reload their value, so each unit test can > use a new directory to be sure they don't get data of previous test. You can use g_test_trap_subprocess() once that lands (bug 679683) and just run each test in a separate subprocess, and have the parent test do the cleanup.
Created attachment 239792 [details] [review] Add helper functions to set XDG dirs to a temporary directory This is useful for unit tests not wanting to mess up user's files
@Dan, I don't see how bug #679683 has anything to do with this one. I've made another patch, maybe that version is better? Note that EDS unit tests does something similar as well now that it uses GTestDBus.
I use and wrote this for Tracker: https://git.gnome.org/browse/tracker/tree/utils/sandbox/tracker-sandbox.py It essentially creates entire DBus sessions and allows for starting a shell in that session by storing the details somewhere ... It's a bit crude, but it could be a start or help if you want to avoid the pitfalls I came across when developing this... Anyway, happy to help :)
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glib/issues/538.