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

Commit

Permalink
dep: add NewManifest
Browse files Browse the repository at this point in the history
This commit adds a constructor for dep.Manifest.

Signed-off-by: Ibrahim AshShohail <ibra.sho@gmail.com>
  • Loading branch information
ibrasho committed Aug 16, 2017
1 parent 53e80dc commit ef1909c
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 89 deletions.
5 changes: 5 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@
[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"

[prune]
non-go = true
go-tests = true
unused-packages = true
5 changes: 2 additions & 3 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,9 +603,8 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm

// Prep post-actions and feedback from adds.
var reqlist []string
appender := &dep.Manifest{
Constraints: make(gps.ProjectConstraints),
}
appender := dep.NewManifest()

for pr, instr := range addInstructions {
for path := range instr.ephReq {
reqlist = append(reqlist, path)
Expand Down
4 changes: 1 addition & 3 deletions cmd/dep/glide_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ func (g *glideImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, e
task.WriteString("...")
g.logger.Println(task)

manifest := &dep.Manifest{
Constraints: make(gps.ProjectConstraints),
}
manifest := dep.NewManifest()

for _, pkg := range append(g.yaml.Imports, g.yaml.TestImports...) {
pc, err := g.buildProjectConstraint(pkg)
Expand Down
4 changes: 1 addition & 3 deletions cmd/dep/godep_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ func (g *godepImporter) load(projectDir string) error {
func (g *godepImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
g.logger.Println("Converting from Godeps.json ...")

manifest := &dep.Manifest{
Constraints: make(gps.ProjectConstraints),
}
manifest := dep.NewManifest()
lock := &dep.Lock{}

for _, pkg := range g.json.Imports {
Expand Down
13 changes: 4 additions & 9 deletions cmd/dep/godep_importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ func TestGodepConfig_Convert(t *testing.T) {
},
},
convertTestCase: &convertTestCase{

projectRoot: gps.ProjectRoot("github.com/sdboyer/deptest"),
wantConstraint: "^1.12.0-12-g2fd980e",
wantLockCount: 1,
wantVersion: "v1.0.0",
projectRoot: gps.ProjectRoot("github.com/sdboyer/deptest"),
wantConstraint: "^1.12.0-12-g2fd980e",
wantLockCount: 1,
wantVersion: "v1.0.0",
},
},
"empty comment": {
Expand All @@ -70,7 +69,6 @@ func TestGodepConfig_Convert(t *testing.T) {
},
},
convertTestCase: &convertTestCase{

projectRoot: gps.ProjectRoot("github.com/sdboyer/deptest"),
wantConstraint: "^1.0.0",
wantRevision: gps.Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf"),
Expand All @@ -83,7 +81,6 @@ func TestGodepConfig_Convert(t *testing.T) {
Imports: []godepPackage{{ImportPath: ""}},
},
convertTestCase: &convertTestCase{

wantConvertErr: true,
},
},
Expand All @@ -96,7 +93,6 @@ func TestGodepConfig_Convert(t *testing.T) {
},
},
convertTestCase: &convertTestCase{

wantConvertErr: true,
},
},
Expand All @@ -116,7 +112,6 @@ func TestGodepConfig_Convert(t *testing.T) {
},
},
convertTestCase: &convertTestCase{

projectRoot: gps.ProjectRoot("github.com/sdboyer/deptest"),
wantLockCount: 1,
wantConstraint: "^1.0.0",
Expand Down
7 changes: 3 additions & 4 deletions cmd/dep/gopath_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ func (g *gopathScanner) InitializeRootManifestAndLock(rootM *dep.Manifest, rootL
return err
}

g.origM = &dep.Manifest{
Constraints: g.pd.constraints,
Ovr: make(gps.ProjectConstraints),
}
g.origM = dep.NewManifest()
g.origM.Constraints = g.pd.constraints

g.origL = &dep.Lock{
P: make([]gps.LockedProject, 0, len(g.pd.ondisk)),
}
Expand Down
21 changes: 8 additions & 13 deletions cmd/dep/gopath_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,14 @@ func TestGopathScanner_OverlayManifestConstraints(t *testing.T) {
v1 := gps.NewVersion("v1.0.0")
v2 := gps.NewVersion("v2.0.0")
v3 := gps.NewVersion("v3.0.0")
rootM := &dep.Manifest{
Constraints: gps.ProjectConstraints{
pi1.ProjectRoot: gps.ProjectProperties{Constraint: v1},
},
}
rootM := dep.NewManifest()
rootM.Constraints[pi1.ProjectRoot] = gps.ProjectProperties{Constraint: v1}
rootL := &dep.Lock{}
origM := dep.NewManifest()
origM.Constraints[pi1.ProjectRoot] = gps.ProjectProperties{Constraint: v2}
origM.Constraints[pi2.ProjectRoot] = gps.ProjectProperties{Constraint: v3}
gs := gopathScanner{
origM: &dep.Manifest{
Constraints: gps.ProjectConstraints{
pi1.ProjectRoot: gps.ProjectProperties{Constraint: v2},
pi2.ProjectRoot: gps.ProjectProperties{Constraint: v3},
},
},
origM: origM,
origL: &dep.Lock{},
ctx: ctx,
pd: projectData{
Expand Down Expand Up @@ -79,7 +74,7 @@ func TestGopathScanner_OverlayLockProjects(t *testing.T) {
h := test.NewHelper(t)
ctx := newTestContext(h)

rootM := &dep.Manifest{}
rootM := dep.NewManifest()
pi1 := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(testProject1)}
pi2 := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(testProject2)}
v1 := gps.NewVersion("v1.0.0")
Expand All @@ -89,7 +84,7 @@ func TestGopathScanner_OverlayLockProjects(t *testing.T) {
P: []gps.LockedProject{gps.NewLockedProject(pi1, v1, []string{})},
}
gs := gopathScanner{
origM: &dep.Manifest{Constraints: make(gps.ProjectConstraints)},
origM: dep.NewManifest(),
origL: &dep.Lock{
P: []gps.LockedProject{
gps.NewLockedProject(pi1, v2, []string{}), // ignored, already exists in lock
Expand Down
8 changes: 3 additions & 5 deletions cmd/dep/root_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ func (a *rootAnalyzer) InitializeRootManifestAndLock(dir string, pr gps.ProjectR
}

if rootM == nil {
rootM = &dep.Manifest{
Constraints: make(gps.ProjectConstraints),
Ovr: make(gps.ProjectConstraints),
}
rootM = dep.NewManifest()
}
if rootL == nil {
rootL = &dep.Lock{}
Expand Down Expand Up @@ -88,7 +85,8 @@ func (a *rootAnalyzer) importManifestAndLock(dir string, pr gps.ProjectRoot, sup
}
}

var emptyManifest = &dep.Manifest{Constraints: make(gps.ProjectConstraints), Ovr: make(gps.ProjectConstraints)}
var emptyManifest = dep.NewManifest()

return emptyManifest, nil, nil
}

Expand Down
8 changes: 3 additions & 5 deletions cmd/dep/vndr_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ func (v *vndrImporter) loadVndrFile(dir string) error {

func (v *vndrImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
var (
manifest = &dep.Manifest{
Constraints: make(gps.ProjectConstraints),
}
lock = &dep.Lock{}
err error
manifest = dep.NewManifest()
lock = &dep.Lock{}
err error
)

for _, pkg := range v.packages {
Expand Down
17 changes: 7 additions & 10 deletions cmd/dep/vndr_importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,13 @@ func TestVndrConfig_Import(t *testing.T) {

constraint, err := gps.NewSemverConstraint("^2.0.0")
h.Must(err)
wantM := &dep.Manifest{
Constraints: gps.ProjectConstraints{
"github.com/sdboyer/deptest": gps.ProjectProperties{
Source: "https://github.com/sdboyer/deptest.git",
Constraint: gps.Revision("3f4c3bea144e112a69bbe5d8d01c1b09a544253f"),
},
"github.com/sdboyer/deptestdos": gps.ProjectProperties{
Constraint: constraint,
},
},
wantM := dep.NewManifest()
wantM.Constraints["github.com/sdboyer/deptest"] = gps.ProjectProperties{
Source: "https://github.com/sdboyer/deptest.git",
Constraint: gps.Revision("3f4c3bea144e112a69bbe5d8d01c1b09a544253f"),
}
wantM.Constraints["github.com/sdboyer/deptestdos"] = gps.ProjectProperties{
Constraint: constraint,
}
if !reflect.DeepEqual(wantM, m) {
t.Errorf("unexpected manifest\nhave=%+v\nwant=%+v", m, wantM)
Expand Down
41 changes: 33 additions & 8 deletions manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
errInvalidOverride = errors.New("\"override\" must be a TOML array of tables")
errInvalidRequired = errors.New("\"required\" must be a TOML list of strings")
errInvalidIgnored = errors.New("\"ignored\" must be a TOML list of strings")
errInvalidPrune = errors.New("\"prune\" must be a TOML table of boolean values")
)

// Manifest holds manifest file data and implements gps.RootManifest.
Expand All @@ -51,6 +52,14 @@ type rawProject struct {
Source string `toml:"source,omitempty"`
}

// NewManifest instantites a new manifest.
func NewManifest() *Manifest {
return &Manifest{
Constraints: make(gps.ProjectConstraints),
Ovr: make(gps.ProjectConstraints),
}
}

func validateManifest(s string) ([]error, error) {
var warns []error
// Load the TomlTree from string
Expand Down Expand Up @@ -103,7 +112,7 @@ func validateManifest(s string) ([]error, error) {
}
default:
// unknown/invalid key
warns = append(warns, fmt.Errorf("Invalid key %q in %q", key, prop))
warns = append(warns, fmt.Errorf("invalid key %q in %q", key, prop))
}
}
}
Expand Down Expand Up @@ -140,8 +149,24 @@ func validateManifest(s string) ([]error, error) {
return warns, errInvalidRequired
}
}
case "prune":
// Check if prune is of Map type
if reflect.TypeOf(val).Kind() != reflect.Map {
warns = append(warns, errors.New("prune should be a TOML table"))
}
for key, value := range val.(map[string]interface{}) {
switch key {
case "non-go", "go-tests", "unused-packages":
if _, ok := value.(bool); !ok {
warns = append(warns, errors.Errorf("unexpected type for prune.%s: %T", key, value))
return warns, errInvalidPrune
}
default:
warns = append(warns, errors.Errorf("unknown field: prune.%s", key))
}
}
default:
warns = append(warns, fmt.Errorf("Unknown field in manifest: %v", prop))
warns = append(warns, fmt.Errorf("unknown field in manifest: %v", prop))
}
}

Expand Down Expand Up @@ -172,12 +197,12 @@ func readManifest(r io.Reader) (*Manifest, []error, error) {
}

func fromRawManifest(raw rawManifest) (*Manifest, error) {
m := &Manifest{
Constraints: make(gps.ProjectConstraints, len(raw.Constraints)),
Ovr: make(gps.ProjectConstraints, len(raw.Overrides)),
Ignored: raw.Ignored,
Required: raw.Required,
}
m := NewManifest()

m.Constraints = make(gps.ProjectConstraints, len(raw.Constraints))
m.Ovr = make(gps.ProjectConstraints, len(raw.Overrides))
m.Ignored = raw.Ignored
m.Required = raw.Required

for i := 0; i < len(raw.Constraints); i++ {
name, prj, err := toProject(raw.Constraints[i])
Expand Down
45 changes: 20 additions & 25 deletions manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,18 @@ func TestWriteManifest(t *testing.T) {
golden := "manifest/golden.toml"
want := h.GetTestFileString(golden)
c, _ := gps.NewSemverConstraint("^0.12.0")
m := &Manifest{
Constraints: map[gps.ProjectRoot]gps.ProjectProperties{
gps.ProjectRoot("github.com/golang/dep/internal/gps"): {
Constraint: c,
},
gps.ProjectRoot("github.com/babble/brook"): {
Constraint: gps.Revision("d05d5aca9f895d19e9265839bffeadd74a2d2ecb"),
},
},
Ovr: map[gps.ProjectRoot]gps.ProjectProperties{
gps.ProjectRoot("github.com/golang/dep/internal/gps"): {
Source: "https://github.com/golang/dep/internal/gps",
Constraint: gps.NewBranch("master"),
},
},
Ignored: []string{"github.com/foo/bar"},
m := NewManifest()
m.Constraints[gps.ProjectRoot("github.com/golang/dep/internal/gps")] = gps.ProjectProperties{
Constraint: c,
}
m.Constraints[gps.ProjectRoot("github.com/babble/brook")] = gps.ProjectProperties{
Constraint: gps.Revision("d05d5aca9f895d19e9265839bffeadd74a2d2ecb"),
}
m.Ovr[gps.ProjectRoot("github.com/golang/dep/internal/gps")] = gps.ProjectProperties{
Source: "https://github.com/golang/dep/internal/gps",
Constraint: gps.NewBranch("master"),
}
m.Ignored = []string{"github.com/foo/bar"}

got, err := m.MarshalTOML()
if err != nil {
Expand Down Expand Up @@ -222,9 +217,9 @@ func TestValidateManifest(t *testing.T) {
version = ""
`,
wantWarn: []error{
errors.New("Unknown field in manifest: foo"),
errors.New("Unknown field in manifest: bar"),
errors.New("Unknown field in manifest: version"),
errors.New("unknown field in manifest: foo"),
errors.New("unknown field in manifest: bar"),
errors.New("unknown field in manifest: version"),
},
wantError: nil,
},
Expand Down Expand Up @@ -308,9 +303,9 @@ func TestValidateManifest(t *testing.T) {
nick = "foo"
`,
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("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"),
},
wantError: nil,
Expand Down Expand Up @@ -361,18 +356,18 @@ func TestValidateManifest(t *testing.T) {

// compare validation errors
if err != c.wantError {
t.Fatalf("Manifest errors are not as expected: \n\t(GOT) %v \n\t(WNT) %v", err, c.wantError)
t.Fatalf("manifest errors are not as expected: \n\t(GOT) %v \n\t(WNT) %v", err, c.wantError)
}

// 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)
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)
}

// check if the expected errors exist in actual errors slice
for _, er := range errs {
if !contains(c.wantWarn, er) {
t.Fatalf("Manifest errors are not as expected: \n\t(MISSING) %v\n\t(FROM) %v", er, c.wantWarn)
t.Fatalf("manifest errors are not as expected: \n\t(MISSING) %v\n\t(FROM) %v", er, c.wantWarn)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ func TestFindRoot(t *testing.T) {
}

func TestProjectMakeParams(t *testing.T) {
m := NewManifest()
m.Ignored = []string{"ignoring this"}

p := Project{
AbsRoot: "someroot",
ImportRoot: gps.ProjectRoot("Some project root"),
Manifest: &Manifest{Ignored: []string{"ignoring this"}},
Manifest: m,
Lock: &Lock{},
}

Expand Down

0 comments on commit ef1909c

Please sign in to comment.