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).
This commit is contained in:
Ivan Trubach 2024-07-18 18:34:26 +03:00
parent fc5c829532
commit b40d043d5a

View File

@ -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"
'';
};
}