diff options
-rw-r--r-- | pkgs/build-support/build-sandbox/default.nix | 8 | ||||
-rw-r--r-- | tests/sandbox.nix | 93 |
2 files changed, 78 insertions, 23 deletions
diff --git a/pkgs/build-support/build-sandbox/default.nix b/pkgs/build-support/build-sandbox/default.nix index fa4bac57..ba4f3625 100644 --- a/pkgs/build-support/build-sandbox/default.nix +++ b/pkgs/build-support/build-sandbox/default.nix @@ -68,6 +68,14 @@ in stdenv.mkDerivation ({ echo 'return true; }' >> params.c ''; + postInstall = '' + for df in "$drv/share/applications/"*.desktop; do + mkdir -p "$out/share/applications" + sed -e 's!'"$drv"'/bin!'"$out"'/bin!g' "$df" \ + > "$out/share/applications/$(basename "$df")" + done + ''; + nativeBuildInputs = [ pkgconfig ]; buildInputs = [ nix ]; makeFlags = [ "BINDIR=${drv}/bin" ]; diff --git a/tests/sandbox.nix b/tests/sandbox.nix index 9a1b4a3a..20ae88d6 100644 --- a/tests/sandbox.nix +++ b/tests/sandbox.nix @@ -12,39 +12,86 @@ "/run/bar-test-sandbox" "/run/baz-test-sandbox" ]; + + # Only needed so we get the right XDG paths in the system path. + services.xserver.enable = true; + systemd.services.display-manager.enable = false; + environment.systemPackages = let - testProgram = pkgs.writeScriptBin "test-sandbox" '' - #!${pkgs.stdenv.shell} -ex + testPackage = pkgs.runCommand "test-sandbox" { + program = '' + #!${pkgs.stdenv.shell} -ex + + if [ "$1" != canary ]; then + echo 'Canary check failed, so the test program probably' \ + 'was not executed via the XDG desktop entry.' >&2 + exit 1 + fi + + # 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' - # Should fail because we can't access the host's PATH - ! echo foo | grep -qF foo + echo XDG1 > /home/foo/.local/share/xdg/1 + echo XDG2 > /home/foo/.config/xdg/2 + echo XDG3 > /home/foo/.cache/xdg/3 + echo > /home/foo/.cache/xdg/done + ''; + } '' + mkdir -p "$out/bin" "$out/share/applications" "$out/share/test-sandbox" - 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 -n "$program" > "$out/bin/test-sandbox" + chmod +x "$out/bin/test-sandbox" - echo XDG1 > /home/foo/.local/share/xdg/1 - echo XDG2 > /home/foo/.config/xdg/2 - echo XDG3 > /home/foo/.cache/xdg/3 + echo '<svg xmlns="http://www.w3.org/2000/svg"/>' \ + > "$out/share/test-sandbox/icon.svg" + + cat > "$out/share/applications/test.desktop" <<EOF + [Desktop Entry] + Name=$fullName + Type=Application + Version=1.1 + Exec=$out/bin/test-sandbox canary + Icon=$out/share/test-sandbox/icon.svg + Categories=Utility + EOF ''; - in lib.singleton (pkgs.vuizvui.buildSandbox testProgram { - 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" ]; - }); + + in [ + # Unfortunately, "xdg-open test-sandbox.desktop" doesn't work, so let's + # use gtk-launch instead. We also need xvfb_run so that we can avoid to + # start a full-blown X server. + # + # See also: + # + # https://askubuntu.com/questions/5172 + # https://bugs.launchpad.net/ubuntu/+source/gvfs/+bug/378783 + # + (lib.getBin pkgs.gtk3) pkgs.xvfb_run + + (pkgs.vuizvui.buildSandbox testPackage { + 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" ]; + }) + ]; users.users.foo.isNormalUser = true; }; testScript = '' $machine->waitForUnit('multi-user.target'); - $machine->succeed('su - -c test-sandbox foo >&2'); + $machine->succeed('su - -c "xvfb-run gtk-launch test" foo >&2'); + $machine->waitForFile('/home/foo/.cache/xdg/done'); $machine->succeed('test -d /home/foo/existing'); $machine->succeed('grep -qF foo /home/foo/existing/bar'); |