Merge pull request #337 from Stratus3D/symlink-tests

Symlink fix and tests
This commit is contained in:
Trevor Brown 2018-06-17 19:37:11 -04:00 committed by GitHub
commit 9a525d6bca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 1 deletions

View File

@ -17,6 +17,11 @@ version_command() {
file="$(pwd)/.tool-versions"
fi
if [ -L "$file" ]; then
# Resolve file path if symlink
file="$(resolve_symlink "$file")"
fi
check_if_plugin_exists "$plugin"
local version
@ -24,6 +29,7 @@ version_command() {
check_if_version_exists "$plugin" "$version"
done
if [ -f "$file" ] && grep "^$plugin " "$file" > /dev/null; then
sed -i.bak -e "s/^$plugin .*$/$plugin ${versions[*]}/" "$file"
rm "$file".bak

View File

@ -354,3 +354,13 @@ find_tool_versions() {
search_path=$(dirname "$search_path")
done
}
resolve_symlink() {
local symlink
symlink="$1"
# This seems to be the only cross-platform way to resolve symlink paths to
# the real file path.
# shellcheck disable=SC2012
ls -l "$symlink" | sed -e 's|.*-> \(.*\)|\1|'
}

View File

@ -2,7 +2,14 @@
load test_helpers
banned_commands=(realpath eval)
banned_commands=(
realpath
# readlink on OSX behaves differently from readlink on other Unix systems
readlink
# It's best to avoid eval as it makes it easier to accidentally execute
# arbitrary strings
eval
)
setup() {
setup_asdf_dir

View File

@ -139,3 +139,47 @@ teardown() {
[ "$(cat $ASDF_DEFAULT_TOOL_VERSIONS_FILENAME)" = "dummy 1.1.0" ]
[ "$(cat $HOME/.tool-versions)" = "" ]
}
@test "local should preserve symlinks when setting versions" {
mkdir other-dir
touch other-dir/.tool-versions
ln -s other-dir/.tool-versions .tool-versions
run local_command "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ -L .tool-versions ]
[ "$(cat other-dir/.tool-versions)" = "dummy 1.1.0" ]
}
@test "local should preserve symlinks when updating versions" {
mkdir other-dir
touch other-dir/.tool-versions
ln -s other-dir/.tool-versions .tool-versions
run local_command "dummy" "1.1.0"
run local_command "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ -L .tool-versions ]
[ "$(cat other-dir/.tool-versions)" = "dummy 1.1.0" ]
}
@test "global should preserve symlinks when setting versions" {
mkdir other-dir
touch other-dir/.tool-versions
ln -s other-dir/.tool-versions $HOME/.tool-versions
run global_command "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ -L $HOME/.tool-versions ]
[ "$(cat other-dir/.tool-versions)" = "dummy 1.1.0" ]
}
@test "global should preserve symlinks when updating versions" {
mkdir other-dir
touch other-dir/.tool-versions
ln -s other-dir/.tool-versions $HOME/.tool-versions
run global_command "dummy" "1.1.0"
run global_command "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ -L $HOME/.tool-versions ]
[ "$(cat other-dir/.tool-versions)" = "dummy 1.1.0" ]
}