diff --git a/manifest.go b/manifest.go index 0d49c6be66..5b22a5da83 100644 --- a/manifest.go +++ b/manifest.go @@ -226,6 +226,7 @@ func validatePruneOptions(val interface{}, root bool) (warns []error, err error) if reflect.TypeOf(value).Kind() != reflect.Slice { return warns, errInvalidPruneProject } + for _, project := range value.([]interface{}) { projectWarns, err := validatePruneOptions(project, false) warns = append(warns, projectWarns...) @@ -559,6 +560,10 @@ func (m *Manifest) RequiredPackages() map[string]bool { return mp } +// PruneOptionsFor returns the prune options for the passed project root. +// +// It will return the root prune options if the project does not have specific +// options or if it does not exists in the manifest. func (m *Manifest) PruneOptionsFor(pr gps.ProjectRoot) gps.PruneOptions { if po, ok := m.PruneProjectOptions[pr]; ok { return po diff --git a/manifest_test.go b/manifest_test.go index 3fb7d070d3..f8f4703089 100644 --- a/manifest_test.go +++ b/manifest_test.go @@ -7,6 +7,7 @@ package dep import ( "bytes" "errors" + "fmt" "io/ioutil" "log" "reflect" @@ -47,7 +48,8 @@ func TestReadManifest(t *testing.T) { Ignored: []string{"github.com/foo/bar"}, PruneOptions: gps.PruneNestedVendorDirs | gps.PruneNonGoFiles, PruneProjectOptions: gps.PruneProjectOptions{ - gps.ProjectRoot("github.com/golang/dep"): gps.PruneNestedVendorDirs, + gps.ProjectRoot("github.com/golang/dep"): gps.PruneNestedVendorDirs, + gps.ProjectRoot("github.com/babble/brook"): gps.PruneNestedVendorDirs | gps.PruneGoTestFiles, }, } @@ -82,11 +84,6 @@ func TestWriteManifest(t *testing.T) { } m.Ignored = []string{"github.com/foo/bar"} - m.PruneOptions = gps.PruneNestedVendorDirs | gps.PruneNonGoFiles - m.PruneProjectOptions = gps.PruneProjectOptions{ - gps.ProjectRoot("github.com/golang/dep"): gps.PruneNestedVendorDirs, - } - got, err := m.MarshalTOML() if err != nil { t.Fatalf("error while marshaling valid manifest to TOML: %q", err) @@ -364,7 +361,9 @@ func TestValidateManifest(t *testing.T) { name = "github.com/foo/bar" revision = "b86ad16" `, - wantWarn: []error{errors.New("revision \"b86ad16\" should not be in abbreviated form")}, + wantWarn: []error{ + errors.New("revision \"b86ad16\" should not be in abbreviated form"), + }, wantError: nil, }, { @@ -380,10 +379,6 @@ func TestValidateManifest(t *testing.T) { { name: "valid prune options", tomlString: ` - [[constraint]] - name = "github.com/foo/bar" - version = "1.0.0" - [prune] non-go = true `, @@ -393,26 +388,37 @@ func TestValidateManifest(t *testing.T) { { name: "invalid root prune options", tomlString: ` - [[constraint]] - name = "github.com/foo/bar" - version = "1.0.0" - [prune] non-go = false `, wantWarn: []error{}, wantError: errInvalidRootPruneValue, }, - } + { + name: "root options should not contain a name", + tomlString: ` + [prune] + non-go = true + name = "github.com/golang/dep" + `, + wantWarn: []error{ + fmt.Errorf("%q should not include a name", "prune"), + }, + wantError: nil, + }, + { + name: "invalid prune project", + tomlString: ` + [prune] + non-go = true - // contains for error - contains := func(s []error, e error) bool { - for _, a := range s { - if a.Error() == e.Error() { - return true - } - } - return false + [prune.project] + name = "github.com/org/project" + non-go = true + `, + wantWarn: []error{}, + wantError: errInvalidPruneProject, + }, } for _, c := range cases { @@ -431,7 +437,7 @@ func TestValidateManifest(t *testing.T) { // check if the expected errors exist in actual errors slice for _, er := range errs { - if !contains(c.wantWarn, er) { + if !containsErr(c.wantWarn, er) { t.Fatalf("manifest errors are not as expected: \n\t(MISSING) %v\n\t(FROM) %v", er, c.wantWarn) } } @@ -439,6 +445,47 @@ func TestValidateManifest(t *testing.T) { } } +func TestCheckRedundantPruneOptions(t *testing.T) { + cases := []struct { + name string + pruneOptions rawPruneOptions + wantWarn []error + }{ + { + name: "redundant project prune options", + pruneOptions: rawPruneOptions{ + NonGoFiles: true, + Projects: []rawPruneProjectOptions{ + rawPruneProjectOptions{ + Name: "github.com/org/project", + NonGoFiles: true, + }, + }, + }, + wantWarn: []error{ + fmt.Errorf("redundant prune option %q set for %q", "non-go", "github.com/org/project"), + }, + }, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + errs := checkRedundantPruneOptions(rawManifest{PruneOptions: c.pruneOptions}) + + // compare length of error slice + if len(errs) != len(c.wantWarn) { + 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.wantWarn), c.wantWarn) + } + + for _, er := range errs { + if !containsErr(c.wantWarn, er) { + t.Fatalf("manifest errors are not as expected:\n\t(MISSING)\n%v\n\t(FROM)\n%v", er, c.wantWarn) + } + } + }) + } +} + func TestValidateProjectRoots(t *testing.T) { cases := []struct { name string @@ -545,3 +592,16 @@ func TestValidateProjectRoots(t *testing.T) { }) } } + +func TestPruneOptionsFor(t *testing.T) { + +} + +func containsErr(s []error, e error) bool { + for _, a := range s { + if a.Error() == e.Error() { + return true + } + } + return false +}