From 801f762593a5db03d6158449ec5cb15d954c6953 Mon Sep 17 00:00:00 2001 From: Yusuke Izawa Date: Wed, 16 Aug 2017 13:09:05 +0900 Subject: [PATCH 1/7] Implement the test for scalaenv-init and fix scalaenv-init --- libexec/scalaenv-init | 32 ++++----- test/init.bats | 108 ++++++++++++++++++++++++++++ test/libexec/scalaenv-echo | 9 +++ test/test_helper.bash | 140 +++++++++++++++++++++++++++++++++++++ 4 files changed, 273 insertions(+), 16 deletions(-) create mode 100644 test/init.bats create mode 100644 test/libexec/scalaenv-echo create mode 100644 test/test_helper.bash diff --git a/libexec/scalaenv-init b/libexec/scalaenv-init index 92c148d..1ffdd88 100755 --- a/libexec/scalaenv-init +++ b/libexec/scalaenv-init @@ -1,5 +1,6 @@ #!/usr/bin/env bash # Summary: Configure the shell environment for scalaenv +# Usage: eval "$(scalaenv init - [--no-rehash] [])" set -e test -n "${SCALAENV_DEBUG}" && set -x @@ -31,10 +32,11 @@ done shell="$1" if [ -z "$shell" ]; then - shell="$(ps c -p "${PPID}" -o 'ucomm=' 2> /dev/null || true)" - shell="${shell##-}" + shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)" shell="${shell%% *}" - shell="$(basename "${shell:-${SHELL}}")" + shell="${shell##-}" + shell="${shell:-$SHELL}" + shell="${shell##*/}" fi READLINK=$(type -p greadlink readlink | head -1) @@ -83,18 +85,16 @@ fi mkdir -p "${SCALAENV_ROOT}/"{shims,versions} -if [[ ":${PATH}:" != *:"${SCALAENV_ROOT}/shims":* ]]; then - case "$shell" in - fish ) - echo 'set -gx PATH "'${SCALAENV_ROOT}'/shims"' '$PATH' ';' - echo "set -gx SCALAENV_SHELL $shell" - ;; - * ) - echo 'export PATH="'${SCALAENV_ROOT}'/shims:${PATH}"' - echo "export SCALAENV_SHELL=$shell" - ;; - esac -fi +case "$shell" in + fish ) + echo "set -gx PATH '${SCALAENV_ROOT}/shims' \$PATH" + echo "set -gx SCALAENV_SHELL $shell" + ;; + * ) + echo 'export PATH="'${SCALAENV_ROOT}'/shims:${PATH}"' + echo "export SCALAENV_SHELL=$shell" + ;; +esac completion="${root}/completions/scalaenv.${shell}" if [ -r "$completion" ]; then @@ -102,7 +102,7 @@ if [ -r "$completion" ]; then fi if [ -z "${no_rehash}" ]; then - echo 'scalaenv rehash 2> /dev/null' + echo 'command scalaenv rehash 2>/dev/null' fi commands=(`scalaenv-commands --sh`) diff --git a/test/init.bats b/test/init.bats new file mode 100644 index 0000000..e1b8f0d --- /dev/null +++ b/test/init.bats @@ -0,0 +1,108 @@ +#!/usr/bin/env bats + +load test_helper + +@test "creates shims and versions directories" { + assert [ ! -d "${SCALAENV_ROOT}/shims" ] + assert [ ! -d "${SCALAENV_ROOT}/versions" ] + run scalaenv-init - + assert_success + assert [ -d "${SCALAENV_ROOT}/shims" ] + assert [ -d "${SCALAENV_ROOT}/versions" ] +} + +@test "auto rehash" { + run scalaenv-init - + assert_success + assert_line "command scalaenv rehash 2>/dev/null" +} + +@test "detect parent shell" { + SHELL=/bin/false run scalaenv-init - + assert_success + assert_line "export SCALAENV_SHELL=bash" +} + +@test "detect parent shell from script" { + mkdir -p "$SCALAENV_TEST_DIR" + cd "$SCALAENV_TEST_DIR" + cat > myscript.sh </dev/null" +} + +@test "adds shims to PATH" { + export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin:/usr/local/bin" + run scalaenv-init - bash + assert_success + assert_line 0 'export PATH="'${SCALAENV_ROOT}'/shims:${PATH}"' +} + +@test "adds shims to PATH (fish)" { + export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin:/usr/local/bin" + run scalaenv-init - fish + assert_success + assert_line 0 "set -gx PATH '${SCALAENV_ROOT}/shims' \$PATH" +} + +@test "can add shims to PATH more than once" { + export PATH="${SCALAENV_ROOT}/shims:$PATH" + run scalaenv-init - bash + assert_success + assert_line 0 'export PATH="'${SCALAENV_ROOT}'/shims:${PATH}"' +} + +@test "can add shims to PATH more than once (fish)" { + export PATH="${SCALAENV_ROOT}/shims:$PATH" + run scalaenv-init - fish + assert_success + assert_line 0 "set -gx PATH '${SCALAENV_ROOT}/shims' \$PATH" +} + +@test "outputs sh-compatible syntax" { + run scalaenv-init - bash + assert_success + assert_line ' case "$command" in' + + run scalaenv-init - zsh + assert_success + assert_line ' case "$command" in' +} + +@test "outputs fish-specific syntax (fish)" { + run scalaenv-init - fish + assert_success + assert_line ' switch "$command"' + refute_line ' case "$command" in' +} diff --git a/test/libexec/scalaenv-echo b/test/libexec/scalaenv-echo new file mode 100644 index 0000000..3ae1eaf --- /dev/null +++ b/test/libexec/scalaenv-echo @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +# Usage: scalaenv echo [-F] VAR + +if [[ $1 == -F* ]]; then + sep="${1:2}" + echo "${!2}" | tr "${sep:-:}" $'\n' +else + echo "${!1}" +fi \ No newline at end of file diff --git a/test/test_helper.bash b/test/test_helper.bash new file mode 100644 index 0000000..b62057b --- /dev/null +++ b/test/test_helper.bash @@ -0,0 +1,140 @@ +unset SCALAENV_VERSION +unset SCALAENV_DIR + +# guard against executing this block twice due to bats internals +if [ -z "$SCALAENV_TEST_DIR" ]; then + SCALAENV_TEST_DIR="${BATS_TMPDIR}/scalaenv" + export SCALAENV_TEST_DIR="$(mktemp -d "${SCALAENV_TEST_DIR}.XXX" 2>/dev/null || echo "$SCALAENV_TEST_DIR")" + + if enable -f "${BATS_TEST_DIRNAME}"/../libexec/scalaenv-realpath.dylib realpath 2>/dev/null; then + export SCALAENV_TEST_DIR="$(realpath "$SCALAENV_TEST_DIR")" + else + if [ -n "$SCALAENV_NATIVE_EXT" ]; then + echo "scalaenv: failed to load \`realpath' builtin" >&2 + exit 1 + fi + fi + + export SCALAENV_ROOT="${SCALAENV_TEST_DIR}/root" + export HOME="${SCALAENV_TEST_DIR}/home" + export SCALAENV_HOOK_PATH="${SCALAENV_ROOT}/scalaenv.d" + + PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin + PATH="${SCALAENV_TEST_DIR}/bin:$PATH" + PATH="${BATS_TEST_DIRNAME}/../libexec:$PATH" + PATH="${BATS_TEST_DIRNAME}/libexec:$PATH" + PATH="${SCALAENV_ROOT}/shims:$PATH" + export PATH + + for xdg_var in `env 2>/dev/null | grep ^XDG_ | cut -d= -f1`; do unset "$xdg_var"; done + unset xdg_var +fi + +teardown() { + rm -rf "$SCALAENV_TEST_DIR" +} + +flunk() { + { if [ "$#" -eq 0 ]; then cat - + else echo "$@" + fi + } | sed "s:${SCALAENV_TEST_DIR}:TEST_DIR:g" >&2 + return 1 +} + +assert_success() { + if [ "$status" -ne 0 ]; then + flunk "command failed with exit status $status" + elif [ "$#" -gt 0 ]; then + assert_output "$1" + fi +} + +assert_failure() { + if [ "$status" -eq 0 ]; then + flunk "expected failed exit status" + elif [ "$#" -gt 0 ]; then + assert_output "$1" + fi +} + +assert_equal() { + if [ "$1" != "$2" ]; then + { echo "expected: $1" + echo "actual: $2" + } | flunk + fi +} + +assert_output() { + local expected + if [ $# -eq 0 ]; then expected="$(cat -)" + else expected="$1" + fi + assert_equal "$expected" "$output" +} + +assert_line() { + if [ "$1" -ge 0 ] 2>/dev/null; then + assert_equal "$2" "${lines[$1]}" + else + local line + for line in "${lines[@]}"; do + if [ "$line" = "$1" ]; then return 0; fi + done + flunk "expected line \`$1'" + fi +} + +refute_line() { + if [ "$1" -ge 0 ] 2>/dev/null; then + local num_lines="${#lines[@]}" + if [ "$1" -lt "$num_lines" ]; then + flunk "output has $num_lines lines" + fi + else + local line + for line in "${lines[@]}"; do + if [ "$line" = "$1" ]; then + flunk "expected to not find line \`$line'" + fi + done + fi +} + +assert() { + if ! "$@"; then + flunk "failed: $@" + fi +} + +# Output a modified PATH that ensures that the given executable is not present, +# but in which system utils necessary for scalaenv operation are still available. +path_without() { + local exe="$1" + local path=":${PATH}:" + local found alt util + for found in $(which -a "$exe"); do + found="${found%/*}" + if [ "$found" != "${SCALAENV_ROOT}/shims" ]; then + alt="${SCALAENV_TEST_DIR}/$(echo "${found#/}" | tr '/' '-')" + mkdir -p "$alt" + for util in bash head cut readlink greadlink; do + if [ -x "${found}/$util" ]; then + ln -s "${found}/$util" "${alt}/$util" + fi + done + path="${path/:${found}:/:${alt}:}" + fi + done + path="${path#:}" + echo "${path%:}" +} + +create_hook() { + mkdir -p "${SCALAENV_HOOK_PATH}/$1" + touch "${SCALAENV_HOOK_PATH}/$1/$2" + if [ ! -t 0 ]; then + cat > "${SCALAENV_HOOK_PATH}/$1/$2" + fi +} From 316aa461cbe8a3deec9aa5e6e5565697a1c066a4 Mon Sep 17 00:00:00 2001 From: Yusuke Izawa Date: Sun, 20 Aug 2017 20:32:46 +0900 Subject: [PATCH 2/7] Fix typo --- plugins/scala-install/bin/scalaenv-install | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/scala-install/bin/scalaenv-install b/plugins/scala-install/bin/scalaenv-install index 958e1ed..f856979 100755 --- a/plugins/scala-install/bin/scalaenv-install +++ b/plugins/scala-install/bin/scalaenv-install @@ -100,7 +100,7 @@ done [ "${#ARGUMENTS[@]}" -le 1 ] || usage 1 >&2 DEFINITION="${ARGUMENTS[0]}" -[ -n "$DEFINITION" ] || DEFINITION="$(pyenv-local 2>/dev/null || true)" +[ -n "$DEFINITION" ] || DEFINITION="$(scalaenv-local 2>/dev/null || true)" [ -n "$DEFINITION" ] || usage 1 >&2 [ -n "$VERSION" ] || VERSION="${DEFINITION##*/}" From 1f5bd5fa7b431857219f20e06ecc393dfb918d1e Mon Sep 17 00:00:00 2001 From: Yusuke Izawa Date: Tue, 22 Aug 2017 21:50:07 +0900 Subject: [PATCH 3/7] Update scalaenv.bash --- completions/scalaenv.bash | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/completions/scalaenv.bash b/completions/scalaenv.bash index 2ee1e06..69696c8 100644 --- a/completions/scalaenv.bash +++ b/completions/scalaenv.bash @@ -1,4 +1,16 @@ _scalaenv() { - COMPREPLY=( $(compgen -W "$(scalaenv commands)" ${COMP_WORDS[COMP_CWORD]}) ) + COMPREPLY=() + local word="${COMP_WORDS[COMP_CWORD]}" + + if [ "$COMP_CWORD" -eq 1 ]; then + COMPREPLY=( $(compgen -W "$(scalaenv commands)" -- "$word") ) + else + local words=("${COMP_WORDS[@]}") + unset words[0] + unset words[$COMP_CWORD] + local completions=$(scalaenv completions "${words[@]}") + COMPREPLY=( $(compgen -W "$completions" -- "$word") ) + fi } + complete -F _scalaenv scalaenv From 0921642985b5aeb0229b907f79c1a1c619e0f5fa Mon Sep 17 00:00:00 2001 From: Keith Date: Sun, 3 Sep 2017 08:37:30 +0800 Subject: [PATCH 4/7] Fix typo in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e26ec4..b0aa9e9 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ $ brew upgrade --fetch-HEAD scalaenv **0.0.15** (Aug 17, 2017) - - Implemented `saclaenv uninstall` + - Implemented `scalaenv uninstall` - Enhanced `scalaenv install` - Added usage for `scalaenv install` - Added completions for `scalaenv install` From 2ede370f3258ba80f16d50963baa98869871baab Mon Sep 17 00:00:00 2001 From: Yusuke Izawa Date: Sat, 21 Oct 2017 16:26:52 +0900 Subject: [PATCH 5/7] Add scala-2.12.4 --- plugins/scala-install/share/scala-2.12.4 | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/scala-install/share/scala-2.12.4 diff --git a/plugins/scala-install/share/scala-2.12.4 b/plugins/scala-install/share/scala-2.12.4 new file mode 100644 index 0000000..5bd1239 --- /dev/null +++ b/plugins/scala-install/share/scala-2.12.4 @@ -0,0 +1,4 @@ +sites=( + "http://www.scala-lang.org/files/archive" + ); +archive_file="scala-2.12.4.tgz" From 3a4c839a969ea1deb786d9d8a16f7e7f4c83f52c Mon Sep 17 00:00:00 2001 From: Yusuke Izawa Date: Sat, 21 Oct 2017 16:28:45 +0900 Subject: [PATCH 6/7] 0.0.16 --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index b0aa9e9..f6873c8 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,10 @@ $ brew upgrade --fetch-HEAD scalaenv ### Version History +0.0.16 (Oct 21, 2017) + + - Added `scala-2.12.4` + **0.0.15** (Aug 17, 2017) - Implemented `scalaenv uninstall` From c646dfa8639e99ebe85158ace33aa462e6a49dff Mon Sep 17 00:00:00 2001 From: Yusuke Izawa Date: Sat, 21 Oct 2017 16:30:06 +0900 Subject: [PATCH 7/7] Fix README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f6873c8..bb3551b 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ $ brew upgrade --fetch-HEAD scalaenv ### Version History -0.0.16 (Oct 21, 2017) +**0.0.16** (Oct 21, 2017) - Added `scala-2.12.4`