Fix local and global commands to accept multiple versions

This commit is contained in:
Daniel Perez 2016-08-30 15:46:14 +09:00
parent c007ac728b
commit f525ba85ff
2 changed files with 22 additions and 6 deletions

View File

@ -1,14 +1,16 @@
version_command() {
local cmd=$1
local plugin=$2
local version=$3
local file
if [ "$#" -ne 3 ]; then
if [ "$#" -lt "3" ]; then
echo "Usage: asdf $cmd <name> <version>"
exit 1
fi
shift 2
local versions=$@
local file
if [ $cmd = "global" ]; then
file=$HOME/.tool-versions
else
@ -16,12 +18,14 @@ version_command() {
fi
check_if_plugin_exists $plugin
check_if_version_exists $plugin $version
for version in $versions; do
check_if_version_exists $plugin $version
done
if [ -f "$file" ] && grep $plugin "$file" > /dev/null; then
sed -i -e "s/$plugin .*/$plugin $version/" "$file"
sed -i -e "s/$plugin .*/$plugin $versions/" "$file"
else
echo "$plugin $version" >> "$file"
echo "$plugin $versions" >> "$file"
fi
}

View File

@ -45,6 +45,12 @@ teardown() {
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ]
}
@test "local should allow multiple versions" {
run local_command "dummy" "1.1.0" "1.0.0"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0 1.0.0" ]
}
@test "local should create a local .tool-versions file if it doesn't exist when the directory name contains whitespace" {
WHITESPACE_DIR="$PROJECT_DIR/whitespace\ dir"
mkdir -p "$WHITESPACE_DIR"
@ -70,6 +76,12 @@ teardown() {
[ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0" ]
}
@test "global should accept multiple versions" {
run global_command "dummy" "1.1.0" "1.0.0"
[ "$status" -eq 0 ]
[ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0 1.0.0" ]
}
@test "global should overwrite the existing version if it's set" {
echo 'dummy 1.0.0' >> $HOME/.tool-versions
run global_command "dummy" "1.1.0"