Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reneleonhardt committed Jun 2, 2024
1 parent 435e82d commit d1064b4
Show file tree
Hide file tree
Showing 6 changed files with 444 additions and 31 deletions.
52 changes: 28 additions & 24 deletions test/goenv-commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,34 @@ OUT
@test "'commands' returns all commands" {
run goenv-commands

assert_success
assert_line "commands"
assert_line "completions"
assert_line "exec"
assert_line "global"
assert_line "help"
assert_line "hooks"
assert_line "init"
assert_line "local"
assert_line "prefix"
assert_line "rehash"
assert_line "root"
assert_line "shell"
assert_line "shims"
assert_line "version"
assert_line "--version"
assert_line "version-file"
assert_line "version-file-read"
assert_line "version-file-write"
assert_line "version-name"
assert_line "version-origin"
assert_line "versions"
assert_line "whence"
assert_line "which"
assert_success "--version
commands
completions
exec
global
help
hooks
init
install
installed
latest
local
prefix
rehash
root
shell
shims
system
uninstall
version
version-file
version-file-read
version-file-write
version-name
version-origin
versions
whence
which"
}

@test "'commands --sh' returns only commands containing 'sh'" {
Expand Down
86 changes: 85 additions & 1 deletion test/goenv-global.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@

load test_helper

teardown() {
rm -rf "${GOENV_ROOT}/versions/" || true
rm -f "${GOENV_ROOT}/version" || true
rm -f "${GOENV_ROOT}/global" || true
rm -f "${GOENV_ROOT}/default" || true
}

setup() {
teardown
}

@test "has usage instructions" {
run goenv-help global
assert_success <<OUT
Usage: goenv global <version>
OUT
}

@test "global has completion support" {
mkdir -p "${GOENV_ROOT}/versions/1.10.9"
mkdir -p "${GOENV_ROOT}/versions/1.9.10"
run goenv-global --complete
assert_success "latest
system
1.10.9
1.9.10"
}

@test "defaults to 'system'" {
run goenv-global
assert_success
Expand Down Expand Up @@ -47,9 +68,72 @@ OUT
assert_success "1.2.3"
}

@test "sets properly sorted latest global version when 'latest' version is given and any version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.10.10"
mkdir -p "${GOENV_ROOT}/versions/1.10.9"
mkdir -p "${GOENV_ROOT}/versions/1.9.10"
mkdir -p "${GOENV_ROOT}/versions/1.9.9"
run goenv-global latest
assert_success ""
run goenv-global
assert_success "1.10.10"
}

@test "sets latest global version when major version is given and any matching version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.2.10"
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/4.5.6"
run goenv-global 1
assert_success ""
run goenv-global
assert_success "1.2.10"
}

@test "fails setting latest global version when major or minor single number is given and does not match at 'GOENV_ROOT/versions/<version>'" {
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/4.5.10"
run goenv-global 9
assert_failure "goenv: version '9' not installed"
}

@test "sets latest global version when minor version is given as single number and any matching major.minor version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.2.10"
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/1.3.11"
mkdir -p "${GOENV_ROOT}/versions/4.5.2"
run goenv-global 2
assert_success ""
run goenv-global
assert_success "1.2.10"
}

@test "sets latest global version when minor version is given as major.minor number and any matching version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.2.10"
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/1.2.2"
mkdir -p "${GOENV_ROOT}/versions/1.3.11"
mkdir -p "${GOENV_ROOT}/versions/2.1.2"
run goenv-global 1.2
assert_success ""
run goenv-global
assert_success "1.2.10"
}

@test "fails setting latest global version when major.minor number is given and does not match at 'GOENV_ROOT/versions/<version>'" {
mkdir -p "${GOENV_ROOT}/versions/1.1.9"
run goenv-global 1.9
assert_failure "goenv: version '1.9' not installed"
}

@test "fails writing specified version to GOENV_ROOT/version if version is not installed" {
run goenv-global system
assert_failure "goenv: system version not found in PATH"

run goenv-global "1.2.3"
assert_failure
assert_failure "goenv: version '1.2.3' not installed"

run goenv-installed "1.2.3"
assert_failure "goenv: version '1.2.3' not installed"

run goenv-global
assert_success "system"
Expand Down
101 changes: 101 additions & 0 deletions test/goenv-installed.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env bats

load test_helper

@test "has usage instructions" {
run goenv-help --usage installed
assert_success <<'OUT'
Usage: goenv installed <version>
OUT
}

@test "installed has completion support" {
mkdir -p "${GOENV_ROOT}/versions/1.10.9"
mkdir -p "${GOENV_ROOT}/versions/1.9.10"
run goenv-installed --complete
assert_success "latest
system
1.10.9
1.9.10"
}

@test "installed fails when no versions are installed" {
run goenv-installed
assert_failure "goenv: no versions installed"
}

@test "prints installed version when no arguments are given and there's a '.go-version' file in current dir" {
mkdir -p "${GOENV_ROOT}/versions/1.2.3"
run goenv-installed
assert_success "1.2.3"
}

@test "prints installed version '.go-version' file in parent directory when no arguments are given" {
mkdir -p "${GOENV_ROOT}/versions/1.2.3"
mkdir -p "subdir"
cd "subdir"

run goenv-installed
assert_success "1.2.3"
}

@test "sets installed version when version argument is given and matches at 'GOENV_ROOT/versions/<version>'" {
mkdir -p "${GOENV_ROOT}/versions/1.2.3"
run goenv-installed 1.2.3
assert_success "1.2.3"
}

@test "goenv installed sets properly sorted latest version when 'latest' version is given and any version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.10.10"
mkdir -p "${GOENV_ROOT}/versions/1.10.9"
mkdir -p "${GOENV_ROOT}/versions/1.9.10"
mkdir -p "${GOENV_ROOT}/versions/1.9.9"
run goenv-installed latest
assert_success "1.10.10"
}

@test "goenv installed sets latest version when major version is given and any matching version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.2.10"
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/4.5.6"
run goenv-installed 1
assert_success "1.2.10"
}

@test "goenv installed fails setting latest version when major or minor single number is given and does not match at 'GOENV_ROOT/versions/<version>'" {
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/4.5.10"
run goenv-installed 9
assert_failure "goenv: version '9' not installed"
}

@test "goenv installed sets latest version when minor version is given as single number and any matching major.minor version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.2.10"
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/1.3.11"
mkdir -p "${GOENV_ROOT}/versions/4.5.2"
run goenv-installed 2
assert_success "1.2.10"
}

@test "goenv installed sets latest version when minor version is given as major.minor number and any matching version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.2.10"
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/1.2.2"
mkdir -p "${GOENV_ROOT}/versions/1.3.11"
mkdir -p "${GOENV_ROOT}/versions/2.1.2"
run goenv-installed 1.2
assert_success "1.2.10"
}

@test "goenv installed fails setting latest version when major.minor number is given and does not match at 'GOENV_ROOT/versions/<version>'" {
mkdir -p "${GOENV_ROOT}/versions/1.1.9"
run goenv-installed 1.9
assert_failure "goenv: version '1.9' not installed"
}

@test "fails setting installed version when version argument is given and does not match at 'GOENV_ROOT/versions/<version>'" {
mkdir -p "${GOENV_ROOT}/versions/1.2.3"
run goenv-installed 1.2.4
assert_failure "goenv: version '1.2.4' not installed"
}
76 changes: 70 additions & 6 deletions test/goenv-local.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

load test_helper

teardown() {
rm -f $(goenv-version-file "$PWD") || true
rm -rf "${GOENV_ROOT}/versions/" || true
}

setup() {
mkdir -p "${GOENV_TEST_DIR}/myproject"
cd "${GOENV_TEST_DIR}/myproject"
teardown
}

@test "has usage instructions" {
Expand All @@ -15,12 +21,14 @@ Usage: goenv local <version>
OUT
}

@test "has completion support" {
run goenv-local --complete
assert_success <<OUT
--unset
@test "local has completion support" {
mkdir -p "${GOENV_ROOT}/versions/1.10.9"
mkdir -p "${GOENV_ROOT}/versions/1.9.10"
run goenv-installed --complete
assert_success "latest
system
OUT
1.10.9
1.9.10"
}

@test "fails when no arguments are given and there's no '.go-version' file in current dir" {
Expand Down Expand Up @@ -74,13 +82,69 @@ OUT
assert [ "$(cat .go-version)" = "1.2.3" ]
}

@test "goenv local sets properly sorted latest version when 'latest' version is given and any version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.10.10"
mkdir -p "${GOENV_ROOT}/versions/1.10.9"
mkdir -p "${GOENV_ROOT}/versions/1.9.10"
mkdir -p "${GOENV_ROOT}/versions/1.9.9"
run goenv-local latest
assert_success ""
assert [ "$(cat .go-version)" = "1.10.10" ]
}

@test "goenv local sets latest version when major version is given and any matching version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.2.10"
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/4.5.6"
run goenv-local 1
assert_success ""
run goenv-local
assert_success "1.2.10"
}

@test "goenv local fails setting latest version when major or minor single number is given and does not match at 'GOENV_ROOT/versions/<version>'" {
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/4.5.10"
run goenv-local 9
assert_failure "goenv: version '9' not installed"
}

@test "goenv local sets latest version when minor version is given as single number and any matching major.minor version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.2.10"
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/1.3.11"
mkdir -p "${GOENV_ROOT}/versions/4.5.2"
run goenv-local 2
assert_success ""
run goenv-local
assert_success "1.2.10"
}

@test "goenv local sets latest version when minor version is given as major.minor number and any matching version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.2.10"
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/1.2.2"
mkdir -p "${GOENV_ROOT}/versions/1.3.11"
mkdir -p "${GOENV_ROOT}/versions/2.1.2"
run goenv-local 1.2
assert_success ""
run goenv-local
assert_success "1.2.10"
}

@test "goenv local fails setting latest version when major.minor number is given and does not match at 'GOENV_ROOT/versions/<version>'" {
mkdir -p "${GOENV_ROOT}/versions/1.1.9"
run goenv-local 1.9
assert_failure "goenv: version '1.9' not installed"
}

@test "fails setting local version when version argument is given and does not match at 'GOENV_ROOT/versions/<version>'" {
mkdir -p "${GOENV_ROOT}/versions/1.2.3"
run goenv-local 1.2.4
assert_failure "goenv: version '1.2.4' not installed"
}

@test "changes local existing version with version argument when version argument is given and does match at 'GOENV_ROOT/versions/<version>'" {
@test "changes local existing version with version argument when version argument is given and does match at 'GOENV_ROOT/versions/<version>'" {
echo "1.0-pre" > .go-version
run goenv-local
assert_success "1.0-pre"
Expand Down
Loading

0 comments on commit d1064b4

Please sign in to comment.