When a test that consists of matching a pattern in a file using grep
fails (sometimes just in the CI), it would be useful to see the file
contents to understand what happened. Add the `bt_grep_ok` utility
function. It tries to match a pattern in a file using `grep`, then
issues a test result. If the test failed, it prints the content of the
file on stderr.
Change-Id: I464e414b334052677799ce201483095fa9bff4c3
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11169
Reviewed-by: Michael Jeanson <mjeanson@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
# Check that expected warning is printed.
# shellcheck disable=SC2016
# Check that expected warning is printed.
# shellcheck disable=SC2016
-bt_grep -q 'No trace was found based on input `some_other_non_opt`' "$stderr_actual_file"
-ok "$?" "warning is printed"
+bt_grep_ok \
+ 'No trace was found based on input `some_other_non_opt`' \
+ "$stderr_actual_file" \
+ "warning is printed"
rm -f "$stdout_actual_file"
rm -f "$stderr_actual_file"
rm -f "$stdout_actual_file"
rm -f "$stderr_actual_file"
# Ensure that a CLI error stack is printed (and that babeltrace doesn't
# abort before that).
# Ensure that a CLI error stack is printed (and that babeltrace doesn't
# abort before that).
- bt_grep --silent "^ERROR: " "${stderr_file}"
- ok $? "${test_name}: babeltrace produces an error stack"
-
- bt_grep --silent "${expected_str}" "${stderr_file}"
- ok "$?" "${test_name}: expect \`${expected_str}\` error message on stderr"
+ bt_grep_ok \
+ "^ERROR: " \
+ "${stderr_file}" \
+ "${test_name}: babeltrace produces an error stack"
+
+ bt_grep_ok \
+ "${expected_str}" \
+ "${stderr_file}" \
+ "${test_name}: expect \`${expected_str}\` error message on stderr"
}
expect_success 'the-object:{}' \
}
expect_success 'the-object:{}' \
bt_cli "${stdout}" "${stderr}" help ctf
ok $? "help ctf plugin exit status"
bt_cli "${stdout}" "${stderr}" help ctf
ok $? "help ctf plugin exit status"
-bt_grep --silent 'Description: CTF input and output' "${stdout}"
-ok $? "help ctf plugin expected output"
+bt_grep_ok \
+ 'Description: CTF input and output' \
+ "${stdout}" \
+ "help ctf plugin expected output"
is_empty "${stderr}"
ok $? "help ctf plugin produces no error"
is_empty "${stderr}"
ok $? "help ctf plugin produces no error"
bt_cli "${stdout}" "${stderr}" help src.ctf.fs
ok $? "help src.ctf.fs component class exit status"
bt_cli "${stdout}" "${stderr}" help src.ctf.fs
ok $? "help src.ctf.fs component class exit status"
-bt_grep --silent 'Description: Read CTF traces from the file system.' "${stdout}"
-ok $? "help src.ctf.fs component class expected output"
+bt_grep_ok \
+ 'Description: Read CTF traces from the file system.' \
+ "${stdout}" \
+ "help src.ctf.fs component class expected output"
is_empty "${stderr}"
ok $? "help src.ctf.fs component class produces no error"
is_empty "${stderr}"
ok $? "help src.ctf.fs component class produces no error"
bt_cli "${stdout}" "${stderr}" help
isnt $? 0 "help without parameter exit status"
bt_cli "${stdout}" "${stderr}" help
isnt $? 0 "help without parameter exit status"
-bt_grep --silent "Missing plugin name or component class descriptor." "${stderr}"
-ok $? "help without parameter produces expected error"
+bt_grep_ok \
+ "Missing plugin name or component class descriptor." \
+ "${stderr}" \
+ "help without parameter produces expected error"
is_empty "${stdout}"
ok $? "help without parameter produces no output"
is_empty "${stdout}"
ok $? "help without parameter produces no output"
bt_cli "${stdout}" "${stderr}" help ctf fs
isnt $? 0 "help with too many parameters exit status"
bt_cli "${stdout}" "${stderr}" help ctf fs
isnt $? 0 "help with too many parameters exit status"
-bt_grep --silent "Extraneous command-line argument specified to \`help\` command:" "${stderr}"
-ok $? "help with too many parameters produces expected error"
+bt_grep_ok \
+ "Extraneous command-line argument specified to \`help\` command:" \
+ "${stderr}" \
+ "help with too many parameters produces expected error"
is_empty "${stdout}"
ok $? "help with too many parameters produces no output"
is_empty "${stdout}"
ok $? "help with too many parameters produces no output"
bt_cli "${stdout}" "${stderr}" help zigotos
isnt $? 0 "help with unknown plugin name"
bt_cli "${stdout}" "${stderr}" help zigotos
isnt $? 0 "help with unknown plugin name"
-bt_grep --silent 'Cannot find plugin: plugin-name="zigotos"' "${stderr}"
-ok $? "help with unknown plugin name produces expected error"
+bt_grep_ok \
+ 'Cannot find plugin: plugin-name="zigotos"' \
+ "${stderr}" \
+ "help with unknown plugin name produces expected error"
is_empty "${stdout}"
ok $? "help with unknown plugin name produces no output"
is_empty "${stdout}"
ok $? "help with unknown plugin name produces no output"
bt_cli "${stdout}" "${stderr}" help src.ctf.bob
isnt $? 0 "help with unknown component class name"
bt_cli "${stdout}" "${stderr}" help src.ctf.bob
isnt $? 0 "help with unknown component class name"
-bt_grep --silent 'Cannot find component class: plugin-name="ctf", comp-cls-name="bob", comp-cls-type=SOURCE' "${stderr}"
-ok $? "help with unknown component class name produces expected error"
+bt_grep_ok \
+ 'Cannot find component class: plugin-name="ctf", comp-cls-name="bob", comp-cls-type=SOURCE' \
+ "${stderr}" \
+ "help with unknown component class name produces expected error"
-bt_grep --silent 'Description: CTF input and output' "${stdout}"
-ok $? "help with unknown component class name prints plugin help"
+bt_grep_ok \
+ 'Description: CTF input and output' \
+ "${stdout}" \
+ "help with unknown component class name prints plugin help"
# Test with unknown component class plugin
bt_cli "${stdout}" "${stderr}" help src.bob.fs
isnt $? 0 "help with unknown component class plugin"
# Test with unknown component class plugin
bt_cli "${stdout}" "${stderr}" help src.bob.fs
isnt $? 0 "help with unknown component class plugin"
-bt_grep --silent 'Cannot find plugin: plugin-name="bob"' "${stderr}"
-ok $? "help with unknown component class plugin produces expected error"
+bt_grep_ok \
+ 'Cannot find plugin: plugin-name="bob"' \
+ "${stderr}" \
+ "help with unknown component class plugin produces expected error"
is_empty "${stdout}"
ok $? "help with unknown component class plugin produces no output"
is_empty "${stdout}"
ok $? "help with unknown component class plugin produces no output"
bt_cli "${stdout}" "${stderr}" --stream-intersection "${trace}"
isnt "$?" 0 "run with --stream-intersection fails"
bt_cli "${stdout}" "${stderr}" --stream-intersection "${trace}"
isnt "$?" 0 "run with --stream-intersection fails"
- bt_grep --silent "${expected_error_message}" "${stderr}"
- ok $? "stderr contains expected error message"
+ bt_grep_ok \
+ "${expected_error_message}" \
+ "${stderr}" \
+ "stderr contains expected error message"
}
diag "Test the stream intersection feature"
}
diag "Test the stream intersection feature"
# even if Babeltrace aborts (e.g. hits an assert). Check that the
# Babeltrace CLI finishes gracefully by checking that the error stream
# contains an error stack printed by the CLI.
# even if Babeltrace aborts (e.g. hits an assert). Check that the
# Babeltrace CLI finishes gracefully by checking that the error stream
# contains an error stack printed by the CLI.
- bt_grep --silent "^CAUSED BY " "${stderr_file}"
- ok $? "Trace ${name}: babeltrace produces an error stack"
-
- bt_grep --silent "${expected_error_msg}" "${stderr_file}"
- ok $? "Trace ${name}: babeltrace produces the expected error message"
+ bt_grep_ok \
+ "^CAUSED BY " \
+ "$stderr_file" \
+ "Trace $name: babeltrace produces an error stack"
+
+ bt_grep_ok \
+ "$expected_error_msg" \
+ "$stderr_file" \
+ "Trace $name: babeltrace produces the expected error message"
bt_diff "/dev/null" "${stdout_file}"
ok $? "non existent trace dir: babeltrace produces the expected stdout"
bt_diff "/dev/null" "${stdout_file}"
ok $? "non existent trace dir: babeltrace produces the expected stdout"
- bt_grep --silent "^CAUSED BY " "${stderr_file}"
- ok $? "non existent trace dir: babeltrace produces an error stack"
-
- bt_grep --silent "Failed to open metadata file: No such file or directory: path=\".*metadata\"" \
- "${stderr_file}"
- ok $? "non existent trace dir: babeltrace produces the expected error message"
+ bt_grep_ok \
+ "^CAUSED BY " \
+ "${stderr_file}" \
+ "non existent trace dir: babeltrace produces an error stack"
+
+ bt_grep_ok \
+ "Failed to open metadata file: No such file or directory: path=\".*metadata\"" \
+ "$stderr_file" \
+ "non existent trace dir: babeltrace produces the expected error message"
rm -f "${stdout_file}" "${stderr_file}"
rmdir "${empty_dir}"
rm -f "${stdout_file}" "${stderr_file}"
rmdir "${empty_dir}"
-c src.ctf.fs -p "inputs=[${inputs}]"
isnt 0 "$?" "${test_name}: exit status is not 0"
-c src.ctf.fs -p "inputs=[${inputs}]"
isnt 0 "$?" "${test_name}: exit status is not 0"
- bt_grep --silent "^ERROR: " "${stderr_file}"
- ok "$?" "${test_name}: error stack is produced"
-
- bt_grep --silent "No event class with ID of event class ID to use in stream class" "${stderr_file}"
- ok "$?" "${test_name}: expected error message is present"
+ bt_grep_ok \
+ "^ERROR: " \
+ "${stderr_file}" \
+ "${test_name}: error stack is produced"
+
+ bt_grep_ok \
+ "No event class with ID of event class ID to use in stream class" \
+ "$stderr_file" \
+ "$test_name: expected error message is present"
"$BT_TESTS_GREP_BIN" "$@"
}
"$BT_TESTS_GREP_BIN" "$@"
}
+# ok() with the test name `$3` on the result of bt_grep() matching the
+# pattern `$1` within the file `$2`.
+bt_grep_ok() {
+ local pattern=$1
+ local file=$2
+ local test_name=$3
+
+ bt_grep --silent "$pattern" "$file"
+
+ local ret=$?
+
+ if ! ok $ret "$test_name"; then
+ {
+ echo "Pattern \`$pattern\` doesn't match the contents of \`$file\`:"
+ echo '--- 8< ---'
+ cat "$file"
+ echo '--- >8 ---'
+ } >&2
+ fi
+
+ return $ret
+}
+
### Functions ###
check_coverage() {
### Functions ###
check_coverage() {