Skip to content

Commit

Permalink
Merge pull request #61 from artis3n/master
Browse files Browse the repository at this point in the history
feat: adds the missing assert_file_not_contains function
  • Loading branch information
martin-schulze-vireso authored Jul 13, 2023
2 parents c0f822a + c852e0b commit cb914cd
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
build:
strategy:
matrix:
os: ['macos-10.15', 'ubuntu-latest']
os: ['macos-12', 'ubuntu-latest']
runs-on: ${{ matrix.os }}

steps:
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ load this library.
| Test File Types | Test File Attributes | Test File Content |
| ----------- | ----------- | ----------- |
| _Check if a **file or directory** exists!_ <br/> - [assert_exists](#assert_exists) <br/> - [assert_not_exists](#assert_not_exists) | _Check if file is **executable**!_ <br/> - [assert_file_executable](#assert_file_executable) <br/> - [assert_file_not_executable](#assert_file_not_executable) | _Check if file is **empty**!_ <br/> - [assert_file_empty](#assert_file_empty) <br/> - [assert_file_not_empty](#assert_file_not_empty) |
| _Check if a **file** exists!_ <br/> - [assert_file_exists](#assert_file_exists) <br/> - [assert_file_not_exists](#assert_file_not_exists) | _Check the **owner** of a file!_ <br/> - [assert_file_owner](#assert_file_owner) <br/> - [assert_file_not_owner](#assert_file_not_owner) | _Check if file **contains regex**!_ <br/> - [assert_file_contains](#assert_file_contains) <br/> - ~~assert_file_not_contains~~ |
| _Check if a **file** exists!_ <br/> - [assert_file_exists](#assert_file_exists) <br/> - [assert_file_not_exists](#assert_file_not_exists) | _Check the **owner** of a file!_ <br/> - [assert_file_owner](#assert_file_owner) <br/> - [assert_file_not_owner](#assert_file_not_owner) | _Check if file **contains regex**!_ <br/> - [assert_file_contains](#assert_file_contains) <br/> - [assert_file_not_contains](#assert_file_not_contains) |
| _Check if a **directory** exists!_ <br/> - [assert_dir_exists](#assert_dir_exists) <br/> - [assert_dir_not_exists](#assert_dir_not_exists) | _Check the **permission** of a file!_ <br/> - [assert_file_permission](#assert_file_permission) <br/> - [assert_not_file_permission](#assert_not_file_permission) | _Check if file is a **symlink to target**!_ <br/> - [assert_symlink_to](#assert_symlink_to) <br/> - [assert_not_symlink_to](#assert_not_symlink_to) |
| _Check if a **link** exists!_ <br/> - [assert_link_exists](#assert_link_exists) <br/> - [assert_link_not_exists](#assert_link_not_exists) | _Check the **size** of a file **by bytes**!_ <br/> - [assert_file_size_equals](#assert_file_size_equals) |
| _Check if a **block special file** exists!_ <br/> - [assert_block_exists](#assert_block_exists) <br/> - [assert_block_not_exists](#assert_block_not_exists) | _Check if a file have **zero bytes**!_ <br/> - [assert_size_zero](#assert_size_zero) <br/> - [assert_size_not_zero](#assert_size_not_zero) |
Expand Down Expand Up @@ -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 or if the file does not exist.
```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
Expand Down
34 changes: 34 additions & 0 deletions src/file.bash
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,40 @@ 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 [[ ! -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.
# This function is the logical complement of `assert_file_not_empty'.
#
Expand Down
53 changes: 53 additions & 0 deletions test/67-assert-10-assert_file_not_contains.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bats
load 'test_helper'
fixtures 'empty'
# Correctness
@test 'assert_file_not_contains() <file>: returns 0 and displays content if <file> 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() <file>: returns 1 and displays content if <file> 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() <file>: 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() <file>: 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() <file>: 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() <file>: 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 contains regex --' ]
[ "${lines[1]}" == "path : $file" ]
[ "${lines[2]}" == "regex : Not empty" ]
[ "${lines[3]}" == '--' ]
}
@test 'assert_file_not_contains() <file>: returns 1 and displays path if <file> does not exist' {
local -r file="${TEST_FIXTURE_ROOT}/missing"
run assert_file_not_contains "$file" "XXX"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- file does not exist --' ]
[ "${lines[1]}" == "path : $file" ]
[ "${lines[2]}" == "regex : XXX" ]
[ "${lines[3]}" == '--' ]
}

0 comments on commit cb914cd

Please sign in to comment.