Skip to content

Commit

Permalink
versionCompare() needs to have a trinary result
Browse files Browse the repository at this point in the history
- Added tests for `versionCompare()`
  • Loading branch information
dshafik committed Aug 10, 2017
1 parent a2e9a27 commit 73f3a25
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
40 changes: 20 additions & 20 deletions akamai.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ func installPHP(dir string, cmdPackage commandPackage) (bool, error) {
return false, cli.NewExitError(fmt.Sprintf("PHP %s is required to install this command. Unable to determine installed version.", cmdPackage.Requirements.Php), 1)
}

if !versionCompare(matches[1], cmdPackage.Requirements.Php) {
if versionCompare(cmdPackage.Requirements.Php, matches[1]) == -1 {
return false, cli.NewExitError(fmt.Sprintf("PHP %s is required to install this command.", cmdPackage.Requirements.Php), 1)
}
}
Expand Down Expand Up @@ -809,7 +809,7 @@ func installJavaScript(dir string, cmdPackage commandPackage) (bool, error) {
output, _ := cmd.Output()
r, _ := regexp.Compile("^v(.*?)\\s*$")
matches := r.FindStringSubmatch(string(output))
if !versionCompare(matches[1], cmdPackage.Requirements.Node) {
if versionCompare(cmdPackage.Requirements.Node, matches[1]) == -1 {
return false, cli.NewExitError(fmt.Sprintf("Node.js %s is required to install this command.", cmdPackage.Requirements.Node), 1)
}
}
Expand Down Expand Up @@ -854,7 +854,7 @@ func installRuby(dir string, cmdPackage commandPackage) (bool, error) {
output, _ := cmd.Output()
r, _ := regexp.Compile("^ruby (.*?)(p.*?) (.*)")
matches := r.FindStringSubmatch(string(output))
if !versionCompare(matches[1], cmdPackage.Requirements.Ruby) {
if versionCompare(cmdPackage.Requirements.Ruby, matches[1]) == -1 {
return false, cli.NewExitError(fmt.Sprintf("Ruby %s is required to install this command.", cmdPackage.Requirements.Ruby), 1)
}
}
Expand Down Expand Up @@ -882,7 +882,7 @@ func installPython(dir string, cmdPackage commandPackage) (bool, error) {
)

if cmdPackage.Requirements.Python != "" && cmdPackage.Requirements.Python != "*" {
if !versionCompare("3.0.0", cmdPackage.Requirements.Python) {
if versionCompare("3.0.0", cmdPackage.Requirements.Python) != -1 {
bin, err = exec.LookPath("python3")
if err != nil {
bin, err = exec.LookPath("python")
Expand Down Expand Up @@ -920,14 +920,14 @@ func installPython(dir string, cmdPackage commandPackage) (bool, error) {
output, _ := cmd.CombinedOutput()
r, _ := regexp.Compile(`Python (\d+\.\d+\.\d+).*`)
matches := r.FindStringSubmatch(string(output))
if !versionCompare(matches[1], cmdPackage.Requirements.Python) {
if versionCompare(cmdPackage.Requirements.Python, matches[1]) == -1 {
return false, cli.NewExitError(fmt.Sprintf("Python %s is required to install this command.", cmdPackage.Requirements.Python), 1)
}
}

if _, err := os.Stat(dir + string(os.PathSeparator) + "/requirements.txt"); err == nil {
if cmdPackage.Requirements.Python != "" && cmdPackage.Requirements.Python != "*" {
if !versionCompare("3.0.0", cmdPackage.Requirements.Python) {
if versionCompare("3.0.0", cmdPackage.Requirements.Python) != -1 {
bin, err = exec.LookPath("pip3")
if err != nil {
bin, err = exec.LookPath("pip")
Expand Down Expand Up @@ -994,7 +994,7 @@ func installGolang(dir string, cmdPackage commandPackage) (bool, error) {
output, _ := cmd.Output()
r, _ := regexp.Compile("go version go(.*?) .*")
matches := r.FindStringSubmatch(string(output))
if !versionCompare(matches[1], cmdPackage.Requirements.Go) {
if versionCompare(cmdPackage.Requirements.Go, matches[1]) == -1 {
return false, cli.NewExitError(fmt.Sprintf("Go %s is required to install this command.", cmdPackage.Requirements.Go), 1)
}
}
Expand Down Expand Up @@ -1226,7 +1226,7 @@ func findExec(cmd string) ([]string, error) {
}
cmd = []string{bin, cmdFile}
case language == "python":
if !versionCompare("3.0.0", cmdPackage.Requirements.Python) {
if versionCompare("3.0.0", cmdPackage.Requirements.Python) != -1 {
bin, err = exec.LookPath("python3")
if err != nil {
bin, err = exec.LookPath("python")
Expand Down Expand Up @@ -1292,14 +1292,14 @@ func findPackageDir(dir string) string {
return dir
}

func versionCompare(compareTo string, isNewer string) bool {
leftParts := strings.Split(compareTo, ".")
func versionCompare(left string, right string) int {
leftParts := strings.Split(left, ".")
leftMajor, _ := strconv.Atoi(leftParts[0])
leftMinor := 0
leftMicro := 0

if compareTo == isNewer {
return false
if left == right {
return 0
}

if len(leftParts) > 1 {
Expand All @@ -1309,7 +1309,7 @@ func versionCompare(compareTo string, isNewer string) bool {
leftMicro, _ = strconv.Atoi(leftParts[2])
}

rightParts := strings.Split(isNewer, ".")
rightParts := strings.Split(right, ".")
rightMajor, _ := strconv.Atoi(rightParts[0])
rightMinor := 0
rightMicro := 0
Expand All @@ -1321,19 +1321,19 @@ func versionCompare(compareTo string, isNewer string) bool {
rightMicro, _ = strconv.Atoi(rightParts[2])
}

if leftMajor < rightMajor {
return false
if leftMajor > rightMajor {
return -1
}

if leftMajor == rightMajor && leftMinor < rightMinor {
return false
if leftMajor == rightMajor && leftMinor > rightMinor {
return -1
}

if leftMajor == rightMajor && leftMinor == rightMinor && leftMicro < rightMicro {
return false
if leftMajor == rightMajor && leftMinor == rightMinor && leftMicro > rightMicro {
return -1
}

return true
return 1
}

func getSpinner(prefix string, finalMsg string) *spinner.Spinner {
Expand Down
25 changes: 25 additions & 0 deletions akamai_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import "testing"

func TestVersionCompare(t *testing.T) {
versionTests := []struct {
left string
right string
result int
}{
{"0.9.9", "1.0.0", 1},
{"0.1.0", "0.2.0", 1},
{"0.3.0", "0.3.1", 1},
{"0.1.0", "0.1.0", 0},
{"1.0.0", "0.9.9", -1},
{"0.2.0", "0.1.0", -1},
{"0.3.1", "0.3.0", -1},
}

for _, tt := range versionTests {
if result := versionCompare(tt.left, tt.right); result != tt.result {
t.Errorf("versionCompare(%s, %s) => %d, wanted: %d", tt.left, tt.right, result, tt.result)
}
}
}
2 changes: 1 addition & 1 deletion firstrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func firstRun() error {
checkUpdate:

cliPath, _ := getAkamaiCliPath()
updateFile := cliPath + string(os.PathSeparator) + ".update-check"
updateFile := cliPath + string(os.PathSeparator) + ".upgrade-check"
_, err = os.Stat(updateFile)
if os.IsNotExist(err) {
if inPath {
Expand Down
2 changes: 1 addition & 1 deletion upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func checkForUpgrade(force bool) string {
}

latestVersion := getLatestReleaseVersion()
if versionCompare(latestVersion, VERSION) {
if versionCompare(VERSION, latestVersion) == 1 {
if !force {
fmt.Printf(
"New upgrade found: %s (you are running: %s). Upgrade now? [Y/n]: ",
Expand Down

0 comments on commit 73f3a25

Please sign in to comment.