From fcdc24bc76113651c24d2b3edcc3cd2875801c04 Mon Sep 17 00:00:00 2001 From: "warren.veerasingam@gmail.com" Date: Mon, 20 May 2019 23:54:02 -0500 Subject: [PATCH] updated test cases --- lib/download_test.go | 81 ++++++++++++++++++++++++++++++++++++++------ lib/install.go | 11 ++++-- lib/list_versions.go | 14 ++++++-- main.go | 6 ++-- 4 files changed, 93 insertions(+), 19 deletions(-) diff --git a/lib/download_test.go b/lib/download_test.go index d21b08e7..f4785ae7 100644 --- a/lib/download_test.go +++ b/lib/download_test.go @@ -39,11 +39,16 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) { } /* test download lowest terraform version */ - lowestVersion := "0.0.1" + lowestVersion := "0.1.0" url := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS expectedFile := usr.HomeDir + installPath + installVersion + lowestVersion + macOS - installedFile, _ := lib.DownloadFromURL(installLocation, url) + installedFile, errDownload := lib.DownloadFromURL(installLocation, url) + + if errDownload != nil { + t.Logf("Expected file name %v to be downloaded", expectedFile) + t.Error("Download not possible (unexpected)") + } if installedFile == expectedFile { t.Logf("Expected file %v", expectedFile) @@ -52,7 +57,7 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) { } else { t.Logf("Expected file %v", expectedFile) t.Logf("Downloaded file %v", installedFile) - t.Error("Download file mismatches expected file") + t.Error("Download file mismatches expected file (unexpected)") } /* test download latest terraform version */ @@ -60,7 +65,12 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) { url = hashiURL + latestVersion + "/" + installVersion + latestVersion + macOS expectedFile = usr.HomeDir + installPath + installVersion + latestVersion + macOS - installedFile, _ = lib.DownloadFromURL(installLocation, url) + installedFile, errDownload = lib.DownloadFromURL(installLocation, url) + + if errDownload != nil { + t.Logf("Expected file name %v to be downloaded", expectedFile) + t.Error("Download not possible (unexpected)") + } if installedFile == expectedFile { t.Logf("Expected file name %v", expectedFile) @@ -69,7 +79,7 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) { } else { t.Logf("Expected file name %v", expectedFile) t.Logf("Downloaded file name %v", installedFile) - t.Error("Downoad file name mismatches expected file") + t.Error("Dowload file name mismatches expected file (unexpected)") } cleanUp(installLocation) @@ -104,11 +114,16 @@ func TestDownloadFromURL_FileExist(t *testing.T) { } /* test download lowest terraform version */ - lowestVersion := "0.0.1" + lowestVersion := "0.1.0" url := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS expectedFile := usr.HomeDir + installPath + installVersion + lowestVersion + macOS - installedFile, _ := lib.DownloadFromURL(installLocation, url) + installedFile, errDownload := lib.DownloadFromURL(installLocation, url) + + if errDownload != nil { + t.Logf("Expected file name %v to be downloaded", expectedFile) + t.Error("Download not possible (unexpected)") + } if checkFileExist(expectedFile) { t.Logf("Expected file %v", expectedFile) @@ -117,7 +132,7 @@ func TestDownloadFromURL_FileExist(t *testing.T) { } else { t.Logf("Expected file %v", expectedFile) t.Logf("Downloaded file %v", installedFile) - t.Error("Downoad file mismatches expected file") + t.Error("Download file mismatches expected file (unexpected)") } /* test download latest terraform version */ @@ -125,7 +140,12 @@ func TestDownloadFromURL_FileExist(t *testing.T) { url = hashiURL + latestVersion + "/" + installVersion + latestVersion + macOS expectedFile = usr.HomeDir + installPath + installVersion + latestVersion + macOS - installFile, _ = lib.DownloadFromURL(installLocation, url) + installFile, errDownload = lib.DownloadFromURL(installLocation, url) + + if errDownload != nil { + t.Logf("Expected file name %v to be downloaded", expectedFile) + t.Error("Download not possible (unexpected)") + } if checkFileExist(expectedFile) { t.Logf("Expected file %v", expectedFile) @@ -134,12 +154,53 @@ func TestDownloadFromURL_FileExist(t *testing.T) { } else { t.Logf("Expected file %v", expectedFile) t.Logf("Downloaded file %v", installFile) - t.Error("Downoad file mismatches expected file") + t.Error("Download file mismatches expected file (unexpected)") + } + + cleanUp(installLocation) +} + +// TestInvalidURL : Invalid url should throw an error +func TestInvalidURL(t *testing.T) { + + hashiURL := "https://releases.hashicorp.com/terraform/" + installVersion := "terraform_" + installPath := "/.terraform.versions_test/" + macOS := "_darwin_amd64.zip" + invalidVersion := "0.11.7-nonexistent" + + // get current user + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + + fmt.Printf("Current user: %v \n", usr.HomeDir) + installLocation := usr.HomeDir + installPath + + // create /.terraform.versions_test/ directory to store code + if _, err := os.Stat(installLocation); os.IsNotExist(err) { + log.Printf("Creating directory for terraform: %v\n", installLocation) + err = os.MkdirAll(installLocation, 0755) + if err != nil { + fmt.Printf("Unable to create directory for terraform: %v\n", installLocation) + panic(err) + } + } + + url := hashiURL + invalidVersion + "/" + installVersion + invalidVersion + macOS + //expectedFile := usr.HomeDir + installPath + installVersion + invalidVersion + macOS + _, errDownload := lib.DownloadFromURL(installLocation, url) + + if errDownload != nil { + t.Logf("Unable to download from %s - invalid url or version (expected)\n", url) + t.Logf("Download not possible (expected)") } cleanUp(installLocation) } +// TestDownloadFromURL_Valid : Test if https://releases.hashicorp.com/terraform/ is still valid func TestDownloadFromURL_Valid(t *testing.T) { hashiURL := "https://releases.hashicorp.com/terraform/" diff --git a/lib/install.go b/lib/install.go index 8e615796..618269d7 100644 --- a/lib/install.go +++ b/lib/install.go @@ -78,6 +78,7 @@ func Install(tfversion string) { /* set symlink to desired version */ CreateSymlink(installLocation+installVersion+tfversion, installedBinPath) fmt.Printf("Switched terraform to version %q \n", tfversion) + AddRecent(tfversion) //add to recent file for faster lookup os.Exit(0) } @@ -86,6 +87,7 @@ func Install(tfversion string) { url := hashiURL + tfversion + "/" + installVersion + tfversion + "_" + goos + "_" + goarch + ".zip" zipFile, errDownload := DownloadFromURL(installLocation, url) + /* If unable to download file from url, exit(1) immediately */ if errDownload != nil { fmt.Println(errDownload) os.Exit(1) @@ -166,7 +168,7 @@ func GetRecentVersions() ([]string, error) { if fileExist { lines, errRead := ReadLines(installLocation + recentFile) - appendRecent := []string{} + outputRecent := []string{} if errRead != nil { fmt.Printf("Error: %s\n", errRead) @@ -183,10 +185,13 @@ func GetRecentVersions() ([]string, error) { return nil, errRead } - appendRecent = append(appendRecent, fmt.Sprintf("%s *recent", line)) + /* output can be confusing since it displays the 3 most recent used terraform version + append the string *recent to the output to make it more user friendly + */ + outputRecent = append(outputRecent, fmt.Sprintf("%s *recent", line)) } - return appendRecent, nil + return outputRecent, nil } return nil, nil diff --git a/lib/list_versions.go b/lib/list_versions.go index 6991cf2a..c7ca1fb2 100644 --- a/lib/list_versions.go +++ b/lib/list_versions.go @@ -36,10 +36,13 @@ func GetTFList(hashiURL string, listAll bool) ([]string, error) { var tfVersionList tfVersionList for i := range result { - //getting versions from body; should return match /X.X.X/ + // Getting versions from body; should return match /X.X.X/ where X is a number + // Follow https://semver.org/spec/v2.0.0.html r, _ := regexp.Compile(`\/(\d+\.\d+\.\d+)\/`) - //r, _ := regexp.Compile(`\/(\d+)(\.)(\d+)(\.)(\d+)\/`) if listAll { + // Getting versions from body; should return match /X.X.X-@/ where X is a number,@ is a word character between a-z or A-Z + // Follow https://semver.org/spec/v1.0.0-beta.html + // Check regular expression at https://rubular.com/r/ju3PxbaSBALpJB r, _ = regexp.Compile(`\/(\d+\.\d+\.\d+)(-[a-zA-z]+\d*)?\/`) } @@ -95,12 +98,17 @@ func RemoveDuplicateVersions(elements []string) []string { } // ValidVersionFormat : returns valid version format -// For example: 0.1.2 = valid +/* For example: 0.1.2 = valid // For example: 0.1.2-beta1 = valid +// For example: 0.1.2-alpha = valid // For example: a.1.2 = invalid // For example: 0.1. 2 = invalid +*/ func ValidVersionFormat(version string) bool { + // Getting versions from body; should return match /X.X.X-@/ where X is a number,@ is a word character between a-z or A-Z + // Follow https://semver.org/spec/v1.0.0-beta.html + // Check regular expression at https://rubular.com/r/ju3PxbaSBALpJB semverRegex := regexp.MustCompile(`^(\d+\.\d+\.\d+)(-[a-zA-z]+\d*)?$`) return semverRegex.MatchString(version) diff --git a/main.go b/main.go index d7231b62..523ef183 100644 --- a/main.go +++ b/main.go @@ -63,9 +63,9 @@ func main() { if len(args) == 1 { //if tf version is provided in command line if lib.ValidVersionFormat(args[0]) { - requestedVersion := args[0] - listAll := true //set list all true - all versions including beta and rc will be displayed + requestedVersion := args[0] + listAll := true //set list all true - all versions including beta and rc will be displayed tflist, _ := lib.GetTFList(hashiURL, listAll) //get list of versions exist := lib.VersionExist(requestedVersion, tflist) //check if version exist before downloading it @@ -132,7 +132,7 @@ func installOption(listAll bool) { } _, tfversion, errPrompt := prompt.Run() - tfversion = strings.Trim(tfversion, " *recent") //trim versions with the word *recent included + tfversion = strings.Trim(tfversion, " *recent") //trim versions with the string " *recent" appended if errPrompt != nil { log.Printf("Prompt failed %v\n", errPrompt)