From ee2871fbd7e526c8cad4a82174a76f097e24e69b Mon Sep 17 00:00:00 2001 From: dsbibby Date: Thu, 28 Jul 2022 00:16:35 +0100 Subject: [PATCH 1/3] Enable extended regex in assert_file_contains Signed-off-by: David Bibby --- src/file.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/file.bash b/src/file.bash index a79ff09..5fe0ef5 100644 --- a/src/file.bash +++ b/src/file.bash @@ -551,7 +551,7 @@ assert_file_size_equals() { assert_file_contains() { local -r file="$1" local -r regex="$2" - if ! grep -q "$regex" "$file"; then + if ! grep -E -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}" \ From 9b91bfda23e04cd16b5c9225ef724311dc65f7bb Mon Sep 17 00:00:00 2001 From: David Bibby Date: Fri, 29 Jul 2022 09:13:26 +0000 Subject: [PATCH 2/3] Changed to allow specifying engine Optional argument added to maintain backward compatibility Signed-off-by: David Bibby --- README.md | 4 +++- src/file.bash | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 48fe5b5..86c56b0 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/file.bash b/src/file.bash index 5fe0ef5..8eee540 100644 --- a/src/file.bash +++ b/src/file.bash @@ -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 @@ -551,7 +552,13 @@ assert_file_size_equals() { assert_file_contains() { local -r file="$1" local -r regex="$2" - if ! grep -E -q "$regex" "$file"; then + local -r cmd="${3:-grep}" + local -r engines=(grep egrep pcregrep) + if [ -z "$(echo ${engines[@]} | grep -w ${cmd})" ] || ! which ${cmd} > /dev/null 2>&1; then + batslib_decorate "Regex engine \"${cmd}\" not available on this system" \ + | fail + fi + 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}" \ From 75c8bbd4caf5e74c4a5310f7da5a02b7c676152b Mon Sep 17 00:00:00 2001 From: Martin Schulze Date: Thu, 12 Jan 2023 23:10:25 +0100 Subject: [PATCH 3/3] Reduce subshells, add quoting --- src/file.bash | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/file.bash b/src/file.bash index 8eee540..ca3afc3 100644 --- a/src/file.bash +++ b/src/file.bash @@ -553,12 +553,20 @@ assert_file_contains() { local -r file="$1" local -r regex="$2" local -r cmd="${3:-grep}" - local -r engines=(grep egrep pcregrep) - if [ -z "$(echo ${engines[@]} | grep -w ${cmd})" ] || ! which ${cmd} > /dev/null 2>&1; then - batslib_decorate "Regex engine \"${cmd}\" not available on this system" \ - | fail - fi - if ! $cmd -q "$regex" "$file"; then + + 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}" \