diff --git a/.lagoon.yml b/.lagoon.yml index cc3dd3a949..062b5da243 100644 --- a/.lagoon.yml +++ b/.lagoon.yml @@ -61,7 +61,6 @@ environments: schedule: '*/15 * * * *' command: drush cron --root=/app service: cli - routes: - nginx-php: - default.civictheme.io diff --git a/scripts/test-tooling.sh b/scripts/test-tooling.sh index 0c4e4fee73..3bac8b0a42 100755 --- a/scripts/test-tooling.sh +++ b/scripts/test-tooling.sh @@ -6,14 +6,7 @@ set -e [ -n "${DREVOPS_DEBUG}" ] && set -x -echo "==> Test BATS helpers." -bats tests/bats/helpers.bats --tap - -echo "==> Test BATS mock." -bats tests/bats/mock.bats --tap - -echo "==> Test Assets mock." -bats tests/bats/assets.bats --tap +[ ! -d "tests/bats/node_modules" ] && npm --prefix tests/bats ci if [ "${CIVICTHEME_LIBRARY_INSTALL_SKIP}" != "1" ]; then echo " > Test Library assets." diff --git a/tests/bats/.gitignore b/tests/bats/.gitignore new file mode 100644 index 0000000000..3c3629e647 --- /dev/null +++ b/tests/bats/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/tests/bats/_helper.bash b/tests/bats/_helper.bash index 7af75ab239..a70133df13 100644 --- a/tests/bats/_helper.bash +++ b/tests/bats/_helper.bash @@ -1,505 +1,37 @@ #!/usr/bin/env bash -## -# @file -# Bats test helpers. # -# shellcheck disable=SC2119,SC2120,SC2044 - -# Guard against bats executing this twice -if [ -z "$TEST_PATH_INITIALIZED" ]; then - export TEST_PATH_INITIALIZED=true - - # Add BATS test directory to the PATH. - PATH="$(dirname "${BATS_TEST_DIRNAME}"):$PATH" - - # BATS_TMPDIR - the location to a directory that may be used to store - # temporary files. Provided by bats. Created once for the duration of whole - # suite run. - # Do not use BATS_TMPDIR, instead use BATS_TEST_TMPDIR. - # - # BATS_TEST_TMPDIR - unique location for temp files per test. - # shellcheck disable=SC2002 - random_suffix=$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 4 | head -n 1) - BATS_TEST_TMPDIR="${BATS_TMPDIR}/bats-test-tmp-${random_suffix}" - [ -d "${BATS_TEST_TMPDIR}" ] && rm -Rf "${BATS_TEST_TMPDIR}" >/dev/null - mkdir -p "${BATS_TEST_TMPDIR}" - - export BATS_TEST_TMPDIR - - echo "BATS_TEST_TMPDIR dir: ${BATS_TEST_TMPDIR}" >&3 -fi - -flunk() { - { - if [ "$#" -eq 0 ]; then - cat - - else - echo "$@" - fi - } | sed "s:${BATS_TEST_TMPDIR}:\${BATS_TEST_TMPDIR}:g" >&2 - return 1 -} - -assert_success() { - # shellcheck disable=SC2154 - if [ "${status}" -ne 0 ]; then - format_error "command failed with exit status ${status}" | flunk - elif [ "$#" -gt 0 ]; then - assert_output "${1}" - fi -} - -assert_failure() { - # shellcheck disable=SC2154 - if [ "${status}" -eq 0 ]; then - format_error "expected failed exit status" | flunk - elif [ "$#" -gt 0 ]; then - assert_output "${1}" - fi -} - -assert_equal() { - if [ "$1" != "$2" ]; then - { - echo "expected: ${1}" - echo "actual: ${2}" - } | flunk - fi -} - -assert_contains() { - local needle="${1}" - local haystack="${2}" - - if echo "$haystack" | $(type -p ggrep grep | head -1) -i -F -- "$needle" >/dev/null; then - return 0 - else - format_error "String '${haystack}' does not contain '${needle}'" | flunk - fi -} - -assert_not_contains() { - local needle="${1}" - local haystack="${2}" - - if echo "$haystack" | $(type -p ggrep grep | head -1) -i -F -- "$needle" >/dev/null; then - format_error "String '${haystack}' contains '${needle}', but should not" | flunk - else - return 0 - fi -} - -assert_file_exists() { - local file="${1}" - - for f in ${file}; do - if [ -e "$f" ]; then - return 0 - else - format_error "File ${file} does not exist" | flunk - fi - ## This is all we needed to know, so we can break after the first iteration. - break - done - - format_error "File ${file} does not exist" | flunk -} - -assert_file_not_exists() { - local file="${1}" - - for f in ${file}; do - if [ -e "$f" ]; then - format_error "File ${file} exists, but should not" | flunk - else - return 0 - fi - done -} - -assert_dir_exists() { - local dir="${1}" - - if [ -d "${dir}" ]; then - return 0 - else - format_error "Directory ${dir} does not exist" | flunk - fi -} - -assert_dir_not_exists() { - local dir="${1:-$(pwd)}" - - if [ -d "${dir}" ]; then - format_error "Directory ${dir} exists, but should not" | flunk - else - return 0 - fi -} - -assert_dir_empty() { - local dir="${1:-$(pwd)}" - assert_dir_exists "${dir}" || return 1 - - if [ "$(ls -A "${dir}")" ]; then - format_error "Directory ${dir} is not empty, but should be" | flunk - else - return 0 - fi -} - -assert_dir_not_empty() { - local dir="${1:-$(pwd)}" - assert_dir_exists "${dir}" - - if [ "$(ls -A "${dir}")" ]; then - return 0 - else - format_error "Directory ${dir} is empty, but should not be" | flunk - fi -} - -assert_symlink_exists() { - local file="${1}" - - if [ ! -h "${file}" ] && [ -f "${file}" ]; then - format_error "Regular file ${file} exists, but symlink is expected" | flunk - elif [ ! -h "${file}" ]; then - format_error "Symlink ${file} does not exist" | flunk - else - return 0 - fi -} - -assert_symlink_not_exists() { - local file="${1}" - - if [ ! -h "${file}" ] && [ -f "${file}" ]; then - return 0 - elif [ ! -h "${file}" ]; then - return 0 - else - format_error "Symlink ${file} exists, but should not" | flunk - fi -} - -assert_file_mode() { - local file="${1}" - local perm="${2}" - assert_file_exists "${file}" - - if [ "$(uname)" == "Darwin" ]; then - parsed=$(printf "%.3o\n" $(($(stat -f '0%Lp' "$file") & ~0022))) - else - parsed=$(printf "%.3o\n" $(($(stat --printf '0%a' "$file") & ~0022))) - fi - - if [ "${parsed}" != "${perm}" ]; then - format_error "File permissions for file ${file} is '${parsed}', but expected '${perm}'" | flunk - else - return 0 - fi -} - -assert_file_contains() { - local file="${1}" - local string="${2}" - assert_file_exists "${file}" - - contents="$(cat "${file}")" - assert_contains "${string}" "${contents}" -} - -assert_file_not_contains() { - local file="${1}" - local string="${2}" - - [ ! -f "${file}" ] && return 0 - - contents="$(cat "${file}")" - assert_not_contains "${string}" "${contents}" -} - -assert_dir_contains_string() { - local dir="${1}" - local string="${2}" - - assert_dir_exists "${dir}" || return 1 - - if grep -rI --exclude-dir=".git" --exclude-dir=".idea" --exclude-dir="vendor" --exclude-dir="node_modules" --exclude-dir=".data" -l "${string}" "${dir}"; then - return 0 - else - format_error "Directory ${dir} does not contain a string '${string}'" | flunk - fi -} - -assert_dir_not_contains_string() { - local dir="${1}" - local string="${2}" - - [ ! -d "${dir}" ] && return 0 - - if grep -rI --exclude-dir=".git" --exclude-dir=".idea" --exclude-dir="vendor" --exclude-dir="node_modules" --exclude-dir=".data" -l "${string}" "${dir}"; then - format_error "Directory ${dir} contains string '${string}', but should not" | flunk - else - return 0 - fi -} - -assert_git_repo() { - local dir="${1:-$(pwd)}" - - assert_dir_exists "${dir}" || return 1 - - if [ -d "${dir}/.git" ]; then - log=$(git --work-tree="${dir}" --git-dir="${dir}/.git" status 2>&1) - - if echo "${log}" | $(type -p ggrep grep | head -1) -i -F -- "not a git repository" >/dev/null; then - format_error "Directory ${dir} exists, but it is not a git repository" - return 1 - fi - - return 0 - else - format_error "Directory ${dir} exists, but it is not a git repository" | flunk - fi -} - -assert_not_git_repo() { - local dir="${1:-$(pwd)}" - - assert_dir_exists "${dir}" || return 1 - - if [ -d "${dir}/.git" ]; then - format_error "Directory ${dir} exists and it is a git repository, but should not be" | flunk - else - return 0 - fi -} - -assert_git_clean() { - local dir="${1:-$(pwd)}" - local message - - assert_git_repo "${dir}" - - message="$(git --work-tree="${dir}" --git-dir="${dir}/.git" status)" - assert_contains "nothing to commit" "${message}" -} - -assert_git_not_clean() { - local dir="${1:-$(pwd)}" - local message - - assert_git_repo "${dir}" - - message="$(git --work-tree="${dir}" --git-dir="${dir}/.git" status)" - assert_not_contains "nothing to commit" "${message}" -} - -assert_git_file_is_tracked() { - local file="${1:-}" - local dir="${2:-$(pwd)}" - - if [ ! -d "${dir}/.git" ]; then - return 1 - fi - - git --work-tree="${dir}" --git-dir="${dir}/.git" ls-files --error-unmatch "${1}" &>/dev/null - return $? -} - -assert_git_file_is_not_tracked() { - local file="${1:-}" - local dir="${2:-$(pwd)}" - - if [ ! -d "${dir}/.git" ]; then - return 1 - fi - - if git --work-tree="${dir}" --git-dir="${dir}/.git" ls-files --error-unmatch "${1}" &>/dev/null; then - return 1 - else - return 0 - fi -} - -assert_files_equal() { - local file1="${1}" - local file2="${2}" - - assert_file_exists "${file1}" || return 1 - assert_file_exists "${file2}" || return 1 - - if cmp "${file1}" "${file2}"; then - return 0 - else - format_error "File ${file1} is not equal to file ${file2}" | flunk - fi -} - -assert_files_not_equal() { - local file1="${1}" - local file2="${2}" - - assert_file_exists "${file1}" || return 1 - assert_file_exists "${file2}" || return 1 - - if cmp "${file1}" "${file2}"; then - format_error "File ${file1} is equal to file ${file2}, but it should not be" | flunk - else - return 0 - fi -} - -assert_dirs_equal() { - local dir1="${1}" - local dir2="${2}" - - assert_dir_exists "${dir1}" || return 1 - assert_dir_exists "${dir2}" || return 1 - - for file in $(find "${dir1}/" -type f); do - assert_files_equal "${file}" "${file/${dir1}/${dir2}}" || return 1 - done +# Helpers related to common testing functionality. +# +# Run with "--verbose-run" to see debug output. +# - for file in $(find "${dir2}/" -type f); do - assert_files_equal "${file}" "${file/${dir2}/${dir1}}" || return 1 - done +################################################################################ +# BATS HOOK IMPLEMENTATIONS # +################################################################################ - return 0 -} +setup() { + # For a list of available variables see: + # @see https://bats-core.readthedocs.io/en/stable/writing-tests.html#special-variables -assert_empty() { - if [ "${1}" == "" ]; then - return 0 - else - format_error "String ${1} is not empty, but should be" | flunk - fi -} + # Register a path to libraries. + export BATS_LIB_PATH="${BATS_TEST_DIRNAME}/node_modules" -assert_not_empty() { - if [ "${1}" == "" ]; then - format_error "String ${1} is empty, but should not be" | flunk - else - return 0 - fi -} + # Load 'bats-helpers' library. + bats_load_library bats-helpers -assert_output() { - local expected - if [ $# -eq 0 ]; then - expected="$(cat -)" - else - expected="${1}" - fi - # shellcheck disable=SC2154 - assert_equal "${expected}" "${output}" -} + # Setup command mocking. + setup_mock -assert_output_contains() { - local expected - if [ $# -eq 0 ]; then - expected="$(cat -)" - else - expected="${1}" - fi - # shellcheck disable=SC2154 - assert_contains "${expected}" "${output}" -} + # Current directory where the test is run from. + # shellcheck disable=SC2155 + export CUR_DIR="$(pwd)" -assert_output_not_contains() { - local expected - if [ $# -eq 0 ]; then - expected="$(cat -)" - else - expected="${1}" + # Print debug information if "--verbose-run" is passed. + # LCOV_EXCL_START + if [ "${BATS_VERBOSE_RUN-}" = "1" ]; then + echo "BUILD_DIR: ${BUILD_DIR}" >&3 fi - # shellcheck disable=SC2154 - assert_not_contains "${expected}" "${output}" -} - -prepare_git_config() { - [ "$(git config --global user.name)" = "" ] && echo "==> Configuring global git user name." && git config --global user.name "Some User" || true - [ "$(git config --global user.email)" = "" ] && echo "==> Configuring global git user email." && git config --global user.email "some.user@example.com" || true -} - -random_string() { - local len="${1:-8}" - local ret - # shellcheck disable=SC2002 - ret=$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w "${len}" | head -n 1) - echo "${ret}" -} - -prepare_fixture_dir() { - local dir="${1:-$(pwd)}" - rm -Rf "${dir}" >/dev/null - mkdir -p "${dir}" - assert_dir_exists "${dir}" -} - -mktouch() { - local file="${1}" - mkdir -p "$(dirname "${file}")" && touch "${file}" -} - -# Format error message with optional output, if present. -format_error() { - local message="${1}" - echo "##################################################" - echo "# BEGIN ERROR MESSAGE #" - echo "##################################################" - echo - echo "${message}" - echo - echo "##################################################" - echo "# END ERROR MESSAGE #" - echo "##################################################" - echo - - if [ "${output}" != "" ]; then - echo "----------------------------------------" - echo "${BATS_TEST_TMPDIR}" - echo "${output}" - echo "----------------------------------------" - fi -} - -read_env() { - # shellcheck disable=SC1090,SC1091 - [ -f "./.env" ] && t=$(mktemp) && export -p >"$t" && set -a && . "./.env" && set +a && . "$t" && rm "$t" && unset t - - eval echo "${@}" -} - -# Trim the last line of the file. -trim_file() { - local sed_opts - sed_opts=(-i) && [ "$(uname)" == "Darwin" ] && sed_opts=(-i '') - sed_opts+=(-e '$ d') - sed "${sed_opts[@]}" "${1}" -} - -add_var_to_file() { - local file="${1}" - local name="${2}" - local value="${3}" - - local backup=/tmp/bkp/"${file}" - mkdir -p "$(dirname "${backup}")" - - cp -f "${file}" "${backup}" - - # shellcheck disable=SC2086 - echo $name=$value >>"${file}" -} - -restore_file() { - local file="${1}" - local backup=/tmp/bkp/"${file}" - - cp -f "${backup}" "${file}" + # LCOV_EXCL_END } # Run bats with `--tap` option to debug the output. diff --git a/tests/bats/_helper_civictheme.bash b/tests/bats/_helper_civictheme.bash deleted file mode 100644 index ddcb5b9f55..0000000000 --- a/tests/bats/_helper_civictheme.bash +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env bash -# -# Helpers related to CivicTheme common testing functionality. -# -# shellcheck disable=SC2155 - -load "${BASH_SOURCE[0]%/*}"/_mock.bash - -################################################################################ -# HOOK IMPLEMENTATIONS # -################################################################################ - -setup() { - export CUR_DIR="$(pwd)" - BUILD_DIR="$BATS_TEST_TMPDIR/civictheme-$(random_string)" - export BUILD_DIR - prepare_fixture_dir "${BUILD_DIR}" - echo "BUILD_DIR dir: ${BUILD_DIR}" >&3 - - # Setup command mocking. - setup_mock - - pushd "${BUILD_DIR}" >/dev/null || exit 1 -} - -teardown() { - restore_global_gitignore - popd >/dev/null || cd "${CUR_DIR}" || exit 1 -} - -################################################################################ -# COMMAND MOCKING # -################################################################################ - -# Setup mock support. -# Call this function from your test's setup() method. -setup_mock() { - # Command and functions mocking support. - # @see https://github.com/grayhemp/bats-mock - # - # Prepare directory with mock binaries, get it's path, and export it so that - # bats-mock could use it internally. - BATS_MOCK_TMPDIR="$(mock_prepare_tmp)" - export "BATS_MOCK_TMPDIR" - # Set the path to temp mocked binaries directory as the first location in - # PATH to lookup in mock directories first. This change lives only for the - # duration of the test and will be reset after. It does not modify the PATH - # outside of the running test. - PATH="${BATS_MOCK_TMPDIR}:$PATH" -} - -# Prepare temporary mock directory. -mock_prepare_tmp() { - rm -rf "${BATS_TMPDIR}/bats-mock-tmp" >/dev/null - mkdir -p "${BATS_TMPDIR}/bats-mock-tmp" - echo "${BATS_TMPDIR}/bats-mock-tmp" -} - -# Mock provided command. -# Arguments: -# 1. Mocked command name, -# Outputs: -# STDOUT: path to created mock file. -mock_command() { - mocked_command="${1}" - mock="$(mock_create)" - mock_path="${mock%/*}" - mock_file="${mock##*/}" - ln -sf "${mock_path}/${mock_file}" "${mock_path}/${mocked_command}" - echo "$mock" -} diff --git a/tests/bats/_mock.bash b/tests/bats/_mock.bash deleted file mode 100644 index 970cbe8f77..0000000000 --- a/tests/bats/_mock.bash +++ /dev/null @@ -1,216 +0,0 @@ -#!/usr/bin/env bash -# -# A Bats helper library providing mocking functionality. -# @see https://github.com/grayhemp/bats-mock -# -# This file was modified - look for @note comments. -# shellcheck disable=SC2086,SC1090,SC2005,SC2061 - -# Creates a mock program -# Globals: -# BATS_TMPDIR -# BATS_MOCK_TMPDIR -# Outputs: -# STDOUT: Path to the mock -mock_create() { - local index - - # @note: Modification to the original file: allow to provide custom temp - # directory. BATS_TMPDIR below was changed to BATS_MOCK_TMPDIR. - BATS_MOCK_TMPDIR="${BATS_MOCK_TMPDIR:-$BATS_TMPDIR}" - - index="$(find ${BATS_MOCK_TMPDIR} -name bats-mock.$$.* | wc -l | tr -d ' ')" - local mock - mock="${BATS_MOCK_TMPDIR}/bats-mock.$$.${index}" - echo -n 0 >"${mock}.call_num" - echo -n 0 >"${mock}.status" - echo -n '' >"${mock}.output" - echo -n '' >"${mock}.side_effect" - - cat <"${mock}" -#!/usr/bin/env bash - -set -e - -mock="${mock}" - -call_num="\$(( \$(cat \${mock}.call_num) + 1 ))" -echo "\${call_num}" > "\${mock}.call_num" - -echo "\${_USER:-\$(id -un)}" > "\${mock}.user.\${call_num}" - -echo "\$@" > "\${mock}.args.\${call_num}" - -for var in \$(compgen -e); do - declare -p "\${var}" -done > "\${mock}.env.\${call_num}" - -if [[ -e "\${mock}.output.\${call_num}" ]]; then - cat "\${mock}.output.\${call_num}" -else - cat "\${mock}.output" -fi - -if [[ -e "\${mock}.side_effect.\${call_num}" ]]; then - source "\${mock}.side_effect.\${call_num}" -else - source "\${mock}.side_effect" -fi - -if [[ -e "\${mock}.status.\${call_num}" ]]; then - exit "\$(cat \${mock}.status.\${call_num})" -else - exit "\$(cat \${mock}.status)" -fi -EOF - chmod +x "${mock}" - - echo "${mock}" -} - -# Sets the exit status of the mock -# Arguments: -# 1: Path to the mock -# 2: Status -# 3: Index of the call, optional -mock_set_status() { - local mock="${1?'Mock must be specified'}" - local status="${2?'Status must be specified'}" - local n="$3" - - mock_set_property "${mock}" 'status' "${status}" "${n}" -} - -# Sets the output of the mock -# Arguments: -# 1: Path to the mock -# 2: Output or - for STDIN -# 3: Index of the call, optional -mock_set_output() { - local mock="${1?'Mock must be specified'}" - local output="${2?'Output must be specified'}" - local n="$3" - - mock_set_property "${mock}" 'output' "${output}" "${n}" -} - -# Sets the side effect of the mock -# Arguments: -# 1: Path to the mock -# 2: Side effect or - for STDIN -# 3: Index of the call, optional -mock_set_side_effect() { - local mock="${1?'Mock must be specified'}" - local side_effect="${2?'Side effect must be specified'}" - local n="$3" - - mock_set_property "${mock}" 'side_effect' "${side_effect}" "${n}" -} - -# Returns the number of times the mock was called -# Arguments: -# 1: Path to the mock -# Outputs: -# STDOUT: Number of calls -mock_get_call_num() { - local mock="${1?'Mock must be specified'}" - - echo "$(cat ${mock}.call_num)" -} - -# Returns the user the mock was called with -# Arguments: -# 1: Path to the mock -# 2: Index of the call, optional -# Outputs: -# STDOUT: User name -mock_get_call_user() { - local mock="${1?'Mock must be specified'}" - local n - n="$(mock_default_n ${mock} $2)" || exit "$?" - - echo "$(cat ${mock}.user.${n})" -} - -# Returns the arguments line the mock was called with -# Arguments: -# 1: Path to the mock -# 2: Index of the call, optional -# Outputs: -# STDOUT: Arguments line -mock_get_call_args() { - local mock="${1?'Mock must be specified'}" - local n - n="$(mock_default_n ${mock} $2)" || exit "$?" - - echo "$(cat ${mock}.args.${n})" -} - -# Returns the value of the environment variable the mock was called with -# Arguments: -# 1: Path to the mock -# 2: Variable name -# 3: Index of the call, optional -# Outputs: -# STDOUT: Variable value -mock_get_call_env() { - local mock="${1?'Mock must be specified'}" - local var="${2?'Variable name must be specified'}" - local n="$3" - n="$(mock_default_n ${mock} $3)" || exit "$?" - - source "${mock}.env.${n}" - echo "${!var}" -} - -# Sets a specific property of the mock -# Arguments: -# 1: Path to the mock -# 2: Property name -# 3: Property value or - for STDIN -# 4: Index of the call, optional -# Inputs: -# STDIN: Property value if 2 is - -mock_set_property() { - local mock="${1?'Mock must be specified'}" - local property_name="${2?'Property name must be specified'}" - local property_value="${3?'Property value must be specified'}" - local n="$4" - - if [[ "${property_value}" = '-' ]]; then - property_value="$(cat -)" - fi - - if [[ -n "${n}" ]]; then - echo -e "${property_value}" >"${mock}.${property_name}.${n}" - else - echo -e "${property_value}" >"${mock}.${property_name}" - fi -} - -# Defaults call index to the last one if not specified explicitly -# Arguments: -# 1: Path to the mock -# 2: Index of the call, optional -# Returns: -# 1: If mock is not called enough times -# Outputs: -# STDOUT: Call index -# STDERR: Corresponding error message -mock_default_n() { - local mock="${1?'Mock must be specified'}" - local call_num - call_num="$(cat ${mock}.call_num)" - local n="${2:-${call_num}}" - - if [[ "${n}" -eq 0 ]]; then - n=1 - fi - - if [[ "${n}" -gt "${call_num}" ]]; then - echo "$(basename $0): Mock must be called at least ${n} time(s)" >&2 - exit 1 - fi - - echo "${n}" -} diff --git a/tests/bats/assets.bats b/tests/bats/assets.bats index 06d345f4ac..aa03115432 100644 --- a/tests/bats/assets.bats +++ b/tests/bats/assets.bats @@ -7,7 +7,7 @@ load _helper @test "CivicTheme Theme assets" { - dir="${DREVOPS_EXPORT_CODE_DIR:-$(pwd)}/web/themes/contrib/civictheme" + dir="${DREVOPS_EXPORT_CODE_DIR:-${CUR_DIR}}/web/themes/contrib/civictheme" pushd "${dir}" >/dev/null || exit 1 diff --git a/tests/bats/assets_demo_custom.bats b/tests/bats/assets_demo_custom.bats index 26fe27b637..1ccc5db743 100644 --- a/tests/bats/assets_demo_custom.bats +++ b/tests/bats/assets_demo_custom.bats @@ -7,7 +7,9 @@ load _helper @test "CivicTheme Demo Theme assets" { - dir="${DREVOPS_EXPORT_CODE_DIR:-$(pwd)}/web/themes/custom/civictheme_demo" + dir="${DREVOPS_EXPORT_CODE_DIR:-${CUR_DIR}}/web/themes/custom/civictheme_demo" + + [ ! -d "${dir}" ] && debug "Skipping tests" && return pushd "${dir}" >/dev/null || exit 1 diff --git a/tests/bats/assets_demo_sibling.bats b/tests/bats/assets_demo_sibling.bats index f9abba0168..ec4ce88b46 100644 --- a/tests/bats/assets_demo_sibling.bats +++ b/tests/bats/assets_demo_sibling.bats @@ -7,7 +7,7 @@ load _helper @test "CivicTheme Demo Sibling Theme assets" { - dir="${DREVOPS_EXPORT_CODE_DIR:-$(pwd)}/web/themes/contrib/civictheme_demo" + dir="${DREVOPS_EXPORT_CODE_DIR:-${CUR_DIR}}/web/themes/contrib/civictheme_demo" pushd "${dir}" >/dev/null || exit 1 diff --git a/tests/bats/assets_library.bats b/tests/bats/assets_library.bats index 60b174b0a8..cdb2cea1e8 100644 --- a/tests/bats/assets_library.bats +++ b/tests/bats/assets_library.bats @@ -7,7 +7,7 @@ load _helper @test "CivicTheme Library assets" { - dir="${DREVOPS_EXPORT_CODE_DIR:-$(pwd)}/web/themes/contrib/civictheme/civictheme_library" + dir="${DREVOPS_EXPORT_CODE_DIR:-${CUR_DIR}}/web/themes/contrib/civictheme/civictheme_library" pushd "${dir}" >/dev/null || exit 1 diff --git a/tests/bats/helpers.bats b/tests/bats/helpers.bats deleted file mode 100644 index e8482c483e..0000000000 --- a/tests/bats/helpers.bats +++ /dev/null @@ -1,588 +0,0 @@ -#!/usr/bin/env bats -# -# Tests for Bats helpers. -# -# Each assertion tests positive and negative behaviour. -# -# shellcheck disable=SC2129 - -load _helper - -@test "assert_success" { - status=0 - assert_success - - status=1 - run assert_success - [ "$status" -eq 1 ] -} - -@test "assert_failure" { - status=1 - assert_failure - - status=0 - run assert_failure - [ "$status" -eq 1 ] -} - -@test "assert_output" { - output="output needle" - assert_output "output needle" - - output="output not needle" - run assert_output "output needle" - assert_failure -} - -@test "assert_output_contains" { - run echo "some existing text" - assert_output_contains "some existing text" - - run echo "some existing text" - assert_output_contains "some EXISTING text" - - run echo "some existing text" - assert_output_contains "existing" - - run assert_output_contains "non-existing" - assert_failure -} - -@test "assert_output_not_contains" { - run echo "some existing text" - assert_output_not_contains "non-existing" - - run assert_output_not_contains "some existing text" - assert_failure - - run assert_output_not_contains "some EXISTING text" - assert_failure - - run assert_output_not_contains "existing" - assert_failure -} - -@test "assert_equal" { - assert_equal 1 1 - - run assert_equal 1 2 - assert_failure -} - -@test "assert_empty" { - assert_empty "" - - run assert_empty "something" - assert_failure -} - -@test "assert_not_empty" { - assert_not_empty "something" - - run assert_not_empty "" - assert_failure -} - -@test "assert_contains" { - assert_contains "needle" "some needle in a haystack" - assert_contains "n[ee]dle" "some n[ee]dle in a haystack" - - run assert_contains "needle" "some ne edle in a haystack" - assert_failure -} - -@test "assert_not_contains" { - assert_not_contains "otherneedle" "some needle in a haystack" - assert_not_contains "othern[ee]dle" "some n[ee]dle in a haystack" - - run assert_not_contains "needle" "some needle in a haystack" - assert_failure - run assert_not_contains "n[ee]dle" "some n[ee]dle in a haystack" - assert_failure -} - -@test "assert_file_exists" { - assert_file_exists "${BATS_TEST_FILENAME}" - - mktouch "${BATS_TEST_TMPDIR}/file1.txt" - mktouch "${BATS_TEST_TMPDIR}/file2.txt" - mktouch "${BATS_TEST_TMPDIR}/file3.md" - mktouch "${BATS_TEST_TMPDIR}/a.b.c.d.doc" - - assert_file_exists "${BATS_TEST_TMPDIR}/file1.txt" - assert_file_exists "${BATS_TEST_TMPDIR}/file2.txt" - assert_file_exists "${BATS_TEST_TMPDIR}/file3.md" - - assert_file_exists "${BATS_TEST_TMPDIR}/file*" - assert_file_exists "${BATS_TEST_TMPDIR}/*.txt" - assert_file_exists "${BATS_TEST_TMPDIR}/*.doc" - - run assert_file_exists "some_file.txt" - assert_failure - - run assert_file_exists "${BATS_TEST_TMPDIR}/*.rtf" - assert_failure - - run assert_file_exists "${BATS_TEST_TMPDIR}/other*" - assert_failure -} - -@test "assert_file_not_exists" { - assert_file_not_exists "some_file.txt" - - mktouch "${BATS_TEST_TMPDIR}/file1.txt" - mktouch "${BATS_TEST_TMPDIR}/file2.txt" - mktouch "${BATS_TEST_TMPDIR}/file3.md" - - assert_file_not_exists "${BATS_TEST_TMPDIR}/otherfile1.txt" - assert_file_not_exists "${BATS_TEST_TMPDIR}/otherfile*" - assert_file_not_exists "${BATS_TEST_TMPDIR}/*.rtf" - - run assert_file_not_exists "${BATS_TEST_FILENAME}" - assert_failure - - run assert_file_not_exists "${BATS_TEST_TMPDIR}/file1.txt" - assert_failure - - run assert_file_not_exists "${BATS_TEST_TMPDIR}/file*" - assert_failure - - run assert_file_not_exists "${BATS_TEST_TMPDIR}/*.txt" - assert_failure -} - -@test "assert_dir_exists" { - assert_dir_exists "${BATS_TEST_DIRNAME}" - - run assert_dir_exists "some dir" - assert_failure -} - -@test "assert_dir_not_exists" { - assert_dir_not_exists "some dir" - - run assert_dir_not_exists "${BATS_TEST_DIRNAME}" - assert_failure -} - -@test "assert_symlink_exists" { - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture_symlink" - - # Assert file. - echo "some existing text" >"${BATS_TEST_TMPDIR}/fixture_symlink/src.txt" - ln -s "${BATS_TEST_TMPDIR}/fixture_symlink/src.txt" "${BATS_TEST_TMPDIR}/fixture_symlink/dst.txt" - assert_symlink_exists "${BATS_TEST_TMPDIR}/fixture_symlink/dst.txt" - - run assert_symlink_exists "${BATS_TEST_TMPDIR}/fixture_symlink/not-existing.txt" - assert_failure - - # Assert dir. - mkdir "${BATS_TEST_TMPDIR}/fixture_symlink/symlink_src" - ln -s "${BATS_TEST_TMPDIR}/fixture_symlink/symlink_src" "${BATS_TEST_TMPDIR}/fixture_symlink/symlink_dst" - assert_symlink_exists "${BATS_TEST_TMPDIR}/fixture_symlink/symlink_dst" - run assert_symlink_exists "${BATS_TEST_TMPDIR}/fixture_symlink/symlink_dst_not_exisitng" - assert_failure -} - -@test "assert_symlink_not_exists" { - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture_symlink" - - echo "some existing text" >"${BATS_TEST_TMPDIR}/fixture_symlink/src.txt" - ln -s "${BATS_TEST_TMPDIR}/fixture_symlink/src.txt" "${BATS_TEST_TMPDIR}/fixture_symlink/dst.txt" - - # Assert others. - assert_symlink_not_exists "${BATS_TEST_TMPDIR}/fixture_symlink/src.txt" - assert_symlink_not_exists "${BATS_TEST_TMPDIR}/fixture_symlink/other_dst.txt" - assert_symlink_not_exists "${BATS_TEST_TMPDIR}/fixture_symlink/some_dir" - - run assert_symlink_not_exists "${BATS_TEST_TMPDIR}/fixture_symlink/dst.txt" - assert_failure -} - -@test "assert_file_mode" { - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture_mode" - echo "some existing text" >"${BATS_TEST_TMPDIR}/fixture_mode/1.txt" - chmod 644 "${BATS_TEST_TMPDIR}/fixture_mode/1.txt" - assert_file_mode "${BATS_TEST_TMPDIR}/fixture_mode/1.txt" "644" - chmod 664 "${BATS_TEST_TMPDIR}/fixture_mode/1.txt" - assert_file_mode "${BATS_TEST_TMPDIR}/fixture_mode/1.txt" "644" - chmod 755 "${BATS_TEST_TMPDIR}/fixture_mode/1.txt" - assert_file_mode "${BATS_TEST_TMPDIR}/fixture_mode/1.txt" "755" - chmod 775 "${BATS_TEST_TMPDIR}/fixture_mode/1.txt" - assert_file_mode "${BATS_TEST_TMPDIR}/fixture_mode/1.txt" "755" - chmod 777 "${BATS_TEST_TMPDIR}/fixture_mode/1.txt" - assert_file_mode "${BATS_TEST_TMPDIR}/fixture_mode/1.txt" "755" - - run assert_file_mode "${BATS_TEST_TMPDIR}/fixture_mode/1.txt" "644" - assert_failure -} - -@test "assert_file_contains" { - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture_file_assert" - echo "some existing text" >>"${BATS_TEST_TMPDIR}/fixture_file_assert/1.txt" - echo "other existing text" >>"${BATS_TEST_TMPDIR}/fixture_file_assert/1.txt" - echo "one more line of existing text" >>"${BATS_TEST_TMPDIR}/fixture_file_assert/1.txt" - - assert_file_contains "${BATS_TEST_TMPDIR}/fixture_file_assert/1.txt" "some existing text" - - run assert_file_contains "${BATS_TEST_TMPDIR}/fixture_file_assert/1.txt" "other non-existing text" - assert_failure -} - -@test "assert_file_not_contains" { - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture_file_assert" - echo "some existing text" >>"${BATS_TEST_TMPDIR}/fixture_file_assert/1.txt" - echo "other existing text" >>"${BATS_TEST_TMPDIR}/fixture_file_assert/1.txt" - echo "one more line of existing text" >>"${BATS_TEST_TMPDIR}/fixture_file_assert/1.txt" - - assert_file_not_contains "${BATS_TEST_TMPDIR}/fixture_file_assert/1.txt" "other non-existing text" - - run assert_file_not_contains "${BATS_TEST_TMPDIR}/fixture_file_assert/1.txt" "some existing text" - assert_failure - - # Text exists, non-existing file. - assert_file_not_contains "${BATS_TEST_TMPDIR}/fixture_file_assert/somefile.txt" "some existing text" -} - -@test "assert_dir_empty" { - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/dir1" - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/dir2" - echo "some existing text" >"${BATS_TEST_TMPDIR}/fixture/dir2/1.txt" - - assert_dir_empty "${BATS_TEST_TMPDIR}/fixture/dir1" - - run assert_dir_empty "${BATS_TEST_TMPDIR}/fixture/dir2" - assert_failure - - run assert_dir_empty "${BATS_TEST_TMPDIR}/non_existing" - assert_failure -} - -@test "assert_dir_not_empty" { - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/dir1" - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/dir2" - echo "some existing text" >"${BATS_TEST_TMPDIR}/fixture/dir2/1.txt" - - assert_dir_not_empty "${BATS_TEST_TMPDIR}/fixture/dir2" - - run assert_dir_not_empty "${BATS_TEST_TMPDIR}/fixture/dir1" - assert_failure - - run assert_dir_not_empty "${BATS_TEST_TMPDIR}/non_existing" - assert_failure -} - -@test "assert_dir_contains_string" { - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture" - echo "some existing text" >"${BATS_TEST_TMPDIR}/fixture/1.txt" - - assert_dir_contains_string "${BATS_TEST_TMPDIR}/fixture" "existing" - - run assert_dir_contains_string "${BATS_TEST_TMPDIR}/fixture" "non-existing" - assert_failure - - run assert_dir_contains_string "${BATS_TEST_TMPDIR}/non_existing" - assert_failure -} - -@test "assert_dir_not_contains_string" { - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture" - echo "some existing text" >"${BATS_TEST_TMPDIR}/fixture/1.txt" - echo "some other text" >"${BATS_TEST_TMPDIR}/fixture/2.txt" - echo "some existing text" >"${BATS_TEST_TMPDIR}/fixture/3.txt" - - assert_dir_not_contains_string "${BATS_TEST_TMPDIR}/fixture" "non-existing" - - run assert_dir_not_contains_string "${BATS_TEST_TMPDIR}/fixture" "existing" - assert_failure - assert_output_contains "fixture/1.txt" - assert_output_contains "fixture/3.txt" - assert_output_not_contains "fixture/2.txt" - - # Non-existing dir. - assert_dir_not_contains_string "${BATS_TEST_TMPDIR}/non_existing" "existing" -} - -@test "assert_git_repo" { - prepare_git_config - - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/git_repo" - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/git_repo_empty_dot_git" - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/not_git_repo" - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" init >/dev/null - - assert_git_repo "${BATS_TEST_TMPDIR}/fixture/git_repo" - - mkdir "${BATS_TEST_TMPDIR}/fixture/git_repo_empty_dot_git/.git" - assert_dir_exists "${BATS_TEST_TMPDIR}/fixture/git_repo_empty_dot_git/.git" - assert_file_not_exists "${BATS_TEST_TMPDIR}/fixture/git_repo_empty_dot_git/HEAD" - run assert_git_repo "${BATS_TEST_TMPDIR}/fixture/git_repo_empty_dot_git" - assert_failure - - run assert_git_repo "${BATS_TEST_TMPDIR}/fixture/not_git_repo" - assert_failure - - run assert_git_repo "${BATS_TEST_TMPDIR}/fixture/some_dir" - assert_failure -} - -@test "assert_not_git_repo" { - prepare_git_config - - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/git_repo" - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/not_git_repo" - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" init >/dev/null - - assert_not_git_repo "${BATS_TEST_TMPDIR}/fixture/not_git_repo" - - run assert_not_git_repo "${BATS_TEST_TMPDIR}/fixture/git_repo" - assert_failure - - run assert_not_git_repo "${BATS_TEST_TMPDIR}/fixture/some_dir" - assert_failure -} - -@test "assert_git_clean" { - prepare_git_config - - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/git_repo" - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" init >/dev/null - assert_git_repo "${BATS_TEST_TMPDIR}/fixture/git_repo" - - assert_git_clean "${BATS_TEST_TMPDIR}/fixture/git_repo" - - mktouch "${BATS_TEST_TMPDIR}/fixture/git_repo/uncommitted_file" - run assert_git_clean "${BATS_TEST_TMPDIR}/fixture/git_repo" - assert_failure - - # Now, commit first file and create another, but do not add. - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" add -A >/dev/null - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" commit -m "First commit" >/dev/null - assert_git_clean "${BATS_TEST_TMPDIR}/fixture/git_repo" - mktouch "${BATS_TEST_TMPDIR}/fixture/git_repo/other_uncommitted_file" - run assert_git_clean "${BATS_TEST_TMPDIR}/fixture/git_repo" - assert_failure -} - -@test "assert_git_not_clean" { - prepare_git_config - - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/git_repo" - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" init >/dev/null - assert_git_repo "${BATS_TEST_TMPDIR}/fixture/git_repo" - - run assert_git_not_clean "${BATS_TEST_TMPDIR}/fixture/git_repo" - assert_failure - - mktouch "${BATS_TEST_TMPDIR}/fixture/git_repo/uncommitted_file" - assert_git_not_clean "${BATS_TEST_TMPDIR}/fixture/git_repo" - - # Now, commit first file and create another, but do not add. - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" add -A >/dev/null - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" commit -m "First commit" >/dev/null - run assert_git_not_clean "${BATS_TEST_TMPDIR}/fixture/git_repo" - assert_failure - mktouch "${BATS_TEST_TMPDIR}/fixture/git_repo/other_uncommitted_file" - assert_git_not_clean "${BATS_TEST_TMPDIR}/fixture/git_repo" -} - -@test "assert_git_file_is_tracked" { - prepare_git_config - - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/git_repo" - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/not_git_repo" - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" init >/dev/null - assert_git_repo "${BATS_TEST_TMPDIR}/fixture/git_repo" - touch "${BATS_TEST_TMPDIR}/fixture/git_repo/1.txt" - touch "${BATS_TEST_TMPDIR}/fixture/git_repo/2.txt" - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" add 1.txt >/dev/null - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" commit -m "some message" >/dev/null - - assert_git_file_is_tracked "1.txt" "${BATS_TEST_TMPDIR}/fixture/git_repo" - - run assert_git_file_is_tracked "2.txt" "${BATS_TEST_TMPDIR}/fixture/git_repo" - assert_failure - - run assert_git_file_is_tracked "1.txt" "${BATS_TEST_TMPDIR}/fixture/not_git_repo" - assert_failure -} - -@test "assert_git_file_is_not_tracked" { - prepare_git_config - - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/git_repo" - prepare_fixture_dir "${BATS_TEST_TMPDIR}/fixture/not_git_repo" - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" init >/dev/null - assert_git_repo "${BATS_TEST_TMPDIR}/fixture/git_repo" - touch "${BATS_TEST_TMPDIR}/fixture/git_repo/1.txt" - touch "${BATS_TEST_TMPDIR}/fixture/git_repo/2.txt" - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" add 1.txt >/dev/null - git --work-tree="${BATS_TEST_TMPDIR}/fixture/git_repo" --git-dir="${BATS_TEST_TMPDIR}/fixture/git_repo/.git" commit -m "some message" >/dev/null - - assert_git_file_is_not_tracked "2.txt" "${BATS_TEST_TMPDIR}/fixture/git_repo" - - run assert_git_file_is_not_tracked "1.txt" "${BATS_TEST_TMPDIR}/fixture/git_repo" - assert_failure - - run assert_git_file_is_not_tracked "2.txt" "${BATS_TEST_TMPDIR}/fixture/not_git_repo" - assert_failure -} - -@test "assert_files_equal" { - prepare_git_config - - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/fixture1.png" - echo "some other file" >"${BATS_TEST_TMPDIR}/fixture2.png" - - assert_files_equal "${BATS_TEST_TMPDIR}/fixture1.png" "${BATS_TEST_TMPDIR}/fixture1.png" - - run assert_files_equal "${BATS_TEST_TMPDIR}/fixture1.png" "${BATS_TEST_TMPDIR}/fixture2.png" - assert_failure - - run assert_files_equal "${BATS_TEST_TMPDIR}/fixture3.png" "${BATS_TEST_TMPDIR}/fixture4.png" - assert_failure - - run assert_files_equal "${BATS_TEST_TMPDIR}/fixture1.png" "${BATS_TEST_TMPDIR}/fixture3.png" - assert_failure -} - -@test "assert_files_not_equal" { - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/fixture1.png" - echo "some other file" >"${BATS_TEST_TMPDIR}/fixture2.png" - - assert_files_not_equal "${BATS_TEST_TMPDIR}/fixture1.png" "${BATS_TEST_TMPDIR}/fixture2.png" - - run assert_files_not_equal "${BATS_TEST_TMPDIR}/fixture1.png" "${BATS_TEST_TMPDIR}/fixture1.png" - assert_failure - - run assert_files_not_equal "${BATS_TEST_TMPDIR}/fixture3.png" "${BATS_TEST_TMPDIR}/fixture1.png" - assert_failure - - run assert_files_not_equal "${BATS_TEST_TMPDIR}/fixture1.png" "${BATS_TEST_TMPDIR}/fixture3.png" - assert_failure -} - -@test "assert_dirs_equal" { - # Assert that files in the root are equal. - mkdir -p "${BATS_TEST_TMPDIR}/t11" - mkdir -p "${BATS_TEST_TMPDIR}/t12" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t11/fixture1.png" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t12/fixture1.png" - assert_dirs_equal "${BATS_TEST_TMPDIR}/t11" "${BATS_TEST_TMPDIR}/t12" - - # Assert that files in the root are not equal. - echo "some other file" >"${BATS_TEST_TMPDIR}/t12/fixture1.png" - run assert_dirs_equal "${BATS_TEST_TMPDIR}/t11" "${BATS_TEST_TMPDIR}/t12" - assert_failure - - # Assert that files in the subdirs are equal. - mkdir -p "${BATS_TEST_TMPDIR}/t31/subdir" - mkdir -p "${BATS_TEST_TMPDIR}/t32/subdir" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t31/subdir/fixture1.png" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t32/subdir/fixture1.png" - assert_dirs_equal "${BATS_TEST_TMPDIR}/t31" "${BATS_TEST_TMPDIR}/t32" - - # Assert that files in the subdirs are not equal. - echo "some other file" >"${BATS_TEST_TMPDIR}/t32/subdir/fixture1.png" - run assert_dirs_equal "${BATS_TEST_TMPDIR}/t31" "${BATS_TEST_TMPDIR}/t32" - assert_failure - - # Assert that files in the root and subdirs are equal. - mkdir -p "${BATS_TEST_TMPDIR}/t41/subdir" - mkdir -p "${BATS_TEST_TMPDIR}/t42/subdir" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t41/fixture1.png" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t41/.hidden" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t41/subdir/fixture1.png" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t42/fixture1.png" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t42/.hidden" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t42/subdir/fixture1.png" - assert_dirs_equal "${BATS_TEST_TMPDIR}/t41" "${BATS_TEST_TMPDIR}/t42" - - # Assert that files in the root and subdirs are not equal. - echo "some other file" >"${BATS_TEST_TMPDIR}/t42/subdir/fixture1.png" - run assert_dirs_equal "${BATS_TEST_TMPDIR}/t41" "${BATS_TEST_TMPDIR}/t42" - assert_failure - - # Assert that missing files trigger a failure. - mkdir -p "${BATS_TEST_TMPDIR}/t51/subdir" - mkdir -p "${BATS_TEST_TMPDIR}/t52/subdir" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t51/fixture1.png" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t51/.hidden" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t51/subdir/fixture1.png" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t51/subdir/fixture2.png" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t52/fixture1.png" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t52/.hidden" - cp "${BATS_TEST_DIRNAME}/fixtures/fixture.png" "${BATS_TEST_TMPDIR}/t52/subdir/fixture1.png" - run assert_dirs_equal "${BATS_TEST_TMPDIR}/t51" "${BATS_TEST_TMPDIR}/t52" - assert_failure - - # Assert non-existing dirs are failing. - run assert_dirs_equal "${BATS_TEST_TMPDIR}/t61" "${BATS_TEST_TMPDIR}/t62" - assert_failure -} - -@test "mktouch" { - assert_file_not_exists "${BATS_TEST_TMPDIR}/dir1/dir2/dir3/file.txt" - mktouch "${BATS_TEST_TMPDIR}/dir1/dir2/dir3/file.txt" - assert_file_exists "${BATS_TEST_TMPDIR}/dir1/dir2/dir3/file.txt" -} - -@test "read_env" { - pushd "${BATS_TEST_TMPDIR}" - - assert_file_not_exists ".env" - - echo "VAR1=val1" >>.env - echo "VAR2=val2" >>.env - run read_env "\$VAR1" - assert_output_contains "val1" - run read_env "\$VAR2" - assert_output_contains "val2" - - popd -} - -@test "trim_file" { - echo "line1" >>"${BATS_TEST_TMPDIR}/file.txt" - echo "line2" >>"${BATS_TEST_TMPDIR}/file.txt" - echo "line3" >>"${BATS_TEST_TMPDIR}/file.txt" - - trim_file "${BATS_TEST_TMPDIR}/file.txt" - - assert_file_contains "${BATS_TEST_TMPDIR}/file.txt" "line1" - assert_file_contains "${BATS_TEST_TMPDIR}/file.txt" "line2" - assert_file_not_contains "${BATS_TEST_TMPDIR}/file.txt" "line3" - - trim_file "${BATS_TEST_TMPDIR}/file.txt" - - assert_file_contains "${BATS_TEST_TMPDIR}/file.txt" "line1" - assert_file_not_contains "${BATS_TEST_TMPDIR}/file.txt" "line2" - assert_file_not_contains "${BATS_TEST_TMPDIR}/file.txt" "line3" -} - -@test "add_var_to_file and restore_file" { - rm -fr /tmp/bkp - - echo "line1" >>"${BATS_TEST_TMPDIR}/.env" - echo "line2" >>"${BATS_TEST_TMPDIR}/.env" - - add_var_to_file "${BATS_TEST_TMPDIR}/.env" "VAR" "value" - - assert_file_exists "${BATS_TEST_TMPDIR}/.env" - assert_file_contains "${BATS_TEST_TMPDIR}/.env" "line1" - assert_file_contains "${BATS_TEST_TMPDIR}/.env" "line2" - assert_file_contains "${BATS_TEST_TMPDIR}/.env" "VAR=value" - - assert_file_exists "/tmp/bkp/${BATS_TEST_TMPDIR}/.env" - assert_file_contains "/tmp/bkp/${BATS_TEST_TMPDIR}/.env" "line1" - assert_file_contains "/tmp/bkp/${BATS_TEST_TMPDIR}/.env" "line2" - assert_file_not_contains "/tmp/bkp/${BATS_TEST_TMPDIR}/.env" "VAR=value" - - restore_file "${BATS_TEST_TMPDIR}/.env" - - assert_file_exists "${BATS_TEST_TMPDIR}/.env" - assert_file_contains "${BATS_TEST_TMPDIR}/.env" "line1" - assert_file_contains "${BATS_TEST_TMPDIR}/.env" "line2" - assert_file_not_contains "${BATS_TEST_TMPDIR}/.env" "VAR=value" -} diff --git a/tests/bats/mock.bats b/tests/bats/mock.bats deleted file mode 100644 index f77617d2c0..0000000000 --- a/tests/bats/mock.bats +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env bats -# -# Tests for Bats mock. -# -# shellcheck disable=SC2129 - -load _helper -load _helper_civictheme - -@test "Mock: calls and arguments" { - mock_curl=$(mock_command "curl") - - "${CUR_DIR}/tests/bats/fixtures/fixture.sh" - - assert_equal 2 "$(mock_get_call_num "${mock_curl}")" - - assert_equal "-L -s -o /dev/null -w %{http_code} example.com" "$(mock_get_call_args "${mock_curl}" 1)" - assert_equal "example.com" "$(mock_get_call_args "${mock_curl}" 2)" -} - -@test "Mock: output" { - mock_curl=$(mock_command "curl") - mock_set_output "${mock_curl}" "testoutput1" 1 - mock_set_output "${mock_curl}" "testoutput2" 2 - - run "${CUR_DIR}/tests/bats/fixtures/fixture.sh" - assert_success - assert_equal 2 "$(mock_get_call_num "${mock_curl}")" - assert_output_contains "testoutput1" - assert_output_contains "testoutput2" -} - -@test "Mock: exit status" { - mock_curl=$(mock_command "curl") - mock_set_status "${mock_curl}" 1 1 - - run "${CUR_DIR}/tests/bats/fixtures/fixture.sh" - assert_failure - assert_equal 1 "$(mock_get_call_num "${mock_curl}")" -} diff --git a/tests/bats/package.json b/tests/bats/package.json new file mode 100644 index 0000000000..8b5696b65e --- /dev/null +++ b/tests/bats/package.json @@ -0,0 +1,9 @@ +{ + "name": "civictheme_source-tests", + "version": "1.0.0", + "description": "Packages used for testing", + "license": "GPL-2.0-or-later", + "devDependencies": { + "bats-helpers": "npm:@drevops/bats-helpers@^1.2" + } +}