From 65d80ff71e685213c40db5e5e5b8327915a4c5ca Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Wed, 2 Oct 2024 21:56:50 -0400 Subject: [PATCH] tests: make `bt_diff()` able to write expected files 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 Reviewed-on: https://review.lttng.org/c/babeltrace/+/13313 Tested-by: jenkins Reviewed-by: Philippe Proulx --- tests/utils/utils.sh | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/utils/utils.sh b/tests/utils/utils.sh index 37ffc1e1..03756fb5 100644 --- a/tests/utils/utils.sh +++ b/tests/utils/utils.sh @@ -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: -- 2.34.1