Skip to content

Commit

Permalink
🏁💻 Made rm_test.go; Fixed some path to use PathSeparator
Browse files Browse the repository at this point in the history
 Issues related:
 	-> #16
 	-> #9

 On branch devel
 Changes to be committed:
	modified:   cmd/rm.go
	modified:   cmd/rm_test.go
  • Loading branch information
ChacaS0 committed Mar 11, 2018
1 parent e5cfeff commit 4288769
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 5 deletions.
10 changes: 5 additions & 5 deletions cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func rmInSlice(index int, record string, list []string) []string {
return listToRet
}

// xxx saves the new list of paths meant to be targets for TEMPest
// writeTempestcf saves the new list of paths meant to be targets for TEMPest
func writeTempestcf(targets []string) error {
// Backup of the old one
if errBp := backupTempestcf(); errBp != nil {
Expand All @@ -165,9 +165,9 @@ func writeTempestcf(targets []string) error {
} */

// Open the file to write to
tmpcf, err := os.OpenFile(conf.Home+"/.tempestcf", os.O_APPEND|os.O_WRONLY, os.ModeAppend)
tmpcf, err := os.OpenFile(Tempestcf, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
fmt.Println(redB("::"), color.RedString("Sorry! Could not find ~/.tempestcf ! :("))
fmt.Println(redB("::"), color.RedString("Sorry! Could not find "+Tempestcf+" ! :("))
return err
}
defer tmpcf.Close()
Expand Down Expand Up @@ -197,7 +197,7 @@ func writeTempestcf(targets []string) error {

// backupTempestcf save the current .tempestcf as .tempestcf.old
func backupTempestcf() error {
errBup := os.Rename(conf.Home+"/.tempestcf", conf.Home+"/.tempestcf.old")
errBup := os.Rename(Tempestcf, Tempestcf+".old")
if errBup != nil {
return errBup
}
Expand All @@ -206,7 +206,7 @@ func backupTempestcf() error {

// retoreTempestcf bring back the previous .tempestcf
func restoreTempestcf() error {
errRestore := os.Rename(conf.Home+"/.tempestcf.old", conf.Home+"/.tempestcf")
errRestore := os.Rename(Tempestcf+".old", Tempestcf)
if errRestore != nil {
return errRestore
}
Expand Down
163 changes: 163 additions & 0 deletions cmd/rm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"os"
"testing"
)

Expand Down Expand Up @@ -41,6 +42,168 @@ func TestRmInSlice(t *testing.T) {
}
}

// TestBackupTempestcf checks if the backup is really done
// by backupTempestcf
func TestBackupTempestcf(t *testing.T) {
// Save old Tempestcf
tempestcfBup := Tempestcf
// Set the new Tempestcf for testing
Tempestcf = conf.Gopath + string(os.PathSeparator) + ".tempestcf"

// 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()

sl1 := []string{
conf.Gobin,
conf.Gopath,
}

// Add a few things to it
if err := addLine(sl1); err != nil {
t.Log("[ERROR]:: Failed to add lines to", Tempestcf, "\n\t->", err)
if errDel := os.Remove(Tempestcf); errDel != nil {
t.Log("[ERROR]:: Could not delete the .tempestcf.temp")
}
Tempestcf = tempestcfBup
t.FailNow()
}

// Try to make a backup
if err := backupTempestcf(); err != nil {
t.Log("[FAIL]:: Could not back up the file:", Tempestcf, "\n\t->", err)
t.Fail()
}

// Check if the bup still has the previous content (sl1)
Tempestcf += ".old"
sl2, errPaths := getPaths()
if errPaths != nil {
t.Log("[ERROR]:: Could not get the paths of the .tempestcf.temp:", Tempestcf, "\n\t->", errPaths)
t.Fail()
}
if !SameSlices(sl1, sl2) {
t.Log("[FAIL]:: Not the same values for the two files, backup changed data!")
t.Fail()
}

// In the end we restore the previous Tempestcf and delete the .tempestcf.temp just in case
if errDel := os.Remove(Tempestcf); errDel != nil {
t.Log("[ERROR]:: Could not delete the .tempestcf.temp")
}
Tempestcf = tempestcfBup
}

// TestRestoreTempestcf check if it restores well a bup of .tempestcf
// with restoreTempestcf() error {}
func TestRestoreTempestcf(t *testing.T) {
// Save old Tempestcf
tempestcfBup := Tempestcf
// Set the new Tempestcf for testing
Tempestcf = conf.Gopath + string(os.PathSeparator) + ".tempestcf"

// 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()

sl1 := []string{
conf.Gobin,
conf.Gopath,
}

// Add a few things to it
if err := addLine(sl1); err != nil {
t.Log("[ERROR]:: Failed to add lines to", Tempestcf, "\n\t->", err)
if errDel := os.Remove(Tempestcf); errDel != nil {
t.Log("[ERROR]:: Could not delete the .tempestcf.temp")
}
Tempestcf = tempestcfBup
t.FailNow()
}

// Try to make a backup
if err := backupTempestcf(); err != nil {
t.Log("[FAIL]:: Could not back up the file:", Tempestcf, "\n\t->", err)
t.Fail()
}

// Now try to restore the previous file
if err := restoreTempestcf(); err != nil {
t.Log("[FAIL]:: Failed while restoring .tempestcf. Shame!\n\t->", err)
Tempestcf += ".old"
t.Fail()
}

// Then we restore the previous setup!?
if errDel := os.Remove(Tempestcf); errDel != nil {
t.Log("[ERROR]:: Could not delete the .tempestcf.temp")
}
Tempestcf = tempestcfBup
}

// TestWriteTempestcf checks if new data is well written to .tempestcf
// with ``writeTempestcf(targets []string) error {}``.
// It is supposed to override the .tempestcf targets with a new slice of targets.
func TestWriteTempestcf(t *testing.T) {
// Save old Tempestcf
tempestcfBup := Tempestcf
// Set the new Tempestcf for testing
Tempestcf = conf.Gopath + string(os.PathSeparator) + ".tempestcf"

// 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()

sl1 := []string{
conf.Gobin,
conf.Gopath,
}

// Add a few things to it
if err := addLine(sl1); err != nil {
t.Log("[ERROR]:: Failed to add lines to", Tempestcf, "\n\t->", err)
if errDel := os.Remove(Tempestcf); errDel != nil {
t.Log("[ERROR]:: Could not delete the .tempestcf.temp")
}
Tempestcf = tempestcfBup
t.FailNow()
}

newSl := rmInSlice(0, "", sl1)

// Try to use writeTempestcf
if err := writeTempestcf(newSl); err != nil {
t.Log("[ERROR]:: Error while using writeTempestcf(newSl)\n\t->", err)
t.Fail()
}

// Verifications
actualSl, errPaths := getPaths()
if errPaths != nil {
t.Log("[ERROR]:: Could not read the new paths!!\n\t->", errPaths)
t.Fail()
}
if !SameSlices(actualSl, newSl) {
t.Log("[FAIL]:: After writing .tempestcf, data do not match you piece of ****!\n\t->")
t.Fail()
}

// Then we restore the previous setup!?
if errDel := os.Remove(Tempestcf); errDel != nil {
t.Log("[ERROR]:: Could not delete the .tempestcf.temp")
}
Tempestcf = tempestcfBup
}

// SameSlices checks equality between two slices
// returns true if they are identiques
func SameSlices(a, b []string) bool {
Expand Down

0 comments on commit 4288769

Please sign in to comment.