Skip to content

Commit

Permalink
Add full test coverage to key package
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed May 31, 2018
1 parent c335ee8 commit 4821de0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
26 changes: 13 additions & 13 deletions key/key.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package key

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/user"
"sort"
"strings"
Expand Down Expand Up @@ -57,18 +58,16 @@ var defaultBinding = Binding{
// and returns a map representation of the file
// It also looks for errors, and ambiguities within the file and notifies the user of them
func LoadSettings() (b Binding, err error) {
b, err = parseSettings()
if err != nil {
return
}

b = parseSettings()
warnings, fatals := b.verify()

if len(fatals) != 0 {
fmt.Println(color.Red(color.Regular, "🚫 %d unrecoverable error(s) detected in .fac.yml\n", len(fatals)))
var errorMsg bytes.Buffer
errorMsg.WriteString(color.Red(color.Regular, "🚫 %d unrecoverable error(s) detected in .fac.yml\n", len(fatals)))
for _, msg := range fatals {
fmt.Println(color.Red(color.Regular, "%s", msg))
errorMsg.WriteString(color.Red(color.Regular, "\n%s", msg))
}
os.Exit(1)
return nil, errors.New(errorMsg.String())
}

if len(warnings) != 0 {
Expand All @@ -86,25 +85,26 @@ func LoadSettings() (b Binding, err error) {

// parseSettings looks for `$HOME/.fac.yml` and parses it into a `Binding` value
// If the file does not exist, it returns the `defaultBinding`
func parseSettings() (b Binding, err error) {
func parseSettings() (b Binding) {
usr, err := currentUser()
if err != nil {
fmt.Println(color.Yellow(color.Regular, "fac: %s. Default key-bindings will be used", err.Error()))
return defaultBinding, nil
return defaultBinding
}

// Read config file
f, err := ioutil.ReadFile(usr.HomeDir + "/.fac.yml")
if err != nil {
fmt.Println(color.Yellow(color.Regular, "fac: %s. Default key-bindings will be used", err.Error()))
return defaultBinding, nil
return defaultBinding
}

// Parse config file
if err = yaml.Unmarshal(f, &b); err != nil {
fmt.Println(color.Yellow(color.Regular, "fac: %s. Default key-bindings will be used", err.Error()))
return defaultBinding, nil
return defaultBinding
}

return
}

Expand Down
27 changes: 15 additions & 12 deletions key/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/user"
"sort"
"strings"
"testing"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -66,10 +67,6 @@ func TestLoadSettings(t *testing.T) {
}

for _, test := range tests {
if test.fatals != nil {
continue
}

// Create dummy yml file with content
f, err := os.Create(".fac.yml")
testhelper.Ok(t, err)
Expand All @@ -78,10 +75,16 @@ func TestLoadSettings(t *testing.T) {
f.Close()

output, err := LoadSettings()
testhelper.Ok(t, err)

test.expected.consolidate()
testhelper.Equals(t, test.expected, output)
if len(test.fatals) == 0 {
testhelper.Ok(t, err)
test.expected.consolidate()
testhelper.Equals(t, test.expected, output)
} else {
testhelper.Assert(t, err != nil, "%v should fail", test)
for _, msg := range test.fatals {
testhelper.Assert(t, strings.Contains(err.Error(), msg), "Error message should contain message %s", msg)
}
}
}
}

Expand All @@ -90,14 +93,14 @@ func TestParseSettings(t *testing.T) {
currentUser = func() (*user.User, error) {
return nil, errors.New("Could not find current user")
}
binding, _ := parseSettings()
binding := parseSettings()
testhelper.Equals(t, defaultBinding, binding)

// Test with invalid directory
currentUser = func() (*user.User, error) {
return &user.User{HomeDir: "foobar"}, nil
}
binding, _ = parseSettings()
binding = parseSettings()
testhelper.Equals(t, defaultBinding, binding)

// Test with valid directory with empty file
Expand All @@ -108,12 +111,12 @@ func TestParseSettings(t *testing.T) {
testhelper.Ok(t, err)
defer f.Close()

binding, _ = parseSettings()
binding = parseSettings()
testhelper.Equals(t, 0, len(binding))

// Test valid directory with erroneous content
f.WriteString("erroneous content")
binding, _ = parseSettings()
binding = parseSettings()
testhelper.Equals(t, defaultBinding, binding)
}

Expand Down

0 comments on commit 4821de0

Please # to comment.