Skip to content

Commit

Permalink
Merge pull request #63 from bats-core/pr/dsbibby/46
Browse files Browse the repository at this point in the history
Enable extended regex in assert_file_contains
  • Loading branch information
martin-schulze-vireso authored Aug 25, 2023
2 parents 13ad5e2 + 75c8bbd commit 048aa4c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,11 @@ path : /path/to/non-empty-file
Fail if the given file does not contain the regex.
```bash
@test 'assert_file_contains() {
assert_file_contains /path/to/non-empty-file regex
assert_file_contains /path/to/non-empty-file regex engine
}
```
`engine` is optional and can be one of `grep`, `egrep` or `pcregrep`. The specified engine must be available on the system running the tests.
On failure, the path and expected regex are displayed.
[Back to index](#Index-of-all-functions)
Expand Down
17 changes: 16 additions & 1 deletion src/file.bash
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ assert_file_size_equals() {
# Arguments:
# $1 - path
# $2 - regex
# $3 - grep engine to use (grep, egrep, pcregrep) - optional
# Returns:
# 0 - file contains regex
# 1 - otherwise
Expand All @@ -540,7 +541,21 @@ assert_file_size_equals() {
assert_file_contains() {
local -r file="$1"
local -r regex="$2"
if ! grep -q "$regex" "$file"; then
local -r cmd="${3:-grep}"

case "$cmd" in
grep|egrep|pcregrep)
if ! type "${cmd}" &>/dev/null; then
batslib_decorate "Regex engine \"${cmd}\" not available on this system" \
| fail
fi
;;
*)
batslib_decorate "Regex engine \"${cmd}\" not in allow list" \
| fail
;;
esac
if ! "$cmd" -q "$regex" "$file"; then
local -r rem="${BATSLIB_FILE_PATH_REM-}"
local -r add="${BATSLIB_FILE_PATH_ADD-}"
batslib_print_kv_single 4 'path' "${file/$rem/$add}" 'regex' "$regex" \
Expand Down

0 comments on commit 048aa4c

Please sign in to comment.