From 25ae0f71202f1aeed295ed9e282e576b0479eb77 Mon Sep 17 00:00:00 2001 From: William Beuil Date: Wed, 3 Feb 2021 22:32:23 +0100 Subject: [PATCH] Automatic installation in use command --- libexec/dctlenv-use | 10 +++++++--- test/dctlenv-use.bats | 32 +++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/libexec/dctlenv-use b/libexec/dctlenv-use index 18d1cfb..ae150cf 100755 --- a/libexec/dctlenv-use +++ b/libexec/dctlenv-use @@ -16,9 +16,13 @@ version="$(\find "$DCTLENV_ROOT/versions" -type d -exec basename {} \; \ | head -n 1 )" -[ -n "$version" ] \ - && log_debug "Found version: $version" \ - || log_error "No installed versions of driftctl matched '$1'" +if [ -n "$version" ]; then + log_debug "Found version: $version" +else + echo "No installed versions of driftctl matched '$1', let's install it" + $(dctlenv-install $1 1>/dev/null) || log_error "Installation of version $1 failed" + version=$1 +fi target_path="$DCTLENV_ROOT/versions/$version" [ -f "$target_path/driftctl" ] \ diff --git a/test/dctlenv-use.bats b/test/dctlenv-use.bats index de90160..a68605e 100644 --- a/test/dctlenv-use.bats +++ b/test/dctlenv-use.bats @@ -31,13 +31,18 @@ setup() { assert_output 'usage: dctlenv use []' } -@test "dctlenv use []: prints an error message if we try to use a non-installed version" { +@test "dctlenv use []: prints an error message if we try to use a non-installed version that fails to install" { mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" - run dctlenv use 0.3.0 + dctlenv-install() { exit 1; }; export -f dctlenv-install; + + run dctlenv use 0.0.0 assert_failure - assert_output "No installed versions of driftctl matched '0.3.0'" + assert_output <]: prints an error message if we try to use a version where its binary is not present" { @@ -75,6 +80,27 @@ Switching completed OUT } +@test "dctlenv use []: prints a success message if we successfuly install and use a non-installed version" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + + dctlenv-install() { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.0" + touch "$DCTLENV_TMPDIR/versions/0.3.0/driftctl" + chmod +x $DCTLENV_TMPDIR/versions/0.3.0/driftctl + exit 0 + }; export -f dctlenv-install; + driftctl() { exit 0; }; export -f driftctl; + + run dctlenv use 0.3.0 + + assert_success + assert_output <