-
Notifications
You must be signed in to change notification settings - Fork 75
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
Require STDIN to be specified explicitly with -
#14
base: master
Are you sure you want to change the base?
Changes from all commits
1a75841
b4e90ad
0fb9d2a
ae447cd
0dc0b49
581aa70
adc1c7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,8 +172,9 @@ assert_failure() { | |
# Options: | ||
# -p, --partial - partial matching | ||
# -e, --regexp - extended regular expression matching | ||
# -, --stdin - read expected output from the standard input | ||
# Arguments: | ||
# $1 - [=STDIN] expected output | ||
# $1 - expected output | ||
# Returns: | ||
# 0 - expected matches the actual output | ||
# 1 - otherwise | ||
|
@@ -185,12 +186,14 @@ assert_failure() { | |
assert_output() { | ||
local -i is_mode_partial=0 | ||
local -i is_mode_regexp=0 | ||
local -i use_stdin=0 | ||
|
||
# Handle options. | ||
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 | ||
|
@@ -205,17 +208,19 @@ assert_output() { | |
|
||
# Arguments. | ||
local expected | ||
(( $# == 0 )) && expected="$(cat -)" || expected="$1" | ||
if (( use_stdin )); then | ||
expected="$(cat -)" | ||
else | ||
expected="$1" | ||
fi | ||
|
||
# Matching. | ||
if (( is_mode_regexp )); then | ||
if [[ '' =~ $expected ]] || (( $? == 2 )); then | ||
echo "Invalid extended regular expression: \`$expected'" \ | ||
| batslib_decorate 'ERROR: assert_output' \ | ||
| fail | ||
return $? | ||
fi | ||
if ! [[ $output =~ $expected ]]; then | ||
elif ! [[ $output =~ $expected ]]; then | ||
batslib_print_kv_single_or_multi 6 \ | ||
'regexp' "$expected" \ | ||
'output' "$output" \ | ||
|
@@ -265,8 +270,9 @@ assert_output() { | |
# Options: | ||
# -p, --partial - partial matching | ||
# -e, --regexp - extended regular expression matching | ||
# -, --stdin - read unexpected output from the standard input | ||
# Arguments: | ||
# $1 - [=STDIN] unexpected output | ||
# $1 - unexpected output | ||
# Returns: | ||
# 0 - unexpected matches the actual output | ||
# 1 - otherwise | ||
|
@@ -278,12 +284,14 @@ assert_output() { | |
refute_output() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you update the function comment too? For example: # Options:
# -p, --partial - partial matching
# -e, --regexp - extended regular expression matching
+# - - read unexpected output from the standard input
# Arguments:
-# $1 - [=STDIN] unexpected output
+# $1 - unexpected output
# Returns:
# 0 - unexpected matches the actual output
# 1 - otherwise |
||
local -i is_mode_partial=0 | ||
local -i is_mode_regexp=0 | ||
local -i use_stdin=0 | ||
|
||
# Handle options. | ||
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 | ||
|
@@ -298,7 +306,11 @@ refute_output() { | |
|
||
# Arguments. | ||
local unexpected | ||
(( $# == 0 )) && unexpected="$(cat -)" || unexpected="$1" | ||
if (( use_stdin )); then | ||
unexpected="$(cat -)" | ||
else | ||
unexpected="$1" | ||
fi | ||
|
||
if (( is_mode_regexp == 1 )) && [[ '' =~ $unexpected ]] || (( $? == 2 )); then | ||
echo "Invalid extended regular expression: \`$unexpected'" \ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,12 +26,20 @@ load test_helper | |
[ "${lines[3]}" == '--' ] | ||
} | ||
|
||
@test 'assert_output(): reads <expected> from STDIN' { | ||
@test 'assert_output() - : reads <expected> from STDIN' { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please factor out common parts of tests to avoid duplicating code. Like it's done here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually have a strong dislike of that extraction. Generally I favor non-dry tests because tests should be explicit and self-contained. In this case (and with If anything should be extracted, I'd extract the duplicated assertions from all tests:
The implementation of those statements are not germane to the test, so abstracting them away can improve readability. However, extracting away the actual invocation is a bad idea, IMO. The unique invocation is literally the raison d'être for the test(s). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened a PR to this PR to show usage of just the assertion helper rather than attempting to dry up the entire test(s). |
||
run echo 'a' | ||
run assert_output <<STDIN | ||
run assert_output - <<STDIN | ||
a | ||
STDIN | ||
[ "$status" -eq 0 ] | ||
[ "${#lines[@]}" -eq 0 ] | ||
} | ||
|
||
@test 'assert_output() --stdin : reads <expected> from STDIN' { | ||
run echo 'a' | ||
run assert_output --stdin <<STDIN | ||
a | ||
STDIN | ||
echo "$output" | ||
[ "$status" -eq 0 ] | ||
[ "${#lines[@]}" -eq 0 ] | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,9 +25,18 @@ load test_helper | |
[ "${lines[2]}" == '--' ] | ||
} | ||
|
||
@test 'refute_output(): reads <unexpected> from STDIN' { | ||
run echo 'a' | ||
run refute_output <<INPUT | ||
@test 'refute_output() - : reads <unexpected> from STDIN' { | ||
run echo '-' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you change the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a stronger assertion that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, okay got it. Thanks! |
||
run refute_output - <<INPUT | ||
b | ||
INPUT | ||
[ "$status" -eq 0 ] | ||
[ "${#lines[@]}" -eq 0 ] | ||
} | ||
|
||
@test 'refute_output() --stdin : reads <unexpected> from STDIN' { | ||
run echo '--stdin' | ||
run refute_output --stdin <<INPUT | ||
b | ||
INPUT | ||
[ "$status" -eq 0 ] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you update the function comment too? For example: