Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix running script from other directory #11

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
runs-on: ${{matrix.runs-on}}
steps:
- uses: actions/checkout@v4
- run: ./backport.functions_tests
- run: ./test_scripts
8 changes: 5 additions & 3 deletions backport
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@ function usage() {
echo " - create a PR from the new branch to 5.2.z branch with body and labels from the original PR (if found)"
}

SCRIPT_DIR=$(readlink -f "$0")
SCRIPT_DIR="$(dirname "${SCRIPT_DIR}")"
# shellcheck source=/dev/null
. backport.functions
. "${SCRIPT_DIR}"/backport.functions

check_command() {
if ! [ -x "$(command -v "$1")" ]; then
echo "Error: '$1' tool required - $2"
exit 2
exit 1
fi
}

check_command "gh" "https://cli.github.com/"

if [[ $# -lt 2 || $# -gt 5 ]]; then
usage
exit 1
exit 2
fi

get_opts() {
Expand Down
6 changes: 4 additions & 2 deletions backport.functions_tests
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env bash

set -eu
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
SCRIPT_DIR=$(readlink -f "$0")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

To fix https://www.shellcheck.net/wiki/SC2312

Consider invoking this command separately to avoid masking its return value (or use '|| true' to ignore).shellcheck(2312)

SCRIPT_DIR="$(dirname "${SCRIPT_DIR}")"

# Source the latest version of assert.sh unit testing library and include in current shell
curl --silent https://raw.githubusercontent.com/hazelcast/assert.sh/main/assert.sh --output assert.sh
Expand All @@ -11,7 +12,8 @@ curl --silent https://raw.githubusercontent.com/hazelcast/assert.sh/main/assert.
# . <(echo "${assert_script_content}")
# But this doesn't work on the MacOS GitHub runner (but does on MacOS locally)
. assert.sh
. "$SCRIPT_DIR"/backport.functions
# shellcheck source=/dev/null
. "${SCRIPT_DIR}"/backport.functions

TESTS_RESULT=0

Expand Down
37 changes: 37 additions & 0 deletions backport_tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

set -eu
SCRIPT_DIR=$(readlink -f "$0")
SCRIPT_DIR="$(dirname "${SCRIPT_DIR}")"

# Source the latest version of assert.sh unit testing library and include in current shell
curl --silent https://raw.githubusercontent.com/hazelcast/assert.sh/main/assert.sh --output assert.sh

# shellcheck source=/dev/null
# You _should_ be able to avoid a temporary file with something like
# . <(echo "${assert_script_content}")
# But this doesn't work on the MacOS GitHub runner (but does on MacOS locally)
. assert.sh
# shellcheck source=/dev/null
# . "${SCRIPT_DIR}"/backport

TESTS_RESULT=0

function test_get_exit_code {
local expected_exit_code=$1
local backport_arguments=( "${@:2:99}" )
local actual_exit_code
("${SCRIPT_DIR}"/backport "${backport_arguments[@]:-}") && true
actual_exit_code=$?
local msg="Expected exit code with arguments \"${backport_arguments[*]:-}\" should be equal to \"${expected_exit_code}\""
assert_eq "${expected_exit_code}" "${actual_exit_code}" "${msg}" && log_success "${msg}" || TESTS_RESULT=$?
}

log_header "Tests exit code"
# https://github.com/hazelcast/backport/issues/10
# Find location of a temp directory, where script is _unlikely_ to exist
TMPDIR=$(mktemp)
TMPDIR="$(dirname "${TMPDIR}")"
(cd "${TMPDIR}"; test_get_exit_code 2)

assert_eq 0 "${TESTS_RESULT}" "All tests should pass"
6 changes: 6 additions & 0 deletions test_scripts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

SCRIPT_DIR=$(readlink -f "$0")
SCRIPT_DIR="$(dirname "${SCRIPT_DIR}")"

find "$SCRIPT_DIR" -name "*_tests" -print0 | xargs -0 -n1 bash