Skip to content

Commit

Permalink
🏁 Added test file for cmd/set.go
Browse files Browse the repository at this point in the history
 Issues related:
 	-> #16

 On branch devel
 Changes to be committed:
	modified:   cmd/init.go
	new file:   cmd/set_test.go
  • Loading branch information
ChacaS0 committed Mar 1, 2018
1 parent 74765b8 commit 53528df
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
4 changes: 1 addition & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,8 @@ func initializeTP() error {
// initializeCfFile creates the file ``$HOME/.tempest.yaml``
// if it doesn't already exist with ``duration: 5``
func initializeCfFile() error {
defConf := `{
duration: 5
defConf := `duration: 5
auto-mode: false
}
`
_, errDir := IsDirectory(conf.Home + "/.tempest.yaml")
if errDir == nil {
Expand Down
74 changes: 74 additions & 0 deletions cmd/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd

import (
"fmt"
"os"
"testing"

"github.com/fatih/color"
"github.com/spf13/viper"
)

// func Test(t *testing.T)

// TestSetAge checks if when changing the age,
// it is stored corretly in viper.Config()
func TestSetAge(t *testing.T) {
// Current settings
currAge := viper.GetInt("duration")
currCfFile := viper.ConfigFileUsed()

// set env for testing
_, errDir := IsDirectory(conf.Gopath + "/.tempest_test.yaml")
if errDir != nil {
// if already exists, we create
f, err := os.OpenFile(conf.Gopath+"/.tempest_test.yaml", os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(color.HiRedString("::Huge error! Could not recreate file! God lost faith in you!"))
t.Log("[ERROR]File creation::", err)
t.FailNow()
}
defConf := `duration: 5
auto-mode: false
`
_, errWrite := f.WriteString(defConf)
if errWrite != nil {
t.Log("Could not write default config to test file: ", errWrite)
t.FailNow()
}
defer f.Close()
viper.SetConfigFile(conf.Gopath + "/.tempest_test.yaml")
}

//* First test - upgrade age
if currAge > 1 {
age = currAge + 5
fmt.Println("AGE:", age)
} else {
age = 6
}
// Set the age
_ = setAge()
// Check if it changed
if age != viper.GetInt("duration") && viper.GetInt("duration") != 6 {
t.Log("Couldn't set the age")
t.FailNow()
}

// Go back to the previous configuration
// Age
viper.Set("duration", currAge)
errS := viper.WriteConfigAs(viper.ConfigFileUsed())
if errS != nil {
color.Red(errS.Error())
}
// ConfigFile
// clean the one just created
errRm := os.Remove(conf.Gopath + "/.tempest_test.yaml")
if errRm != nil {
t.Log("Could not remove the test file, you might one to remove it yourself!")
t.Log(errRm)
}
viper.SetConfigFile(currCfFile)

}

0 comments on commit 53528df

Please sign in to comment.