Skip to content

Commit

Permalink
added proper symlink test
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed Jul 2, 2018
1 parent 67c0899 commit 77f8aa3
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 12 deletions.
39 changes: 39 additions & 0 deletions lib/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -162,3 +163,41 @@ func ReadLines(path string) (lines []string, err error) {
}
return
}

//IsDirEmpty : check if directory is empty (TODO UNIT TEST)
func IsDirEmpty(name string) bool {

exist := false

f, err := os.Open(name)
if err != nil {
log.Fatal(err)
}
defer f.Close()

_, err = f.Readdirnames(1) // Or f.Readdir(1)
if err == io.EOF {
exist = true
}
return exist // Either not empty or error, suits both cases
}

//CheckDirHasTGBin : // check binary exist (TODO UNIT TEST)
func CheckDirHasTGBin(dir, prefix string) bool {

exist := false

files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
//return exist, err
}
res := []string{}
for _, f := range files {
if !f.IsDir() && strings.HasPrefix(f.Name(), prefix) {
res = append(res, filepath.Join(dir, f.Name()))
exist = true
}
}
return exist
}
69 changes: 69 additions & 0 deletions lib/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"os/user"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
"time"

"github.com/warrensbox/terraform-switcher/lib"
)
Expand Down Expand Up @@ -289,3 +291,70 @@ func TestReadLines(t *testing.T) {
cleanUp(installLocation)

}

// TestIsDirEmpty : create empty directory, check if empty
func TestIsDirEmpty(t *testing.T) {

current := time.Now()

installPath := "/.terraform.versions_test/"

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

test_dir := current.Format("2006-01-02")
t.Logf("Create test dir: %v \n", test_dir)

createDirIfNotExist(installLocation)

createDirIfNotExist(installLocation + "/" + test_dir)

empty := lib.IsDirEmpty(installLocation + "/" + test_dir)

t.Logf("Expected directory to be empty %v [expected]", installLocation+"/"+test_dir)

if empty == true {
t.Logf("Directory empty")
} else {
t.Error("Directory not empty")
}

cleanUp(installLocation + "/" + test_dir)

cleanUp(installLocation)

}

// TestCheckDirHasTGBin : create tg file in directory, check if exist
func TestCheckDirHasTFBin(t *testing.T) {

goarch := runtime.GOARCH
goos := runtime.GOOS
installPath := "/.terraform.versions_test/"
installFile := "terraform"

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

createDirIfNotExist(installLocation)

createFile(installLocation + installFile + "_" + goos + "_" + goarch)

empty := lib.CheckDirHasTGBin(installLocation, installFile)

t.Logf("Expected directory to have tf file %v [expected]", installLocation+installFile+"_"+goos+"_"+goarch)

if empty == true {
t.Logf("Directory empty")
} else {
t.Error("Directory not empty")
}

cleanUp(installLocation)
}
20 changes: 8 additions & 12 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const (
installVersion = "terraform_"
binLocation = "/usr/local/bin/terraform"
installPath = "/.terraform.versions/"
macOS = "_darwin_amd64.zip"
linux = "_darwin_amd64.zip"
recentFile = "RECENT"
)

Expand Down Expand Up @@ -69,19 +67,19 @@ func Install(tfversion string) {

/* if selected version already exist, */
if fileExist {

/* remove current symlink if exist*/
exist := CheckFileExist(installedBinPath)
symlinkExist := CheckSymlink(installedBinPath)

if !exist {
fmt.Println("Symlink does not exist")
} else {
if symlinkExist {
fmt.Println("Reset symlink")
RemoveSymlink(installedBinPath)
}

/* set symlink to desired version */
CreateSymlink(installLocation+installVersion+tfversion, installedBinPath)
fmt.Printf("Swicthed terraform to version %q \n", tfversion)
os.Exit(0)
return
}

/* if selected version already exist, */
Expand All @@ -108,12 +106,10 @@ func Install(tfversion string) {
RemoveFiles(installLocation + installVersion + tfversion + "_" + goos + "_" + goarch + ".zip")

/* remove current symlink if exist*/
exist := CheckFileExist(installedBinPath)
symlinkExist := CheckSymlink(installedBinPath)

if !exist {
fmt.Println("Symlink does not exist")
} else {
fmt.Println("Symlink exist")
if symlinkExist {
fmt.Println("Reset symlink")
RemoveSymlink(installedBinPath)
}

Expand Down
22 changes: 22 additions & 0 deletions lib/symlink.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lib

import (
"fmt"
"log"
"os"
)
Expand Down Expand Up @@ -30,3 +31,24 @@ func RemoveSymlink(symlinkPath string) {
}
}
}

// CheckSymlink : check file is symlink
func CheckSymlink(symlinkPath string) bool {

//symlink := false
//fmt.Println("Checking symlink")

fi, err := os.Lstat(symlinkPath)
if err != nil {
fmt.Println(err)
// symlink = false
return false
}

if fi.Mode()&os.ModeSymlink != 0 {
//symlink = true
return true
}

return false
}
34 changes: 34 additions & 0 deletions lib/symlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,37 @@ func TestRemoveSymlink(t *testing.T) {
t.Logf("Symlink was removed %v [expected]", lnCheck)
}
}

// TestCheckSymlink : Create symlink, test if file is symlink
func TestCheckSymlink(t *testing.T) {

testSymlinkSrc := "/test-tgshifter-src"

testSymlinkDest := "/test-tgshifter-dest"

usr, errCurr := user.Current()
if errCurr != nil {
log.Fatal(errCurr)
}
symlinkPathSrc := usr.HomeDir + testSymlinkSrc
symlinkPathDest := usr.HomeDir + testSymlinkDest

ln, _ := os.Readlink(symlinkPathSrc)

if ln != symlinkPathDest {
t.Log("Creating symlink")
if err := os.Symlink(symlinkPathDest, symlinkPathSrc); err != nil {
t.Error(err)
}
}

symlinkExist := lib.CheckSymlink(symlinkPathSrc)

if symlinkExist {
t.Logf("Symlink does exist %v [expected]", ln)
} else {
t.Logf("Symlink does not exist %v [unexpected]", ln)
}

os.Remove(symlinkPathSrc)
}

0 comments on commit 77f8aa3

Please sign in to comment.