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

Enable extended regex in assert_file_contains #63

Merged
merged 3 commits into from
Aug 25, 2023
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
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 @@ -543,6 +543,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 @@ -551,7 +552,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}" \
Expand Down
Loading