Merge pull request #11 from wbeuil/install-on-use

Automatic installation in use command
main
William BEUIL 2021-02-03 22:35:21 +01:00 committed by GitHub
commit 6e1468414f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 6 deletions

View File

@ -16,9 +16,13 @@ version="$(\find "$DCTLENV_ROOT/versions" -type d -exec basename {} \; \
| head -n 1 | head -n 1
)" )"
[ -n "$version" ] \ if [ -n "$version" ]; then
&& log_debug "Found version: $version" \ log_debug "Found version: $version"
|| log_error "No installed versions of driftctl matched '$1'" 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" target_path="$DCTLENV_ROOT/versions/$version"
[ -f "$target_path/driftctl" ] \ [ -f "$target_path/driftctl" ] \

View File

@ -31,13 +31,18 @@ setup() {
assert_output 'usage: dctlenv use [<version>]' assert_output 'usage: dctlenv use [<version>]'
} }
@test "dctlenv use [<version>]: prints an error message if we try to use a non-installed version" { @test "dctlenv use [<version>]: 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" 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_failure
assert_output "No installed versions of driftctl matched '0.3.0'" assert_output <<OUT
No installed versions of driftctl matched '0.0.0', let's install it
Installation of version 0.0.0 failed
OUT
} }
@test "dctlenv use [<version>]: prints an error message if we try to use a version where its binary is not present" { @test "dctlenv use [<version>]: prints an error message if we try to use a version where its binary is not present" {
@ -75,6 +80,27 @@ Switching completed
OUT OUT
} }
@test "dctlenv use [<version>]: 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 <<OUT
No installed versions of driftctl matched '0.3.0', let's install it
Switching version to v0.3.0
Switching completed
OUT
}
teardown() { teardown() {
rm -rf "$DCTLENV_TMPDIR" rm -rf "$DCTLENV_TMPDIR"
} }