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

status: add validateFlags() #1330

Merged
merged 2 commits into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 40 additions & 12 deletions cmd/dep/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,20 @@ func (cmd *statusCommand) LongHelp() string { return statusLongHelp }
func (cmd *statusCommand) Hidden() bool { return false }

func (cmd *statusCommand) Register(fs *flag.FlagSet) {
fs.BoolVar(&cmd.detailed, "detailed", false, "report more detailed status")
fs.BoolVar(&cmd.json, "json", false, "output in JSON format")
fs.StringVar(&cmd.template, "f", "", "output in text/template format")
fs.BoolVar(&cmd.dot, "dot", false, "output the dependency graph in GraphViz format")
fs.BoolVar(&cmd.old, "old", false, "only show out-of-date dependencies")
fs.BoolVar(&cmd.missing, "missing", false, "only show missing dependencies")
fs.BoolVar(&cmd.unused, "unused", false, "only show unused dependencies")
fs.BoolVar(&cmd.modified, "modified", false, "only show modified dependencies")
}

type statusCommand struct {
detailed bool
json bool
template string
output string
dot bool
old bool
missing bool
unused bool
modified bool
}

type outputter interface {
Expand Down Expand Up @@ -211,6 +205,10 @@ func (out *templateOutput) MissingLine(ms *MissingStatus) {
}

func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
if err := cmd.validateFlags(); err != nil {
return err
}

p, err := ctx.LoadProject()
if err != nil {
return err
Expand All @@ -230,16 +228,10 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
var buf bytes.Buffer
var out outputter
switch {
case cmd.modified:
return errors.Errorf("not implemented")
case cmd.unused:
return errors.Errorf("not implemented")
case cmd.missing:
return errors.Errorf("not implemented")
case cmd.old:
return errors.Errorf("not implemented")
case cmd.detailed:
return errors.Errorf("not implemented")
case cmd.json:
out = &jsonOutput{
w: &buf,
Expand Down Expand Up @@ -301,6 +293,42 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
return nil
}

func (cmd *statusCommand) validateFlags() error {
// Operating mode flags.
opModes := []string{}

if cmd.old {
opModes = append(opModes, "-old")
}

if cmd.missing {
opModes = append(opModes, "-missing")
}

// Check if any other flags are passed with -dot.
if cmd.dot {
if cmd.template != "" {
return errors.New("cannot pass template string with -dot")
}

if cmd.json {
return errors.New("cannot pass multiple output format flags")
}

if len(opModes) > 0 {
return errors.New("-dot generates dependency graph; cannot pass other flags")
}
}

if len(opModes) > 1 {
// List the flags because which flags are for operation mode might not
// be apparent to the users.
return errors.Wrapf(errors.New("cannot pass multiple operating mode flags"), "%v", opModes)
}

return nil
}

type rawStatus struct {
ProjectRoot string
Constraint string
Expand Down
59 changes: 59 additions & 0 deletions cmd/dep/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/golang/dep"
"github.com/golang/dep/gps"
"github.com/golang/dep/internal/test"
"github.com/pkg/errors"
)

func TestStatusFormatVersion(t *testing.T) {
Expand Down Expand Up @@ -406,3 +407,61 @@ func TestCollectConstraints(t *testing.T) {
})
}
}

func TestValidateFlags(t *testing.T) {
testCases := []struct {
name string
cmd statusCommand
wantErr error
}{
{
name: "no flags",
cmd: statusCommand{},
wantErr: nil,
},
{
name: "-dot only",
cmd: statusCommand{dot: true},
wantErr: nil,
},
{
name: "-dot with template",
cmd: statusCommand{dot: true, template: "foo"},
wantErr: errors.New("cannot pass template string with -dot"),
},
{
name: "-dot with -json",
cmd: statusCommand{dot: true, json: true},
wantErr: errors.New("cannot pass multiple output format flags"),
},
{
name: "-dot with operating mode",
cmd: statusCommand{dot: true, old: true},
wantErr: errors.New("-dot generates dependency graph; cannot pass other flags"),
},
{
name: "single operating mode",
cmd: statusCommand{old: true},
wantErr: nil,
},
{
name: "multiple operating modes",
cmd: statusCommand{missing: true, old: true},
wantErr: errors.Wrapf(errors.New("cannot pass multiple operating mode flags"), "[-old -missing]"),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.cmd.validateFlags()

if err == nil {
if tc.wantErr != nil {
t.Errorf("unexpected error: \n\t(GOT): %v\n\t(WNT): %v", err, tc.wantErr)
}
} else if err.Error() != tc.wantErr.Error() {
t.Errorf("unexpected error: \n\t(GOT): %v\n\t(WNT): %v", err, tc.wantErr)
}
})
}
}