Skip to content

Commit

Permalink
test: quick fix add assert_file_not_contains bats-file fct [NMO-507]
Browse files Browse the repository at this point in the history
  • Loading branch information
RedLeader962 committed Jan 22, 2024
1 parent b35e9dc commit 52e0575
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/bats_testing_tools/bats_helper_functions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,46 @@ function fake_IS_TEAMCITY_RUN() {
TEAMCITY_VERSION=fake
fi
}

# ====bats-file utility============================================================================

# Temporary workaround for missing implementation of "assert_file_not_contains"
# ToDo: remove when bats-file PR #61 is merge
# - PR #61 at https://github.com/bats-core/bats-file/pull/61
#
# Credit https://github.com/bats-core/bats-file/pull/61
#
# 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
}

0 comments on commit 52e0575

Please sign in to comment.