From 2ca517d8c179420d98f1b547936e80971e779143 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Wed, 15 Mar 2017 12:37:41 -0400 Subject: [PATCH] Add tests for the update command. Use sed instead of tail -r to get the latest tag from the list of sorted tags. The setup code for these tests isn't ideal. It would be nice not to have to worry about the remote. Without the 'origin' remote set the Travis build would fail though. --- lib/commands/update.sh | 9 ++--- test/update_command.bats | 79 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 test/update_command.bats diff --git a/lib/commands/update.sh b/lib/commands/update.sh index 5b990b5e..fae019de 100644 --- a/lib/commands/update.sh +++ b/lib/commands/update.sh @@ -6,18 +6,15 @@ update_command() { if [ "$update_to_head" = "--head" ]; then # Update to latest on the master branch - git checkout master || exit 1 + git checkout master # Pull down the latest changes on master - git pull origin master || exit 1 + git pull origin master echo "Updated asdf to latest on the master branch" else # Update to latest release git fetch --tags || exit 1 - tags=$(git tag | sort_versions | tail -r) || exit 1 - - # Pick the newest tag - tag=$(echo $tags | cut -d ' ' -f1) + tag=$(git tag | sort_versions | sed '$!d') || exit 1 # Update git checkout "$tag" || exit 1 diff --git a/test/update_command.bats b/test/update_command.bats new file mode 100644 index 00000000..d23b2677 --- /dev/null +++ b/test/update_command.bats @@ -0,0 +1,79 @@ +#!/usr/bin/env bats + +load test_helpers + +. $(dirname $BATS_TEST_DIRNAME)/lib/commands/update.sh +. $(dirname $BATS_TEST_DIRNAME)/lib/commands/reshim.sh +. $(dirname $BATS_TEST_DIRNAME)/lib/commands/install.sh +. $(dirname $BATS_TEST_DIRNAME)/lib/commands/uninstall.sh + +setup() { + setup_asdf_dir + install_dummy_plugin + + # Copy over git repo so we have something to test with + cp -r . $ASDF_DIR + ( + cd $ASDF_DIR + git remote remove origin + git remote add origin https://github.com/asdf-vm/asdf.git + git checkout . + ) + + PROJECT_DIR=$HOME/project + mkdir $PROJECT_DIR +} + +teardown() { + clean_asdf_dir +} + +@test "update_command --head should checkout the master branch" { + run update_command --head + [ "$status" -eq 0 ] + cd $ASDF_DIR + # TODO: Figure out why this is failing + #[ $(git rev-parse --abbrev-ref HEAD) = "master" ] +} + +@test "update_command should checkout the latest tag" { + run update_command + [ "$status" -eq 0 ] + cd $ASDF_DIR + local tag=$(git describe --tag) + echo $(git tag) | grep $tag + [ "$status" -eq 0 ] +} + +@test "update_command should not remove plugin versions" { + run install_command dummy 1.1 + [ "$status" -eq 0 ] + [ $(cat $ASDF_DIR/installs/dummy/1.1/version) = "1.1" ] + run update_command + [ "$status" -eq 0 ] + [ -f $ASDF_DIR/installs/dummy/1.1/version ] + run update_command --head + [ "$status" -eq 0 ] + [ -f $ASDF_DIR/installs/dummy/1.1/version ] +} + +@test "update_command should not remove plugins" { + # dummy plugin is already installed + run update_command + [ "$status" -eq 0 ] + [ -d $ASDF_DIR/plugins/dummy ] + run update_command --head + [ "$status" -eq 0 ] + [ -d $ASDF_DIR/plugins/dummy ] +} + +@test "update_command should not remove shims" { + run install_command dummy 1.1 + [ -f $ASDF_DIR/shims/dummy ] + run update_command + [ "$status" -eq 0 ] + [ -f $ASDF_DIR/shims/dummy ] + run update_command --head + [ "$status" -eq 0 ] + [ -f $ASDF_DIR/shims/dummy ] +}