-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b3bef1
commit de4ce84
Showing
3 changed files
with
131 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |