From 8cce73aa845a5c6ad8e199d47c31795962a30519 Mon Sep 17 00:00:00 2001 From: William Beuil Date: Mon, 1 Feb 2021 19:16:55 +0100 Subject: [PATCH] Add tests --- .editorconfig | 3 + .gitignore | 1 + Makefile | 15 +++++ libexec/dctlenv-install | 4 +- libexec/dctlenv-use | 2 +- test/dctlenv-exec.bats | 42 ++++++++++++++ test/dctlenv-help.bats | 31 ++++++++++ test/dctlenv-install.bats | 102 +++++++++++++++++++++++++++++++++ test/dctlenv-list-remote.bats | 30 ++++++++++ test/dctlenv-list.bats | 38 ++++++++++++ test/dctlenv-root.bats | 10 ++++ test/dctlenv-uninstall.bats | 60 +++++++++++++++++++ test/dctlenv-use.bats | 80 ++++++++++++++++++++++++++ test/dctlenv-version-file.bats | 10 ++++ test/dctlenv-version-name.bats | 49 ++++++++++++++++ test/dctlenv-version.bats | 15 +++++ test/dctlenv.bats | 25 ++++++++ test/mocks/list-remote.json | 82 ++++++++++++++++++++++++++ test/test_helper.bash | 7 +++ 19 files changed, 603 insertions(+), 3 deletions(-) create mode 100644 Makefile create mode 100644 test/dctlenv-exec.bats create mode 100644 test/dctlenv-help.bats create mode 100644 test/dctlenv-install.bats create mode 100644 test/dctlenv-list-remote.bats create mode 100644 test/dctlenv-list.bats create mode 100644 test/dctlenv-root.bats create mode 100644 test/dctlenv-uninstall.bats create mode 100644 test/dctlenv-use.bats create mode 100644 test/dctlenv-version-file.bats create mode 100644 test/dctlenv-version-name.bats create mode 100644 test/dctlenv-version.bats create mode 100644 test/dctlenv.bats create mode 100644 test/mocks/list-remote.json create mode 100644 test/test_helper.bash diff --git a/.editorconfig b/.editorconfig index 99580d0..68751ea 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,3 +7,6 @@ insert_final_newline = true indent_style = space indent_size = 2 trim_trailing_whitespace = true + +[Makefile] +indent_style = tab diff --git a/.gitignore b/.gitignore index 18e56fb..e78ead8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ versions/ version +test/libs/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7cbe79e --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +all: test + +test: bats + $(PWD)/test/libs/bats-core/bin/bats test + +bats: + if [ ! -d "$(PWD)/test/libs/bats-core" ]; then \ + echo "Installing bats-core and its plugins"; \ + git clone https://github.com/bats-core/bats-core.git test/libs/bats-core; \ + git clone https://github.com/ztombol/bats-assert test/libs/bats-assert; \ + git clone https://github.com/ztombol/bats-support test/libs/bats-support; \ + fi + +.SILENT: test bats +.PHONY: test bats diff --git a/libexec/dctlenv-install b/libexec/dctlenv-install index 18d144b..46f055c 100755 --- a/libexec/dctlenv-install +++ b/libexec/dctlenv-install @@ -39,9 +39,9 @@ driftctl_url="https://github.com/cloudskiff/driftctl/releases/download" echo "Installing driftctl v$version" echo "Downloading release tarball from $driftctl_url/v$version/driftctl_$os" -curlw -# -f -L -o "$dst_path/driftctl" --create-dirs "$driftctl_url/v$version/driftctl_$os" || log_error 'Tarball download failed' +$(curlw -# -f -L -o "$dst_path/driftctl" --create-dirs "$driftctl_url/v$version/driftctl_$os") || log_error 'Tarball download failed' echo "Making the $dst_path/driftctl binary executable" -chmod +x "$dst_path/driftctl" +$(chmod +x "$dst_path/driftctl") || log_error "Fail to make the binary executable" echo "Installation of driftctl v${version} successful. To make this your default version, run 'dctlenv use ${version}'" diff --git a/libexec/dctlenv-use b/libexec/dctlenv-use index d71cba1..18d1cfb 100755 --- a/libexec/dctlenv-use +++ b/libexec/dctlenv-use @@ -32,6 +32,6 @@ version_file="$(dctlenv-version-file)" log_debug "Writing \"$version\" to \"$version_file\"" echo "$version" > "$version_file" || log_error "Failed to switch to v$version" -driftctl version 1>/dev/null || log_error "'driftctl version' failed, something is wrong" +$(driftctl version 1>/dev/null) || log_error "'driftctl version' failed, something is wrong" echo "Switching completed" diff --git a/test/dctlenv-exec.bats b/test/dctlenv-exec.bats new file mode 100644 index 0000000..fe2d1ca --- /dev/null +++ b/test/dctlenv-exec.bats @@ -0,0 +1,42 @@ +#!/usr/bin/env bats + +load "./test_helper" + +setup() { + export DCTLENV_TMPDIR="$BATS_TMPDIR/dctlenv" + export DCTLENV_TMPDIR="$(mktemp -d "$DCTLENV_TMPDIR.XXX" 2>/dev/null || echo "$DCTLENV_TMPDIR")" + export DCTLENV_ROOT="$DCTLENV_TMPDIR" +} + +@test "dctlenv exec: prints an error message it can't get the version" { + run dctlenv exec + + assert_failure + assert_output 'Failed to get version from dctlenv-version-name' +} + +@test "dctlenv exec: prints error messages if it fails to execute" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + echo "echo 'Usage: driftctl [flags]'" > "$DCTLENV_TMPDIR/versions/0.3.1/driftctl" + echo "0.3.1" > "$DCTLENV_ROOT/version" + + run dctlenv exec + + assert_failure +} + +@test "dctlenv exec: execute successfuly driftctl commands" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + echo "echo 'Usage: driftctl [flags]'" > "$DCTLENV_TMPDIR/versions/0.3.1/driftctl" + chmod +x $DCTLENV_TMPDIR/versions/0.3.1/driftctl + echo "0.3.1" > "$DCTLENV_ROOT/version" + + run dctlenv exec + + assert_success + assert_output 'Usage: driftctl [flags]' +} + +teardown() { + rm -rf "$DCTLENV_TMPDIR" +} diff --git a/test/dctlenv-help.bats b/test/dctlenv-help.bats new file mode 100644 index 0000000..1c0c915 --- /dev/null +++ b/test/dctlenv-help.bats @@ -0,0 +1,31 @@ +#!/usr/bin/env bats + +load "./test_helper" + +@test "dctlenv [help|-h|--help]: returns the help message" { + cases=("help" "-h" "--help") + + for t in ${cases[@]}; do + run dctlenv $t + + assert_success + assert_output < [] + +Commands: + install Install a specific version of driftctl + uninstall Uninstall a specific version of driftctl + use Switch a version to use + list List all installed versions + list-remote List all installable versions + version Display dctlenv version + help Show this help output + +Flags: + -v, --version An alias for the "version" command + -h, --help An alias for the "help" command + +For full documentation, see: https://github.com/wbeuil/dctlenv#readme +OUT + done +} diff --git a/test/dctlenv-install.bats b/test/dctlenv-install.bats new file mode 100644 index 0000000..803d473 --- /dev/null +++ b/test/dctlenv-install.bats @@ -0,0 +1,102 @@ +#!/usr/bin/env bats + +load "./test_helper" + +setup() { + export DCTLENV_TMPDIR="$BATS_TMPDIR/dctlenv" + export DCTLENV_TMPDIR="$(mktemp -d "$DCTLENV_TMPDIR.XXX" 2>/dev/null || echo "$DCTLENV_TMPDIR")" + export DCTLENV_ROOT="$DCTLENV_TMPDIR" + + dctlenv-list-remote() { + echo "0.1.0 +0.1.1 +0.2.0 +0.2.1 +0.2.2 +0.2.3 +0.3.0 +0.3.1" + } + export -f dctlenv-list-remote; +} + +@test "dctlenv install []: prints an error message if we try to install more than one version" { + run dctlenv install 0.3.1 0.3.0 + + assert_failure + assert_output 'usage: dctlenv install []' +} + +@test "dctlenv install []: prints an error message if there is no version to install" { + run dctlenv install + + assert_failure + assert_output 'usage: dctlenv install []' +} + +@test "dctlenv install []: prints an error message if the version requested doesn't exist" { + run dctlenv install 0.0.0 + + assert_failure + assert_output 'No version 0.0.0 found in remote' +} + +@test "dctlenv install []: prints a message when the version is already installed" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + touch "$DCTLENV_TMPDIR/versions/0.3.1/driftctl" + + run dctlenv install 0.3.1 + + assert_success + assert_output 'driftctl v0.3.1 is already installed' +} + +@test "dctlenv install []: prints an error message if it failed to download the tarball" { + curlw() { exit 1; }; export -f curlw; + uname() { echo "Linux"; }; export -f uname; + + run dctlenv install 0.3.1 + + assert_failure + assert_output <]: prints an error message if it failed to make the binary executable" { + chmod() { exit 1; }; export -f chmod; + curlw() { exit 0; }; export -f curlw; + uname() { echo "Linux"; }; export -f uname; + + run dctlenv install 0.3.1 + + assert_failure + assert_output <]: prints a success message at the end of the install" { + chmod() { exit 0; }; export -f chmod; + curlw() { exit 0; }; export -f curlw; + uname() { echo "Linux"; }; export -f uname; + + run dctlenv install 0.3.1 + + assert_success + assert_output </dev/null || echo "$DCTLENV_TMPDIR")" + export DCTLENV_ROOT="$DCTLENV_TMPDIR" +} + +@test "dctlenv list: prints an error message if no versions is installed" { + run dctlenv list + + assert_failure + assert_output 'No versions of driftctl installed. Please install one with: dctlenv install []' +} + +@test "dctlenv list: prints all installed versions" { + curlw() { echo "$(cat ./test/mocks/list-remote.json)"; }; export -f curlw; + + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.0" + mkdir -p "$DCTLENV_TMPDIR/versions/0.2.3" + echo "0.3.1" > "$DCTLENV_ROOT/version" + + run dctlenv list + + assert_success + assert_output </dev/null || echo "$DCTLENV_TMPDIR")" + export DCTLENV_ROOT="$DCTLENV_TMPDIR" + + dctlenv-list() { + echo " 0.3.1 + 0.3.0 + 0.2.3" + } + export -f dctlenv-list; +} + +@test "dctlenv uninstall []: prints an error message if we try to uninstall more than one version" { + run dctlenv uninstall 0.3.1 0.3.0 + + assert_failure + assert_output 'usage: dctlenv uninstall []' +} + +@test "dctlenv uninstall []: prints an error message if there is no version to uninstall" { + run dctlenv uninstall + + assert_failure + assert_output 'usage: dctlenv uninstall []' +} + +@test "dctlenv uninstall []: prints an error message if the version requested doesn't exist in local" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.0" + mkdir -p "$DCTLENV_TMPDIR/versions/0.2.3" + + run dctlenv uninstall 0.0.0 + + assert_failure + assert_output "No versions matching '0.0.0' found in local" +} + +@test "dctlenv uninstall []: prints a success message at the end of the uninstall" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.0" + mkdir -p "$DCTLENV_TMPDIR/versions/0.2.3" + touch "$DCTLENV_TMPDIR/versions/0.3.1/driftctl" + + run dctlenv uninstall 0.3.1 + + assert_success + assert_output </dev/null || echo "$DCTLENV_TMPDIR")" + export DCTLENV_ROOT="$DCTLENV_TMPDIR" +} + +@test "dctlenv use []: prints an error message if we try to use more than one version" { + run dctlenv use 0.3.1 0.3.0 + + assert_failure + assert_output 'usage: dctlenv use []' +} + +@test "dctlenv use []: prints an error message if there is no version installed" { + run dctlenv use 0.3.1 + + assert_failure + assert_output 'No versions of driftctl installed. Please install one with: dctlenv install []' +} + +@test "dctlenv use []: prints an error message if we don't specified a version to use" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + + run dctlenv use + + assert_failure + assert_output 'usage: dctlenv use []' +} + +@test "dctlenv use []: prints an error message if we try to use a non-installed version" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + + run dctlenv use 0.3.0 + + assert_failure + assert_output "No installed versions of driftctl matched '0.3.0'" +} + +@test "dctlenv use []: prints an error message if we try to use a version where its binary is not present" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + + run dctlenv use 0.3.1 + + assert_failure + assert_output "Version directory for 0.3.1 is present, but the driftctl binary is not! Manual intervention required" +} + +@test "dctlenv use []: prints an error message if we try to use a version where its binary is not executable" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + touch "$DCTLENV_TMPDIR/versions/0.3.1/driftctl" + + run dctlenv use 0.3.1 + + assert_failure + assert_output "Version directory for 0.3.1 is present, but the driftctl binary is not executable! Manual intervention required" +} + +@test "dctlenv use []: prints a success message when switching versions" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.3.1" + touch "$DCTLENV_TMPDIR/versions/0.3.1/driftctl" + chmod +x $DCTLENV_TMPDIR/versions/0.3.1/driftctl + + driftctl() { exit 0; }; export -f driftctl; + + run dctlenv use 0.3.1 + + assert_success + assert_output </dev/null || echo "$DCTLENV_TMPDIR")" + export DCTLENV_ROOT="$DCTLENV_TMPDIR" +} + +@test "dctlenv version-name: prints an error message if directory DCTLENV_ROOT/versions does not exist" { + run dctlenv version-name + + assert_failure + assert_output 'No versions of driftctl installed. Please install one with: dctlenv install []' +} + +@test "dctlenv version-name: prints an error message if file DCTLENV_ROOT/version could not be read" { + mkdir -p "$DCTLENV_TMPDIR/versions" + + run dctlenv version-name + + assert_failure + assert_output "Version could not be resolved (set by $DCTLENV_ROOT/version or dctlenv use )" +} + +@test "dctlenv version-name: prints an error message when no version is present in DCTLENV_ROOT/versions" { + mkdir -p "$DCTLENV_TMPDIR/versions" + echo "0.0.1" > "$DCTLENV_ROOT/version" + + run dctlenv version-name + + assert_failure + assert_output "Version '0.0.1' is not installed (set by $DCTLENV_ROOT/version)" +} + +@test "dctlenv version-name: prints the installed version" { + mkdir -p "$DCTLENV_TMPDIR/versions/0.0.1" + echo "0.0.1" > "$DCTLENV_ROOT/version" + + run dctlenv version-name + + assert_success + assert_output "0.0.1" +} + +teardown() { + rm -rf "$DCTLENV_TMPDIR" +} diff --git a/test/dctlenv-version.bats b/test/dctlenv-version.bats new file mode 100644 index 0000000..29497af --- /dev/null +++ b/test/dctlenv-version.bats @@ -0,0 +1,15 @@ +#!/usr/bin/env bats + +load "./test_helper" + +expected_version="dctlenv 0.0.4-2-gc013c6c" + +@test "dctlenv [version|-v|--version]: returns the expected version" { + cases=("version" "-v" "--version") + + for t in ${cases[@]}; do + run dctlenv $t + + assert_success "$expected_version" + done +} diff --git a/test/dctlenv.bats b/test/dctlenv.bats new file mode 100644 index 0000000..d03dfc1 --- /dev/null +++ b/test/dctlenv.bats @@ -0,0 +1,25 @@ +#!/usr/bin/env bats + +load "./test_helper" + +@test "dctlenv: prints help when no command argument is given" { + run dctlenv + + assert_success + assert_output <