Skip to content

Commit

Permalink
Added assert_file_contains, assert_file_size_equals, assert_file_empt…
Browse files Browse the repository at this point in the history
…y,… (#23)

* Add assert_file_contains, assert_file_size_equals, assert_file_empty, assert_file_not_empty

* Modify README.md
  • Loading branch information
SalimHouari authored and peshay committed Dec 14, 2018
1 parent 1a4308a commit c18122f
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 5 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,54 @@ path : /path/to/nopermission
permission: $permission
--
```
### `assert_file_empty`
Fail if the given file or directory is not empty.
```bash
@test 'assert_file_empty()' {
assert_file_empty /path/to/empty-file
}
```
On failure, the path and the content of the file is displayed.
```
-- file is not empty --
path : /path/to/empty-file
output (2 lines) : content-line-1
content-line-2
--
```
### `assert_file_contains`
Fail if the given file does not contain the regex.
```bash
@test 'assert_file_contains() {
assert_file_contains /path/to/non-empty-file regex
}
```
On failure, the path and expected regex are displayed.
```
```
### `assert_file_size_equals`
Fail if the given file size does not match the input.
```bash
@test 'assert_file_size_equals() {
assert_file_size_equals /path/to/non-empty-file bytecount
}
```
On failure, the path and expected bytecount are displayed.
```
### `assert_file_not_empty`
Fail if the given file or directory empty.
```bash
@test 'assert_file_not_empty() {
assert_file_not_empty /path/to/non-empty-file
}
```
On failure, the path is displayed.
```
-- file empty, but it was expected to contain something --
path : /path/to/non-empty-file
--
```

### `assert_not_file_permission`

Expand Down
108 changes: 103 additions & 5 deletions src/file.bash
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ assert_file_owner() {
local -r owner="$1"
local -r file="$2"
if [[ `uname` == "Darwin" ]]; then
sudo chown root ${TEST_FIXTURE_ROOT}/dir/owner
sudo chown root ${TEST_FIXTURE_ROOT}/dir/owner
sudo chown daemon ${TEST_FIXTURE_ROOT}/dir/notowner
if [ `stat -f '%Su' "$file"` != "$owner" ]; then
local -r rem="$BATSLIB_FILE_PATH_REM"
Expand All @@ -294,7 +294,7 @@ assert_file_owner() {
| fail
fi
elif [[ `uname` == "Linux" ]]; then
sudo chown root ${TEST_FIXTURE_ROOT}/dir/owner
sudo chown root ${TEST_FIXTURE_ROOT}/dir/owner
sudo chown daemon ${TEST_FIXTURE_ROOT}/dir/notowner
if [ `stat -c "%U" "$file"` != "$owner" ]; then
local -r rem="$BATSLIB_FILE_PATH_REM"
Expand Down Expand Up @@ -516,7 +516,82 @@ assert_symlink_to() {
fi
fi
}

# Fail and display path of the file (or directory) if it does not match a size.
#
# Globals:
# BATSLIB_FILE_PATH_REM
# BATSLIB_FILE_PATH_ADD
# Arguments:
# $1 - path
# $2 - expected size (bytes)
# Returns:
# 0 - file is correct size
# 1 - otherwise
# Outputs:
# STDERR - details, on failure
assert_file_size_equals() {
local -r file="$1"
local -r expectedsize="$2"
local -r size=$( wc -c "$file" | awk '{print $1}' )
if [ ! "$expectedsize" = "$size" ]; then
local -r rem="$BATSLIB_FILE_PATH_REM"
local -r add="$BATSLIB_FILE_PATH_ADD"
batslib_print_kv_single 4 'path' "${file/$rem/$add}" \
| batslib_decorate 'file size does not match expected size' \
| fail
fi
}
# Fail and display path of the file (or directory) if it does not contain a string.
# This function is the logical complement of `assert_file_not_contains'.
#
# Globals:
# BATSLIB_FILE_PATH_REM
# BATSLIB_FILE_PATH_ADD
# Arguments:
# $1 - path
# $2 - regex
# Returns:
# 0 - file contains regex
# 1 - otherwise
# Outputs:
# STDERR - details, on failure
assert_file_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}" \
| batslib_decorate 'file does not contain 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'.
#
# Globals:
# BATSLIB_FILE_PATH_REM
# BATSLIB_FILE_PATH_ADD
# Arguments:
# $1 - path
# Returns:
# 0 - file empty
# 1 - otherwise
# Outputs:
# STDERR - details, on failure
assert_file_empty() {
local -r file="$1"
if [[ -s "$file" ]]; then
local -r rem="$BATSLIB_FILE_PATH_REM"
local -r add="$BATSLIB_FILE_PATH_ADD"
{ local -ir width=8
batslib_print_kv_single "$width" 'path' "${file/$rem/$add}"
batslib_print_kv_single_or_multi "$width" \
'output' "$(cat $file)"
} | batslib_decorate 'file is not empty' \
| fail
fi
}
# Fail and display path of the file (or directory) if it exists. This
# function is the logical complement of `assert_exist'.
#
Expand Down Expand Up @@ -750,7 +825,7 @@ assert_not_file_owner() {
local -r owner="$1"
local -r file="$2"
if [[ `uname` == "Darwin" ]]; then
sudo chown root ${TEST_FIXTURE_ROOT}/dir/owner
sudo chown root ${TEST_FIXTURE_ROOT}/dir/owner
sudo chown daemon ${TEST_FIXTURE_ROOT}/dir/notowner
if [ `stat -f '%Su' "$file"` = "$owner" ]; then
local -r rem="$BATSLIB_FILE_PATH_REM"
Expand All @@ -760,7 +835,7 @@ assert_not_file_owner() {
| fail
fi
elif [[ `uname` == "Linux" ]]; then
sudo chown root ${TEST_FIXTURE_ROOT}/dir/owner
sudo chown root ${TEST_FIXTURE_ROOT}/dir/owner
sudo chown daemon ${TEST_FIXTURE_ROOT}/dir/notowner
if [ `stat -c "%U" "$file"` = "$owner" ]; then
local -r rem="$BATSLIB_FILE_PATH_REM"
Expand Down Expand Up @@ -1000,3 +1075,26 @@ assert_not_symlink_to() {
fi
fi
}
# Fail and display path of the file (or directory) if it is empty. This
# function is the logical complement of `assert_file_empty'.
#
# Globals:
# BATSLIB_FILE_PATH_REM
# BATSLIB_FILE_PATH_ADD
# Arguments:
# $1 - path
# Returns:
# 0 - file is not empty
# 1 - otherwise
# Outputs:
# STDERR - details, on failure
assert_file_not_empty() {
local -r file="$1"
if [[ ! -s "$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}" \
| batslib_decorate 'file empty, but it was expected to contain something' \
| fail
fi
}
39 changes: 39 additions & 0 deletions test/66-assert-10-assert_file_size_equals.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bats
load 'test_helper'
fixtures 'empty'
# Correctness
@test 'assert_file_size_equals() <file>: returns 0 if <file> <size> correct' {
local -r file="${TEST_FIXTURE_ROOT}/dir/empty-file"
run assert_file_size_equals "$file" "0"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}
@test 'assert_file_size_equals() <file>: returns 1 if <file> <size> is not correct' {
local -r file="${TEST_FIXTURE_ROOT}/dir/non-empty-file"
run assert_file_size_equals "$file" "5"
[ "$status" -eq 1 ]
}
@test 'assert_file_size_equals() <file>: returns 0 if <file> <size> is correct, non-zero case' {
local -r file="${TEST_FIXTURE_ROOT}/dir/non-empty-file"
run assert_file_size_equals "$file" "10"
[ "$status" -eq 0 ]
}
# Transforming path
@test 'assert_file_size_equals() <file>: replace prefix of displayed path' {
local -r BATSLIB_FILE_PATH_REM="#${TEST_FIXTURE_ROOT}"
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_file_size_equals "${TEST_FIXTURE_ROOT}/dir/non-empty-file" "5"
[ "$status" -eq 1 ]
}
@test 'assert_file_size_equals() <file>: replace suffix of displayed path' {
local -r BATSLIB_FILE_PATH_REM='%non-empty-file'
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_file_size_equals "${TEST_FIXTURE_ROOT}/dir/non-empty-file" "5"
[ "$status" -eq 1 ]
}
@test 'assert_file_size_equals() <file>: replace infix of displayed path' {
local -r BATSLIB_FILE_PATH_REM='dir'
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_file_size_equals "${TEST_FIXTURE_ROOT}/dir/non-empty-file" "5"
[ "$status" -eq 1 ]
}
33 changes: 33 additions & 0 deletions test/67-assert-10-assert_file_contains.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bats
load 'test_helper'
fixtures 'empty'
# Correctness
@test 'assert_file_contains() <file>: returns 0 and displays content if <file> matches string' {
local -r file="${TEST_FIXTURE_ROOT}/dir/non-empty-file"
run assert_file_contains "$file" "Not empty"
[ "$status" -eq 0 ]
}
@test 'assert_file_contains() <file>: returns 1 and displays content if <file> does not match string' {
local -r file="${TEST_FIXTURE_ROOT}/dir/non-empty-file"
run assert_file_contains "$file" "XXX"
[ "$status" -eq 1 ]
}
# Transforming path
@test 'assert_file_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_contains "${TEST_FIXTURE_ROOT}/dir/non-empty-file" "XXX"
[ "$status" -eq 1 ]
}
@test 'assert_file_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_contains "${TEST_FIXTURE_ROOT}/dir/non-empty-file" "XXX"
[ "$status" -eq 1 ]
}
@test 'assert_file_contains() <file>: replace infix of displayed path' {
local -r BATSLIB_FILE_PATH_REM='dir'
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_file_contains "${TEST_FIXTURE_ROOT}/dir/non-empty-file" "XXX"
[ "$status" -eq 1 ]
}
54 changes: 54 additions & 0 deletions test/68-assert-10-assert_file_empty.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bats
load 'test_helper'
fixtures 'empty'
# Correctness
@test 'assert_file_empty() <file>: returns 0 if <file> empty' {
local -r file="${TEST_FIXTURE_ROOT}/dir/empty-file"
run assert_file_empty "$file"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}
@test 'assert_file_empty() <file>: returns 1 and displays content if <file> is not empty' {
local -r file="${TEST_FIXTURE_ROOT}/dir/non-empty-file"
run assert_file_empty "$file"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- file is not empty --' ]
[ "${lines[1]}" == "path : ${TEST_FIXTURE_ROOT}/dir/non-empty-file" ]
[ "${lines[2]}" == 'output : Not empty' ]
[ "${lines[3]}" == '--' ]
}
# Transforming path
@test 'assert_file_empty() <file>: replace prefix of displayed path' {
local -r BATSLIB_FILE_PATH_REM="#${TEST_FIXTURE_ROOT}"
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_file_empty "${TEST_FIXTURE_ROOT}/dir/non-empty-file"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- file is not empty --' ]
[ "${lines[1]}" == "path : ../dir/non-empty-file" ]
[ "${lines[2]}" == 'output : Not empty' ]
[ "${lines[3]}" == '--' ]
}
@test 'assert_file_empty() <file>: replace suffix of displayed path' {
local -r BATSLIB_FILE_PATH_REM='%non-empty-file'
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_file_empty "${TEST_FIXTURE_ROOT}/dir/non-empty-file"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- file is not empty --' ]
[ "${lines[1]}" == "path : ${TEST_FIXTURE_ROOT}/dir/.." ]
[ "${lines[2]}" == 'output : Not empty' ]
[ "${lines[3]}" == '--' ]
}
@test 'assert_file_empty() <file>: replace infix of displayed path' {
local -r BATSLIB_FILE_PATH_REM='dir'
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_file_empty "${TEST_FIXTURE_ROOT}/dir/non-empty-file"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- file is not empty --' ]
[ "${lines[1]}" == "path : ${TEST_FIXTURE_ROOT}/../non-empty-file" ]
[ "${lines[2]}" == 'output : Not empty' ]
[ "${lines[3]}" == '--' ]
}
51 changes: 51 additions & 0 deletions test/68-assert-11-assert_file_not_empty.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bats
load 'test_helper'
fixtures 'empty'
# Correctness
@test 'assert_file_not_empty() <file>: returns 0 if <file> is not empty' {
local -r file="${TEST_FIXTURE_ROOT}/dir/non-empty-file"
run assert_file_not_empty "$file"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}
@test 'assert_file_not_empty() <file>: returns 1 and displays path if <file> emptys' {
local -r file="${TEST_FIXTURE_ROOT}/dir/empty-file"
run assert_file_not_empty "$file"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- file empty, but it was expected to contain something --' ]
[ "${lines[1]}" == "path : $file" ]
[ "${lines[2]}" == '--' ]
}
# Transforming path
@test 'assert_file_not_empty() <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_empty "${TEST_FIXTURE_ROOT}/dir/empty-file"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- file empty, but it was expected to contain something --' ]
[ "${lines[1]}" == "path : ../dir/empty-file" ]
[ "${lines[2]}" == '--' ]
}
@test 'assert_file_not_empty() <file>: replace suffix of displayed path' {
local -r BATSLIB_FILE_PATH_REM='%empty-file'
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_file_not_empty "${TEST_FIXTURE_ROOT}/dir/empty-file"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- file empty, but it was expected to contain something --' ]
echo [ "${lines[1]}" == "path : ${TEST_FIXTURE_ROOT}/dir/.." ]
[ "${lines[1]}" == "path : ${TEST_FIXTURE_ROOT}/dir/.." ]
[ "${lines[2]}" == '--' ]
}
@test 'assert_file_not_empty() <file>: replace infix of displayed path' {
local -r BATSLIB_FILE_PATH_REM='dir'
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_file_not_empty "${TEST_FIXTURE_ROOT}/dir/empty-file"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- file empty, but it was expected to contain something --' ]
[ "${lines[1]}" == "path : ${TEST_FIXTURE_ROOT}/../empty-file" ]
[ "${lines[2]}" == '--' ]
}
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/empty/dir/non-empty-file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Not empty

0 comments on commit c18122f

Please sign in to comment.