From 3a34ccc7665f429d8fac4b8855c02b18b9e83726 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Fri, 3 May 2024 16:34:00 +1000 Subject: [PATCH] Fixed coverage for shell sctipt command. --- .github/workflows/test-shell.yml | 2 +- shell-command.sh | 20 ++++++++++++++++---- tests/bats/_helper.bash | 2 +- tests/bats/shell-command.bats | 14 +++++++++----- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/.github/workflows/test-shell.yml b/.github/workflows/test-shell.yml index 54c100a..235f242 100644 --- a/.github/workflows/test-shell.yml +++ b/.github/workflows/test-shell.yml @@ -55,7 +55,7 @@ jobs: - name: Run Tests and Code Coverage on Ubuntu if: matrix.os == 'ubuntu-latest' - run: kcov --clean --include-pattern=.sh,.bash --bash-parse-files-in-dir=. --exclude-path=node_modules,vendor,.coverage-html,docs,tests .coverage-html ./tests/bats/node_modules/.bin/bats tests/bats + run: kcov --include-pattern=.sh,.bash --bash-parse-files-in-dir=. --exclude-pattern=vendor,node_modules,.coverage-html,docs,tests "$(pwd)"/.coverage-html ./tests/bats/node_modules/.bin/bats tests/bats shell: bash continue-on-error: ${{ vars.CI_TEST_IGNORE_FAILURE == '1' }} diff --git a/shell-command.sh b/shell-command.sh index c6388ba..b1048e9 100755 --- a/shell-command.sh +++ b/shell-command.sh @@ -14,7 +14,7 @@ set -euo pipefail [ "${SCRIPT_DEBUG-}" = "1" ] && set -x # URL endpoint to fetch the data from. -URL_ENDPOINT="${JOKE_URL_ENDPOINT:-https://official-joke-api.appspot.com/jokes/<>/random}" +URL_ENDPOINT="${JOKE_URL_ENDPOINT:-https://official-joke-api.appspot.com/jokes/__TOPIC__/random}" # Topic. topic="${1-}" @@ -29,18 +29,23 @@ ask() { local default="${2-}" local result="" - if [[ -n $default ]]; then + if [[ -n ${default} ]]; then prompt="${prompt} [${default}]: " else + # LCOV_EXCL_START prompt="${prompt}: " + # LCOV_EXCL_END fi while [[ -z ${result} ]]; do read -p "${prompt}" result - if [[ -n $default && -z ${result} ]]; then + # LCOV_EXCL_START + if [[ -z ${result} && -n ${default} ]]; then result="${default}" fi + # LCOV_EXCL_END done + echo "${result}" } @@ -74,7 +79,14 @@ main() { echo "Fetching joke for topic: ${topic}..." echo - response="$(curl -sL "${URL_ENDPOINT//<>/${topic}}")" + local url + url="${URL_ENDPOINT//__TOPIC__/${topic}}" + + echo + echo "URL: ${url}" + echo + + response="$(curl -sL "${url}")" # Extract 'setup' setup=$(echo "${response}" | sed -E 's/.*"setup":"([^"]+)".*/\1/') # Extract 'punchline' diff --git a/tests/bats/_helper.bash b/tests/bats/_helper.bash index 251cdc7..45f07f6 100644 --- a/tests/bats/_helper.bash +++ b/tests/bats/_helper.bash @@ -32,7 +32,7 @@ setup() { export ROOT_DIR="${CUR_DIR}" # Directory where the shell command script will be running in. - export BUILD_DIR="${BUILD_DIR:-"${BATS_TEST_TMPDIR//\/\//\/}/scaffold-$(date +%s)"}" + export BUILD_DIR="${BUILD_DIR:-"${BATS_TEST_TMPDIR//\/\//\/}/shell-$(date +%s)"}" fixture_prepare_dir "${BUILD_DIR}" # Copy codebase at the last commit into the BUILD_DIR. diff --git a/tests/bats/shell-command.bats b/tests/bats/shell-command.bats index 6d5c82c..bf9d7e2 100644 --- a/tests/bats/shell-command.bats +++ b/tests/bats/shell-command.bats @@ -2,7 +2,8 @@ # # Test shell-command.sh functionality. # -# bats --tap tests/bats/shell-command.bats +# Example usage: +# ./tests/scaffold/node_modules/.bin/bats --no-tempdir-cleanup --formatter tap --filter-tags smoke tests/bats # # shellcheck disable=SC2030,SC2031,SC2034 @@ -11,24 +12,27 @@ load _helper export BATS_FIXTURE_EXPORT_CODEBASE_ENABLED=1 # Script file for TUI testing. -export SCRIPT_FILE=./shell-command.sh +export SCRIPT_FILE="shell-command.sh" +# bats test_tags=smoke @test "Data can be fetched from the API with user input" { tui_run general y assert_success + assert_output_contains "https://official-joke-api.appspot.com/jokes/general/random" } @test "Data can be fetched from the API with CLI arguments" { export SHOULD_PROCEED=y - run "${SCRIPT_FILE}" programming + tui_run programming assert_success + assert_output_contains "https://official-joke-api.appspot.com/jokes/programming/random" } @test "Data can be fetched from the API with CLI arguments, aborting" { export SHOULD_PROCEED=n - run "${SCRIPT_FILE}" programming + tui_run programming assert_success assert_output_contains "Aborting." } @@ -41,7 +45,7 @@ export SCRIPT_FILE=./shell-command.sh mocks="$(run_steps "setup")" export SHOULD_PROCEED=y - run "${SCRIPT_FILE}" mocked_topic + tui_run mocked_topic assert_success assert_output_contains "mocked_setup" assert_output_contains "mocked_punchline"