Skip to content

Commit

Permalink
fix: add some error output on invalid configuration loading
Browse files Browse the repository at this point in the history
  • Loading branch information
kernle32dll committed Oct 26, 2020
1 parent b4c2b9a commit bd16fa0
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 7 deletions.
10 changes: 9 additions & 1 deletion internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,23 @@ func contains(s []string, e string) bool {
// ParseConfigFromFolder parses the given folder for
// a valid ew config, or returns the default (empty)
// config if none can be found.
func ParseConfigFromFolder(path string) Config {
func ParseConfigFromFolder(output io.Writer, path string) Config {
yamlConf, err := parseConfigFromYaml(path)
if err == nil {
return yamlConf
} else if errors.Is(err, io.EOF) {
fmt.Fprintln(output, color.YellowString("Skipping empty yaml config in %s", path))
} else if !errors.Is(err, os.ErrNotExist) {
fmt.Fprintln(output, color.RedString("Failed to read yaml config in %s: %s", path, err))
}

jsonConf, err := parseConfigFromJson(path)
if err == nil {
return jsonConf
} else if errors.Is(err, io.EOF) {
fmt.Fprintln(output, color.YellowString("Skipping empty json config in %s", path))
} else if !errors.Is(err, os.ErrNotExist) {
fmt.Fprintln(output, color.RedString("Failed to read json config in %s: %s", path, err))
}

// If no config is found, use default yaml
Expand Down
97 changes: 92 additions & 5 deletions internal/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package internal

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -233,9 +235,16 @@ func writeTempFile(t *testing.T, filename string, fileString string) string {
func TestParseConfigFromFolder(t *testing.T) {
t.Run("no config found", func(t *testing.T) {
want := Config{Source: YamlSrc, LoadedFrom: "does-not-exist", Tags: map[string][]string{}}
if got := ParseConfigFromFolder("does-not-exist"); !reflect.DeepEqual(got, want) {
wantOutput := ""

output := &bytes.Buffer{}
if got := ParseConfigFromFolder(output, "does-not-exist"); !reflect.DeepEqual(got, want) {
t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want)
}

if got := output.String(); got != wantOutput {
t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput)
}
})

t.Run("json config found, and ok", func(t *testing.T) {
Expand All @@ -247,9 +256,16 @@ func TestParseConfigFromFolder(t *testing.T) {
}()

want := Config{Source: JsonSrc, LoadedFrom: folder, Tags: Tags{"some-tag": []string{"path1", "path2"}}}
if got := ParseConfigFromFolder(folder); !reflect.DeepEqual(got, want) {
wantOutput := ""

output := &bytes.Buffer{}
if got := ParseConfigFromFolder(output, folder); !reflect.DeepEqual(got, want) {
t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want)
}

if got := output.String(); got != wantOutput {
t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput)
}
})

t.Run("json config found, but borked", func(t *testing.T) {
Expand All @@ -259,9 +275,41 @@ func TestParseConfigFromFolder(t *testing.T) {
}()

want := Config{Source: YamlSrc, LoadedFrom: folder, Tags: map[string][]string{}}
if got := ParseConfigFromFolder(folder); !reflect.DeepEqual(got, want) {
wantOutput := fmt.Sprintf(
"Failed to read json config in %s: invalid character ']' looking for beginning of object key string\n",
folder,
)

output := &bytes.Buffer{}
if got := ParseConfigFromFolder(output, folder); !reflect.DeepEqual(got, want) {
t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want)
}

if got := output.String(); got != wantOutput {
t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput)
}
})

t.Run("json config found, but EOF", func(t *testing.T) {
folder := writeTempFile(t, ".ewconfig.json", ``)
defer func() {
clearFolder(t, folder)
}()

want := Config{Source: YamlSrc, LoadedFrom: folder, Tags: map[string][]string{}}
wantOutput := fmt.Sprintf(
"Skipping empty json config in %s\n",
folder,
)

output := &bytes.Buffer{}
if got := ParseConfigFromFolder(output, folder); !reflect.DeepEqual(got, want) {
t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want)
}

if got := output.String(); got != wantOutput {
t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput)
}
})

t.Run("yaml config found, and ok", func(t *testing.T) {
Expand All @@ -278,9 +326,16 @@ tags:
}()

want := Config{Source: YamlSrc, LoadedFrom: folder, Tags: Tags{"some-tag": []string{"path1", "path2"}}}
if got := ParseConfigFromFolder(folder); !reflect.DeepEqual(got, want) {
wantOutput := ""

output := &bytes.Buffer{}
if got := ParseConfigFromFolder(output, folder); !reflect.DeepEqual(got, want) {
t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want)
}

if got := output.String(); got != wantOutput {
t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput)
}
})

t.Run("yaml config found, but borked", func(t *testing.T) {
Expand All @@ -290,8 +345,40 @@ tags:
}()

want := Config{Source: YamlSrc, LoadedFrom: folder, Tags: map[string][]string{}}
if got := ParseConfigFromFolder(folder); !reflect.DeepEqual(got, want) {
wantOutput := fmt.Sprintf(
"Failed to read yaml config in %s: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `t{]` into internal.Config\n",
folder,
)

output := &bytes.Buffer{}
if got := ParseConfigFromFolder(output, folder); !reflect.DeepEqual(got, want) {
t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want)
}

if got := output.String(); got != wantOutput {
t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput)
}
})

t.Run("yaml config found, but EOF", func(t *testing.T) {
folder := writeTempFile(t, ".ewconfig.yml", ``)
defer func() {
clearFolder(t, folder)
}()

want := Config{Source: YamlSrc, LoadedFrom: folder, Tags: map[string][]string{}}
wantOutput := fmt.Sprintf(
"Skipping empty yaml config in %s\n",
folder,
)

output := &bytes.Buffer{}
if got := ParseConfigFromFolder(output, folder); !reflect.DeepEqual(got, want) {
t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want)
}

if got := output.String(); got != wantOutput {
t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput)
}
})
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
os.Exit(1)
}

conf := internal.ParseConfigFromFolder(home)
conf := internal.ParseConfigFromFolder(color.Output, home)

exec, err := cmd.ParseCommand(color.Output, conf, os.Args[1:])
if err != nil {
Expand Down

0 comments on commit bd16fa0

Please sign in to comment.