-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issues related: -> #16 On branch devel Changes to be committed: modified: cmd/init.go new file: cmd/set_test.go
- Loading branch information
Showing
2 changed files
with
75 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |