From 60e575ae673e3a9543aafdcbe81ced83a470991b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Sat, 26 Oct 2013 02:21:46 +0200 Subject: [PATCH] Improve installer script * Shorter. * Preserve timestamps and permissions. * Overwrite existing files. --- install.sh | 17 ++++++---------- test/installer.bats | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 test/installer.bats diff --git a/install.sh b/install.sh index fa077982..de2473b7 100755 --- a/install.sh +++ b/install.sh @@ -1,4 +1,7 @@ #!/bin/sh +# Usage: PREFIX=/usr/local ./install.sh +# +# Installs ruby-build under $PREFIX. set -e @@ -11,15 +14,7 @@ fi BIN_PATH="${PREFIX}/bin" SHARE_PATH="${PREFIX}/share/ruby-build" -mkdir -p "${BIN_PATH}" -mkdir -p "${SHARE_PATH}" +mkdir -p "$BIN_PATH" "$SHARE_PATH" -for file in bin/*; do - cp "${file}" "${BIN_PATH}" -done - -for file in share/ruby-build/*; do - cp "${file}" "${SHARE_PATH}" -done - -echo "Installed ruby-build at ${PREFIX}" +install -p bin/* "$BIN_PATH" +install -p share/ruby-build/* "$SHARE_PATH" diff --git a/test/installer.bats b/test/installer.bats new file mode 100644 index 00000000..14caadba --- /dev/null +++ b/test/installer.bats @@ -0,0 +1,49 @@ +#!/usr/bin/env bats + +load test_helper + +@test "installs ruby-build into PREFIX" { + cd "$TMP" + PREFIX="${PWD}/usr" run "${BATS_TEST_DIRNAME}/../install.sh" + assert_success "" + + cd usr + + assert [ -x bin/ruby-build ] + assert [ -x bin/rbenv-install ] + assert [ -x bin/rbenv-uninstall ] + + assert [ -e share/ruby-build/1.8.6-p383 ] + assert [ -e share/ruby-build/ree-1.8.7-2012.02 ] +} + +@test "overwrites old installation" { + cd "$TMP" + mkdir -p bin share/ruby-build + touch bin/ruby-build + touch share/ruby-build/1.8.6-p383 + + PREFIX="$PWD" run "${BATS_TEST_DIRNAME}/../install.sh" + assert_success "" + + assert [ -x bin/ruby-build ] + run grep "install_package" share/ruby-build/1.8.6-p383 + assert_success +} + +@test "unrelated files are untouched" { + cd "$TMP" + mkdir -p bin share/bananas + chmod g-w bin + touch bin/bananas + touch share/bananas/docs + + PREFIX="$PWD" run "${BATS_TEST_DIRNAME}/../install.sh" + assert_success "" + + assert [ -e bin/bananas ] + assert [ -e share/bananas/docs ] + + run ls -ld bin + assert_equal "r-x" "${output:4:3}" +}