diff --git a/contrib/source-control-tools/CMakeLists.txt b/contrib/source-control-tools/CMakeLists.txt --- a/contrib/source-control-tools/CMakeLists.txt +++ b/contrib/source-control-tools/CMakeLists.txt @@ -7,8 +7,9 @@ set(SOURCE_CONTROL_TOOLS_TESTS test/test-autopatch.sh - test/test-sanitize-conduit-token.sh test/test-check-revision-accepted.sh + test/test-land-patch.sh + test/test-sanitize-conduit-token.sh ) foreach(TEST ${SOURCE_CONTROL_TOOLS_TESTS}) diff --git a/contrib/source-control-tools/land-patch.sh b/contrib/source-control-tools/land-patch.sh --- a/contrib/source-control-tools/land-patch.sh +++ b/contrib/source-control-tools/land-patch.sh @@ -18,15 +18,21 @@ Options: -d, --dry-run Dry run. Does everything but actually landing the change. -h, --help Display this help message. + +Environment Variables (for testing): + SANITY_CHECKS_COMMAND The command to override sanity checks (smoke tests). + GIT_COMMAND The command to override 'git' calls. EOF } +DRY_RUN=no GIT_ARGS=() # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in -d|--dry-run) + DRY_RUN=yes GIT_ARGS+=("--dry-run") shift # shift past argument ;; @@ -42,7 +48,7 @@ esac done -if [[ "$(git rev-parse --abbrev-ref HEAD)" != "master" ]]; then +if [[ ${DRY_RUN} == "no" ]] && [[ "$(git rev-parse --abbrev-ref HEAD)" != "master" ]]; then echo "Error: This script assumes the commit to land is on master" exit 10 fi @@ -50,7 +56,9 @@ TOPLEVEL=$(git rev-parse --show-toplevel) # Sanity checks -"${TOPLEVEL}"/contrib/devtools/smoke-tests.sh +: "${SANITY_CHECKS_COMMAND:=${TOPLEVEL}/contrib/devtools/smoke-tests.sh}" +${SANITY_CHECKS_COMMAND} # Push the change. Phabricator will automatically close the associated revision. -git push "${GIT_ARGS[@]}" origin master +: "${GIT_COMMAND:=git}" +${GIT_COMMAND} push "${GIT_ARGS[@]}" origin master diff --git a/contrib/source-control-tools/test/test-land-patch.sh b/contrib/source-control-tools/test/test-land-patch.sh new file mode 100755 --- /dev/null +++ b/contrib/source-control-tools/test/test-land-patch.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# Copyright (c) 2020 The Bitcoin developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +export LC_ALL=C.UTF-8 + +set -euxo pipefail + +TOPLEVEL=$(git rev-parse --show-toplevel) +SOURCE_CONTROL_TOOLS="${TOPLEVEL}"/contrib/source-control-tools + +mock_smoke_tests() { + echo 'Mock smoke tests passed' +} +export -f mock_smoke_tests +export SANITY_CHECKS_COMMAND=mock_smoke_tests + +# Successful git push works as expected +mock_git_push_success() { + echo 'Mock git push succeeded' +} +export -f mock_git_push_success +GIT_COMMAND=mock_git_push_success "${SOURCE_CONTROL_TOOLS}"/land-patch.sh --dry-run + +# Unsuccessful git push fails to land +mock_git_push_fail() { + echo 'Mock git push failed' + exit 1 +} +export -f mock_git_push_fail + +# shellcheck disable=SC2015 +GIT_COMMAND=mock_git_push_fail "${SOURCE_CONTROL_TOOLS}"/land-patch.sh --dry-run && { + echo "Error: A git push failure is expected to fail" + exit 1 +} || true + +echo "PASSED"