Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 8129217

Browse files
committed
Return []error from ValidateManifest and add tests
- Modifies ValidateManifest() to return []error, as a collection of all the errors, instead of logging directly. - Adds tests for ValidateManifest().
1 parent 878ae28 commit 8129217

File tree

2 files changed

+87
-8
lines changed

2 files changed

+87
-8
lines changed

manifest.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package dep
66

77
import (
88
"bytes"
9+
"fmt"
910
"io"
1011
"reflect"
1112
"sort"
@@ -44,11 +45,12 @@ type rawProject struct {
4445
Source string `toml:"source,omitempty"`
4546
}
4647

47-
func validateManifest(b *bytes.Buffer) error {
48-
// Load the TomlTree from reader
49-
tree, err := toml.Load(b.String())
48+
func validateManifest(s string) ([]error, error) {
49+
var errs []error
50+
// Load the TomlTree from string
51+
tree, err := toml.Load(s)
5052
if err != nil {
51-
return errors.Wrap(err, "Unable to load TomlTree from string")
53+
return errs, errors.Wrap(err, "Unable to load TomlTree from string")
5254
}
5355
// Convert tree to a map
5456
manifest := tree.ToMap()
@@ -74,17 +76,17 @@ func validateManifest(b *bytes.Buffer) error {
7476
}
7577
}
7678
if !isValidMetadata {
77-
internal.Logf("WARNING: metadata should consist of key-value pairs")
79+
errs = append(errs, errors.New("metadata should consist of key-value pairs"))
7880
}
7981
}
8082
}
8183
}
8284
if !isValidField {
83-
internal.Logf("WARNING: Unknown field in manifest: %v", prop)
85+
errs = append(errs, fmt.Errorf("Unknown field in manifest: %v", prop))
8486
}
8587
}
8688

87-
return nil
89+
return errs, nil
8890
}
8991

9092
func readManifest(r io.Reader) (*Manifest, error) {
@@ -93,10 +95,15 @@ func readManifest(r io.Reader) (*Manifest, error) {
9395
if err != nil {
9496
return nil, errors.Wrap(err, "Unable to read byte stream")
9597
}
96-
err = validateManifest(buf)
98+
99+
// Validate manifest and log warnings
100+
errs, err := validateManifest(buf.String())
97101
if err != nil {
98102
return nil, errors.Wrap(err, "Manifest validation failed")
99103
}
104+
for _, e := range errs {
105+
internal.Logf("WARNING: %v", e)
106+
}
100107

101108
raw := rawManifest{}
102109
err = toml.Unmarshal(buf.Bytes(), &raw)

manifest_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package dep
66

77
import (
8+
"errors"
89
"reflect"
910
"strings"
1011
"testing"
@@ -119,3 +120,74 @@ func TestReadManifestErrors(t *testing.T) {
119120
}
120121
}
121122
}
123+
124+
func TestValidateManifest(t *testing.T) {
125+
cases := []struct {
126+
tomlString string
127+
want []error
128+
}{
129+
{
130+
tomlString: `
131+
[[dependencies]]
132+
name = "github.com/foo/bar"
133+
`,
134+
want: []error{},
135+
},
136+
{
137+
tomlString: `
138+
foo = "some-value"
139+
version = 14
140+
141+
[[bar]]
142+
author = "xyz"
143+
144+
[[dependencies]]
145+
name = "github.com/foo/bar"
146+
version = ""
147+
`,
148+
want: []error{
149+
errors.New("Unknown field in manifest: foo"),
150+
errors.New("Unknown field in manifest: bar"),
151+
errors.New("Unknown field in manifest: version"),
152+
},
153+
},
154+
{
155+
tomlString: `
156+
metadata = "project-name"
157+
158+
[[dependencies]]
159+
name = "github.com/foo/bar"
160+
`,
161+
want: []error{errors.New("metadata should consist of key-value pairs")},
162+
},
163+
}
164+
165+
// constains for error
166+
contains := func(s []error, e error) bool {
167+
for _, a := range s {
168+
if a.Error() == e.Error() {
169+
return true
170+
}
171+
}
172+
return false
173+
}
174+
175+
for _, c := range cases {
176+
errs, err := validateManifest(c.tomlString)
177+
if err != nil {
178+
t.Fatal(err)
179+
}
180+
181+
// compare length of error slice
182+
if len(errs) != len(c.want) {
183+
t.Fatalf("Number of manifest errors are not as expected: \n\t(GOT) %v errors(%v)\n\t(WNT) %v errors(%v).", len(errs), errs, len(c.want), c.want)
184+
}
185+
186+
// check if the expected errors exist in actual errors slice
187+
for _, er := range errs {
188+
if !contains(c.want, er) {
189+
t.Fatalf("Manifest errors are not as expected: \n\t(MISSING) %v\n\t(FROM) %v", er, c.want)
190+
}
191+
}
192+
}
193+
}

0 commit comments

Comments
 (0)