From e6c273fdfd631be8a53f62f409a7fef19ee359a1 Mon Sep 17 00:00:00 2001 From: Ari Kalfus Date: Mon, 3 Jul 2023 23:41:51 +0000 Subject: [PATCH 1/7] Adds the missing 'assert_file_not_contains' function Closes https://github.com/bats-core/bats-file/issues/60 --- src/file.bash | 25 +++++++++++ ...67-assert-10-assert_file_not_contains.bats | 43 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 test/67-assert-10-assert_file_not_contains.bats diff --git a/src/file.bash b/src/file.bash index 355c580..28192b5 100644 --- a/src/file.bash +++ b/src/file.bash @@ -548,6 +548,31 @@ assert_file_contains() { | fail fi } +# Fail and display path of the file (or directory) if it does contain a string. +# This function is the logical complement of `assert_file_contains'. +# +# Globals: +# BATSLIB_FILE_PATH_REM +# BATSLIB_FILE_PATH_ADD +# Arguments: +# $1 - path +# $2 - regex +# Returns: +# 0 - file does not contain regex +# 1 - otherwise +# Outputs: +# STDERR - details, on failure +assert_file_not_contains() { + local -r file="$1" + local -r regex="$2" + if grep -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" \ + | batslib_decorate 'file contains regex' \ + | fail + fi +} # Fail and display path of the file (or directory) if it is not empty. # This function is the logical complement of `assert_file_not_empty'. # diff --git a/test/67-assert-10-assert_file_not_contains.bats b/test/67-assert-10-assert_file_not_contains.bats new file mode 100644 index 0000000..f495a7f --- /dev/null +++ b/test/67-assert-10-assert_file_not_contains.bats @@ -0,0 +1,43 @@ +#!/usr/bin/env bats +load 'test_helper' +fixtures 'empty' +# Correctness +@test 'assert_file_not_contains() : returns 0 and displays content if does not match string' { + local -r file="${TEST_FIXTURE_ROOT}/dir/non-empty-file" + run assert_file_not_contains "$file" "XXX" + [ "$status" -eq 0 ] +} +@test 'assert_file_not_contains() : returns 1 and displays content if does match string' { + local -r file="${TEST_FIXTURE_ROOT}/dir/non-empty-file" + run assert_file_not_contains "$file" "Not empty" + [ "$status" -eq 1 ] +} +# Transforming path +@test 'assert_file_not_contains() : replace prefix of displayed path' { + local -r BATSLIB_FILE_PATH_REM="#${TEST_FIXTURE_ROOT}" + local -r BATSLIB_FILE_PATH_ADD='..' + run assert_file_not_contains "${TEST_FIXTURE_ROOT}/dir/non-empty-file" "Not empty" + [ "$status" -eq 1 ] +} +@test 'assert_file_not_contains() : replace suffix of displayed path' { + local -r BATSLIB_FILE_PATH_REM='%non-empty-file' + local -r BATSLIB_FILE_PATH_ADD='..' + run assert_file_not_contains "${TEST_FIXTURE_ROOT}/dir/non-empty-file" "Not empty" + [ "$status" -eq 1 ] +} +@test 'assert_file_not_contains() : replace infix of displayed path' { + local -r BATSLIB_FILE_PATH_REM='dir' + local -r BATSLIB_FILE_PATH_ADD='..' + run assert_file_not_contains "${TEST_FIXTURE_ROOT}/dir/non-empty-file" "Not empty" + [ "$status" -eq 1 ] +} +@test 'assert_file_not_contains() : show missing regex in case of failure' { + local -r file="${TEST_FIXTURE_ROOT}/dir/non-empty-file" + run assert_file_not_contains "$file" "Not empty" + [ "$status" -eq 1 ] + [ "${#lines[@]}" -eq 4 ] + [ "${lines[0]}" == '-- file does not contain regex --' ] + [ "${lines[1]}" == "path : $file" ] + [ "${lines[2]}" == "regex : Not empty" ] + [ "${lines[3]}" == '--' ] +} From 33be16cf73249b569dc9505b30563926bf6440c8 Mon Sep 17 00:00:00 2001 From: Ari Kalfus Date: Mon, 3 Jul 2023 23:46:07 +0000 Subject: [PATCH 2/7] update README with assert_file_not_contains --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 48fe5b5..5aa973b 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ load this library. | Test File Types | Test File Attributes | Test File Content | | ----------- | ----------- | ----------- | | _Check if a **file or directory** exists!_
- [assert_exists](#assert_exists)
- [assert_not_exists](#assert_not_exists) | _Check if file is **executable**!_
- [assert_file_executable](#assert_file_executable)
- [assert_file_not_executable](#assert_file_not_executable) | _Check if file is **empty**!_
- [assert_file_empty](#assert_file_empty)
- [assert_file_not_empty](#assert_file_not_empty) | -| _Check if a **file** exists!_
- [assert_file_exists](#assert_file_exists)
- [assert_file_not_exists](#assert_file_not_exists) | _Check the **owner** of a file!_
- [assert_file_owner](#assert_file_owner)
- [assert_file_not_owner](#assert_file_not_owner) | _Check if file **contains regex**!_
- [assert_file_contains](#assert_file_contains)
- ~~assert_file_not_contains~~ | +| _Check if a **file** exists!_
- [assert_file_exists](#assert_file_exists)
- [assert_file_not_exists](#assert_file_not_exists) | _Check the **owner** of a file!_
- [assert_file_owner](#assert_file_owner)
- [assert_file_not_owner](#assert_file_not_owner) | _Check if file **contains regex**!_
- [assert_file_contains](#assert_file_contains)
- [assert_file_not_contains](#assert_file_not_contains) | | _Check if a **directory** exists!_
- [assert_dir_exists](#assert_dir_exists)
- [assert_dir_not_exists](#assert_dir_not_exists) | _Check the **permission** of a file!_
- [assert_file_permission](#assert_file_permission)
- [assert_not_file_permission](#assert_not_file_permission) | _Check if file is a **symlink to target**!_
- [assert_symlink_to](#assert_symlink_to)
- [assert_not_symlink_to](#assert_not_symlink_to) | | _Check if a **link** exists!_
- [assert_link_exists](#assert_link_exists)
- [assert_link_not_exists](#assert_link_not_exists) | _Check the **size** of a file **by bytes**!_
- [assert_file_size_equals](#assert_file_size_equals) | | _Check if a **block special file** exists!_
- [assert_block_exists](#assert_block_exists)
- [assert_block_not_exists](#assert_block_not_exists) | _Check if a file have **zero bytes**!_
- [assert_size_zero](#assert_size_zero)
- [assert_size_not_zero](#assert_size_not_zero) | @@ -748,6 +748,19 @@ On failure, the path and expected regex are displayed. --- +### `assert_file_not_contains` +Fail if the given file contains the regex. +```bash +@test 'assert_file_not_contains() { + assert_file_not_contains /path/to/non-empty-file regex +} +``` +On failure, the path and regex are displayed. + +[Back to index](#Index-of-all-functions) + +--- + ### `assert_symlink_to` Fail if the given file is not a symbolic to a defined target. ```bash From ddaebbea6a34428707f62e9fa6605364316a84db Mon Sep 17 00:00:00 2001 From: Ari Kalfus Date: Mon, 3 Jul 2023 23:48:37 +0000 Subject: [PATCH 3/7] update final test with proper string --- test/67-assert-10-assert_file_not_contains.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/67-assert-10-assert_file_not_contains.bats b/test/67-assert-10-assert_file_not_contains.bats index f495a7f..b12a641 100644 --- a/test/67-assert-10-assert_file_not_contains.bats +++ b/test/67-assert-10-assert_file_not_contains.bats @@ -36,7 +36,7 @@ fixtures 'empty' run assert_file_not_contains "$file" "Not empty" [ "$status" -eq 1 ] [ "${#lines[@]}" -eq 4 ] - [ "${lines[0]}" == '-- file does not contain regex --' ] + [ "${lines[0]}" == '-- file contains regex --' ] [ "${lines[1]}" == "path : $file" ] [ "${lines[2]}" == "regex : Not empty" ] [ "${lines[3]}" == '--' ] From d3b7eb9d80e21edc0aa29e90d5d1614a0066df82 Mon Sep 17 00:00:00 2001 From: Ari Kalfus Date: Thu, 6 Jul 2023 18:00:30 -0400 Subject: [PATCH 4/7] adds missing file check to assert_file_not_contains --- src/file.bash | 11 ++++++++++- test/67-assert-10-assert_file_not_contains.bats | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/file.bash b/src/file.bash index 28192b5..16f6d80 100644 --- a/src/file.bash +++ b/src/file.bash @@ -565,12 +565,21 @@ assert_file_contains() { assert_file_not_contains() { local -r file="$1" local -r regex="$2" - if grep -q "$regex" "$file"; then + + if [[ ! -f "$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" \ + | batslib_decorate 'file does not exist' \ + | fail + + elif grep -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" \ | batslib_decorate 'file contains regex' \ | fail + fi } # Fail and display path of the file (or directory) if it is not empty. diff --git a/test/67-assert-10-assert_file_not_contains.bats b/test/67-assert-10-assert_file_not_contains.bats index b12a641..e59c43d 100644 --- a/test/67-assert-10-assert_file_not_contains.bats +++ b/test/67-assert-10-assert_file_not_contains.bats @@ -41,3 +41,15 @@ fixtures 'empty' [ "${lines[2]}" == "regex : Not empty" ] [ "${lines[3]}" == '--' ] } +@test 'assert_file_not_contains() : returns 1 and displays path if does not exist' { + local -r file="${TEST_FIXTURE_ROOT}/missing" + run assert_file_not_contains "$file" "XXX" + echo "status: $status" + echo "output: $output" + [ "$status" -eq 1 ] + [ "${#lines[@]}" -eq 4 ] + [ "${lines[0]}" == '-- file does not exist --' ] + [ "${lines[1]}" == "path : $file" ] + [ "${lines[2]}" == "regex : XXX" ] + [ "${lines[3]}" == '--' ] +} From 5b7199ad1068b524ea37d9a3ab1f3113d23ef7ee Mon Sep 17 00:00:00 2001 From: Ari Kalfus Date: Thu, 6 Jul 2023 18:01:16 -0400 Subject: [PATCH 5/7] adds readme description --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5aa973b..fc0460f 100644 --- a/README.md +++ b/README.md @@ -749,7 +749,7 @@ On failure, the path and expected regex are displayed. --- ### `assert_file_not_contains` -Fail if the given file contains the regex. +Fail if the given file contains the regex or if the file does not exist. ```bash @test 'assert_file_not_contains() { assert_file_not_contains /path/to/non-empty-file regex From 125ec8bdbb61afad40d8a892ecf42b75d37dba93 Mon Sep 17 00:00:00 2001 From: Ari Kalfus Date: Thu, 6 Jul 2023 18:02:27 -0400 Subject: [PATCH 6/7] removes debugging echos --- test/67-assert-10-assert_file_not_contains.bats | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/67-assert-10-assert_file_not_contains.bats b/test/67-assert-10-assert_file_not_contains.bats index e59c43d..e92c272 100644 --- a/test/67-assert-10-assert_file_not_contains.bats +++ b/test/67-assert-10-assert_file_not_contains.bats @@ -44,8 +44,6 @@ fixtures 'empty' @test 'assert_file_not_contains() : returns 1 and displays path if does not exist' { local -r file="${TEST_FIXTURE_ROOT}/missing" run assert_file_not_contains "$file" "XXX" - echo "status: $status" - echo "output: $output" [ "$status" -eq 1 ] [ "${#lines[@]}" -eq 4 ] [ "${lines[0]}" == '-- file does not exist --' ] From c852e0bdad2be0b6720e2ce7cb80f7c221a28ecc Mon Sep 17 00:00:00 2001 From: Ari Kalfus Date: Wed, 12 Jul 2023 14:25:38 +0000 Subject: [PATCH 7/7] ci: replace eol mac runner with mac-12 --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index bbf450d..efa6be3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,7 +18,7 @@ jobs: build: strategy: matrix: - os: ['macos-10.15', 'ubuntu-latest'] + os: ['macos-12', 'ubuntu-latest'] runs-on: ${{ matrix.os }} steps: