Skip to content

Commit

Permalink
💻🏁 Added test for rm.go:simpleDelAllString()
Browse files Browse the repository at this point in the history
	-> Few changes (variadic params instead of slice)
	-> Some update for the ``$HOME`` on Windows

 Issues related:
 	-> #21
 	-> #16
 	-> #9

 On branch dev-enhancement-rm
 Changes to be committed:
	modified:   cmd/purge.go
	modified:   cmd/rm.go
	modified:   cmd/rm_test.go
	modified:   cmd/root.go
  • Loading branch information
ChacaS0 committed Mar 28, 2018
1 parent b35ca71 commit 856d8d8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func deleteAllStr(path string, targets []os.FileInfo, testMode bool) error {
func deleteAllInt(index int, testMode bool) error {
allPaths, errPaths := getPaths()
if errPaths != nil {
color.Red("::Error while reading .tempestcf")
color.Red(":: Error while reading .tempestcf")
return errPaths
}
if index >= 0 && index < len(allPaths) {
Expand Down
44 changes: 36 additions & 8 deletions cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
package cmd

import (
"errors"
"fmt"
"log"
"os"
"regexp"
"strconv"
Expand All @@ -33,11 +35,11 @@ import (

// @DEPRECATED
// rmInt is the index to remove from the TEMPest list
var rmInt int
// var rmInt int

// @DEPRECATED
// rmStr is the path to be removed from TEMpest
var rmStr string
// var rmStr string

// rmOrigin defines whether the origin directory/file should be deleted too
var rmOrigin bool
Expand Down Expand Up @@ -114,8 +116,8 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// rmCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rmCmd.Flags().IntVarP(&rmInt, "index", "i", -1, "Points to an index provided by TEMPest")
rmCmd.Flags().StringVarP(&rmStr, "path", "p", "", "The path of a target for TEMPest")
// rmCmd.Flags().IntVarP(&rmInt, "index", "i", -1, "Points to an index provided by TEMPest")
// rmCmd.Flags().StringVarP(&rmStr, "path", "p", "", "The path of a target for TEMPest")

rmCmd.Flags().BoolVarP(&rmOrigin, "origin", "o", false, "Removes the target from TEMPest, but also the original directories/files")
}
Expand Down Expand Up @@ -191,15 +193,14 @@ func processArgsRm(args []string) ([]int, []string) {
// // if RegexManyInt.FindString(strified) == strified {
// // // then that's it
// // }
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> NOTE: WTFFFFFF!!?????
// @DEPRECATED
// for _, arg := range args {
// switch {
// case len(args) == 0 && rmInt[0] == -1 && rmStr[0] == "":
// slRmStr = append(slRmStr, "this")
// slRmInt = append(slRmInt, rmInt[0])
// return slRmInt, slRmStr
// case len(args) > 0 && rmInt[0] == -1:
// //todo
// }

// }
Expand Down Expand Up @@ -236,10 +237,23 @@ func rmInSlice(indexes []int, slRmStr []string, list []string) []string {
// a = a[:1+copy(a[1:], a[2:])]
// listToRet := list[:index+copy(list[index:], list[index+1:])]

listToRet := make([]string, 0)
// if we have to, remove the directories/files
if rmOrigin {
// need to find another way QQ
trashSlice := make([]string, 0)
for _, index := range indexes {
trashSlice = append(trashSlice, list[index])
}

if err := simpleDelAllString(trashSlice...); err != nil {
log.Println(redB(":: [ERROR]"), color.HiRedString("There was an error while delete original files:"), "\n\t->", err)
}
}

//* Remove from slice
listToRet := make([]string, 0)
for _, index := range indexes {
// I don't find this elegant :(
// Is this right? :(
listToRet = append(listToRet, list[:index]...)
listToRet = append(listToRet, list[index+1:]...)
}
Expand All @@ -250,6 +264,20 @@ func rmInSlice(indexes []int, slRmStr []string, list []string) []string {
return listToRet
}

// simpleDelAllString is a simple function to delete files or directories.
func simpleDelAllString(paths ...string) error {
if paths != nil {
for _, path := range paths {
if err := os.RemoveAll(path); err != nil {
return err
}
}
} else {
return errors.New("Parameter is nil at rm.go:simpleDelAllString()")
}
return nil
}

// writeTempestcf saves the new list of paths meant to be targets for TEMPest
func writeTempestcf(targets []string) error {
// Backup of the old one
Expand Down
43 changes: 43 additions & 0 deletions cmd/rm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,46 @@ func TestProcessArgsRm(t *testing.T) {
}
}
}

// TestSimpleDelAllString is the test for the func simpleDelAllString which is supposed
// to delete from the system all paths provided in args
func TestSimpleDelAllString(t *testing.T) {
// create a test directories
dir1 := conf.Gopath + string(os.PathSeparator) + "dir1"
dir2 := conf.Gopath + string(os.PathSeparator) + "dir2"
dir3 := conf.Gopath + string(os.PathSeparator) + "dir3"

dirs := []string{dir1, dir2, dir3}

for _, d := range dirs {
if err := os.Mkdir(d, 0777); err != nil {
t.Log("[ERROR]:: Failed to create test directeory:", d)
t.Fail()
}
}

// apply func on them in order to delete it
if err := simpleDelAllString(dirs...); err != nil {
t.Log("[FAIL]:: Failed to remove those directories")
t.Fail()
}

stillExisting := make([]string, 0)

// check if they got deleted for real
for _, d := range dirs {
if isDir, err := IsDirectory(d); isDir || err == nil {
stillExisting = append(stillExisting, d)
t.Log("[FAIL]:: Think it deletes but it doesn't, this is so mean, imma cry now. Bye!")
t.Fail()
}
}

// clean up the mess if one of the test dirs still exist
for _, se := range stillExisting {
if err := os.RemoveAll(se); err != nil {
t.Log("[ERROR]:: F*ck can't do sh*t.")
t.Fail()
}
}
}
11 changes: 6 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,6 @@ func Execute() {

func init() {
// Initialize the environment variables
if err := envconfig.Init(&conf); err != nil {
log.Fatal(err)
}

cobra.OnInitialize(initConfig)

home, err := homedir.Dir()
if err != nil {
Expand All @@ -166,6 +161,12 @@ func init() {
}
conf.Home = home

cobra.OnInitialize(initConfig)

if err := envconfig.Init(&conf); err != nil {
// log.Println(err)
}

pathProg = conf.Gopath + string(os.PathSeparator) + "src" + string(os.PathSeparator) + "github.com" + string(os.PathSeparator) + "ChacaS0" + string(os.PathSeparator)
pathTempest = pathProg + "tempest" + string(os.PathSeparator)

Expand Down

0 comments on commit 856d8d8

Please sign in to comment.