Skip to content

Commit

Permalink
Complete files library testing
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed May 22, 2018
1 parent 9f2f5fc commit a7e3d4c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions lib/files_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package lib_test

import (
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"strings"
"testing"

"github.com/warren-veerasingam/terraform-switcher/lib"
Expand Down Expand Up @@ -88,3 +92,72 @@ func TestRemoveFiles(t *testing.T) {

cleanUp(installLocation)
}

// TestUnzip : Create a file, check file exist,
// remove file, check file does not exist
func TestUnzip(t *testing.T) {

installPath := "/.terraform.versions_test/"
absPath, _ := filepath.Abs("../test-data/test-data.zip")

fmt.Println(absPath)

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

createDirIfNotExist(installLocation)

files, errUnzip := lib.Unzip(absPath, installLocation)

if errUnzip != nil {
fmt.Println("Unable to unzip zip file")
log.Fatal(errUnzip)
os.Exit(1)
}

tst := strings.Join(files, "")

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

cleanUp(installLocation)
}

// TestCreateDirIfNotExist : Create a directory, check directory exist,
func TestCreateDirIfNotExist(t *testing.T) {

installPath := "/.terraform.versions_test/"

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

cleanUp(installLocation)

if _, err := os.Stat(installLocation); os.IsNotExist(err) {
t.Logf("Directory should not exist %v (expected)", installLocation)
} else {
t.Logf("Directory already exist %v (unexpected)", installLocation)
t.Error("Directory should not exist")
}

lib.CreateDirIfNotExist(installLocation)
t.Logf("Creating directory %v", installLocation)

if _, err := os.Stat(installLocation); err == nil {
t.Logf("Directory exist %v (expected)", installLocation)
} else {
t.Logf("Directory should exist %v (unexpected)", installLocation)
t.Error("Directory should exist")
}

}
Binary file added test-data/test-data.zip
Binary file not shown.

0 comments on commit a7e3d4c

Please sign in to comment.