From 95b95b14075aca3eea5a747aad2e24606f2f0fea Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Fri, 24 Apr 2020 21:41:22 +0200 Subject: [PATCH] tests: improve test purity Before the XDG variables would be set from the user's environment, if available. This would break some tests. With this change the tests should be fully deterministic. Fixes #1190 --- tests/default.nix | 10 +++++++++- .../session-variables-expected.txt | 3 +++ .../misc/xsession/basic-xprofile-expected.txt | 2 +- tests/modules/misc/xsession/basic.nix | 2 -- .../misc/xsession/keyboard-without-layout.nix | 2 -- .../programs/bash/session-variables-expected.txt | 2 +- tests/modules/programs/bash/session-variables.nix | 2 -- tests/modules/programs/getmail/getmail.nix | 3 --- tests/modules/programs/lieer/lieer.nix | 3 --- tests/modules/programs/mbsync/mbsync.nix | 3 --- .../programs/neomutt/neomutt-with-msmtp.nix | 5 ----- tests/modules/programs/neomutt/neomutt.nix | 5 ----- tests/modules/programs/tmux/hm-session-vars.sh | 5 ----- .../programs/tmux/secure-socket-enabled.nix | 5 ++--- tests/modules/services/lieer/lieer-service.nix | 3 --- .../generic-linux-session-vars-expected.txt | 6 ------ tests/modules/targets/generic-linux.nix | 14 +++++--------- 17 files changed, 21 insertions(+), 54 deletions(-) delete mode 100644 tests/modules/programs/tmux/hm-session-vars.sh delete mode 100644 tests/modules/targets/generic-linux-session-vars-expected.txt diff --git a/tests/default.nix b/tests/default.nix index b29b27ace..9eb5afcde 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -14,7 +14,15 @@ let modules = import ../modules/modules.nix { inherit lib pkgs; check = false; - }; + } ++ [ + # Fix impurities. Without these some of the user's environment + # will leak into the tests through `builtins.getEnv`. + { + xdg.enable = true; + home.username = "hm-user"; + home.homeDirectory = "/home/hm-user"; + } + ]; in diff --git a/tests/modules/home-environment/session-variables-expected.txt b/tests/modules/home-environment/session-variables-expected.txt index 5c3868c39..4e18e2b31 100644 --- a/tests/modules/home-environment/session-variables-expected.txt +++ b/tests/modules/home-environment/session-variables-expected.txt @@ -4,3 +4,6 @@ export __HM_SESS_VARS_SOURCED=1 export V1="v1" export V2="v2-v1" +export XDG_CACHE_HOME="/home/hm-user/.cache" +export XDG_CONFIG_HOME="/home/hm-user/.config" +export XDG_DATA_HOME="/home/hm-user/.local/share" diff --git a/tests/modules/misc/xsession/basic-xprofile-expected.txt b/tests/modules/misc/xsession/basic-xprofile-expected.txt index 05733a974..282f8f5c5 100644 --- a/tests/modules/misc/xsession/basic-xprofile-expected.txt +++ b/tests/modules/misc/xsession/basic-xprofile-expected.txt @@ -1,4 +1,4 @@ -. "/test-home/.nix-profile/etc/profile.d/hm-session-vars.sh" +. "/home/hm-user/.nix-profile/etc/profile.d/hm-session-vars.sh" if [ -e "$HOME/.profile" ]; then . "$HOME/.profile" diff --git a/tests/modules/misc/xsession/basic.nix b/tests/modules/misc/xsession/basic.nix index 39df63623..d6756291c 100644 --- a/tests/modules/misc/xsession/basic.nix +++ b/tests/modules/misc/xsession/basic.nix @@ -4,8 +4,6 @@ with lib; { config = { - home.homeDirectory = "/test-home"; - xsession = { enable = true; windowManager.command = "window manager command"; diff --git a/tests/modules/misc/xsession/keyboard-without-layout.nix b/tests/modules/misc/xsession/keyboard-without-layout.nix index 015efe615..90038cfd0 100644 --- a/tests/modules/misc/xsession/keyboard-without-layout.nix +++ b/tests/modules/misc/xsession/keyboard-without-layout.nix @@ -6,8 +6,6 @@ with lib; config = { home.stateVersion = "19.09"; - home.homeDirectory = "/test-home"; - home.keyboard = { options = [ "ctrl:nocaps" "altwin:no_win" ]; }; xsession = { diff --git a/tests/modules/programs/bash/session-variables-expected.txt b/tests/modules/programs/bash/session-variables-expected.txt index e13d63d4c..0d93217a9 100644 --- a/tests/modules/programs/bash/session-variables-expected.txt +++ b/tests/modules/programs/bash/session-variables-expected.txt @@ -1,6 +1,6 @@ # -*- mode: sh -*- -. "/home/testuser/.nix-profile/etc/profile.d/hm-session-vars.sh" +. "/home/hm-user/.nix-profile/etc/profile.d/hm-session-vars.sh" export V1="v1" export V2="v2-v1" diff --git a/tests/modules/programs/bash/session-variables.nix b/tests/modules/programs/bash/session-variables.nix index ea789a1d0..1ef65a344 100644 --- a/tests/modules/programs/bash/session-variables.nix +++ b/tests/modules/programs/bash/session-variables.nix @@ -13,8 +13,6 @@ with lib; }; }; - home.homeDirectory = "/home/testuser"; - nmt.script = '' assertFileExists home-files/.profile assertFileContent \ diff --git a/tests/modules/programs/getmail/getmail.nix b/tests/modules/programs/getmail/getmail.nix index fe10b98f9..b0d979c46 100644 --- a/tests/modules/programs/getmail/getmail.nix +++ b/tests/modules/programs/getmail/getmail.nix @@ -6,9 +6,6 @@ with lib; imports = [ ../../accounts/email-test-accounts.nix ]; config = { - home.username = "hm-user"; - home.homeDirectory = "/home/hm-user"; - accounts.email.accounts = { "hm@example.com" = { getmail = { diff --git a/tests/modules/programs/lieer/lieer.nix b/tests/modules/programs/lieer/lieer.nix index 1671f4f5e..2ce4fb4e0 100644 --- a/tests/modules/programs/lieer/lieer.nix +++ b/tests/modules/programs/lieer/lieer.nix @@ -6,9 +6,6 @@ with lib; imports = [ ../../accounts/email-test-accounts.nix ]; config = { - home.username = "hm-user"; - home.homeDirectory = "/home/hm-user"; - programs.lieer.enable = true; accounts.email.accounts = { "hm@example.com".lieer.enable = true; }; diff --git a/tests/modules/programs/mbsync/mbsync.nix b/tests/modules/programs/mbsync/mbsync.nix index 03a54c178..fa8e28cb4 100644 --- a/tests/modules/programs/mbsync/mbsync.nix +++ b/tests/modules/programs/mbsync/mbsync.nix @@ -6,9 +6,6 @@ with lib; imports = [ ../../accounts/email-test-accounts.nix ]; config = { - home.username = "hm-user"; - home.homeDirectory = "/home/hm-user"; - programs.mbsync = { enable = true; groups.inboxes = { diff --git a/tests/modules/programs/neomutt/neomutt-with-msmtp.nix b/tests/modules/programs/neomutt/neomutt-with-msmtp.nix index 5f94d21a1..22f65599c 100644 --- a/tests/modules/programs/neomutt/neomutt-with-msmtp.nix +++ b/tests/modules/programs/neomutt/neomutt-with-msmtp.nix @@ -6,11 +6,6 @@ with lib; imports = [ ../../accounts/email-test-accounts.nix ]; config = { - home.username = "hm-user"; - home.homeDirectory = "/home/hm-user"; - xdg.configHome = mkForce "/home/hm-user/.config"; - xdg.cacheHome = mkForce "/home/hm-user/.cache"; - accounts.email.accounts = { "hm@example.com" = { primary = true; diff --git a/tests/modules/programs/neomutt/neomutt.nix b/tests/modules/programs/neomutt/neomutt.nix index 91cb9dca2..c0caa44af 100644 --- a/tests/modules/programs/neomutt/neomutt.nix +++ b/tests/modules/programs/neomutt/neomutt.nix @@ -6,11 +6,6 @@ with lib; imports = [ ../../accounts/email-test-accounts.nix ]; config = { - home.username = "hm-user"; - home.homeDirectory = "/home/hm-user"; - xdg.configHome = mkForce "/home/hm-user/.config"; - xdg.cacheHome = mkForce "/home/hm-user/.cache"; - accounts.email.accounts = { "hm@example.com" = { primary = true; diff --git a/tests/modules/programs/tmux/hm-session-vars.sh b/tests/modules/programs/tmux/hm-session-vars.sh deleted file mode 100644 index 40d9c24b5..000000000 --- a/tests/modules/programs/tmux/hm-session-vars.sh +++ /dev/null @@ -1,5 +0,0 @@ -# Only source this once. -if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi -export __HM_SESS_VARS_SOURCED=1 - -export TMUX_TMPDIR="${XDG_RUNTIME_DIR:-"/run/user/\$(id -u)"}" diff --git a/tests/modules/programs/tmux/secure-socket-enabled.nix b/tests/modules/programs/tmux/secure-socket-enabled.nix index 7e8c535c0..ca2de6631 100644 --- a/tests/modules/programs/tmux/secure-socket-enabled.nix +++ b/tests/modules/programs/tmux/secure-socket-enabled.nix @@ -11,9 +11,8 @@ with lib; nmt.script = '' assertFileExists home-path/etc/profile.d/hm-session-vars.sh - assertFileContent home-path/etc/profile.d/hm-session-vars.sh ${ - ./hm-session-vars.sh - } + assertFileContains home-path/etc/profile.d/hm-session-vars.sh \ + 'export TMUX_TMPDIR="''${XDG_RUNTIME_DIR:-"/run/user/\$(id -u)"}"' ''; }; } diff --git a/tests/modules/services/lieer/lieer-service.nix b/tests/modules/services/lieer/lieer-service.nix index faef5053c..03dcdc749 100644 --- a/tests/modules/services/lieer/lieer-service.nix +++ b/tests/modules/services/lieer/lieer-service.nix @@ -6,9 +6,6 @@ with lib; imports = [ ../../accounts/email-test-accounts.nix ]; config = { - home.username = "hm-user"; - home.homeDirectory = "/home/hm-user"; - services.lieer.enable = true; accounts.email.accounts = { diff --git a/tests/modules/targets/generic-linux-session-vars-expected.txt b/tests/modules/targets/generic-linux-session-vars-expected.txt deleted file mode 100644 index ad9e0ada6..000000000 --- a/tests/modules/targets/generic-linux-session-vars-expected.txt +++ /dev/null @@ -1,6 +0,0 @@ -# Only source this once. -if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi -export __HM_SESS_VARS_SOURCED=1 - -export XDG_DATA_DIRS="/nix/var/nix/profiles/default/share:/homeless-shelter/.nix-profile/share${XDG_DATA_DIRS:+:}$XDG_DATA_DIRS" -. "@nix@/etc/profile.d/nix.sh" diff --git a/tests/modules/targets/generic-linux.nix b/tests/modules/targets/generic-linux.nix index 2e06b2545..8596ac3d9 100644 --- a/tests/modules/targets/generic-linux.nix +++ b/tests/modules/targets/generic-linux.nix @@ -4,20 +4,16 @@ with lib; { config = { - home.homeDirectory = "/homeless-shelter"; - targets.genericLinux.enable = true; nmt.script = '' assertFileExists home-path/etc/profile.d/hm-session-vars.sh - assertFileContent \ + assertFileContains \ home-path/etc/profile.d/hm-session-vars.sh \ - ${ - pkgs.substituteAll { - src = ./generic-linux-session-vars-expected.txt; - nix = pkgs.nix; - } - } + 'export XDG_DATA_DIRS="/nix/var/nix/profiles/default/share:/home/hm-user/.nix-profile/share''${XDG_DATA_DIRS:+:}$XDG_DATA_DIRS"' + assertFileContains \ + home-path/etc/profile.d/hm-session-vars.sh \ + '. "${pkgs.nix}/etc/profile.d/nix.sh"' ''; }; }