Skip to content

Commit

Permalink
added files
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed May 22, 2018
1 parent 7b3bef1 commit de4ce84
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 30 deletions.
43 changes: 43 additions & 0 deletions lib/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package lib_test

import (
"fmt"
"log"
"os"
)

func checkFileExist(file string) bool {
_, err := os.Stat(file)
if err != nil {
return false
}
return true
}

func createFile(path string) {
// detect if file exists
var _, err = os.Stat(path)

// create file if not exists
if os.IsNotExist(err) {
file, err := os.Create(path)
if err != nil {
fmt.Printf("%v", err)
return
}
defer file.Close()
}

fmt.Println("==> done creating file", path)
}

func createDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
log.Printf("Creating directory for teraform: %v", dir)
err = os.MkdirAll(dir, 0755)
if err != nil {
fmt.Printf("Unable to create directory for teraform: %v", dir)
panic(err)
}
}
}
67 changes: 37 additions & 30 deletions lib/download_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lib
package lib_test

import (
"fmt"
Expand All @@ -8,19 +8,26 @@ import (
"os/user"
"path/filepath"
"testing"
)

const (
hashiURL = "https://releases.hashicorp.com/terraform/"
installFile = "terraform"
installVersion = "terraform_"
binLocation = "/usr/local/bin/terraform"
installPath = "/.terraform.versions_test/"
macOS = "_darwin_amd64.zip"
lib "github.com/warren-veerasingam/terraform-switcher/lib"
)

// const (
// hashiURL = "https://releases.hashicorp.com/terraform/"
// installFile = "terraform"
// installVersion = "terraform_"
// binLocation = "/usr/local/bin/terraform"
// installPath = "/.terraform.versions_test/"
// macOS = "_darwin_amd64.zip"
// )

func TestDownloadURL(t *testing.T) {

hashiURL := "https://releases.hashicorp.com/terraform/"
installVersion := "terraform_"
installPath := "/.terraform.versions_test/"
macOS := "_darwin_amd64.zip"

// get current user
usr, errCurr := user.Current()
if errCurr != nil {
Expand All @@ -45,32 +52,32 @@ func TestDownloadURL(t *testing.T) {

url := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS
expectedFile := usr.HomeDir + installPath + installVersion + lowestVersion + macOS
installFile, _ := DownloadFromURL(installLocation, url)
installedFile, _ := lib.DownloadFromURL(installLocation, url)

if installFile == expectedFile {
if installedFile == expectedFile {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installFile)
t.Logf("Downloaded file %v", installedFile)
t.Log("Download file matches expected file")
} else {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installFile)
t.Error("Downoad file mismatches expected file")
t.Logf("Downloaded file %v", installedFile)
t.Error("Download file mismatches expected file")
}

/* test download latest terraform version */
latestVersion := "0.11.7"

url = hashiURL + latestVersion + "/" + installVersion + latestVersion + macOS
expectedFile = usr.HomeDir + installPath + installVersion + latestVersion + macOS
installFile, _ = DownloadFromURL(installLocation, url)
installedFile, _ = lib.DownloadFromURL(installLocation, url)

if installFile == expectedFile {
if installedFile == expectedFile {
t.Logf("Expected file name %v", expectedFile)
t.Logf("Downloaded file name %v", installFile)
t.Logf("Downloaded file name %v", installedFile)
t.Log("Download file name matches expected file")
} else {
t.Logf("Expected file name %v", expectedFile)
t.Logf("Downloaded file name %v", installFile)
t.Logf("Downloaded file name %v", installedFile)
t.Error("Downoad file name mismatches expected file")
}

Expand All @@ -79,6 +86,12 @@ func TestDownloadURL(t *testing.T) {

func TestDownloadedFileExist(t *testing.T) {

hashiURL := "https://releases.hashicorp.com/terraform/"
installFile := "terraform"
installVersion := "terraform_"
installPath := "/.terraform.versions_test/"
macOS := "_darwin_amd64.zip"

// get current user
usr, errCurr := user.Current()
if errCurr != nil {
Expand All @@ -103,15 +116,15 @@ func TestDownloadedFileExist(t *testing.T) {

url := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS
expectedFile := usr.HomeDir + installPath + installVersion + lowestVersion + macOS
installFile, _ := DownloadFromURL(installLocation, url)
installedFile, _ := lib.DownloadFromURL(installLocation, url)

if checkFileExist(expectedFile) {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installFile)
t.Logf("Downloaded file %v", installedFile)
t.Log("Download file matches expected file")
} else {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installFile)
t.Logf("Downloaded file %v", installedFile)
t.Error("Downoad file mismatches expected file")
}

Expand All @@ -120,7 +133,7 @@ func TestDownloadedFileExist(t *testing.T) {

url = hashiURL + latestVersion + "/" + installVersion + latestVersion + macOS
expectedFile = usr.HomeDir + installPath + installVersion + latestVersion + macOS
installFile, _ = DownloadFromURL(installLocation, url)
installFile, _ = lib.DownloadFromURL(installLocation, url)

if checkFileExist(expectedFile) {
t.Logf("Expected file %v", expectedFile)
Expand All @@ -137,6 +150,8 @@ func TestDownloadedFileExist(t *testing.T) {

func TestURLValid(t *testing.T) {

hashiURL := "https://releases.hashicorp.com/terraform/"

url, err := url.ParseRequestURI(hashiURL)
if err != nil {
t.Errorf("Valid URL provided: %v", err)
Expand All @@ -151,14 +166,6 @@ func cleanUp(path string) {
removeFiles(path)
}

func checkFileExist(file string) bool {
_, err := os.Stat(file)
if err != nil {
return false
}
return true
}

func removeFiles(src string) {
files, err := filepath.Glob(src)
if err != nil {
Expand Down
51 changes: 51 additions & 0 deletions lib/files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package lib_test

import (
"log"
"os/user"
"testing"

"github.com/warren-veerasingam/terraform-switcher/lib"
)

func TestRenameFile(t *testing.T) {

installFile := "terraform"
installVersion := "terraform_"
installPath := "/.terraform.versions_test/"
version := "0.0.7"

usr, errCurr := user.Current()
if errCurr != nil {
log.Fatal(errCurr)
}
installLocation := usr.HomeDir + installPath

createDirIfNotExist(installLocation)

createFile(installLocation + installFile)

if exist := checkFileExist(installLocation + installFile); exist {
t.Logf("File exist %v", installLocation+installFile)
} else {
t.Logf("File does not exist %v", installLocation+installFile)
t.Error("Missing file")
}

lib.RenameFile(installLocation+installFile, installLocation+installVersion+version)

if exist := checkFileExist(installLocation + installVersion + version); exist {
t.Logf("New file exist %v", installLocation+installVersion+version)
} else {
t.Logf("New file does not exist %v", installLocation+installVersion+version)
t.Error("Missing new file")
}

if exist := checkFileExist(installLocation + installFile); exist {
t.Logf("Old file should not exist %v", installLocation+installFile)
t.Error("Did not rename file")
} else {
t.Logf("Old file does not exist %v", installLocation+installFile)
}

}

0 comments on commit de4ce84

Please sign in to comment.