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

Commit 8f4a82c

Browse files
authored
Merge pull request #1370 from JackyChiu/master
Manifest Version/Source Validation
2 parents bd15b89 + 64543bc commit 8f4a82c

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

context_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,10 @@ func TestCaseInsentitiveGOPATH(t *testing.T) {
325325

326326
h.TempDir("src")
327327
h.TempDir("src/test1")
328-
h.TempFile(filepath.Join("src/test1", ManifestName), `[[constraint]]`)
328+
h.TempFile(filepath.Join("src/test1", ManifestName), `
329+
[[constraint]]
330+
name = "github.com/foo/bar"
331+
branch = "master"`)
329332

330333
// Shuffle letter case
331334
rs := []rune(strings.ToLower(h.Path(".")))

manifest.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var (
4040
errRootPruneContainsName = errors.Errorf("%q should not include a name", "prune")
4141
errInvalidRootPruneValue = errors.New("root prune options must be omitted instead of being set to false")
4242
errInvalidPruneProjectName = errors.Errorf("%q in %q must be a string", "name", "prune.project")
43+
errNoName = errors.New("no name provided")
4344
)
4445

4546
// Manifest holds manifest file data and implements gps.RootManifest.
@@ -133,13 +134,17 @@ func validateManifest(s string) ([]error, error) {
133134
if valid {
134135
// Iterate through each array of tables
135136
for _, v := range rawProj {
137+
ruleProvided := false
138+
props := v.(map[string]interface{})
136139
// Check the individual field's key to be valid
137-
for key, value := range v.(map[string]interface{}) {
140+
for key, value := range props {
138141
// Check if the key is valid
139142
switch key {
140-
case "name", "branch", "version", "source":
141-
// valid key
143+
case "name":
144+
case "branch", "version", "source":
145+
ruleProvided = true
142146
case "revision":
147+
ruleProvided = true
143148
if valueStr, ok := value.(string); ok {
144149
if abbrevRevHash.MatchString(valueStr) {
145150
warns = append(warns, fmt.Errorf("revision %q should not be in abbreviated form", valueStr))
@@ -155,6 +160,11 @@ func validateManifest(s string) ([]error, error) {
155160
warns = append(warns, fmt.Errorf("invalid key %q in %q", key, prop))
156161
}
157162
}
163+
if _, ok := props["name"]; !ok {
164+
warns = append(warns, errNoName)
165+
} else if !ruleProvided && prop == "constraint" {
166+
warns = append(warns, fmt.Errorf("branch, version, revision, or source should be provided for %q", props["name"]))
167+
}
158168
}
159169
}
160170
} else {

manifest_test.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ func TestValidateManifest(t *testing.T) {
255255
`,
256256
wantWarn: []error{
257257
errInvalidMetadata,
258+
errors.New("branch, version, revision, or source should be provided for \"github.com/foo/bar\""),
258259
},
259260
wantError: nil,
260261
},
@@ -264,15 +265,19 @@ func TestValidateManifest(t *testing.T) {
264265
[[constraint]]
265266
name = "github.com/foo/bar"
266267
`,
267-
wantWarn: []error{},
268+
wantWarn: []error{
269+
errors.New("branch, version, revision, or source should be provided for \"github.com/foo/bar\""),
270+
},
268271
wantError: nil,
269272
},
270273
{
271274
name: "empty constraint",
272275
tomlString: `
273276
[[constraint]]
274277
`,
275-
wantWarn: []error{},
278+
wantWarn: []error{
279+
errNoName,
280+
},
276281
wantError: nil,
277282
},
278283
{
@@ -305,7 +310,9 @@ func TestValidateManifest(t *testing.T) {
305310
tomlString: `
306311
[[override]]
307312
`,
308-
wantWarn: []error{},
313+
wantWarn: []error{
314+
errNoName,
315+
},
309316
wantError: nil,
310317
},
311318
{
@@ -339,8 +346,10 @@ func TestValidateManifest(t *testing.T) {
339346
wantWarn: []error{
340347
errors.New("invalid key \"location\" in \"constraint\""),
341348
errors.New("invalid key \"link\" in \"constraint\""),
342-
errors.New("invalid key \"nick\" in \"override\""),
343349
errors.New("metadata in \"constraint\" should be a TOML table"),
350+
errors.New("branch, version, revision, or source should be provided for \"github.com/foo/bar\""),
351+
errors.New("invalid key \"nick\" in \"override\""),
352+
errNoName,
344353
},
345354
wantError: nil,
346355
},
@@ -353,7 +362,9 @@ func TestValidateManifest(t *testing.T) {
353362
[constraint.metadata]
354363
color = "blue"
355364
`,
356-
wantWarn: []error{},
365+
wantWarn: []error{
366+
errors.New("branch, version, revision, or source should be provided for \"github.com/foo/bar\""),
367+
},
357368
wantError: nil,
358369
},
359370
{

0 commit comments

Comments
 (0)