Skip to content

Commit cb914cd

Browse files
Merge pull request #61 from artis3n/master
feat: adds the missing assert_file_not_contains function
2 parents c0f822a + c852e0b commit cb914cd

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
build:
1919
strategy:
2020
matrix:
21-
os: ['macos-10.15', 'ubuntu-latest']
21+
os: ['macos-12', 'ubuntu-latest']
2222
runs-on: ${{ matrix.os }}
2323

2424
steps:

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ load this library.
2929
| Test File Types | Test File Attributes | Test File Content |
3030
| ----------- | ----------- | ----------- |
3131
| _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) |
32-
| _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~~ |
32+
| _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) |
3333
| _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) |
3434
| _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) |
3535
| _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) |
@@ -748,6 +748,19 @@ On failure, the path and expected regex are displayed.
748748
749749
---
750750
751+
### `assert_file_not_contains`
752+
Fail if the given file contains the regex or if the file does not exist.
753+
```bash
754+
@test 'assert_file_not_contains() {
755+
assert_file_not_contains /path/to/non-empty-file regex
756+
}
757+
```
758+
On failure, the path and regex are displayed.
759+
760+
[Back to index](#Index-of-all-functions)
761+
762+
---
763+
751764
### `assert_symlink_to`
752765
Fail if the given file is not a symbolic to a defined target.
753766
```bash

src/file.bash

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,40 @@ assert_file_contains() {
548548
| fail
549549
fi
550550
}
551+
# Fail and display path of the file (or directory) if it does contain a string.
552+
# This function is the logical complement of `assert_file_contains'.
553+
#
554+
# Globals:
555+
# BATSLIB_FILE_PATH_REM
556+
# BATSLIB_FILE_PATH_ADD
557+
# Arguments:
558+
# $1 - path
559+
# $2 - regex
560+
# Returns:
561+
# 0 - file does not contain regex
562+
# 1 - otherwise
563+
# Outputs:
564+
# STDERR - details, on failure
565+
assert_file_not_contains() {
566+
local -r file="$1"
567+
local -r regex="$2"
568+
569+
if [[ ! -f "$file" ]]; then
570+
local -r rem="${BATSLIB_FILE_PATH_REM-}"
571+
local -r add="${BATSLIB_FILE_PATH_ADD-}"
572+
batslib_print_kv_single 4 'path' "${file/$rem/$add}" 'regex' "$regex" \
573+
| batslib_decorate 'file does not exist' \
574+
| fail
575+
576+
elif grep -q "$regex" "$file"; then
577+
local -r rem="${BATSLIB_FILE_PATH_REM-}"
578+
local -r add="${BATSLIB_FILE_PATH_ADD-}"
579+
batslib_print_kv_single 4 'path' "${file/$rem/$add}" 'regex' "$regex" \
580+
| batslib_decorate 'file contains regex' \
581+
| fail
582+
583+
fi
584+
}
551585
# Fail and display path of the file (or directory) if it is not empty.
552586
# This function is the logical complement of `assert_file_not_empty'.
553587
#
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bats
2+
load 'test_helper'
3+
fixtures 'empty'
4+
# Correctness
5+
@test 'assert_file_not_contains() <file>: returns 0 and displays content if <file> does not match string' {
6+
local -r file="${TEST_FIXTURE_ROOT}/dir/non-empty-file"
7+
run assert_file_not_contains "$file" "XXX"
8+
[ "$status" -eq 0 ]
9+
}
10+
@test 'assert_file_not_contains() <file>: returns 1 and displays content if <file> does match string' {
11+
local -r file="${TEST_FIXTURE_ROOT}/dir/non-empty-file"
12+
run assert_file_not_contains "$file" "Not empty"
13+
[ "$status" -eq 1 ]
14+
}
15+
# Transforming path
16+
@test 'assert_file_not_contains() <file>: replace prefix of displayed path' {
17+
local -r BATSLIB_FILE_PATH_REM="#${TEST_FIXTURE_ROOT}"
18+
local -r BATSLIB_FILE_PATH_ADD='..'
19+
run assert_file_not_contains "${TEST_FIXTURE_ROOT}/dir/non-empty-file" "Not empty"
20+
[ "$status" -eq 1 ]
21+
}
22+
@test 'assert_file_not_contains() <file>: replace suffix of displayed path' {
23+
local -r BATSLIB_FILE_PATH_REM='%non-empty-file'
24+
local -r BATSLIB_FILE_PATH_ADD='..'
25+
run assert_file_not_contains "${TEST_FIXTURE_ROOT}/dir/non-empty-file" "Not empty"
26+
[ "$status" -eq 1 ]
27+
}
28+
@test 'assert_file_not_contains() <file>: replace infix of displayed path' {
29+
local -r BATSLIB_FILE_PATH_REM='dir'
30+
local -r BATSLIB_FILE_PATH_ADD='..'
31+
run assert_file_not_contains "${TEST_FIXTURE_ROOT}/dir/non-empty-file" "Not empty"
32+
[ "$status" -eq 1 ]
33+
}
34+
@test 'assert_file_not_contains() <file>: show missing regex in case of failure' {
35+
local -r file="${TEST_FIXTURE_ROOT}/dir/non-empty-file"
36+
run assert_file_not_contains "$file" "Not empty"
37+
[ "$status" -eq 1 ]
38+
[ "${#lines[@]}" -eq 4 ]
39+
[ "${lines[0]}" == '-- file contains regex --' ]
40+
[ "${lines[1]}" == "path : $file" ]
41+
[ "${lines[2]}" == "regex : Not empty" ]
42+
[ "${lines[3]}" == '--' ]
43+
}
44+
@test 'assert_file_not_contains() <file>: returns 1 and displays path if <file> does not exist' {
45+
local -r file="${TEST_FIXTURE_ROOT}/missing"
46+
run assert_file_not_contains "$file" "XXX"
47+
[ "$status" -eq 1 ]
48+
[ "${#lines[@]}" -eq 4 ]
49+
[ "${lines[0]}" == '-- file does not exist --' ]
50+
[ "${lines[1]}" == "path : $file" ]
51+
[ "${lines[2]}" == "regex : XXX" ]
52+
[ "${lines[3]}" == '--' ]
53+
}

0 commit comments

Comments
 (0)