From b40d043d5a2a1fb473f1c6c853dac444ceadab62 Mon Sep 17 00:00:00 2001 From: Ivan Trubach Date: Thu, 18 Jul 2024 18:34:26 +0300 Subject: [PATCH] test.testers: update tests for testers.testEqualContents Updates tests for testers.testEqualContents with diffoscope and fixes some bugs in tests (e.g. fileDiff always succeed because subshell does not inherit errexit option). --- pkgs/build-support/testers/test/default.nix | 214 ++++++++++---------- 1 file changed, 108 insertions(+), 106 deletions(-) diff --git a/pkgs/build-support/testers/test/default.nix b/pkgs/build-support/testers/test/default.nix index ae7bbe810fa9..8e8886342f1e 100644 --- a/pkgs/build-support/testers/test/default.nix +++ b/pkgs/build-support/testers/test/default.nix @@ -1,4 +1,4 @@ -{ testers, lib, pkgs, hello, runCommand, ... }: +{ testers, lib, pkgs, hello, runCommand, emptyFile, emptyDirectory, ... }: let pkgs-with-overlay = pkgs.extend(final: prev: { proof-of-overlay-hello = prev.hello; @@ -101,131 +101,133 @@ lib.recurseIntoAttrs { testEqualContents = lib.recurseIntoAttrs { equalDir = testers.testEqualContents { assertion = "The same directory contents at different paths are recognized as equal"; - expected = runCommand "expected" {} '' - mkdir -p $out/c - echo a >$out/a - echo b >$out/b - echo d >$out/c/d - echo e >$out/e - chmod a+x $out/e + expected = runCommand "expected" { } '' + mkdir -p -- "$out/c" + echo a >"$out/a" + echo b >"$out/b" + echo d >"$out/c/d" + echo e >"$out/e" + chmod a+x -- "$out/e" ''; - actual = runCommand "actual" {} '' - mkdir -p $out/c - echo a >$out/a - echo b >$out/b - echo d >$out/c/d - echo e >$out/e - chmod a+x $out/e + actual = runCommand "actual" { } '' + mkdir -p -- "$out/c" + echo a >"$out/a" + echo b >"$out/b" + echo d >"$out/c/d" + echo e >"$out/e" + chmod a+x -- "$out/e" ''; }; + fileMissing = testers.testBuildFailure ( + testers.testEqualContents { + assertion = "Directories with different file list are not recognized as equal"; + expected = runCommand "expected" { } '' + mkdir -p -- "$out/c" + echo a >"$out/a" + echo b >"$out/b" + echo d >"$out/c/d" + ''; + actual = runCommand "actual" { } '' + mkdir -p -- "$out/c" + echo a >"$out/a" + echo d >"$out/c/d" + ''; + } + ); + equalExe = testers.testEqualContents { assertion = "The same executable file contents at different paths are recognized as equal"; expected = runCommand "expected" { } '' - echo test >$out - chmod a+x $out + echo test >"$out" + chmod a+x -- "$out" ''; actual = runCommand "actual" { } '' - echo test >$out - chmod a+x $out + echo test >"$out" + chmod a+x -- "$out" ''; }; - unequalExe = - runCommand "testEqualContents-unequalExe" { - log = testers.testBuildFailure (testers.testEqualContents { - assertion = "The same directory contents at different paths are recognized as equal"; - expected = runCommand "expected" {} '' - mkdir -p $out/c - echo a >$out/a - chmod a+x $out/a - echo b >$out/b - echo d >$out/c/d - ''; - actual = runCommand "actual" {} '' - mkdir -p $out/c - echo a >$out/a - echo b >$out/b - chmod a+x $out/b - echo d >$out/c/d - ''; - }); - } '' - ( - set -x - grep -F -- "executable bits don't match" $log/testBuildFailure.log - grep -E -- '+.*-actual/a' $log/testBuildFailure.log - grep -E -- '-.*-actual/b' $log/testBuildFailure.log - grep -F -- "--- actual-executables" $log/testBuildFailure.log - grep -F -- "+++ expected-executables" $log/testBuildFailure.log - ) || { - echo "Test failed: could not find pattern in build log $log" - exit 1 - } - echo 'All good.' - touch $out - ''; + unequalExe = testers.testBuildFailure ( + testers.testEqualContents { + assertion = "Different file mode bits are not recognized as equal"; + expected = runCommand "expected" { } '' + touch -- "$out" + chmod a+x -- "$out" + ''; + actual = runCommand "actual" { } '' + touch -- "$out" + ''; + } + ); + + unequalExeInDir = testers.testBuildFailure ( + testers.testEqualContents { + assertion = "Different file mode bits are not recognized as equal in directory"; + expected = runCommand "expected" { } '' + mkdir -p -- "$out/a" + echo b >"$out/b" + chmod a+x -- "$out/b" + ''; + actual = runCommand "actual" { } '' + mkdir -p -- "$out/a" + echo b >"$out/b" + ''; + } + ); + + nonExistentPath = testers.testBuildFailure ( + testers.testEqualContents { + assertion = "Non existent paths are not recognized as equal"; + expected = "${emptyDirectory}/foo"; + actual = "${emptyDirectory}/bar"; + } + ); + + emptyFileAndDir = testers.testBuildFailure ( + testers.testEqualContents { + assertion = "Empty file and directory are not recognized as equal"; + expected = emptyFile; + actual = emptyDirectory; + } + ); fileDiff = - runCommand "testEqualContents-fileDiff" { - log = testers.testBuildFailure (testers.testEqualContents { - assertion = "The same directory contents at different paths are recognized as equal"; - expected = runCommand "expected" {} '' - mkdir -p $out/c - echo a >$out/a - echo b >$out/b - echo d >$out/c/d - ''; - actual = runCommand "actual" {} '' - mkdir -p $out/c - echo a >$out/a - echo B >$out/b - echo d >$out/c/d - ''; - }); - } '' + let + log = testers.testBuildFailure ( + testers.testEqualContents { + assertion = "Different files are not recognized as equal in subdirectories"; + expected = runCommand "expected" { } '' + mkdir -p -- "$out/b" + echo a >"$out/a" + echo EXPECTED >"$out/b/c" + ''; + actual = runCommand "actual" { } '' + mkdir -p "$out/b" + echo a >"$out/a" + echo ACTUAL >"$out/b/c" + ''; + } + ); + in + runCommand "testEqualContents-fileDiff" { inherit log; } '' ( set -x - grep -F -- "Contents must be equal but were not" $log/testBuildFailure.log - grep -E -- '+++ .*-actual/b' $log/testBuildFailure.log - grep -E -- '--- .*-actual/b' $log/testBuildFailure.log - grep -F -- "-B" $log/testBuildFailure.log - grep -F -- "+b" $log/testBuildFailure.log + # Note: use `&&` operator to chain commands because errexit (set -e) + # does not work in this context (even when set explicitly and with + # inherit_errexit), otherwise the subshell exits with the status of + # the last run command and ignores preceding failures. + grep -F -- 'Contents must be equal, but were not!' "$log/testBuildFailure.log" && + grep -E -- '\+\+\+ .*-expected/b/c' "$log/testBuildFailure.log" && + grep -E -- '--- .*-actual/b/c' "$log/testBuildFailure.log" && + grep -F -- -ACTUAL "$log/testBuildFailure.log" && + grep -F -- +EXPECTED "$log/testBuildFailure.log" ) || { echo "Test failed: could not find pattern in build log $log" - exit 1 + false } echo 'All good.' - touch $out - ''; - - fileMissing = - runCommand "testEqualContents-fileMissing" { - log = testers.testBuildFailure (testers.testEqualContents { - assertion = "The same directory contents at different paths are recognized as equal"; - expected = runCommand "expected" {} '' - mkdir -p $out/c - echo a >$out/a - echo b >$out/b - echo d >$out/c/d - ''; - actual = runCommand "actual" {} '' - mkdir -p $out/c - echo a >$out/a - echo d >$out/c/d - ''; - }); - } '' - ( - set -x - grep -F -- "Contents must be equal but were not" $log/testBuildFailure.log - grep -E -- 'Only in .*-expected: b' $log/testBuildFailure.log - ) || { - echo "Test failed: could not find pattern in build log $log" - exit 1 - } - echo 'All good.' - touch $out + touch -- "$out" ''; }; }