Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules
/yarn.lock
/bats-assert-*.tgz
test/.bats/run-logs/
7 changes: 2 additions & 5 deletions src/assert_line.bash
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,8 @@ Did you mean to call \`assert_line\` or \`assert_stderr_line\`?" \
# Arguments.
local -r expected="$1"

if (( is_mode_regexp == 1 )) && [[ '' =~ $expected ]] || (( $? == 2 )); then
echo "Invalid extended regular expression: \`$expected'" \
| batslib_decorate "ERROR: ${caller}" \
| fail
return $?
if (( is_mode_regexp == 1 )) then
Copy link

Choose a reason for hiding this comment

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

Don't we need a ; at the end?

Suggested change
if (( is_mode_regexp == 1 )) then
if (( is_mode_regexp == 1 )); then

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, it seems to not be required anymore with bash 5.2 onwards, so it got through the tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

I fixed that in #85.

Copy link

Choose a reason for hiding this comment

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

Thank you very much 🍺 !

__check_is_valid_regex "$expected" "$caller" || return 1
fi

# Matching.
Expand Down
6 changes: 2 additions & 4 deletions src/assert_output.bash
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,8 @@ Did you mean to call \`assert_output\` or \`assert_stderr\`?" |
fi
elif (( is_mode_regexp )); then
# shellcheck disable=2319
if [[ '' =~ $expected ]] || (( $? == 2 )); then
echo "Invalid extended regular expression: \`$expected'" \
| batslib_decorate "ERROR: ${caller}" \
| fail
if ! __check_is_valid_regex "$expected" "$caller"; then
return 1
elif ! [[ $stream =~ $expected ]]; then
batslib_print_kv_single_or_multi 6 \
'regexp' "$expected" \
Expand Down
6 changes: 2 additions & 4 deletions src/assert_regex.bash
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ assert_regex() {
local -r value="${1}"
local -r pattern="${2}"

if [[ '' =~ ${pattern} ]]; (( ${?} == 2 )); then
echo "Invalid extended regular expression: \`${pattern}'" \
| batslib_decorate 'ERROR: assert_regex' \
| fail
if ! __check_is_valid_regex "$pattern" assert_regex; then
return 1
elif ! [[ "${value}" =~ ${pattern} ]]; then
if shopt -p nocasematch &>/dev/null; then
local case_sensitive=insensitive
Expand Down
33 changes: 28 additions & 5 deletions src/refute_line.bash
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,32 @@ refute_stderr_line() {
__refute_stream_line "$@"
}

# __check_is_vvalid_regex
# =======================
#
# Summary: checks if the regex in unexpected is valid, also prints an error message if not.
# IO:
# STDERR - details, on error
# Globals:
# caller - readonly
# Returns:
# 0 - if regex is valid
# 1 - otherwise
__check_is_valid_regex() { # <regex> <caller>
local -r regex="$1" caller="$2"
local error
error=$([[ '' =~ $regex ]] 2>&1) # capture the detailed error message on Bash >=5.3
if [[ $? == 2 ]]; then
local err_msg="Invalid extended regular expression: \`$regex'"
if [[ $error =~ (invalid regular expression .*) ]]; then
err_msg="${BASH_REMATCH[1]}"
fi
echo "$err_msg" \
| batslib_decorate "ERROR: ${caller}" \
| fail
fi
}

__refute_stream_line() {
local -r caller=${FUNCNAME[1]}
local -i is_match_line=0
Expand Down Expand Up @@ -214,11 +240,8 @@ Did you mean to call \`refute_line\` or \`refute_stderr_line\`?" |
# Arguments.
local -r unexpected="$1"

if (( is_mode_regexp == 1 )) && [[ '' =~ $unexpected ]] || (( $? == 2 )); then
echo "Invalid extended regular expression: \`$unexpected'" \
| batslib_decorate "ERROR: ${caller}" \
| fail
return $?
if (( is_mode_regexp == 1 )); then
__check_is_valid_regex "$unexpected" "$caller" || return $?
fi

# Matching.
Expand Down
7 changes: 2 additions & 5 deletions src/refute_output.bash
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,8 @@ __refute_stream() {
unexpected="${1-}"
fi

if (( is_mode_regexp == 1 )) && [[ '' =~ $unexpected ]] || (( $? == 2 )); then
echo "Invalid extended regular expression: \`$unexpected'" \
| batslib_decorate "ERROR: ${caller}" \
| fail
return $?
if (( is_mode_regexp == 1 )); then
__check_is_valid_regex "$unexpected" "$caller" || return 1
fi

# Matching.
Expand Down
6 changes: 2 additions & 4 deletions src/refute_regex.bash
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ refute_regex() {
local -r value="${1}"
local -r pattern="${2}"

if [[ '' =~ ${pattern} ]] || (( ${?} == 2 )); then
echo "Invalid extended regular expression: \`${pattern}'" \
| batslib_decorate 'ERROR: refute_regex' \
| fail
if ! __check_is_valid_regex "${pattern}" "${FUNCNAME[0]}"; then
return 1
elif [[ "${value}" =~ ${pattern} ]]; then
if shopt -p nocasematch &>/dev/null; then
local case_sensitive=insensitive
Expand Down
11 changes: 10 additions & 1 deletion test/assert_line.bats
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,21 @@ ERR_MSG
@test 'assert_line() --regexp <regexp>: returns 1 and displays an error message if <regexp> is not a valid extended regular expression' {
run assert_line --regexp '[.*'

assert_test_fail <<'ERR_MSG'
if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >=3) )); then
assert_test_fail <<'ERR_MSG'

-- ERROR: assert_line --
invalid regular expression `[.*': Missing ']'
--
ERR_MSG
else
assert_test_fail <<'ERR_MSG'

-- ERROR: assert_line --
Invalid extended regular expression: `[.*'
--
ERR_MSG
fi
}

@test "assert_line(): \`--' stops parsing options" {
Expand Down
11 changes: 10 additions & 1 deletion test/assert_output.bats
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,21 @@ ERR_MSG
@test 'assert_output() --regexp <regexp>: returns 1 and displays an error message if <regexp> is not a valid extended regular expression' {
run assert_output --regexp '[.*'

assert_test_fail <<'ERR_MSG'
if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >=3) )); then
assert_test_fail <<'ERR_MSG'

-- ERROR: assert_output --
invalid regular expression `[.*': Missing ']'
--
ERR_MSG
else
assert_test_fail <<'ERR_MSG'

-- ERROR: assert_output --
Invalid extended regular expression: `[.*'
--
ERR_MSG
fi
}


Expand Down
11 changes: 10 additions & 1 deletion test/assert_regex.bats
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,21 @@ ERR_MSG
@test "assert_regex() <value> <pattern>: returns 1 and displays an error message if <pattern> is not a valid extended regular expression" {
run assert_regex value '[.*'

assert_test_fail <<'ERR_MSG'
if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >=3) )); then
assert_test_fail <<'ERR_MSG'

-- ERROR: assert_regex --
invalid regular expression `[.*': Missing ']'
--
ERR_MSG
else
assert_test_fail <<'ERR_MSG'

-- ERROR: assert_regex --
Invalid extended regular expression: `[.*'
--
ERR_MSG
fi
}

@test "assert_regex allows regex matching empty string (see #53)" {
Expand Down
11 changes: 10 additions & 1 deletion test/assert_stderr.bats
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,21 @@ ERR_MSG
@test 'assert_stderr() --regexp <regexp>: returns 1 and displays an error message if <regexp> is not a valid extended regular expression' {
run assert_stderr --regexp '[.*'

assert_test_fail <<'ERR_MSG'
if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >=3) )); then
assert_test_fail <<'ERR_MSG'

-- ERROR: assert_stderr --
invalid regular expression `[.*': Missing ']'
--
ERR_MSG
else
assert_test_fail <<'ERR_MSG'

-- ERROR: assert_stderr --
Invalid extended regular expression: `[.*'
--
ERR_MSG
fi
}


Expand Down
11 changes: 10 additions & 1 deletion test/assert_stderr_line.bats
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,21 @@ ERR_MSG
@test 'assert_stderr_line() --regexp <regexp>: returns 1 and displays an error message if <regexp> is not a valid extended regular expression' {
run assert_stderr_line --regexp '[.*'

assert_test_fail <<'ERR_MSG'
if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >=3) )); then
assert_test_fail <<'ERR_MSG'

-- ERROR: assert_stderr_line --
invalid regular expression `[.*': Missing ']'
--
ERR_MSG
else
assert_test_fail <<'ERR_MSG'

-- ERROR: assert_stderr_line --
Invalid extended regular expression: `[.*'
--
ERR_MSG
fi
}

@test "assert_stderr_line(): \`--' stops parsing options" {
Expand Down
11 changes: 10 additions & 1 deletion test/refute_line.bats
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,21 @@ ERR_MSG
@test 'refute_line() --regexp <regexp>: returns 1 and displays an error message if <regexp> is not a valid extended regular expression' {
run refute_line --regexp '[.*'

assert_test_fail <<'ERR_MSG'
if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >=3) )); then
assert_test_fail <<'ERR_MSG'

-- ERROR: refute_line --
invalid regular expression `[.*': Missing ']'
--
ERR_MSG
else
assert_test_fail <<'ERR_MSG'

-- ERROR: refute_line --
Invalid extended regular expression: `[.*'
--
ERR_MSG
fi
}

@test "refute_line(): \`--' stops parsing options" {
Expand Down
10 changes: 9 additions & 1 deletion test/refute_output.bats
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,21 @@ ERR_MSG
# Error handling
@test 'refute_output() --regexp <regexp>: returns 1 and displays an error message if <regexp> is not a valid extended regular expression' {
run refute_output --regexp '[.*'
if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >=3) )); then
assert_test_fail <<'ERR_MSG'

assert_test_fail <<'ERR_MSG'
-- ERROR: refute_output --
invalid regular expression `[.*': Missing ']'
--
ERR_MSG
else
assert_test_fail <<'ERR_MSG'

-- ERROR: refute_output --
Invalid extended regular expression: `[.*'
--
ERR_MSG
fi
}


Expand Down
11 changes: 10 additions & 1 deletion test/refute_regex.bats
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,19 @@ ERR_MSG
@test "refute_regex() <value> <pattern>: returns 1 and displays an error message if <pattern> is not a valid extended regular expression" {
run refute_regex value '[.*'

assert_test_fail <<'ERR_MSG'
if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >=3) )); then
assert_test_fail <<'ERR_MSG'

-- ERROR: refute_regex --
invalid regular expression `[.*': Missing ']'
--
ERR_MSG
else
assert_test_fail <<'ERR_MSG'

-- ERROR: refute_regex --
Invalid extended regular expression: `[.*'
--
ERR_MSG
fi
}
11 changes: 10 additions & 1 deletion test/refute_stderr.bats
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,21 @@ ERR_MSG
@test 'refute_stderr() --regexp <regexp>: returns 1 and displays an error message if <regexp> is not a valid extended regular expression' {
run refute_stderr --regexp '[.*'

assert_test_fail <<'ERR_MSG'
if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >=3) )); then
assert_test_fail <<'ERR_MSG'

-- ERROR: refute_stderr --
invalid regular expression `[.*': Missing ']'
--
ERR_MSG
else
assert_test_fail <<'ERR_MSG'

-- ERROR: refute_stderr --
Invalid extended regular expression: `[.*'
--
ERR_MSG
fi
}


Expand Down
11 changes: 10 additions & 1 deletion test/refute_stderr_line.bats
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,21 @@ ERR_MSG
@test 'refute_stderr_line() --regexp <regexp>: returns 1 and displays an error message if <regexp> is not a valid extended regular expression' {
run refute_stderr_line --regexp '[.*'

assert_test_fail <<'ERR_MSG'
if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >=3) )); then
assert_test_fail <<"ERR_MSG"

-- ERROR: refute_stderr_line --
invalid regular expression `[.*': Missing ']'
--
ERR_MSG
else
assert_test_fail <<'ERR_MSG'

-- ERROR: refute_stderr_line --
Invalid extended regular expression: `[.*'
--
ERR_MSG
fi
}

@test "refute_stderr_line(): \`--' stops parsing options" {
Expand Down