Skip to content

Commit d14ced0

Browse files
authored
Support updating exsting swiftly installation from swiftly-install.sh (#86)
1 parent 2e35b41 commit d14ced0

File tree

7 files changed

+82
-57
lines changed

7 files changed

+82
-57
lines changed

install/run-tests.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ fi
1717

1818
tests_failed=0
1919
tests_passed=0
20+
failed_tests=()
2021

2122
for t in tests/*.sh; do
23+
test_name=$(basename "$t")
2224
line_print
23-
echo "Running test $t"
25+
echo "Running test $test_name"
2426
echo ""
2527
if bash "$t"; then
26-
echo ""
27-
echo "$t PASSED"
2828
((tests_passed++))
2929
else
30-
echo ""
31-
echo "$t FAILED"
3230
((tests_failed++))
31+
failed_tests+=("$test_name")
3332
fi
3433
done
3534

@@ -38,6 +37,10 @@ line_print
3837
if [[ "$tests_failed" -gt 0 ]]; then
3938
echo ""
4039
echo "$tests_failed test(s) FAILED, $tests_passed test(s) PASSED"
40+
echo "Failed tests:"
41+
for failed_test in "${failed_tests[@]}"; do
42+
echo "- $failed_test"
43+
done
4144
exit 1
4245
else
4346
echo "All tests PASSED"

install/swiftly-install.sh

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ set -o errexit
335335
shopt -s extglob
336336

337337
short_options='yhvp:'
338-
long_options='disable-confirmation,no-modify-profile,no-install-system-deps,help,version,platform:'
338+
long_options='disable-confirmation,no-modify-profile,no-install-system-deps,help,version,platform:,overwrite'
339339

340340
args=$(getopt --options "$short_options" --longoptions "$long_options" --name "swiftly-install" -- "${@}")
341341
eval "set -- ${args}"
@@ -351,14 +351,18 @@ USAGE:
351351
swiftly-install [options]
352352
353353
OPTIONS:
354-
-y, --disable-confirmation Disable confirmation prompt.
354+
-y, --disable-confirmation Disable confirmation prompts.
355355
--no-modify-profile Do not attempt to modify the profile file to set environment
356356
variables (e.g. PATH) on login.
357357
--no-install-system-deps Do not attempt to install Swift's required system dependencies.
358358
-p, --platform <platform> Specifies which platform's toolchains swiftly will download. If
359359
unspecified, the platform will be automatically detected. Available
360360
options are "ubuntu22.04", "ubuntu20.04", "ubuntu18.04", "rhel9", and
361361
"amazonlinux2".
362+
--overwrite Overwrite the existing swiftly installation found at the configured
363+
SWIFTLY_HOME, if any. If this option is unspecified and an existing
364+
installation is found, the swiftly executable will be updated, but
365+
the rest of the installation will not be modified.
362366
-h, --help Prints help information.
363367
--version Prints version information.
364368
EOF
@@ -415,6 +419,11 @@ EOF
415419
shift 2
416420
;;
417421

422+
"--overwrite")
423+
overwrite_existing_intallation="true"
424+
shift
425+
;;
426+
418427
--)
419428
shift
420429
break
@@ -535,19 +544,14 @@ while [ -z "$DISABLE_CONFIRMATION" ]; do
535544
done
536545

537546
if [[ -d "$HOME_DIR" ]]; then
538-
if [[ "$DISABLE_CONFIRMATION" == "true" ]]; then
547+
detected_existing_installation="true"
548+
if [[ "$overwrite_existing_intallation" == "true" ]]; then
539549
echo "Overwriting existing swiftly installation at $(replace_home_path $HOME_DIR)"
550+
find $BIN_DIR -lname "$HOME_DIR/toolchains/**/bin/*" -delete
551+
rm -r $HOME_DIR
540552
else
541-
echo "Existing swiftly installation detected at $(replace_home_path $HOME_DIR), overwrite? (Y/n)"
542-
543-
read_yn_input "true"
544-
if [[ "$READ_INPUT_RETURN" == "false" ]]; then
545-
echo "Cancelling installation."
546-
exit 0
547-
fi
553+
echo "Updating existing swiftly installation at $(replace_home_path $HOME_DIR)"
548554
fi
549-
550-
rm -r $HOME_DIR
551555
fi
552556

553557
mkdir -p $HOME_DIR/toolchains
@@ -566,35 +570,37 @@ curl \
566570

567571
chmod +x "$BIN_DIR/swiftly"
568572

569-
echo "$JSON_OUT" > "$HOME_DIR/config.json"
573+
if [[ "$detected_existing_installation" != "true" || "$overwrite_existing_intallation" == "true" ]]; then
574+
echo "$JSON_OUT" > "$HOME_DIR/config.json"
570575

571-
# Verify the downloaded executable works. The script will exit if this fails due to errexit.
572-
SWIFTLY_HOME_DIR="$HOME_DIR" SWIFTLY_BIN_DIR="$BIN_DIR" "$BIN_DIR/swiftly" --version > /dev/null
576+
# Verify the downloaded executable works. The script will exit if this fails due to errexit.
577+
SWIFTLY_HOME_DIR="$HOME_DIR" SWIFTLY_BIN_DIR="$BIN_DIR" "$BIN_DIR/swiftly" --version > /dev/null
573578

574-
ENV_OUT=$(cat <<EOF
579+
ENV_OUT=$(cat <<EOF
575580
export SWIFTLY_HOME_DIR="$(replace_home_path $HOME_DIR)"
576581
export SWIFTLY_BIN_DIR="$(replace_home_path $BIN_DIR)"
577582
if [[ ":\$PATH:" != *":\$SWIFTLY_BIN_DIR:"* ]]; then
578583
export PATH="\$SWIFTLY_BIN_DIR:\$PATH"
579584
fi
580585
EOF
581-
)
586+
)
582587

583-
echo "$ENV_OUT" > "$HOME_DIR/env.sh"
588+
echo "$ENV_OUT" > "$HOME_DIR/env.sh"
584589

585-
if [[ "$MODIFY_PROFILE" == "true" ]]; then
586-
SOURCE_LINE=". $(replace_home_path $HOME_DIR)/env.sh"
590+
if [[ "$MODIFY_PROFILE" == "true" ]]; then
591+
SOURCE_LINE=". $(replace_home_path $HOME_DIR)/env.sh"
587592

588-
# Only append the line if it isn't in .profile already.
589-
if [[ ! -f "$PROFILE_FILE" ]] || [[ ! "$(cat $PROFILE_FILE)" =~ "$SOURCE_LINE" ]]; then
590-
echo "$SOURCE_LINE" >> "$PROFILE_FILE"
593+
# Only append the line if it isn't in .profile already.
594+
if [[ ! -f "$PROFILE_FILE" ]] || [[ ! "$(cat $PROFILE_FILE)" =~ "$SOURCE_LINE" ]]; then
595+
echo "$SOURCE_LINE" >> "$PROFILE_FILE"
596+
fi
591597
fi
592-
fi
593598

594-
if [[ "$SWIFTLY_INSTALL_SYSTEM_DEPS" != "false" ]]; then
595-
echo ""
596-
echo "Installing Swift's system dependencies via $package_manager (note: this may require root access)..."
597-
install_system_deps
599+
if [[ "$SWIFTLY_INSTALL_SYSTEM_DEPS" != "false" ]]; then
600+
echo ""
601+
echo "Installing Swift's system dependencies via $package_manager (note: this may require root access)..."
602+
install_system_deps
603+
fi
598604
fi
599605

600606
echo ""

install/test-util.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@
44

55
export SWIFTLY_READ_FROM_STDIN=1
66

7+
test_log () {
8+
echo "==========================="
9+
echo "$1"
10+
echo "==========================="
11+
}
12+
713
has_command () {
814
command -v "$1" > /dev/null
915
}
1016

17+
test_name () {
18+
basename "$0"
19+
}
20+
1121
test_fail () {
1222
if [ ! -z "$1" ]; then
1323
printf "$1\n"
@@ -17,10 +27,14 @@ test_fail () {
1727
printf "actual: $2\n"
1828
printf "expected: $3\n"
1929
fi
30+
echo ""
31+
echo "$(test_name) FAILED"
2032
exit 1
2133
}
2234

2335
test_pass () {
36+
echo ""
37+
echo "$(test_name) PASSED"
2438
exit 0
2539
}
2640

install/tests/disable-prompt.sh

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,7 @@ bash --login -c "swiftly --version"
2929
. "$HOME/.local/share/swiftly/env.sh"
3030

3131
if ! has_command "swiftly" ; then
32-
fail_test "Can't find swiftly on the PATH"
33-
fi
34-
35-
DUMMY_CONTENT="should be overwritten"
36-
echo "$DUMMY_CONTENT" > "$HOME/.local/share/swiftly/config.json"
37-
38-
# Running it again should overwrite the previous installation without asking us for permission.
39-
./swiftly-install.sh --disable-confirmation
40-
41-
if ! has_command "swiftly" ; then
42-
fail_test "Can't find swiftly on the PATH"
43-
fi
44-
45-
CONFIG_CONTENTS="$(cat $HOME/.local/share/swiftly/config.json)"
46-
if [ "$CONFIG_CONTENTS" == "$DUMMY_CONTENT" ]; then
47-
fail_test "Config should have been overwritten after second install"
32+
test_fail "Can't find swiftly on the PATH"
4833
fi
4934

5035
if has_command dpkg ; then

install/tests/overwrite.sh

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ set -o errexit
77
source ./test-util.sh
88

99
export SWIFTLY_HOME_DIR="./overwrite-test-home"
10-
export SWIFTLY_BIN_DIR="$SWIFTLY_HOME_DIR/bin"
10+
export SWIFTLY_BIN_DIR="./overwrite-bin-dir"
1111

1212
cp "$HOME/.profile" "$HOME/.profile.bak"
1313

1414
cleanup () {
1515
mv "$HOME/.profile.bak" "$HOME/.profile"
1616
rm -r "$SWIFTLY_HOME_DIR"
17+
rm -r "$SWIFTLY_BIN_DIR"
1718
}
1819
trap cleanup EXIT
1920

21+
test_log "Performing initial installation"
2022
./swiftly-install.sh -y --no-install-system-deps
2123

2224
. "$SWIFTLY_HOME_DIR/env.sh"
@@ -29,10 +31,17 @@ fi
2931
DUMMY_CONFIG_CONTENTS="hello world"
3032
PROFILE_CONTENTS="$(cat $HOME/.profile)"
3133
echo "$DUMMY_CONFIG_CONTENTS" > "$SWIFTLY_HOME_DIR/config.json"
32-
mkdir "$SWIFTLY_HOME_DIR/toolchains/5.7.3"
3334

34-
# Attempt the same installation, but decline to overwrite.
35-
printf "1\nn\n" | ./swiftly-install.sh
35+
toolchain_dir="$SWIFTLY_HOME_DIR/toolchains/5.7.3"
36+
mkdir -p "$toolchain_dir/usr/bin"
37+
dummy_executable_name="foo"
38+
touch "$toolchain_dir/usr/bin/$dummy_executable_name"
39+
40+
# Also set up a symlink as if the toolchain were in use.
41+
ln -s -t $SWIFTLY_BIN_DIR "$toolchain_dir/usr/bin/$dummy_executable_name"
42+
43+
test_log "Attempting the same installation (no --overwrite flag specified)"
44+
./swiftly-install.sh -y --no-install-system-deps
3645

3746
if ! has_command "swiftly" ; then
3847
test_fail "Can't find swiftly on the PATH"
@@ -43,12 +52,16 @@ if [[ "$NEW_CONFIG_CONTENTS" != "$DUMMY_CONFIG_CONTENTS" ]]; then
4352
test_fail "Expected config to remain unchanged" "$NEW_CONFIG_CONTENTS" "$DUMMY_CONFIG_CONTENTS"
4453
fi
4554

55+
if ! [ -L "$SWIFTLY_BIN_DIR/$dummy_executable_name" ]; then
56+
test_fail "Expected symlink to still exist, but it has been deleted"
57+
fi
58+
4659
if [[ ! -d "$SWIFTLY_HOME_DIR/toolchains/5.7.3" ]]; then
4760
test_fail "Expected installed toolchain directory to still exist, but it has been deleted"
4861
fi
4962

50-
# Attempt the same installation, but overwrite this time.
51-
printf "1\ny\n" | ./swiftly-install.sh --no-install-system-deps
63+
test_log "Attempting the same installation (--overwrite flag is specified)"
64+
./swiftly-install.sh -y --overwrite --no-install-system-deps
5265

5366
if ! has_command "swiftly" ; then
5467
test_fail "Can't find swiftly on the PATH"
@@ -67,6 +80,10 @@ if [[ -d "$SWIFTLY_HOME_DIR/toolchains/5.7.3" ]]; then
6780
test_fail "Expected installed toolchain directory to have been overwritten, but it still exists"
6881
fi
6982

83+
if [ -L "$SWIFTLY_BIN_DIR/$dummy_executable_name" ]; then
84+
test_fail "Expected symlink to have been deleted, but it still exists"
85+
fi
86+
7087
swiftly --version
7188

7289
test_pass

install/tests/platform-option.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ trap cleanup EXIT
1717
platforms=("ubuntu22.04" "ubuntu20.04" "ubuntu18.04" "amazonlinux2" "rhel9")
1818

1919
for platform in "${platforms[@]}"; do
20-
./swiftly-install.sh --disable-confirmation --no-install-system-deps --platform "$platform"
20+
./swiftly-install.sh --overwrite --disable-confirmation --no-install-system-deps --platform "$platform"
2121
cat $HOME/.local/share/swiftly/config.json
2222

2323
if [[ "$platform" == "rhel9" ]]; then

install/tests/update-bash-profile.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if [[ "$(cat $HOME/.bash_login)" != "" ]]; then
3434
fi
3535

3636
rm "$HOME/.bash_profile"
37-
printf "1\ny\n" | ./swiftly-install.sh --no-install-system-deps
37+
./swiftly-install.sh -y --overwrite --no-install-system-deps
3838

3939
if [[ -f "$HOME/.bash_profile" ]]; then
4040
test_fail "install created .bash_profile when it should not have"

0 commit comments

Comments
 (0)