Skip to content

Support isolating directories for unit tests

Philip Withnall requested to merge pwithnall/glib:1601-appinfo-test-again into master

This was initially meant to just be a fix for #1601 (closed), but then it spiralled out of control into a complete fix for #538 (closed). It works by adding an internal function to override the XDG dirs returned by g_get_user_data_dir() and friends: g_set_user_dirs(). It then calls this for each unit test, iff you’ve passed the G_TEST_OPTION_ISOLATE_DIRS option to g_test_init().

It requires this explicit opt-in behaviour because developers will need to check any spawn() calls in their test code (see below), and ensure that any code called by their test code uses g_get_user_data_dir() rather than checking the XDG_DATA_HOME environment variable manually, for example.

This branch does not set the XDG_* environment variables, other than to set them to /dev/null to catch errors. This branch is, as far as I’m aware, thread safe.

There were various restrictions which prompted this design:

  • setenv() cannot be used at all after any threads might have been spawned.
  • Any test environment needs to be propagated to subprocesses spawned with g_test_subprocess().
  • Any test environment needs to be propagatable to subprocesses manually spawned by the test code.

And it has two caveats:

  • If test code does manually spawn a subprocess, it’s responsible for setting the appropriate XDG_* variables in the subprocess’ environment.
  • It has to create the directory named in XDG_RUNTIME_DIR (the spec requires it) for each unit test, so it always creates a nest of directories in /tmp. They will be cleaned up on a successful test run, though.

Closes #1601 (closed), #538 (closed)

Edited by Philip Withnall

Merge request reports