From fd6660cb9182fde5e593246e311dc3c2dd4b9d13 Mon Sep 17 00:00:00 2001
From: Michael Hoang <enzime@users.noreply.github.com>
Date: Thu, 24 Oct 2024 23:55:25 +1100
Subject: [PATCH] tests: fix negative asserts with `grep` not working

Using `grep -v` without `-z` will return 0 even if there is a match
found as all the non-matching lines will be matched. Instead of using
`grep -vqz`, `(! grep ...)` is more readable.

The brackets are necessary as `! grep` will not trigger `set -e`[0], so we
run it inside a subshell to use its non-zero exit code.

[0]: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#The-Set-Builtin
---
 pkgs/darwin-installer/default.nix |  2 +-
 tests/programs-zsh.nix            |  2 +-
 tests/users-groups.nix            | 18 +++++++++---------
 3 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/pkgs/darwin-installer/default.nix b/pkgs/darwin-installer/default.nix
index 36643a36..37a391c0 100644
--- a/pkgs/darwin-installer/default.nix
+++ b/pkgs/darwin-installer/default.nix
@@ -118,7 +118,7 @@ stdenv.mkDerivation {
         test -e /etc/static
         echo >&2 "checking profile"
         cat /etc/profile
-        grep -v nix-daemon.sh /etc/profile
+        (! grep nix-daemon.sh /etc/profile)
         echo >&2 "checking /run/current-system"
         readlink /run
         test -e /run
diff --git a/tests/programs-zsh.nix b/tests/programs-zsh.nix
index 9c98c335..18680a5d 100644
--- a/tests/programs-zsh.nix
+++ b/tests/programs-zsh.nix
@@ -34,7 +34,7 @@
      echo >&2 "checking compinit in /etc/zshrc"
      grep 'autoload -U compinit && compinit' ${config.out}/etc/zshrc
      echo >&2 "checking bashcompinit in /etc/zshrc"
-     grep -vq 'bashcompinit' ${config.out}/etc/zshrc
+     (! grep 'bashcompinit' ${config.out}/etc/zshrc)
 
      echo >&2 "checking zprofile.d in /etc/zprofile"
      grep 'source /etc/zprofile.d/\*.conf' ${config.out}/etc/zprofile
diff --git a/tests/users-groups.nix b/tests/users-groups.nix
index 7df92ba8..5b4f1ae0 100644
--- a/tests/users-groups.nix
+++ b/tests/users-groups.nix
@@ -28,37 +28,37 @@
     grep "dscl . -create ${lib.escapeShellArg "/Groups/foo"} PrimaryGroupID 42000" ${config.out}/activate
     grep "dscl . -create ${lib.escapeShellArg "/Groups/foo"} RealName ${lib.escapeShellArg "Foo group"}" ${config.out}/activate
     grep "dscl . -create ${lib.escapeShellArg "/Groups/created.group"} PrimaryGroupID 42001" ${config.out}/activate
-    grep -qv "dscl . -delete ${lib.escapeShellArg "/Groups/created.group"}" ${config.out}/activate
+    (! grep "dscl . -delete ${lib.escapeShellArg "/Groups/created.group"}" ${config.out}/activate)
 
     # checking group deletion in /activate
     grep "dscl . -delete ${lib.escapeShellArg "/Groups/deleted.group"}" ${config.out}/activate
-    grep -qv "dscl . -create ${lib.escapeShellArg "/Groups/deleted.group"}" ${config.out}/activate
+    (! grep "dscl . -create ${lib.escapeShellArg "/Groups/deleted.group"}" ${config.out}/activate)
 
     echo "checking group membership in /activate" >&2
     grep "dscl . -create ${lib.escapeShellArg "/Groups/foo"} GroupMembership ${lib.escapeShellArgs [ "admin" "foo" ]}" ${config.out}/activate
     grep "dscl . -create ${lib.escapeShellArg "/Groups/created.group"} GroupMembership" ${config.out}/activate
 
     # checking unknown group in /activate
-    grep -qv "dscl . -create ${lib.escapeShellArg "/Groups/unknown.group"}" ${config.out}/activate
-    grep -qv "dscl . -delete ${lib.escapeShellArg "/Groups/unknown.group"}" ${config.out}/activate
+    (! grep "dscl . -create ${lib.escapeShellArg "/Groups/unknown.group"}" ${config.out}/activate)
+    (! grep "dscl . -delete ${lib.escapeShellArg "/Groups/unknown.group"}" ${config.out}/activate)
 
     # checking user creation in /activate
     grep "sysadminctl -addUser ${lib.escapeShellArgs [ "foo" "-UID" 42000 "-GID" 42000 "-fullName" "Foo user" "-home" "/Users/foo" "-shell" "/run/current-system/sw/bin/bash" ]}" ${config.out}/activate
     grep "createhomedir -cu ${lib.escapeShellArg "foo"}" ${config.out}/activate
     grep "sysadminctl -addUser ${lib.escapeShellArgs [ "created.user" "-UID" 42001 ]} .* ${lib.escapeShellArgs [ "-shell" "/sbin/nologin" ]}" ${config.out}/activate
-    grep -qv "deleteUser ${lib.escapeShellArg "created.user"}" ${config.out}/activate
-    grep -qv "deleteUser ${lib.escapeShellArg "created.user"}" ${config.out}/activate
+    (! grep "deleteUser ${lib.escapeShellArg "created.user"}" ${config.out}/activate)
+    (! grep "deleteUser ${lib.escapeShellArg "created.user"}" ${config.out}/activate)
 
     # checking user properties always get updated in /activate
     grep "dscl . -create ${lib.escapeShellArg "/Users/foo"} UserShell ${lib.escapeShellArg "/run/current-system/sw/bin/bash"}" ${config.out}/activate
 
     # checking user deletion in /activate
     grep "deleteUser ${lib.escapeShellArg "deleted.user"}" ${config.out}/activate
-    grep -qv "sysadminctl -addUser ${lib.escapeShellArg "deleted.user"}" ${config.out}/activate
+    (! grep "sysadminctl -addUser ${lib.escapeShellArg "deleted.user"}" ${config.out}/activate)
 
     # checking unknown user in /activate
-    grep -qv "sysadminctl -addUser ${lib.escapeShellArg "unknown.user"}" ${config.out}/activate
-    grep -qv "deleteUser ${lib.escapeShellArg "unknown.user"}" ${config.out}/activate
+    (! grep "sysadminctl -addUser ${lib.escapeShellArg "unknown.user"}" ${config.out}/activate)
+    (! grep "deleteUser ${lib.escapeShellArg "unknown.user"}" ${config.out}/activate)
 
     set +v
   '';