Skip to content

Commit

Permalink
🎨🏁 Added test for add_test.go; small user improvements
Browse files Browse the repository at this point in the history
 Issues related:
 	-> #19
 	-> #16

 On branch devel
 Changes to be committed:
	modified:   README.md
	modified:   cmd/add.go
	modified:   cmd/add_test.go
	modified:   cmd/purge.go
	modified:   cmd/root.go
	modified:   cmd/version.go
	modified:   list.todo
  • Loading branch information
ChacaS0 committed Mar 4, 2018
1 parent 70ca24b commit 76041e3
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- [To add the current directory](#to-add-the-current-directory)
- [Through command-line](#through-command-line)
- [Through text editor](#through-text-editor)
- [List the current directories added to TEMPest](#list-the-current-directories-added-to-tempest)
- [List the current directories added to **TEMPest**](#list-the-current-directories-added-to-tempest)
- [Using **TEMPest**](#using-tempest)
- [Viewing the file ``~/.tempestcf``](#viewing-the-file-tempestcf)
- [Runing a global purge](#runing-a-global-purge)
Expand Down
33 changes: 24 additions & 9 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"os"
"strings"

"github.com/spf13/viper"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/vrischmann/envconfig"
Expand Down Expand Up @@ -85,7 +87,8 @@ func init() {
// func addLine(argFlags []string, args []string) error {
func addLine(args []string) error {
// Check if the path already exists first in tempestcf
ctnt, errRead := ioutil.ReadFile(conf.Home + "/.tempestcf")
// ctnt, errRead := ioutil.ReadFile(conf.Home + "/.tempestcf")
ctnt, errRead := ioutil.ReadFile(Tempestcf)
if errRead != nil {
fmt.Println(redB("::"), color.RedString("Could not read the file muthafuckkah!"))
return errRead
Expand All @@ -97,9 +100,9 @@ func addLine(args []string) error {

// Open the file to write to (adding new temp path)
// tmpcf, err := os.Open(conf.Home + "/.tempestcf")
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(color.RedString("::Sorry! Could not find ~/.tempestcf ! :("))
fmt.Println(color.RedString("::Sorry! Could not find "), viper.ConfigFileUsed())
return err
}
defer tmpcf.Close()
Expand All @@ -118,17 +121,18 @@ func addLine(args []string) error {
return errors.New(blueB(":: ") + color.HiBlueString("A path provided is already in TEMPest! Later b*tch"))
}

// Check if path points to valid target
// if dir, errExist := IsDirectory()

// Add this directory to the file
nbBytes, errWrite := tmpcf.WriteString(this + "\n")
if errWrite != nil {
fmt.Println(redB("::"), color.RedString("Could not write to the file. Fail bitch!"))
return errWrite
}
fmt.Println(greenB("[NEW TEMP]::"), color.GreenString(fmt.Sprintf("\t%d", nbBytes)+"::>"), this)
fmt.Println(greenB("[NEW TEMP]::")+color.GreenString(fmt.Sprintf("%d", nbBytes)+"::>"), this)
} else {
// Treat the last character
for ind, onePath := range args {
args[ind] = TreatLastChar(onePath)
}
// Check if already exists
if checkRedondance(ctntSlice, args) {
return errors.New(blueB(":: ") + color.HiBlueString("A path provided is already in TEMPest! Later b*tch"))
Expand All @@ -142,9 +146,9 @@ func addLine(args []string) error {
// Add all the paths passed in the file
nbBytes, errWS := tmpcf.WriteString(path + "\n")
if errWS != nil {
fmt.Println(color.RedString("::Are you sure you can handle this much? Without askin your mom first!?"))
fmt.Println(color.RedString(":: Are you sure you can handle this much? Without askin your mom first!?"))
}
fmt.Println(color.GreenString("[NEW TEMP]::"+fmt.Sprintf("\t%d", nbBytes)+"::>"), path)
fmt.Println(color.GreenString("[NEW TEMP]::"+fmt.Sprintf("%d", nbBytes)+"::>"), path)
}
fmt.Println(color.YellowString("::"), "All paths were added to TEMPest !")
}
Expand Down Expand Up @@ -193,3 +197,14 @@ func checkRedondance(slice, sliceArgs []string) (doesit bool) {
// }
// return dir, nil
// }

// TreatLastChar takes a pointer to a string
// It analyzes the last character of this string,
// if it is a path separator character, it gets removed.
// Returns the new path, wether there was change or not.
func TreatLastChar(str string) string {
if str[len(str)-1:] == string(os.PathSeparator) {
str = str[:len(str)-1]
}
return str
}
35 changes: 33 additions & 2 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"errors"
"fmt"
"os"
"testing"
)

Expand Down Expand Up @@ -53,7 +53,38 @@ func TestCheckRedondance(t *testing.T) {
// runing tests
for _, tst := range tests {
got := checkRedondance(tst.p1, tst.p2)
fmt.Println(got)
if got != tst.want {
t.Log(tst.err.Error())
t.Fail()
}
}
}

// TestTreatLastChar is the test func for TreatLastChar.
// It checks if it does strip only the last character
// if it is a path separator character and does nothing otherwise.
func TestTreatLastChar(t *testing.T) {
// test variables
p1 := string(os.PathSeparator) + "path1" + string(os.PathSeparator) + "sub" + string(os.PathSeparator) + "dir"
w1 := p1

p2 := string(os.PathSeparator) + "path1" + string(os.PathSeparator) + "sub" + string(os.PathSeparator) + "dir" + string(os.PathSeparator)
w2 := p1

// tests holds the tests we want to do and the result expected
var tests = []struct {
param string
want string
err error
}{
{p1, w1, errors.New("[CONFUSION]:: The path was correct damn it")},
{p2, w2, errors.New("[FAIL]:: Did not change when it was supposed to")},
}

// runing tests
for _, tst := range tests {
got := TreatLastChar(tst.param)
// fmt.Println(got) // DEBUG
if got != tst.want {
t.Log(tst.err.Error())
t.Fail()
Expand Down
4 changes: 2 additions & 2 deletions cmd/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func deleteAllStr(path string, targets []os.FileInfo, testMode bool) error {
smthToDel := false
if days > 1 {
// color.HiMagenta("List of item to be removed:\n\n")
fmt.Println(magB("\n:: List of item to be removed:\n"))
fmt.Println(magB("\n:: List of items to be removed in:"), path)
// color.HiMagenta("Size\tUnit\t\t Item")
fmt.Println(magB("Size\tUnit\t\t Item"))
if len(targets) == 0 {
Expand Down Expand Up @@ -187,7 +187,7 @@ func deleteAllStr(path string, targets []os.FileInfo, testMode bool) error {
}
// Comment on action
if !smthToDel {
fmt.Println("0\tKB\t\t Forever Alone ?")
fmt.Println("0\tKB\t\t Forever Alone ? Nothing to remove here !")
}
if testMode {
fmt.Println(greenB("::"), color.HiGreenString("Nothing got removed, it is just a recap of what would get deleted <_<"))
Expand Down
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ var pathProg string
// pathTempest is the path to the tempest folder
var pathTempest string

// Tempestcf is the path to the .tempestcf file
// this file holds all the paths (targets) of TEMPest
var Tempestcf string

// isVersion is the flag variable that indicates whether we want to see the version
var isVersion bool

Expand Down Expand Up @@ -144,6 +148,8 @@ func init() {
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)

Tempestcf = conf.Home + string(os.PathSeparator) + ".tempestcf"

//* Bold Colors
yellowB = color.New(color.FgHiYellow, color.Bold).SprintFunc()
blueB = color.New(color.FgHiBlue, color.Bold).SprintFunc()
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var versionCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("version called")
if version, errV := getVersion(); errV == nil {
fmt.Print(yellowB("::TEMPest "), color.HiYellowString(version))
fmt.Print(yellowB(":: TEMPest "), color.HiYellowString(version))
} else {
color.Red("error")
}
Expand Down
23 changes: 7 additions & 16 deletions list.todo
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ General:
visual:
✔ use colorized logs (check paper fluo in bag) @done (2/3/2018, 6:34:39 PM)
☐ refractor helpers with color package ? @low
See the current version @low
See the current version @low @done (3/4/2018, 3:37:40 PM)
☐ [CURRENT VERSION] [REVISION] Would be the last commit
☐ [LATEST VERSION]
☐ Improve visualisation of output on ``tempest start ``
Expand All @@ -48,31 +48,22 @@ Note:
✘ [UPDATE] go build -o $GOBIN/tempest main.go @cancelled (2/5/2018, 4:13:02 AM)
☐ Use GoRoutines for deletion @high
✔ THERE IS STILL A BUG ON UPDATE @critical @done (2/5/2018, 4:12:47 AM)
make.sh -> makefile
Checkout Jenkins?
make.sh -> makefile @cancelled (3/4/2018, 3:37:30 PM)
Checkout Jenkins? @done (3/4/2018, 3:37:25 PM)

Config manager:
✔ update dep @today @done (2/8/2018, 4:12:02 AM)

GLOBAL:
☐ TEST FILES @low
Migration from bitbucket to github-chacas0
Set tempest as submodule? @low
UPDATE make.sh to adapt the switch to beta
Migration from bitbucket to github-chacas0 @done (3/4/2018, 3:37:00 PM)
Set tempest as submodule? @low @cancelled (3/4/2018, 3:37:08 PM)
UPDATE make.sh to adapt the switch to beta @cancelled (3/4/2018, 3:37:16 PM)


TODOS:
☐ Fix display / error handling whan nothing is passed to purge @low
☐ Check if this issue is occuring somewhere else @low

☐ Push @critical @today
☐ adapt make.sh to take github instead ( from bitbucket, taking github instead ;) ) @today
☐ RM from scripts.git (NOT NOW)
☐ Push (NOT NOW)
☐ Pull TEMPest


☐ add warning when using init
☐ add overriding to .tempest.yaml

gocover.io
gocover.io @done (3/4/2018, 3:36:52 PM)

0 comments on commit 76041e3

Please sign in to comment.