Skip to content

Commit

Permalink
Futher fix version compare function.
Browse files Browse the repository at this point in the history
  • Loading branch information
pda committed Mar 3, 2020
1 parent 0c78b1a commit ae9d331
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
11 changes: 6 additions & 5 deletions hooks/environment
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function aws_version_ge() {
version_a_gte_b "$current" "$wanted"
}

# Whether version A >= B, for versions in the form of X.Y.Z
# Whether version A >= B, for versions in the form of major.minor.patch
# Returns success (0) if true, and error (1) if false.
function version_a_gte_b() {
IFS="." read -r -a a <<< "$1"
Expand All @@ -39,10 +39,11 @@ function version_a_gte_b() {
echo "Expected $2 to be in the form x.y.z" >&2
exit 1
fi
[[ ${a[0]} -lt ${b[0]} ]] && return 1
[[ ${a[1]} -lt ${b[1]} ]] && return 1
[[ ${a[2]} -lt ${b[2]} ]] && return 1
return 0
[[ ${a[0]} -lt ${b[0]} ]] && return 1 # major less
[[ ${a[0]} -gt ${b[0]} ]] && return 0 # major more
[[ ${a[1]} -lt ${b[1]} ]] && return 1 # major same, minor less
[[ ${a[1]} -gt ${b[1]} ]] && return 0 # major same, minor more
[[ ${a[2]} -ge ${b[2]} ]] && return 0 # major same, minor same, patch same or more
}

# Retries a command on failure.
Expand Down
40 changes: 14 additions & 26 deletions tests/version.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,27 @@
load '/usr/local/lib/bats/load.bash'
load "$PWD/hooks/environment"

@test "version_a_gte_b: version compare: equal" {
run version_a_gte_b 1.2.3 1.2.3
assert_success
}
@test "version_a_gte_b: version compare: simple major version success" {
run version_a_gte_b 2.0.0 1.0.0
assert_success
}
@test "version_a_gte_b: version compare: simple major version failure" {
run version_a_gte_b 1.0.0 2.0.0
@test "version_a_gte_b: basic: major less; false" {
run version_a_gte_b "1.3.3" "3.2.1"
assert_failure
}
@test "version_a_gte_b: simple minor version success" {
run version_a_gte_b 1.2.0 1.1.0
@test "version_a_gte_b: basic: major more; true" {
run version_a_gte_b "3.2.1" "1.3.2"
assert_success
}
@test "version_a_gte_b: simple minor version failure" {
run version_a_gte_b 1.1.0 1.2.0
@test "version_a_gte_b: basic: major same, minor less; false" {
run version_a_gte_b "3.2.1" "3.3.0"
assert_failure
}
@test "version_a_gte_b: simple patch version success" {
run version_a_gte_b 1.2.1 1.2.0
@test "version_a_gte_b: basic: major same, minor more; true" {
run version_a_gte_b "3.2.1" "3.1.2"
assert_success
}
@test "version_a_gte_b: simple patch version failure" {
run version_a_gte_b 1.2.0 1.2.1
assert_failure
}
@test "version_a_gte_b: smaller major but bigger minor; failure" {
run version_a_gte_b 1.4.0 2.3.0
assert_failure
@test "version_a_gte_b: basic: major same, minor same, patch same; true" {
run version_a_gte_b "1.1.1" "1.1.1"
assert_success
}
@test "version_a_gte_b: smaller minor but bigger patch; failure" {
run version_a_gte_b 1.4.8 1.6.2
assert_failure
@test "version_a_gte_b: basic: major same, minor same, patch more; true" {
run version_a_gte_b "1.1.2" "1.1.1"
assert_success
}

0 comments on commit ae9d331

Please sign in to comment.