Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add assert_stderr and assert_stderr_line #52

Closed
wants to merge 1 commit into from

Conversation

adoyle-h
Copy link

@adoyle-h adoyle-h commented Dec 8, 2022

fix #42

@adoyle-h
Copy link
Author

adoyle-h commented Dec 8, 2022

@martin-schulze-vireso This PR is ready. Can you review it?

adoyle-h added a commit to adoyle-h/lobash that referenced this pull request Dec 11, 2022
Copy link
Member

@martin-schulze-vireso martin-schulze-vireso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really dislike the amount of code duplication. Can we have a central implementation that is used by both assert_output and assert_stderr (and _lines likewise)? Similarly, I'd fold the documentation into assert_output's or link to it from there. Feature changes will sooner or later get lost on one of the two duplicates.

@martin-schulze-vireso
Copy link
Member

@adoyle-h Do you plan to work on this?

@adoyle-h
Copy link
Author

Sorry, I'm occupied with work.

@adoyle-h adoyle-h closed this Aug 24, 2023
@nicola-lunghi
Copy link

Hi @martin-schulze-vireso
just stumbled on this
can we reopen this so we can fix it?

@martin-schulze-vireso
Copy link
Member

martin-schulze-vireso commented Dec 17, 2024

If you are willing to work in this, you can use the code as starting point but have to open your own PR.

I don't have the time to do so myself right now.

@nicola-lunghi
Copy link

nicola-lunghi commented Dec 21, 2024

If you are willing to work in this, you can use the code as starting point but have to open your own PR.

I don't have the time to do so myself right now.

Hi @martin-schulze-vireso started working on this
what about a --stderr option in assert_output?

assert_output --stderr "My stderr out"

I think this wil allows to reuse most of assert_output in

assert_output() {
local -i is_mode_partial=0
local -i is_mode_regexp=0
local -i is_mode_nonempty=0
local -i use_stdin=0
: "${output?}"
# Handle options.
if (( $# == 0 )); then
is_mode_nonempty=1
fi
while (( $# > 0 )); do
case "$1" in
-p|--partial) is_mode_partial=1; shift ;;
-e|--regexp) is_mode_regexp=1; shift ;;
-|--stdin) use_stdin=1; shift ;;
--) shift; break ;;
*) break ;;
esac
done
if (( is_mode_partial )) && (( is_mode_regexp )); then
echo "\`--partial' and \`--regexp' are mutually exclusive" \
| batslib_decorate 'ERROR: assert_output' \
| fail
return $?
fi
# Arguments.
local expected
if (( use_stdin )); then
expected="$(cat -)"
else
expected="${1-}"
fi
# Matching.
if (( is_mode_nonempty )); then
if [ -z "$output" ]; then
echo 'expected non-empty output, but output was empty' \
| batslib_decorate 'no output' \
| fail
fi
elif (( is_mode_regexp )); then
if [[ '' =~ $expected ]] || (( $? == 2 )); then
echo "Invalid extended regular expression: \`$expected'" \
| batslib_decorate 'ERROR: assert_output' \
| fail
elif ! [[ $output =~ $expected ]]; then
batslib_print_kv_single_or_multi 6 \
'regexp' "$expected" \
'output' "$output" \
| batslib_decorate 'regular expression does not match output' \
| fail
fi
elif (( is_mode_partial )); then
if [[ $output != *"$expected"* ]]; then
batslib_print_kv_single_or_multi 9 \
'substring' "$expected" \
'output' "$output" \
| batslib_decorate 'output does not contain substring' \
| fail
fi
else
if [[ $output != "$expected" ]]; then
batslib_print_kv_single_or_multi 8 \
'expected' "$expected" \
'actual' "$output" \
| batslib_decorate 'output differs' \
| fail
fi
fi
}

also we can define an "alias" for assert_stderr => assert_output --stderr

@nicola-lunghi
Copy link

see #69

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing assert_stderr and similar assertions
3 participants