From 7f7cd4dfe36e2998548e446664e307faccbe721c Mon Sep 17 00:00:00 2001 From: Ibrahim AshShohail Date: Sat, 28 Oct 2017 02:52:33 +0300 Subject: [PATCH] dep: update tests for adding prune to manifest Signed-off-by: Ibrahim AshShohail --- internal/gps/prune.go | 2 - manifest.go | 5 ++ manifest_test.go | 110 ++++++++++++++++++++++++++++++++---------- 3 files changed, 90 insertions(+), 27 deletions(-) diff --git a/internal/gps/prune.go b/internal/gps/prune.go index 7fdff4f053..3abbbec808 100644 --- a/internal/gps/prune.go +++ b/internal/gps/prune.go @@ -140,7 +140,6 @@ func pruneUnusedPackages(lp LockedProject, projectDir string, logger *log.Logger // calculateUnusedPackages generates a list of unused packages in lp. func calculateUnusedPackages(lp LockedProject, projectDir string) (map[string]struct{}, error) { - // TODO(ibrasho): optimize this... unused := make(map[string]struct{}) imported := make(map[string]struct{}) for _, pkg := range lp.Packages() { @@ -175,7 +174,6 @@ func calculateUnusedPackages(lp LockedProject, projectDir string) (map[string]st // collectUnusedPackagesFiles returns a slice of all files in the unused packages in projectDir. func collectUnusedPackagesFiles(projectDir string, unusedPackages map[string]struct{}) ([]string, error) { - // TODO(ibrasho): is this useful? files := make([]string, 0, len(unusedPackages)) err := filepath.Walk(projectDir, func(path string, info os.FileInfo, err error) error { 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..c55040e79e 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] + go-tests = 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 +}