From b9d644db550670d74935015e170e3161689dbd0e Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 18 May 2017 09:02:02 -0300 Subject: [PATCH 1/3] improved config package test coverage --- config/config.go | 11 ++++++----- config/config_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index 68516e79a..ca22aed99 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,8 @@ import ( "io" "io/ioutil" + "os" + yaml "gopkg.in/yaml.v1" ) @@ -111,15 +113,14 @@ type Project struct { // Load config file func Load(file string) (config Project, err error) { - data, err := ioutil.ReadFile(file) + f, err := os.Open(file) if err != nil { - return config, err + return } - err = yaml.Unmarshal(data, &config) - return + return LoadReader(f) } -// Load config via io.Reader +// LoadReader config via io.Reader func LoadReader(fd io.Reader) (config Project, err error) { data, err := ioutil.ReadAll(fd) if err != nil { diff --git a/config/config_test.go b/config/config_test.go index 8f14d64d9..c18e25faa 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,9 +1,14 @@ package config import ( + "io/ioutil" + "os" + "path/filepath" "strings" "testing" + "fmt" + "github.com/stretchr/testify/assert" ) @@ -26,3 +31,28 @@ fpm: assert.Nil(err) assert.Equal("http://goreleaser.github.io", prop.FPM.Homepage, "yaml did not load correctly") } + +type errorReader struct{} + +func (errorReader) Read(p []byte) (n int, err error) { + return 1, fmt.Errorf("error") +} +func TestLoadBadReader(t *testing.T) { + var assert = assert.New(t) + _, err := LoadReader(errorReader{}) + assert.Error(err) +} + +func TestFile(t *testing.T) { + var assert = assert.New(t) + f, err := ioutil.TempFile(os.TempDir(), "config") + assert.NoError(err) + _, err = Load(filepath.Join(f.Name())) + assert.NoError(err) +} + +func TestFileNotFound(t *testing.T) { + var assert = assert.New(t) + _, err := Load("/nope/no-way.yml") + assert.Error(err) +} From 8a87be7c3b347115a3e0b334678a0d0f744637aa Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 18 May 2017 09:02:27 -0300 Subject: [PATCH 2/3] imports --- config/config.go | 1 - 1 file changed, 1 deletion(-) diff --git a/config/config.go b/config/config.go index ca22aed99..1db76a004 100644 --- a/config/config.go +++ b/config/config.go @@ -5,7 +5,6 @@ package config import ( "io" "io/ioutil" - "os" yaml "gopkg.in/yaml.v1" From 6f9597d8e898ee5852fec64f97a93cf923ff841f Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 18 May 2017 09:02:44 -0300 Subject: [PATCH 3/3] imports --- config/config_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index c18e25faa..c3ee5af24 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,14 +1,13 @@ package config import ( + "fmt" "io/ioutil" "os" "path/filepath" "strings" "testing" - "fmt" - "github.com/stretchr/testify/assert" )