about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--pkgs/build-support/build-sandbox/src/setup.c34
-rw-r--r--tests/sandbox.nix18
2 files changed, 32 insertions, 20 deletions
diff --git a/pkgs/build-support/build-sandbox/src/setup.c b/pkgs/build-support/build-sandbox/src/setup.c
index 83876b2a..dfe550e0 100644
--- a/pkgs/build-support/build-sandbox/src/setup.c
+++ b/pkgs/build-support/build-sandbox/src/setup.c
@@ -268,6 +268,18 @@ static void free_offsets(struct envar_offset *base)
         free_offsets(next);
 }
 
+#define MK_XDG_EXPAND(varname, fallback) \
+    if (strcmp(xdg_var, varname) == 0) { \
+        result = malloc(homelen + sizeof fallback); \
+        if (result == NULL) { \
+            perror("malloc " varname); \
+            return NULL; \
+        } \
+        memcpy(result, home, homelen); \
+        memcpy(result + homelen, fallback, sizeof fallback); \
+        return result; \
+    }
+
 static char *expand_xdg_fallback(const char *xdg_var)
 {
     static char *home = NULL;
@@ -282,25 +294,9 @@ static char *expand_xdg_fallback(const char *xdg_var)
         homelen = strlen(home);
     }
 
-    if (strcmp(xdg_var, "XDG_DATA_HOME") == 0) {
-        result = malloc(homelen + 14);
-        if (result == NULL) {
-            perror("malloc XDG_DATA_HOME");
-            return NULL;
-        }
-        memcpy(result, home, homelen);
-        memcpy(result + homelen, "/.local/share", 14);
-        return result;
-    } else if (strcmp(xdg_var, "XDG_CONFIG_HOME") == 0) {
-        result = malloc(homelen + 9);
-        if (result == NULL) {
-            perror("malloc XDG_CONFIG_HOME");
-            return NULL;
-        }
-        memcpy(result, home, homelen);
-        memcpy(result + homelen, "/.config", 9);
-        return result;
-    }
+    MK_XDG_EXPAND("XDG_DATA_HOME", "/.local/share");
+    MK_XDG_EXPAND("XDG_CONFIG_HOME", "/.config");
+    MK_XDG_EXPAND("XDG_CACHE_HOME", "/.cache");
 
     return NULL;
 }
diff --git a/tests/sandbox.nix b/tests/sandbox.nix
index 9c247da2..9a1b4a3a 100644
--- a/tests/sandbox.nix
+++ b/tests/sandbox.nix
@@ -15,15 +15,27 @@
     environment.systemPackages = let
       testProgram = pkgs.writeScriptBin "test-sandbox" ''
         #!${pkgs.stdenv.shell} -ex
+
+        # Should fail because we can't access the host's PATH
         ! echo foo | grep -qF foo
+
         export PATH=/run/baz-test-sandbox/bin
         echo foo > /home/foo/existing/bar
         test ! -d /home/foo/nonexisting
         /run/foo-test-sandbox/bin/hello
         echo aaa | /run/bar-test-sandbox/bin/sed -e 's/a/b/g'
+
+        echo XDG1 > /home/foo/.local/share/xdg/1
+        echo XDG2 > /home/foo/.config/xdg/2
+        echo XDG3 > /home/foo/.cache/xdg/3
       '';
     in lib.singleton (pkgs.vuizvui.buildSandbox testProgram {
-      paths.required = [ "/home/foo/existing" ];
+      paths.required = [
+        "/home/foo/existing"
+        "$XDG_DATA_HOME/xdg"
+        "$XDG_CONFIG_HOME/xdg"
+        "$XDG_CACHE_HOME/xdg"
+      ];
       paths.wanted = [ "/home/foo/nonexisting" ];
       paths.runtimeVars = [ "COLLECT_ME" ];
     });
@@ -37,5 +49,9 @@
     $machine->succeed('test -d /home/foo/existing');
     $machine->succeed('grep -qF foo /home/foo/existing/bar');
     $machine->fail('test -d /home/foo/nonexisting');
+
+    $machine->succeed('grep -qF XDG1 /home/foo/.local/share/xdg/1');
+    $machine->succeed('grep -qF XDG2 /home/foo/.config/xdg/2');
+    $machine->succeed('grep -qF XDG3 /home/foo/.cache/xdg/3');
   '';
 }