Skip to content

Commit

Permalink
Fixed coverage for shell sctipt command.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed May 3, 2024
1 parent 0b913e8 commit 3a34ccc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-shell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }}

Expand Down
20 changes: 16 additions & 4 deletions shell-command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/<<TOKEN>>/random}"
URL_ENDPOINT="${JOKE_URL_ENDPOINT:-https://official-joke-api.appspot.com/jokes/__TOPIC__/random}"

# Topic.
topic="${1-}"
Expand All @@ -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}"
}

Expand Down Expand Up @@ -74,7 +79,14 @@ main() {
echo "Fetching joke for topic: ${topic}..."
echo

response="$(curl -sL "${URL_ENDPOINT//<<TOKEN>>/${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'
Expand Down
2 changes: 1 addition & 1 deletion tests/bats/_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 9 additions & 5 deletions tests/bats/shell-command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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."
}
Expand All @@ -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"
Expand Down

0 comments on commit 3a34ccc

Please sign in to comment.