Skip to content

Commit

Permalink
✔️🏁 New feature for rm
Browse files Browse the repository at this point in the history
	-> Added & adapted tests only for the ones impacted by those changes

 Issues related:
 	-> #21
 	-> #16

 On branch dev-enhancement-rm
 Changes to be committed:
	modified:   cmd/rm.go
	modified:   cmd/rm_test.go
	modified:   cmd/root.go
  • Loading branch information
ChacaS0 committed Mar 28, 2018
1 parent fb436d1 commit b35ca71
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 31 deletions.
51 changes: 32 additions & 19 deletions cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ import (
"github.com/spf13/cobra"
)

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

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

Expand Down Expand Up @@ -85,7 +87,9 @@ or
if errAllP != nil {
color.Red(errAllP.Error())
}
slicePaths = rmInSlice(rmInt, rmStr, slicePaths) // TODO change this to take slices rmInt & Str
// slicePaths = rmInSlice(rmInt, rmStr, slicePaths) // @DEPRECATED
slRmInt, slRmStr := processArgsRm(args)
slicePaths = rmInSlice(slRmInt, slRmStr, slicePaths)

if errWrite := writeTempestcf(slicePaths); errWrite != nil {
color.Red(errWrite.Error())
Expand Down Expand Up @@ -118,16 +122,9 @@ func init() {

// processArgsRm takes the args as parameters and process them
func processArgsRm(args []string) ([]int, []string) {
// TODO process stuff hunh?
slRmInt := make([]int, 0)
slRmStr := make([]string, 0)

// Slice into string
var strified string
for _, valStr := range args {
strified += valStr + " "
}

// // regexes
// // RegexManyInt is the regex to see if many int were passed
// // i.e. ``0 1 4`` or ``0``
Expand All @@ -141,9 +138,14 @@ func processArgsRm(args []string) ([]int, []string) {
// RegexJustInt is the regex to see if only an int has been passed as an arg
RegexJustInt := regexp.MustCompile(`^(\d+)$`)

// TODO - Check if the value hasn't been provided twice (specially with range stuffs)
for _, arg := range args { // for each arg in args
arg = strings.Trim(arg, " ")
// Empty arg
if arg == "" {
if !IsStringInSlice("this", slRmStr) {
slRmStr = append(slRmStr, "this")
}
}
// IntToInt
if allItoI := RegexIntToInt.FindString(arg); len(allItoI) > 0 {
// RegexIntToInt.ReplaceAllString(arg, ``)
Expand All @@ -161,12 +163,16 @@ func processArgsRm(args []string) ([]int, []string) {
begin = temp
}
for i := begin; i <= end; i++ {
slRmInt = append(slRmInt, i)
if !IsIntInSlice(i, slRmInt) {
slRmInt = append(slRmInt, i)
}
}
}
// Many Strings
if allMStr := RegexManyStr.FindString(arg); len(allMStr) > 0 {
slRmStr = append(slRmStr, arg)
if !IsStringInSlice(arg, slRmStr) {
slRmStr = append(slRmStr, arg)
}
}
// Just an Int
if allInt := RegexJustInt.FindString(arg); len(allInt) > 0 {
Expand All @@ -175,7 +181,9 @@ func processArgsRm(args []string) ([]int, []string) {
fmt.Println(redB(":: [ERROR]"), color.HiRedString("Sorry, could not understand thise argument", arg), "\n\t->", errInt)
return nil, nil
}
slRmInt = append(slRmInt, val)
if !IsIntInSlice(val, slRmInt) {
slRmInt = append(slRmInt, val)
}
}

}
Expand All @@ -201,10 +209,12 @@ func processArgsRm(args []string) ([]int, []string) {

// rmInSlice give it a slice with an index of path to remove.
// It will return a new slice with the item removed from it!
func rmInSlice(index int, record string, list []string) []string {
//* NOTE: alling this func, check if len(args) == 0, set record at "this"
func rmInSlice(indexes []int, slRmStr []string, list []string) []string {

// NOTE: alling this func, check if len(args) == 0, set record at "this"

if index <= -1 {
// for strings
for _, record := range slRmStr {
//* we use the the path provided
if record == "this" {
this, errDir := os.Getwd()
Expand All @@ -216,7 +226,7 @@ func rmInSlice(index int, record string, list []string) []string {

for i, v := range list {
if v == record {
index = i
indexes = append(indexes, i)
}
}
}
Expand All @@ -226,10 +236,13 @@ func rmInSlice(index int, record string, list []string) []string {
// a = a[:1+copy(a[1:], a[2:])]
// listToRet := list[:index+copy(list[index:], list[index+1:])]

// I don't find this elegant :(
listToRet := make([]string, 0)
listToRet = append(listToRet, list[:index]...)
listToRet = append(listToRet, list[index+1:]...)

for _, index := range indexes {
// I don't find this elegant :(
listToRet = append(listToRet, list[:index]...)
listToRet = append(listToRet, list[index+1:]...)
}

// DEBUG
// color.HiYellow(fmt.Sprintf("%v", list))
Expand Down
30 changes: 18 additions & 12 deletions cmd/rm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ func TestRmInSlice(t *testing.T) {
sl1 := []string{0: "path1/sub1/subsub1", 1: "path2/sub2/subsub2", 2: "path3/sub3/subsub3"}
sl2 := []string{0: "path1/sub1"}
//---
sl22 := []string{}
sl11 := []string{0: "path2/sub2/subsub2", 1: "path3/sub3/subsub3"}
// empty
emptySlInt := make([]int, 0)
emptySlStr := make([]string, 0)

var tests = []struct {
i int
str string
i []int
str []string
slstr []string
want []string
e error
}{
{-1, "path1/sub1/subsub1", sl1, sl11, errors.New("[FAIL]:: Didn't remove shit - str")},
{-1, "path1/sub1", sl2, sl22, errors.New("[FAIL]:: Can't return the nil slice, sad - str")},
{0, "", sl1, sl11, errors.New("[FAIL]:: Didn't remove shit - int")},
{0, "", sl2, sl22, errors.New("[FAIL]:: Can't return the nil slice, sad - int")},
{emptySlInt, []string{"path1/sub1/subsub1"}, sl1, sl11, errors.New("[FAIL]:: Didn't remove shit - str")},
{emptySlInt, sl2, sl2, emptySlStr, errors.New("[FAIL]:: Can't return the nil slice, sad - str")},
{[]int{0}, emptySlStr, sl1, sl11, errors.New("[FAIL]:: Didn't remove shit - int")},
{[]int{0}, emptySlStr, sl2, emptySlStr, errors.New("[FAIL]:: Can't return the nil slice, sad - int")},
}

// running tests
Expand Down Expand Up @@ -124,8 +126,8 @@ func TestWriteTempestcf(t *testing.T) {
// Presets for testing
tempestcfbup := setTestTempestcf(t, sl1)

newSl := rmInSlice(0, "", sl1)
newSl = rmInSlice(-1, "this", sl1)
newSl := rmInSlice([]int{0}, []string{}, sl1)
newSl = rmInSlice([]int{}, []string{"this"}, sl1)

// Try to use writeTempestcf
if err := writeTempestcf(newSl); err != nil {
Expand Down Expand Up @@ -159,7 +161,7 @@ func TestWriteTempestcf(t *testing.T) {
// It verifies that it returns the right slices
func TestProcessArgsRm(t *testing.T) {
// parameter variables
//empty
// empty arg
s3 := []string{""}
// index ranges
s1 := []string{"0-1"}
Expand All @@ -176,8 +178,9 @@ func TestProcessArgsRm(t *testing.T) {
s9 := []string{"/tmp", "7", "2-5"}
s10 := []string{"2-5", "/tmp", "7"}
s11 := []string{"1-2", "3-4", "/temp", "5", "/tmp/user/aur", "6"}
s14 := []string{"0", "", "3-3", "/tmp"}
// Overlaping arguments
// TODO
s13 := []string{"0-2", "1-3", "/tmp", "10", "/tmp"}

emptySliceStr := make([]string, 0)
emptySliceInt := make([]int, 0)
Expand All @@ -190,7 +193,7 @@ func TestProcessArgsRm(t *testing.T) {
err error
}{
// Empty
{s3, emptySliceInt, emptySliceStr, errors.New("[FAIL]:: Failed to prcess empty args")},
{s3, emptySliceInt, []string{"this"}, errors.New("[FAIL]:: Failed to prcess empty args")},
// IntToInt
{s1, []int{0, 1}, emptySliceStr, errors.New("[FAIL]:: Failed to return the slice of ints")},
{s2, []int{0, 1, 2, 5, 6, 7}, emptySliceStr, errors.New("[FAIL]:: Failed to process 2 ranges of ints")},
Expand All @@ -202,11 +205,14 @@ func TestProcessArgsRm(t *testing.T) {
// Many ints
{s6, []int{0, 2, 5}, emptySliceStr, errors.New("[FAIL]:: Failed to process a bunch of ints")},
{s7, []int{1}, emptySliceStr, errors.New("[FAIL]:: Could not process just one fcking int ffs")},
// Overlaping arguments
{s13, []int{0, 1, 2, 3, 10}, s5, errors.New("[FAIL]:: Failed to handle redundant information")},
// Mixed
{s8, []int{0, 2, 3, 4, 5, 7}, s5, errors.New("[FAIL]:: Failed to process many mixed args (1)")},
{s9, []int{7, 2, 3, 4, 5}, s5, errors.New("[FAIL]:: Failed to process many mixed args (2)")},
{s10, []int{2, 3, 4, 5, 7}, s5, errors.New("[FAIL]:: Failed to process many mixed args (3)")},
{s11, []int{1, 2, 3, 4, 5, 6}, s4, errors.New("[FAIL]:: Failed to process many mixed args (4)")},
{s14, []int{0, 3}, []string{"this", "/tmp"}, errors.New("[FAIL]:: Failed to process many mixed args (5)")},
}

// running tests
Expand Down
36 changes: 36 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ var greenB func(...interface{}) string
// magB is a func used to print in bold magenta
var magB func(...interface{}) string

// Target is represented by an index and a path
type Target struct {
Index int
Path string
}

// RootCmd represents the base command when called without any subcommands
//TODO Make full description with full help on how to use the CLI
var RootCmd = &cobra.Command{
Expand Down Expand Up @@ -278,3 +284,33 @@ func IsDirectory(path string) (bool, error) {
}
return fileInfo.IsDir(), nil
}

// IsIntInSlice returns true if the int is in the slice
func IsIntInSlice(index int, sl []int) bool {
if len(sl) <= 0 || sl == nil {
return false
}

for _, val := range sl {
if val == index {
return true
}
}
return false
}

// IsStringInSlice returns true if the string is in the slice.
// This one is more simple and faster than checkRedondance(slice, sliceArgs[]string) bool
func IsStringInSlice(str string, sl []string) bool {
if len(sl) <= 0 || sl == nil {
return false
}

for _, val := range sl {
if val == str {
return true
}
}

return false
}

0 comments on commit b35ca71

Please sign in to comment.