Skip to content

Commit

Permalink
fix(version-file): remove legacy global version files
Browse files Browse the repository at this point in the history
  • Loading branch information
pine committed Dec 29, 2015
1 parent 3b0d471 commit 607be62
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 37 deletions.
5 changes: 1 addition & 4 deletions libexec/crenv-global
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,5 @@ CRENV_VERSION_FILE="${CRENV_ROOT}/version"
if [ -n "$CRENV_VERSION" ]; then
crenv-version-file-write "$CRENV_VERSION_FILE" "$CRENV_VERSION"
else
crenv-version-file-read "$CRENV_VERSION_FILE" ||
crenv-version-file-read "${CRENV_ROOT}/global" ||
crenv-version-file-read "${CRENV_ROOT}/default" ||
echo system
crenv-version-file-read "$CRENV_VERSION_FILE" || echo system
fi
30 changes: 16 additions & 14 deletions libexec/crenv-version-file
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@
set -e
[ -n "$CRENV_DEBUG" ] && set -x

target_dir="$1"

find_local_version_file() {
local root="$1"
while [ -n "$root" ]; do
while ! [[ "$root" =~ ^//[^/]*$ ]]; do
if [ -e "${root}/.crystal-version" ]; then
echo "${root}/.crystal-version"
exit
return 0
elif [ -e "${root}/.crenv-version" ]; then
echo "${root}/.crenv-version"
exit
return 0
fi
[ -n "$root" ] || break
root="${root%/*}"
done
return 1
}

find_local_version_file "$CRENV_DIR"
[ "$CRENV_DIR" = "$PWD" ] || find_local_version_file "$PWD"

global_version_file="${CRENV_ROOT}/version"

if [ -e "$global_version_file" ]; then
find_global_version_file() {
local global_version_file="${CRENV_ROOT}/version"
echo "$global_version_file"
elif [ -e "${CRENV_ROOT}/global" ]; then
echo "${CRENV_ROOT}/global"
elif [ -e "${CRENV_ROOT}/default" ]; then
echo "${CRENV_ROOT}/default"
}

if [ -n "$target_dir" ]; then
find_local_version_file "$target_dir"
else
echo "$global_version_file"
find_local_version_file "$CRENV_DIR" || {
[ "$CRENV_DIR" != "$PWD" ] && find_local_version_file "$PWD"
} || find_global_version_file
fi
24 changes: 5 additions & 19 deletions test/version-file.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,15 @@ create_file() {
touch "$1"
}

@test "prints global file if no version files exist" {
assert [ ! -e "${CRENV_ROOT}/version" ]
assert [ ! -e ".crystal-version" ]
@test "detects global 'version' file" {
create_file "${CRENV_ROOT}/version"
run crenv-version-file
assert_success "${CRENV_ROOT}/version"
}

@test "detects 'global' file" {
create_file "${CRENV_ROOT}/global"
run crenv-version-file
assert_success "${CRENV_ROOT}/global"
}

@test "detects 'default' file" {
create_file "${CRENV_ROOT}/default"
run crenv-version-file
assert_success "${CRENV_ROOT}/default"
}

@test "'version' has precedence over 'global' and 'default'" {
create_file "${CRENV_ROOT}/version"
create_file "${CRENV_ROOT}/global"
create_file "${CRENV_ROOT}/default"
@test "prints global file if no version files exist" {
assert [ ! -e "${CRENV_ROOT}/version" ]
assert [ ! -e ".crystal-version" ]
run crenv-version-file
assert_success "${CRENV_ROOT}/version"
}
Expand Down

0 comments on commit 607be62

Please sign in to comment.