Add tests

main
William Beuil 2021-02-01 19:16:55 +01:00
parent 811f8adbac
commit 8cce73aa84
No known key found for this signature in database
GPG Key ID: BED2072C5C2BF537
19 changed files with 603 additions and 3 deletions

View File

@ -7,3 +7,6 @@ insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
[Makefile]
indent_style = tab

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
versions/
version
test/libs/

15
Makefile Normal file
View File

@ -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

View File

@ -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}'"

View File

@ -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"

42
test/dctlenv-exec.bats Normal file
View File

@ -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 <command> [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 <command> [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 <command> [flags]'
}
teardown() {
rm -rf "$DCTLENV_TMPDIR"
}

31
test/dctlenv-help.bats Normal file
View File

@ -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 <<OUT
Usage: dctlenv <command> [<options>]
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
}

102
test/dctlenv-install.bats Normal file
View File

@ -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 [<version>]: 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 [<version>]'
}
@test "dctlenv install [<version>]: prints an error message if there is no version to install" {
run dctlenv install
assert_failure
assert_output 'usage: dctlenv install [<version>]'
}
@test "dctlenv install [<version>]: 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 [<version>]: 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 [<version>]: 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 <<OUT
Installing driftctl v0.3.1
Downloading release tarball from https://github.com/cloudskiff/driftctl/releases/download/v0.3.1/driftctl_linux_amd64
Tarball download failed
OUT
}
@test "dctlenv install [<version>]: 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 <<OUT
Installing driftctl v0.3.1
Downloading release tarball from https://github.com/cloudskiff/driftctl/releases/download/v0.3.1/driftctl_linux_amd64
Making the $DCTLENV_ROOT/versions/0.3.1/driftctl binary executable
Fail to make the binary executable
OUT
}
@test "dctlenv install [<version>]: 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 <<OUT
Installing driftctl v0.3.1
Downloading release tarball from https://github.com/cloudskiff/driftctl/releases/download/v0.3.1/driftctl_linux_amd64
Making the $DCTLENV_ROOT/versions/0.3.1/driftctl binary executable
Installation of driftctl v0.3.1 successful. To make this your default version, run 'dctlenv use 0.3.1'
OUT
}
teardown() {
rm -rf "$DCTLENV_TMPDIR"
}

View File

@ -0,0 +1,30 @@
#!/usr/bin/env bats
load "./test_helper"
@test "dctlenv list-remote: prints an error message if curl command failed" {
curlw() { exit 1; }; export -f curlw;
run dctlenv list-remote
assert_failure
assert_output 'Failed to download remote versions from GitHub'
}
@test "dctlenv list-remote: returns a list of all installable versions" {
curlw() { echo "$(cat ./test/mocks/list-remote.json)"; }; export -f curlw;
run dctlenv list-remote
assert_success
assert_output <<OUT
0.1.0
0.1.1
0.2.0
0.2.1
0.2.2
0.2.3
0.3.0
0.3.1
OUT
}

38
test/dctlenv-list.bats Normal file
View File

@ -0,0 +1,38 @@
#!/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 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 [<version>]'
}
@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 <<OUT
* 0.3.1 (set by $DCTLENV_ROOT/version)
0.3.0
0.2.3
OUT
}
teardown() {
rm -rf "$DCTLENV_TMPDIR"
}

10
test/dctlenv-root.bats Normal file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env bats
load "./test_helper"
@test "dctlenv root: returns current DCTLENV_ROOT" {
DCTLENV_ROOT=/tmp/dctlenv run dctlenv root
assert_success
assert_output '/tmp/dctlenv'
}

View File

@ -0,0 +1,60 @@
#!/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() {
echo " 0.3.1
0.3.0
0.2.3"
}
export -f dctlenv-list;
}
@test "dctlenv uninstall [<version>]: 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 [<version>]'
}
@test "dctlenv uninstall [<version>]: prints an error message if there is no version to uninstall" {
run dctlenv uninstall
assert_failure
assert_output 'usage: dctlenv uninstall [<version>]'
}
@test "dctlenv uninstall [<version>]: 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 [<version>]: 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 <<OUT
Uninstall driftctl v0.3.1
driftctl v0.3.1 is successfully uninstalled
OUT
}
teardown() {
rm -rf "$DCTLENV_TMPDIR"
}

80
test/dctlenv-use.bats Normal file
View File

@ -0,0 +1,80 @@
#!/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 use [<version>]: 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 [<version>]'
}
@test "dctlenv use [<version>]: 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 [<version>]'
}
@test "dctlenv use [<version>]: 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 [<version>]'
}
@test "dctlenv use [<version>]: 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 [<version>]: 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 [<version>]: 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 [<version>]: 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 <<OUT
Switching version to v0.3.1
Switching completed
OUT
}
teardown() {
rm -rf "$DCTLENV_TMPDIR"
}

View File

@ -0,0 +1,10 @@
#!/usr/bin/env bats
load "./test_helper"
@test "dctlenv version-file: returns current version file" {
DCTLENV_ROOT=/tmp/dctlenv run dctlenv version-file
assert_success
assert_output '/tmp/dctlenv/version'
}

View File

@ -0,0 +1,49 @@
#!/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 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 [<version>]'
}
@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 <version>)"
}
@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"
}

15
test/dctlenv-version.bats Normal file
View File

@ -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
}

25
test/dctlenv.bats Normal file
View File

@ -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 <<OUT
$(dctlenv-version)
$(dctlenv-help)
OUT
}
@test "dctlenv: fails when invalid command argument is given" {
run dctlenv foo
assert_failure
assert_output <<OUT
dctlenv: unknown command "foo"
$(dctlenv-help)
OUT
}

View File

@ -0,0 +1,82 @@
[
{
"name": "v0.3.1",
"zipball_url": "https://api.github.com/repos/cloudskiff/driftctl/zipball/v0.3.1",
"tarball_url": "https://api.github.com/repos/cloudskiff/driftctl/tarball/v0.3.1",
"commit": {
"sha": "cdea16b5dbca1c6205068d74b092b3e5284cabbd",
"url": "https://api.github.com/repos/cloudskiff/driftctl/commits/cdea16b5dbca1c6205068d74b092b3e5284cabbd"
},
"node_id": "MDM6UmVmMjk3NjIyOTQ1OnJlZnMvdGFncy92MC4zLjE="
},
{
"name": "v0.3.0",
"zipball_url": "https://api.github.com/repos/cloudskiff/driftctl/zipball/v0.3.0",
"tarball_url": "https://api.github.com/repos/cloudskiff/driftctl/tarball/v0.3.0",
"commit": {
"sha": "df91468764388841ab0ddf82e793f82372fe8fb7",
"url": "https://api.github.com/repos/cloudskiff/driftctl/commits/df91468764388841ab0ddf82e793f82372fe8fb7"
},
"node_id": "MDM6UmVmMjk3NjIyOTQ1OnJlZnMvdGFncy92MC4zLjA="
},
{
"name": "v0.2.3",
"zipball_url": "https://api.github.com/repos/cloudskiff/driftctl/zipball/v0.2.3",
"tarball_url": "https://api.github.com/repos/cloudskiff/driftctl/tarball/v0.2.3",
"commit": {
"sha": "d3c1e7242767d8425410e9dd49ae6c69eb46f41e",
"url": "https://api.github.com/repos/cloudskiff/driftctl/commits/d3c1e7242767d8425410e9dd49ae6c69eb46f41e"
},
"node_id": "MDM6UmVmMjk3NjIyOTQ1OnJlZnMvdGFncy92MC4yLjM="
},
{
"name": "v0.2.2",
"zipball_url": "https://api.github.com/repos/cloudskiff/driftctl/zipball/v0.2.2",
"tarball_url": "https://api.github.com/repos/cloudskiff/driftctl/tarball/v0.2.2",
"commit": {
"sha": "e42dd7303bb8c7e48b3788fa72e0e37da95f26f8",
"url": "https://api.github.com/repos/cloudskiff/driftctl/commits/e42dd7303bb8c7e48b3788fa72e0e37da95f26f8"
},
"node_id": "MDM6UmVmMjk3NjIyOTQ1OnJlZnMvdGFncy92MC4yLjI="
},
{
"name": "v0.2.1",
"zipball_url": "https://api.github.com/repos/cloudskiff/driftctl/zipball/v0.2.1",
"tarball_url": "https://api.github.com/repos/cloudskiff/driftctl/tarball/v0.2.1",
"commit": {
"sha": "1abe9656e21e9c29aa856983482a794d332ba335",
"url": "https://api.github.com/repos/cloudskiff/driftctl/commits/1abe9656e21e9c29aa856983482a794d332ba335"
},
"node_id": "MDM6UmVmMjk3NjIyOTQ1OnJlZnMvdGFncy92MC4yLjE="
},
{
"name": "v0.2.0",
"zipball_url": "https://api.github.com/repos/cloudskiff/driftctl/zipball/v0.2.0",
"tarball_url": "https://api.github.com/repos/cloudskiff/driftctl/tarball/v0.2.0",
"commit": {
"sha": "28b44e57d75dcd8a267472f60ccf5fe2abc423f2",
"url": "https://api.github.com/repos/cloudskiff/driftctl/commits/28b44e57d75dcd8a267472f60ccf5fe2abc423f2"
},
"node_id": "MDM6UmVmMjk3NjIyOTQ1OnJlZnMvdGFncy92MC4yLjA="
},
{
"name": "v0.1.1",
"zipball_url": "https://api.github.com/repos/cloudskiff/driftctl/zipball/v0.1.1",
"tarball_url": "https://api.github.com/repos/cloudskiff/driftctl/tarball/v0.1.1",
"commit": {
"sha": "621463ef22f8ce0731998cfc3775af2135c2adbf",
"url": "https://api.github.com/repos/cloudskiff/driftctl/commits/621463ef22f8ce0731998cfc3775af2135c2adbf"
},
"node_id": "MDM6UmVmMjk3NjIyOTQ1OnJlZnMvdGFncy92MC4xLjE="
},
{
"name": "v0.1.0",
"zipball_url": "https://api.github.com/repos/cloudskiff/driftctl/zipball/v0.1.0",
"tarball_url": "https://api.github.com/repos/cloudskiff/driftctl/tarball/v0.1.0",
"commit": {
"sha": "ff72de8e77f908fba61df50bc0938744270d1b51",
"url": "https://api.github.com/repos/cloudskiff/driftctl/commits/ff72de8e77f908fba61df50bc0938744270d1b51"
},
"node_id": "MDM6UmVmMjk3NjIyOTQ1OnJlZnMvdGFncy92MC4xLjA="
}
]

7
test/test_helper.bash Normal file
View File

@ -0,0 +1,7 @@
load 'libs/bats-support/load'
load 'libs/bats-assert/load'
load "../lib/log.sh"
PATH="$BATS_TEST_DIRNAME/../libexec:$PATH"
export PATH