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

Commit 1ce6fdb

Browse files
committed
Perform fixes, add tests, add display methods
1 parent 394d1d0 commit 1ce6fdb

File tree

9 files changed

+242
-13
lines changed

9 files changed

+242
-13
lines changed

cmd/dep/status.go

+128-13
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ type outputter interface {
8282
MissingFooter() error
8383
}
8484

85+
// Only a subset of the outputters should be able to output old statuses
86+
type oldOutputter interface {
87+
OldHeader() error
88+
OldLine(*OldStatus) error
89+
OldFooter() error
90+
}
91+
8592
type tableOutput struct{ w *tabwriter.Writer }
8693

8794
func (out *tableOutput) BasicHeader() error {
@@ -124,10 +131,31 @@ func (out *tableOutput) MissingFooter() error {
124131
return out.w.Flush()
125132
}
126133

134+
func (out *tableOutput) OldHeader() error {
135+
_, err := fmt.Fprintf(out.w, "PROJECT\tCONSTRAINT\tREVISION\tLATEST\n")
136+
return err
137+
}
138+
139+
func (out *tableOutput) OldLine(os *OldStatus) error {
140+
_, err := fmt.Fprintf(out.w,
141+
"%s\t%s\t%s\t%s\t\n",
142+
os.ProjectRoot,
143+
os.getConsolidatedConstraint(),
144+
formatVersion(os.Revision),
145+
os.getConsolidatedLatest(shortRev),
146+
)
147+
return err
148+
}
149+
150+
func (out *tableOutput) OldFooter() error {
151+
return out.w.Flush()
152+
}
153+
127154
type jsonOutput struct {
128155
w io.Writer
129156
basic []*rawStatus
130157
missing []*MissingStatus
158+
old []*rawOldStatus
131159
}
132160

133161
func (out *jsonOutput) BasicHeader() error {
@@ -158,6 +186,20 @@ func (out *jsonOutput) MissingFooter() error {
158186
return json.NewEncoder(out.w).Encode(out.missing)
159187
}
160188

189+
func (out *jsonOutput) OldHeader() error {
190+
out.old = []*rawOldStatus{}
191+
return nil
192+
}
193+
194+
func (out *jsonOutput) OldLine(os *OldStatus) error {
195+
out.old = append(out.old, os.marshalJSON())
196+
return nil
197+
}
198+
199+
func (out *jsonOutput) OldFooter() error {
200+
return json.NewEncoder(out.w).Encode(out.old)
201+
}
202+
161203
type dotOutput struct {
162204
w io.Writer
163205
o string
@@ -237,8 +279,6 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
237279
switch {
238280
case cmd.missing:
239281
return errors.Errorf("not implemented")
240-
case cmd.old:
241-
cmd.runOld(ctx, args, p, sm)
242282
case cmd.json:
243283
out = &jsonOutput{
244284
w: &buf,
@@ -269,6 +309,15 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
269309
return errors.Errorf("no Gopkg.lock found. Run `dep ensure` to generate lock file")
270310
}
271311

312+
if cmd.old {
313+
if _, ok := out.(oldOutputter); !ok {
314+
return errors.Errorf("invalid output command usesed")
315+
}
316+
err = cmd.runOld(ctx, out.(oldOutputter), p, sm)
317+
ctx.Out.Print(buf.String())
318+
return err
319+
}
320+
272321
hasMissingPkgs, errCount, err := runStatusAll(ctx, out, p, sm)
273322
if err != nil {
274323
switch err {
@@ -305,6 +354,9 @@ func (cmd *statusCommand) validateFlags() error {
305354
opModes := []string{}
306355

307356
if cmd.old {
357+
if cmd.template != "" {
358+
return errors.New("cannot pass template string with -old")
359+
}
308360
opModes = append(opModes, "-old")
309361
}
310362

@@ -340,12 +392,49 @@ func (cmd *statusCommand) validateFlags() error {
340392
type OldStatus struct {
341393
ProjectRoot string
342394
Constraint gps.Constraint
343-
Version gps.UnpairedVersion
344395
Revision gps.Revision
345396
Latest gps.Version
346397
}
347398

348-
func (cmd *statusCommand) runOld(ctx *dep.Ctx, args []string, p *dep.Project, sm gps.SourceManager) error {
399+
type rawOldStatus struct {
400+
ProjectRoot, Constraint, Revision, Latest string
401+
}
402+
403+
func (os OldStatus) getConsolidatedConstraint() string {
404+
var constraint string
405+
if os.Constraint != nil {
406+
if v, ok := os.Constraint.(gps.Version); ok {
407+
constraint = formatVersion(v)
408+
} else {
409+
constraint = os.Constraint.String()
410+
}
411+
}
412+
return constraint
413+
}
414+
415+
func (os OldStatus) getConsolidatedLatest(revSize uint8) string {
416+
latest := ""
417+
if os.Latest != nil {
418+
switch revSize {
419+
case shortRev:
420+
latest = formatVersion(os.Latest)
421+
case longRev:
422+
latest = os.Latest.String()
423+
}
424+
}
425+
return latest
426+
}
427+
428+
func (os OldStatus) marshalJSON() *rawOldStatus {
429+
return &rawOldStatus{
430+
ProjectRoot: os.ProjectRoot,
431+
Constraint: os.getConsolidatedConstraint(),
432+
Revision: string(os.Revision),
433+
Latest: os.getConsolidatedLatest(longRev),
434+
}
435+
}
436+
437+
func (cmd *statusCommand) runOld(ctx *dep.Ctx, out oldOutputter, p *dep.Project, sm gps.SourceManager) error {
349438
// While the network churns on ListVersions() requests, statically analyze
350439
// code from the current project.
351440
ptree, err := p.ParseRootPackageTree()
@@ -375,22 +464,48 @@ func (cmd *statusCommand) runOld(ctx *dep.Ctx, args []string, p *dep.Project, sm
375464
return errors.Wrap(err, "runOld")
376465
}
377466

378-
var oldLockProjects []gps.LockedProject
379-
lockProjects := p.Lock.Projects()
467+
var oldStatuses []OldStatus
380468
solutionProjects := solution.Projects()
381469

382-
for i := range solutionProjects {
383-
spr, _, _ := gps.VersionComponentStrings(solutionProjects[i].Version())
384-
lpr, _, _ := gps.VersionComponentStrings(lockProjects[i].Version())
470+
for _, proj := range p.Lock.Projects() {
471+
for i := range solutionProjects {
472+
// Look for the same project in solution and lock
473+
if solutionProjects[i].Ident().ProjectRoot != proj.Ident().ProjectRoot {
474+
continue
475+
}
476+
477+
// If revisions are not the same then it is old and we should display it
478+
latestRev, _, _ := gps.VersionComponentStrings(solutionProjects[i].Version())
479+
atRev, _, _ := gps.VersionComponentStrings(proj.Version())
480+
if atRev == latestRev {
481+
continue
482+
}
385483

386-
if spr != lpr {
387-
oldLockProjects = append(oldLockProjects, lockProjects[i])
484+
// Generate the old status data and append it
485+
os := OldStatus{
486+
ProjectRoot: proj.Ident().String(),
487+
Revision: gps.Revision(atRev),
488+
Latest: gps.Revision(latestRev),
489+
}
490+
// Getting Constraint
491+
if pp, has := p.Manifest.Ovr[proj.Ident().ProjectRoot]; has && pp.Constraint != nil {
492+
// manifest has override for project
493+
os.Constraint = pp.Constraint
494+
} else if pp, has := p.Manifest.Constraints[proj.Ident().ProjectRoot]; has && pp.Constraint != nil {
495+
// manifest has normal constraint
496+
os.Constraint = pp.Constraint
497+
} else {
498+
os.Constraint = gps.Any()
499+
}
500+
oldStatuses = append(oldStatuses, os)
388501
}
389502
}
390503

391-
for _, oldLockProject := range oldLockProjects {
392-
ctx.Out.Println(oldLockProject)
504+
out.OldHeader()
505+
for _, ostat := range oldStatuses {
506+
out.OldLine(&ostat)
393507
}
508+
out.OldFooter()
394509

395510
return nil
396511
}

cmd/dep/status_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,16 @@ func TestValidateFlags(t *testing.T) {
487487
cmd: statusCommand{missing: true, old: true},
488488
wantErr: errors.Wrapf(errors.New("cannot pass multiple operating mode flags"), "[-old -missing]"),
489489
},
490+
{
491+
name: "old with -dot",
492+
cmd: statusCommand{dot: true, old: true},
493+
wantErr: errors.New("-dot generates dependency graph; cannot pass other flags"),
494+
},
495+
{
496+
name: "old with template",
497+
cmd: statusCommand{old: true, template: "foo"},
498+
wantErr: errors.New("cannot pass template string with -old"),
499+
},
490500
}
491501

492502
for _, tc := range testCases {

cmd/dep/testdata/harness_tests/status/old_constraints/final/Gopkg.lock

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[constraint]]
2+
name = "github.com/sdboyer/deptestdos"
3+
version = "v2.0.0"
4+
5+
[[constraint]]
6+
name = "github.com/sdboyer/deptest"
7+
version = "v1.0.0"
8+
9+
[[constraint]]
10+
name = "github.com/carolynvs/go-dep-test"
11+
version = "v0.1.0"

cmd/dep/testdata/harness_tests/status/old_constraints/initial/Gopkg.lock

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[constraint]]
2+
name = "github.com/sdboyer/deptestdos"
3+
version = "v2.0.0"
4+
5+
[[constraint]]
6+
name = "github.com/sdboyer/deptest"
7+
version = "v1.0.0"
8+
9+
[[constraint]]
10+
name = "github.com/carolynvs/go-dep-test"
11+
version = "v0.1.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
_ "github.com/carolynvs/go-dep-test"
9+
_ "github.com/sdboyer/deptest"
10+
_ "github.com/sdboyer/deptestdos"
11+
)
12+
13+
func main() {
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PROJECT CONSTRAINT REVISION LATEST
2+
github.com/carolynvs/go-dep-test ^0.1.0 b9c5511 4069198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"commands": [
3+
["ensure"],
4+
["status", "-old"]
5+
],
6+
"error-expected": "",
7+
"vendor-final": [
8+
"github.com/carolynvs/go-dep-test",
9+
"github.com/sdboyer/deptest",
10+
"github.com/sdboyer/deptestdos"
11+
]
12+
}

0 commit comments

Comments
 (0)