libtest-core: Add assert_files_equal
authorAlexander Larsson <alexl@redhat.com>
Tue, 26 Nov 2019 08:25:15 +0000 (09:25 +0100)
committerSimon McVittie <smcv@collabora.com>
Fri, 18 Jun 2021 08:46:31 +0000 (09:46 +0100)
[Originally from bubblewrap commits c5c999a7 "tests: test --userns"
and 3e5fe1bf "tests: Better error message if assert_files_equal fails";
separated into this commit by Simon McVittie.]

tests/kolainst/libtest-core.sh

index 64b3e0a30e0ea2de30faf6833f76ca20c6d48855..471a63f01dc20383a917de7c23cacf47a0d987e5 100644 (file)
@@ -83,6 +83,18 @@ _fatal_print_file() {
     fatal "$@"
 }
 
+_fatal_print_files() {
+    file1="$1"
+    shift
+    file2="$1"
+    shift
+    ls -al "$file1" >&2
+    sed -e 's/^/# /' < "$file1" >&2
+    ls -al "$file2" >&2
+    sed -e 's/^/# /' < "$file2" >&2
+    fatal "$@"
+}
+
 assert_not_has_file () {
     if test -f "$1"; then
         _fatal_print_file "$1" "File '$1' exists"
@@ -156,6 +168,12 @@ assert_file_empty() {
     fi
 }
 
+assert_files_equal() {
+    if ! cmp "$1" "$2"; then
+        _fatal_print_files "$1" "$2" "File '$1' and '$2' is not equal"
+    fi
+}
+
 # Use to skip all of these tests
 skip() {
     echo "1..0 # SKIP" "$@"