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

Manifest Version/Source Validation #1370

Merged
merged 5 commits into from
Dec 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,10 @@ func TestCaseInsentitiveGOPATH(t *testing.T) {

h.TempDir("src")
h.TempDir("src/test1")
h.TempFile(filepath.Join("src/test1", ManifestName), `[[constraint]]`)
h.TempFile(filepath.Join("src/test1", ManifestName), `
[[constraint]]
name = "github.com/foo/bar"
branch = "master"`)

// Shuffle letter case
rs := []rune(strings.ToLower(h.Path(".")))
Expand Down
16 changes: 13 additions & 3 deletions manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
errRootPruneContainsName = errors.Errorf("%q should not include a name", "prune")
errInvalidRootPruneValue = errors.New("root prune options must be omitted instead of being set to false")
errInvalidPruneProjectName = errors.Errorf("%q in %q must be a string", "name", "prune.project")
errNoName = errors.New("no name provided")
)

// Manifest holds manifest file data and implements gps.RootManifest.
Expand Down Expand Up @@ -133,13 +134,17 @@ func validateManifest(s string) ([]error, error) {
if valid {
// Iterate through each array of tables
for _, v := range rawProj {
ruleProvided := false
props := v.(map[string]interface{})
// Check the individual field's key to be valid
for key, value := range v.(map[string]interface{}) {
for key, value := range props {
// Check if the key is valid
switch key {
case "name", "branch", "version", "source":
// valid key
case "name":
case "branch", "version", "source":
ruleProvided = true
case "revision":
ruleProvided = true
if valueStr, ok := value.(string); ok {
if abbrevRevHash.MatchString(valueStr) {
warns = append(warns, fmt.Errorf("revision %q should not be in abbreviated form", valueStr))
Expand All @@ -155,6 +160,11 @@ func validateManifest(s string) ([]error, error) {
warns = append(warns, fmt.Errorf("invalid key %q in %q", key, prop))
}
}
if _, ok := props["name"]; !ok {
warns = append(warns, errNoName)
} else if !ruleProvided && prop == "constraint" {
warns = append(warns, fmt.Errorf("branch, version, revision, or source should be provided for %q", props["name"]))
}
}
}
} else {
Expand Down
21 changes: 16 additions & 5 deletions manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func TestValidateManifest(t *testing.T) {
`,
wantWarn: []error{
errInvalidMetadata,
errors.New("branch, version, revision, or source should be provided for \"github.com/foo/bar\""),
},
wantError: nil,
},
Expand All @@ -264,15 +265,19 @@ func TestValidateManifest(t *testing.T) {
[[constraint]]
name = "github.com/foo/bar"
`,
wantWarn: []error{},
wantWarn: []error{
errors.New("branch, version, revision, or source should be provided for \"github.com/foo/bar\""),
},
wantError: nil,
},
{
name: "empty constraint",
tomlString: `
[[constraint]]
`,
wantWarn: []error{},
wantWarn: []error{
errNoName,
},
wantError: nil,
},
{
Expand Down Expand Up @@ -305,7 +310,9 @@ func TestValidateManifest(t *testing.T) {
tomlString: `
[[override]]
`,
wantWarn: []error{},
wantWarn: []error{
errNoName,
},
wantError: nil,
},
{
Expand Down Expand Up @@ -339,8 +346,10 @@ func TestValidateManifest(t *testing.T) {
wantWarn: []error{
errors.New("invalid key \"location\" in \"constraint\""),
errors.New("invalid key \"link\" in \"constraint\""),
errors.New("invalid key \"nick\" in \"override\""),
errors.New("metadata in \"constraint\" should be a TOML table"),
errors.New("branch, version, revision, or source should be provided for \"github.com/foo/bar\""),
errors.New("invalid key \"nick\" in \"override\""),
errNoName,
},
wantError: nil,
},
Expand All @@ -353,7 +362,9 @@ func TestValidateManifest(t *testing.T) {
[constraint.metadata]
color = "blue"
`,
wantWarn: []error{},
wantWarn: []error{
errors.New("branch, version, revision, or source should be provided for \"github.com/foo/bar\""),
},
wantError: nil,
},
{
Expand Down