Skip to content

Commit

Permalink
🏁✔️ Improved a few stuff; Completed test for add_test.go
Browse files Browse the repository at this point in the history
 Issues related:
 	-> #9
 	-> #16

 On branch devel
 Changes to be committed:
	modified:   cmd/add_test.go
	modified:   cmd/init.go
	modified:   cmd/list.go
  • Loading branch information
ChacaS0 committed Mar 5, 2018
1 parent 76041e3 commit 640ee42
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
70 changes: 70 additions & 0 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,73 @@ func TestTreatLastChar(t *testing.T) {
}
}
}

// TestAddLine is the test for addLine(args []string) error {}
// Check if it does add the proper line to a .tempestcf file.
func TestAddLine(t *testing.T) {
// TODO: use a temporary Tempestcf file by changing value of ``Tempestcf``
// save old Tempestcf
TempestcfOld := Tempestcf
// set the new Tempestcf
Tempestcf = conf.Gopath + string(os.PathSeparator) + ".tempestcf.temp"

// Create first the temporary new .tempestcf as $GOPATH/.tempestcf.temp
f, err := os.OpenFile(Tempestcf, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
t.Error(err)
}
defer f.Close()

// args to add
args := []string{
conf.Gopath + string(os.PathSeparator) + "src" + string(os.PathSeparator) + "github.com" + string(os.PathSeparator) + "ChacaS0" + string(os.PathSeparator) + "tempest" + string(os.PathSeparator) + "vendor",
conf.Gopath + string(os.PathSeparator) + "src" + string(os.PathSeparator) + "github.com" + string(os.PathSeparator) + "ChacaS0" + string(os.PathSeparator) + "tempest" + string(os.PathSeparator) + "cmd",
conf.Gopath + string(os.PathSeparator) + "src" + string(os.PathSeparator) + "github.com" + string(os.PathSeparator) + "ChacaS0" + string(os.PathSeparator) + "tempest",
}

// Add the test lines (args)
errAddLn := addLine(args)
if errAddLn != nil {
errDel := os.Remove(Tempestcf)
if errDel != nil {
t.Log(errDel)
}
t.Error(errAddLn)
}

// Get all paths added in Tempestcf
allPaths, errAllP := getPaths()
if errAllP != nil {
errDel := os.Remove(Tempestcf)
if errDel != nil {
t.Log(errDel)
}
t.Error(errAllP)
}

var cpt int
for _, onePath := range allPaths {
for _, oneArg := range args {
if onePath == oneArg {
cpt++
}
}
}
if cpt != len(args) {
errDel := os.Remove(Tempestcf)
if errDel != nil {
t.Log(errDel)
}
t.Log("[FAIL]:: Did not add all paths to ", Tempestcf)
t.Fail()
}

// Delete the temporary Tempestcf
errDel := os.Remove(Tempestcf)
if errDel != nil {
t.Log(errDel)
}
// Set back the old Tempestcf
Tempestcf = TempestcfOld

}
6 changes: 3 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ func init() {
// initializeTP creates the ~/.tempestcf file or
// empty it if already exists
func initializeTP() error {
f, err := os.OpenFile(conf.Home+"/.tempestcf", os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0644)
f, err := os.OpenFile(Tempestcf, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
errDel := os.Remove(conf.Home + "/.tempestcf")
errDel := os.Remove(Tempestcf)
if errDel != nil {
fmt.Println(redB("::"), color.RedString("Error while replacing existing file (.tempestcf)"))
return errDel
}
f2, err2 := os.OpenFile(conf.Home+"/.tempestcf", os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0644)
f2, err2 := os.OpenFile(Tempestcf, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0644)
if err2 != nil {
fmt.Println(redB("::"), color.HiRedString("Huge error! All data lost, could not recreate file! Your life officially sucks!"))
return err2
Expand Down
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func printList() error {
// getPaths returns a slice of string being all the paths for TEMPest to purge
// and an error if something goes wrong
func getPaths() (returnSlice []string, pathsError error) {
ctnt, pathsError := ioutil.ReadFile(conf.Home + "/.tempestcf")
ctnt, pathsError := ioutil.ReadFile(Tempestcf)
if pathsError != nil {
fmt.Println(color.RedString("::Could not read the file muthafuckkah!"))
return nil, pathsError
Expand Down

0 comments on commit 640ee42

Please sign in to comment.