mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-10-04 20:59:21 +02:00
gtestutils: Add XDG directory isolation
Add a new G_TEST_OPTIONS_ISOLATE_XDG_DIRS option for g_test_init() which automatically creates a temporary set of XDG directories, and a temporary home directory, and overrides the g_get_user_data_dir() (etc.) functions for the duration of the unit test with the temporary values. This is intended to better isolate unit tests from the user’s actual data and home directory. It works with g_test_subprocess(), but does not work with subprocesses spawned manually by the test — each unit test’s code will need to be amended to correctly set the XDG_* environment variables in the environment of any spawned subprocess. “Why not solve that by setting the XDG environment variables for the whole unit test process tree?” I hear you say. Setting environment variables is not thread safe and they would need to be re-set for each unit test, once worker threads have potentially been spawned. Signed-off-by: Philip Withnall <withnall@endlessm.com> https://gitlab.gnome.org/GNOME/glib/issues/538
This commit is contained in:
@@ -165,6 +165,36 @@ void g_test_init (int *argc,
|
||||
char ***argv,
|
||||
...) G_GNUC_NULL_TERMINATED;
|
||||
|
||||
/**
|
||||
* G_TEST_OPTION_ISOLATE_DIRS:
|
||||
*
|
||||
* Creates a unique temporary directory for each unit test and uses
|
||||
* g_set_user_dirs() to set XDG directories to point into subdirectories of it
|
||||
* for the duration of the unit test. The directory tree is cleaned up after the
|
||||
* test finishes successfully. Note that this doesn’t take effect until
|
||||
* g_test_run() is called, so calls to (for example) g_get_user_home_dir() will
|
||||
* return the system-wide value when made in a test program’s main() function.
|
||||
*
|
||||
* The following functions will return subdirectories of the temporary directory
|
||||
* when this option is used. The specific subdirectory paths in use are not
|
||||
* guaranteed to be stable API — always use a getter function to retrieve them.
|
||||
*
|
||||
* - g_get_home_dir()
|
||||
* - g_get_user_cache_dir()
|
||||
* - g_get_system_config_dirs()
|
||||
* - g_get_user_config_dir()
|
||||
* - g_get_system_data_dirs()
|
||||
* - g_get_user_data_dir()
|
||||
* - g_get_user_runtime_dir()
|
||||
*
|
||||
* The subdirectories may not be created by the test harness; as with normal
|
||||
* calls to functions like g_get_user_cache_dir(), the caller must be prepared
|
||||
* to create the directory if it doesn’t exist.
|
||||
*
|
||||
* Since: 2.60
|
||||
*/
|
||||
#define G_TEST_OPTION_ISOLATE_DIRS "isolate_dirs"
|
||||
|
||||
/* While we discourage its use, g_assert() is often used in unit tests
|
||||
* (especially in legacy code). g_assert_*() should really be used instead.
|
||||
* g_assert() can be disabled at client program compile time, which can render
|
||||
|
Reference in New Issue
Block a user