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

Commit

Permalink
update tests for adding prune to manifest
Browse files Browse the repository at this point in the history
Signed-off-by: Ibrahim AshShohail <ibra.sho@gmail.com>
  • Loading branch information
ibrasho committed Nov 10, 2017
1 parent 505c236 commit 3d69984
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 25 deletions.
5 changes: 5 additions & 0 deletions manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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
Expand Down
110 changes: 85 additions & 25 deletions manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package dep
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"reflect"
Expand Down Expand Up @@ -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,
},
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
},
{
Expand All @@ -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
`,
Expand All @@ -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 {
Expand All @@ -431,14 +437,55 @@ 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)
}
}
})
}
}

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
Expand Down Expand Up @@ -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
}

0 comments on commit 3d69984

Please sign in to comment.