From 5c48614875b4e8a4095bca9290b621c1bc352dd5 Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Sat, 9 Dec 2023 21:04:37 -0500 Subject: [PATCH 1/4] Support updating swiftly from swiftly-install.sh --- install/swiftly-install.sh | 61 +++++++++++++++++++++----------------- install/tests/overwrite.sh | 4 +-- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/install/swiftly-install.sh b/install/swiftly-install.sh index 086047bb..8f28ba8c 100755 --- a/install/swiftly-install.sh +++ b/install/swiftly-install.sh @@ -335,7 +335,7 @@ set -o errexit shopt -s extglob short_options='yhvp:' -long_options='disable-confirmation,no-modify-profile,no-install-system-deps,help,version,platform:' +long_options='disable-confirmation,no-modify-profile,no-install-system-deps,help,version,platform:,overwrite' args=$(getopt --options "$short_options" --longoptions "$long_options" --name "swiftly-install" -- "${@}") eval "set -- ${args}" @@ -351,7 +351,7 @@ USAGE: swiftly-install [options] OPTIONS: - -y, --disable-confirmation Disable confirmation prompt. + -y, --disable-confirmation Disable confirmation prompts. --no-modify-profile Do not attempt to modify the profile file to set environment variables (e.g. PATH) on login. --no-install-system-deps Do not attempt to install Swift's required system dependencies. @@ -359,6 +359,10 @@ OPTIONS: unspecified, the platform will be automatically detected. Available options are "ubuntu22.04", "ubuntu20.04", "ubuntu18.04", "rhel9", and "amazonlinux2". + --overwrite Overwrite the existing swiftly installation found at the configured + SWIFTLY_HOME, if any. If this option is unspecified and an existing + installation is found, the swiftly executable will be updated, but + the rest of the installation will not be modified. -h, --help Prints help information. --version Prints version information. EOF @@ -415,6 +419,11 @@ EOF shift 2 ;; + "--overwrite") + overwrite_existing_intallation="true" + shift + ;; + --) shift break @@ -535,19 +544,13 @@ while [ -z "$DISABLE_CONFIRMATION" ]; do done if [[ -d "$HOME_DIR" ]]; then - if [[ "$DISABLE_CONFIRMATION" == "true" ]]; then + detected_existing_installation="true" + if [[ "$overwrite_existing_intallation" == "true" ]]; then echo "Overwriting existing swiftly installation at $(replace_home_path $HOME_DIR)" + rm -r $HOME_DIR else - echo "Existing swiftly installation detected at $(replace_home_path $HOME_DIR), overwrite? (Y/n)" - - read_yn_input "true" - if [[ "$READ_INPUT_RETURN" == "false" ]]; then - echo "Cancelling installation." - exit 0 - fi + echo "Updating existing swiftly installation at $(replace_home_path $HOME_DIR)" fi - - rm -r $HOME_DIR fi mkdir -p $HOME_DIR/toolchains @@ -566,35 +569,37 @@ curl \ chmod +x "$BIN_DIR/swiftly" -echo "$JSON_OUT" > "$HOME_DIR/config.json" +if [[ "$detected_existing_installation" != "true" || "$overwrite_existing_intallation" == "true" ]]; then + echo "$JSON_OUT" > "$HOME_DIR/config.json" -# Verify the downloaded executable works. The script will exit if this fails due to errexit. -SWIFTLY_HOME_DIR="$HOME_DIR" SWIFTLY_BIN_DIR="$BIN_DIR" "$BIN_DIR/swiftly" --version > /dev/null + # Verify the downloaded executable works. The script will exit if this fails due to errexit. + SWIFTLY_HOME_DIR="$HOME_DIR" SWIFTLY_BIN_DIR="$BIN_DIR" "$BIN_DIR/swiftly" --version > /dev/null -ENV_OUT=$(cat < "$HOME_DIR/env.sh" + echo "$ENV_OUT" > "$HOME_DIR/env.sh" -if [[ "$MODIFY_PROFILE" == "true" ]]; then - SOURCE_LINE=". $(replace_home_path $HOME_DIR)/env.sh" + if [[ "$MODIFY_PROFILE" == "true" ]]; then + SOURCE_LINE=". $(replace_home_path $HOME_DIR)/env.sh" - # Only append the line if it isn't in .profile already. - if [[ ! -f "$PROFILE_FILE" ]] || [[ ! "$(cat $PROFILE_FILE)" =~ "$SOURCE_LINE" ]]; then - echo "$SOURCE_LINE" >> "$PROFILE_FILE" + # Only append the line if it isn't in .profile already. + if [[ ! -f "$PROFILE_FILE" ]] || [[ ! "$(cat $PROFILE_FILE)" =~ "$SOURCE_LINE" ]]; then + echo "$SOURCE_LINE" >> "$PROFILE_FILE" + fi fi -fi -if [[ "$SWIFTLY_INSTALL_SYSTEM_DEPS" != "false" ]]; then - echo "" - echo "Installing Swift's system dependencies via $package_manager (note: this may require root access)..." - install_system_deps + if [[ "$SWIFTLY_INSTALL_SYSTEM_DEPS" != "false" ]]; then + echo "" + echo "Installing Swift's system dependencies via $package_manager (note: this may require root access)..." + install_system_deps + fi fi echo "" diff --git a/install/tests/overwrite.sh b/install/tests/overwrite.sh index 9f5b16ed..a1c76082 100755 --- a/install/tests/overwrite.sh +++ b/install/tests/overwrite.sh @@ -32,7 +32,7 @@ echo "$DUMMY_CONFIG_CONTENTS" > "$SWIFTLY_HOME_DIR/config.json" mkdir "$SWIFTLY_HOME_DIR/toolchains/5.7.3" # Attempt the same installation, but decline to overwrite. -printf "1\nn\n" | ./swiftly-install.sh +./swiftly-install.sh -y --no-install-system-deps if ! has_command "swiftly" ; then test_fail "Can't find swiftly on the PATH" @@ -48,7 +48,7 @@ if [[ ! -d "$SWIFTLY_HOME_DIR/toolchains/5.7.3" ]]; then fi # Attempt the same installation, but overwrite this time. -printf "1\ny\n" | ./swiftly-install.sh --no-install-system-deps +./swiftly-install.sh -y --overwrite --no-install-system-deps if ! has_command "swiftly" ; then test_fail "Can't find swiftly on the PATH" From 54d24af46a1f62b462ebc1d2b62b354b52146f03 Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Sat, 9 Dec 2023 22:52:23 -0500 Subject: [PATCH 2/4] clean up existing symlinks when overwriting installation --- install/swiftly-install.sh | 1 + install/test-util.sh | 6 ++++++ install/tests/overwrite.sh | 25 +++++++++++++++++++++---- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/install/swiftly-install.sh b/install/swiftly-install.sh index 8f28ba8c..4799da40 100755 --- a/install/swiftly-install.sh +++ b/install/swiftly-install.sh @@ -547,6 +547,7 @@ if [[ -d "$HOME_DIR" ]]; then detected_existing_installation="true" if [[ "$overwrite_existing_intallation" == "true" ]]; then echo "Overwriting existing swiftly installation at $(replace_home_path $HOME_DIR)" + find $BIN_DIR -lname "$HOME_DIR/toolchains/**/bin/*" -delete rm -r $HOME_DIR else echo "Updating existing swiftly installation at $(replace_home_path $HOME_DIR)" diff --git a/install/test-util.sh b/install/test-util.sh index f86d5d53..e309466e 100644 --- a/install/test-util.sh +++ b/install/test-util.sh @@ -4,6 +4,12 @@ export SWIFTLY_READ_FROM_STDIN=1 +test_log () { + echo "===========================" + echo "$1" + echo "===========================" +} + has_command () { command -v "$1" > /dev/null } diff --git a/install/tests/overwrite.sh b/install/tests/overwrite.sh index a1c76082..7a935115 100755 --- a/install/tests/overwrite.sh +++ b/install/tests/overwrite.sh @@ -7,16 +7,18 @@ set -o errexit source ./test-util.sh export SWIFTLY_HOME_DIR="./overwrite-test-home" -export SWIFTLY_BIN_DIR="$SWIFTLY_HOME_DIR/bin" +export SWIFTLY_BIN_DIR="./overwrite-bin-dir" cp "$HOME/.profile" "$HOME/.profile.bak" cleanup () { mv "$HOME/.profile.bak" "$HOME/.profile" rm -r "$SWIFTLY_HOME_DIR" + rm -r "$SWIFTLY_BIN_DIR" } trap cleanup EXIT +test_log "Performing initial installation" ./swiftly-install.sh -y --no-install-system-deps . "$SWIFTLY_HOME_DIR/env.sh" @@ -29,9 +31,16 @@ fi DUMMY_CONFIG_CONTENTS="hello world" PROFILE_CONTENTS="$(cat $HOME/.profile)" echo "$DUMMY_CONFIG_CONTENTS" > "$SWIFTLY_HOME_DIR/config.json" -mkdir "$SWIFTLY_HOME_DIR/toolchains/5.7.3" -# Attempt the same installation, but decline to overwrite. +toolchain_dir="$SWIFTLY_HOME_DIR/toolchains/5.7.3" +mkdir -p "$toolchain_dir/usr/bin" +dummy_executable_name="foo" +touch "$toolchain_dir/usr/bin/$dummy_executable_name" + +# Also set up a symlink as if the toolchain were in use. +ln -s -t $SWIFTLY_BIN_DIR "$toolchain_dir/usr/bin/$dummy_executable_name" + +test_log "Attempting the same installation (no --overwrite flag specified)" ./swiftly-install.sh -y --no-install-system-deps if ! has_command "swiftly" ; then @@ -43,11 +52,15 @@ if [[ "$NEW_CONFIG_CONTENTS" != "$DUMMY_CONFIG_CONTENTS" ]]; then test_fail "Expected config to remain unchanged" "$NEW_CONFIG_CONTENTS" "$DUMMY_CONFIG_CONTENTS" fi +if ! [ -L "$SWIFTLY_BIN_DIR/$dummy_executable_name" ]; then + test_fail "Expected symlink to still exist, but it has been deleted" +fi + if [[ ! -d "$SWIFTLY_HOME_DIR/toolchains/5.7.3" ]]; then test_fail "Expected installed toolchain directory to still exist, but it has been deleted" fi -# Attempt the same installation, but overwrite this time. +test_log "Attempting the same installation (--overwrite flag is specified)" ./swiftly-install.sh -y --overwrite --no-install-system-deps if ! has_command "swiftly" ; then @@ -67,6 +80,10 @@ if [[ -d "$SWIFTLY_HOME_DIR/toolchains/5.7.3" ]]; then test_fail "Expected installed toolchain directory to have been overwritten, but it still exists" fi +if [ -L "$SWIFTLY_BIN_DIR/$dummy_executable_name" ]; then + test_fail "Expected symlink to have been deleted, but it still exists" +fi + swiftly --version test_pass From 9f3540f9236b941d971e38773bdf724361652541 Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Sat, 9 Dec 2023 23:30:21 -0500 Subject: [PATCH 3/4] import swiftly-install.sh test output --- install/run-tests.sh | 13 ++++++++----- install/test-util.sh | 8 ++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/install/run-tests.sh b/install/run-tests.sh index 76a5db8b..f8eea39c 100755 --- a/install/run-tests.sh +++ b/install/run-tests.sh @@ -17,19 +17,18 @@ fi tests_failed=0 tests_passed=0 +failed_tests=() for t in tests/*.sh; do + test_name=$(basename "$t") line_print - echo "Running test $t" + echo "Running test $test_name" echo "" if bash "$t"; then - echo "" - echo "$t PASSED" ((tests_passed++)) else - echo "" - echo "$t FAILED" ((tests_failed++)) + failed_tests+=("$test_name") fi done @@ -38,6 +37,10 @@ line_print if [[ "$tests_failed" -gt 0 ]]; then echo "" echo "$tests_failed test(s) FAILED, $tests_passed test(s) PASSED" + echo "Failed tests:" + for failed_test in "${failed_tests[@]}"; do + echo "- $failed_test" + done exit 1 else echo "All tests PASSED" diff --git a/install/test-util.sh b/install/test-util.sh index e309466e..f2c2ffad 100644 --- a/install/test-util.sh +++ b/install/test-util.sh @@ -14,6 +14,10 @@ has_command () { command -v "$1" > /dev/null } +test_name () { + basename "$0" +} + test_fail () { if [ ! -z "$1" ]; then printf "$1\n" @@ -23,10 +27,14 @@ test_fail () { printf "actual: $2\n" printf "expected: $3\n" fi + echo "" + echo "$(test_name) FAILED" exit 1 } test_pass () { + echo "" + echo "$(test_name) PASSED" exit 0 } From 027af63eb18be5c6f45bee6fe26016e804037e20 Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Sat, 9 Dec 2023 23:30:40 -0500 Subject: [PATCH 4/4] update existing tests for new default overwrite behavior --- install/tests/disable-prompt.sh | 17 +---------------- install/tests/platform-option.sh | 2 +- install/tests/update-bash-profile.sh | 2 +- 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/install/tests/disable-prompt.sh b/install/tests/disable-prompt.sh index 383b93c4..fbe66807 100755 --- a/install/tests/disable-prompt.sh +++ b/install/tests/disable-prompt.sh @@ -29,22 +29,7 @@ bash --login -c "swiftly --version" . "$HOME/.local/share/swiftly/env.sh" if ! has_command "swiftly" ; then - fail_test "Can't find swiftly on the PATH" -fi - -DUMMY_CONTENT="should be overwritten" -echo "$DUMMY_CONTENT" > "$HOME/.local/share/swiftly/config.json" - -# Running it again should overwrite the previous installation without asking us for permission. -./swiftly-install.sh --disable-confirmation - -if ! has_command "swiftly" ; then - fail_test "Can't find swiftly on the PATH" -fi - -CONFIG_CONTENTS="$(cat $HOME/.local/share/swiftly/config.json)" -if [ "$CONFIG_CONTENTS" == "$DUMMY_CONTENT" ]; then - fail_test "Config should have been overwritten after second install" + test_fail "Can't find swiftly on the PATH" fi if has_command dpkg ; then diff --git a/install/tests/platform-option.sh b/install/tests/platform-option.sh index 34c4a026..fe776f93 100755 --- a/install/tests/platform-option.sh +++ b/install/tests/platform-option.sh @@ -17,7 +17,7 @@ trap cleanup EXIT platforms=("ubuntu22.04" "ubuntu20.04" "ubuntu18.04" "amazonlinux2" "rhel9") for platform in "${platforms[@]}"; do - ./swiftly-install.sh --disable-confirmation --no-install-system-deps --platform "$platform" + ./swiftly-install.sh --overwrite --disable-confirmation --no-install-system-deps --platform "$platform" cat $HOME/.local/share/swiftly/config.json if [[ "$platform" == "rhel9" ]]; then diff --git a/install/tests/update-bash-profile.sh b/install/tests/update-bash-profile.sh index d7bb52ff..ff89b805 100755 --- a/install/tests/update-bash-profile.sh +++ b/install/tests/update-bash-profile.sh @@ -34,7 +34,7 @@ if [[ "$(cat $HOME/.bash_login)" != "" ]]; then fi rm "$HOME/.bash_profile" -printf "1\ny\n" | ./swiftly-install.sh --no-install-system-deps +./swiftly-install.sh -y --overwrite --no-install-system-deps if [[ -f "$HOME/.bash_profile" ]]; then test_fail "install created .bash_profile when it should not have"