tests: make `bt_diff()` able to write expected files
authorSimon Marchi <simon.marchi@efficios.com>
Thu, 3 Oct 2024 01:56:50 +0000 (21:56 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 9 Oct 2024 02:56:57 +0000 (22:56 -0400)
Make `bt_diff()` copy the "actual" file over to the "expected" file, if:

 - the comparison between "actual" and "expected" failed
 - the "expected" file is a regular file
 - the `BT_TESTS_DIFF_WRITE_EXPECTED` env var is set and not `0`

This makes it much easier to update `.expect` files than doing it by
hand.

Change-Id: Ia55b55035591d521c627b1022d6dcbde9ed541a7
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/13313
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
tests/utils/utils.sh

index 37ffc1e1109a023320cf312915f27138b2bb874c..03756fb50dba7963cbbbda773c9723042ddea8f2 100644 (file)
@@ -234,11 +234,19 @@ bt_cli() {
 # Checks the differences between:
 #
 # • The (expected) contents of the file having the path `$1`.
-#
 # • The contents of another file having the path `$2`.
 #
-# Both files are passed through bt_remove_cr_inline() to remove CR
-# characters.
+# Both files are passed through bt_remove_cr_inline() to remove
+# CR characters.
+#
+# ┌──────────────────────────────────────────────────────────────────┐
+# │ NOTE: A common activity being to update an existing expectation  │
+# │ file from an actual output when developing features and fixing   │
+# │ bugs, and knowing that this function processes the majority of   │
+# │ expectation and result files, this function overwrites `$1` with │
+# │ `$2` when the `BT_TESTS_DIFF_WRITE_EXPECTED` variable isn't `0`  │
+# │ and `diff` finds differences.                                    │
+# └──────────────────────────────────────────────────────────────────┘
 #
 # Returns 0 if there's no difference, or not zero otherwise.
 bt_diff() {
@@ -256,6 +264,14 @@ bt_diff() {
        fi
 
        diff -u <(bt_remove_cr_inline "$expected_file") <(bt_remove_cr_inline "$actual_file") 1>&2
+
+       local -r ret=$?
+
+       if [[ $ret != 0 && -f $expected_file && ${BT_TESTS_DIFF_WRITE_EXPECTED:-0} != 0 ]]; then
+               cp "$actual_file" "$expected_file"
+       fi
+
+       return $ret
 }
 
 # Checks the difference between:
This page took 0.024892 seconds and 4 git commands to generate.