Skip to content

Commit

Permalink
fullfilling the unit tests for lib/config.go
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Jun 5, 2019
1 parent abdb640 commit 87c1ebc
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 15 deletions.
8 changes: 0 additions & 8 deletions internal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,6 @@ func Example_listCommand_Run() {
// RRH_TIME_FORMAT: relative (default)
}

func TestOpenConfigBrokenJson(t *testing.T) {
os.Setenv(lib.RrhConfigPath, "../testdata/broken.json")
var config = lib.OpenConfig()
if config != nil {
t.Error("broken json returns nil")
}
}

func TestLoadConfigFile(t *testing.T) {
os.Setenv(lib.RrhConfigPath, "../testdata/config.json")

Expand Down
6 changes: 3 additions & 3 deletions lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ func (config *Config) StoreConfig() error {
return err1
}
var bytes, err2 = json.Marshal(config.values)
if err2 == nil {
return ioutil.WriteFile(configPath, bytes, 0644)
if err2 != nil {
return err2
}
return err2
return ioutil.WriteFile(configPath, bytes, 0644)
}

/*
Expand Down
88 changes: 88 additions & 0 deletions lib/config_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package lib

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/mitchellh/go-homedir"
)

func TestValidateArgumentsOnUpdate(t *testing.T) {
Expand Down Expand Up @@ -163,3 +168,86 @@ func TestUpdateValue(t *testing.T) {
defer os.Remove(dbfile)
}
}

func TestConfigIsSet(t *testing.T) {
var dbFile = Rollback("../testata/test_db.json", "../testdata/config.json", func(config *Config, db *Database) {
if config.IsSet(RrhConfigPath) {
t.Errorf("not boolean variable is specified")
}
var home, _ = homedir.Dir()
if config.GetDefaultValue(RrhConfigPath) != filepath.Join(home, ".rrh/config.json") {
t.Errorf("RrhConfigPath did not match")
}
var _, from1 = config.findDefaultValue("UnknownVariable")
if from1 != NotFound {
t.Errorf("Unknown variable can get")
}
var _, from2 = config.GetString("UnknownVariable")
if from2 != NotFound {
t.Errorf("Unknown variable can get")
}
var err = config.Unset("UnknownVariable")
if err == nil {
t.Errorf("Unknown variable can Unset")
}
var beforeFlag = config.IsSet(RrhAutoCreateGroup)
config.Unset(RrhAutoCreateGroup)
var afterFlag = config.IsSet(RrhAutoCreateGroup)
if afterFlag || !beforeFlag {
t.Errorf("beforeFlag should be true, and afterFlag should be false after Unset of RrhAutoCreateGroup")
}
config.StoreConfig()
var config2 = OpenConfig()
var afterFlag2 = config2.IsSet(RrhAutoCreateGroup)
if afterFlag2 {
t.Errorf("afterFlag2 should be false because unset and store the config")
}
})
defer os.Remove(dbFile)
}

func convertToErrors(messages []string) []error {
var errs = []error{}
for _, msg := range messages {
errs = append(errs, errors.New(msg))
}
return errs
}

func TestPrintErrors(t *testing.T) {
var errs = convertToErrors([]string{"msg1", "msg2"})
var testcases = []struct {
givesOnError string
wontOutput string
wontStatus int
}{
{Ignore, "", 0},
{Fail, "msg1+msg2", 5},
}

var dbFile = Rollback("../testdata/test_db.json", "../testdata/config.json", func(config *Config, db *Database) {
for _, tc := range testcases {
var output = CaptureStdout(func() {
config.Update(RrhOnError, tc.givesOnError)
var status = config.PrintErrors(errs)
if status != tc.wontStatus {
t.Errorf("Status code of printErrors did not match, wont %d, got %d", tc.wontStatus, status)
}
})
output = strings.TrimSpace(output)
output = strings.ReplaceAll(output, "\n", "+")
if output != tc.wontOutput {
t.Errorf("output by printErrors did not match, wont %s, got %s", tc.wontOutput, output)
}
}
})
defer os.Remove(dbFile)
}

func TestOpenConfigBrokenJson(t *testing.T) {
os.Setenv(RrhConfigPath, "../testdata/broken.json")
var config = OpenConfig()
if config != nil {
t.Error("broken json returns nil")
}
}
5 changes: 1 addition & 4 deletions lib/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import (

func copyfile(fromfile string) string {
var content, _ = ioutil.ReadFile(fromfile)
var file, err = ioutil.TempFile("../testdata/", "tmp")
if err != nil {
fmt.Println(err)
}
var file, _ = ioutil.TempFile("../testdata/", "tmp")
file.Write(content)
defer file.Close()
return file.Name()
Expand Down

0 comments on commit 87c1ebc

Please sign in to comment.