From 54445a6b585c48a8fd21412226813f4f60916e60 Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 30 Oct 2017 21:46:53 +0530 Subject: [PATCH 1/2] status: add validateFlags() --- cmd/dep/status.go | 52 +++++++++++++++++++++++++++++++++++++ cmd/dep/status_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index 9cae286b..4d90456e 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -211,6 +211,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 @@ -301,6 +305,54 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error { return nil } +func (cmd *statusCommand) validateFlags() error { + // Operating mode flags. + opModes := []string{} + + if cmd.detailed { + opModes = append(opModes, "-detailed") + } + + if cmd.old { + opModes = append(opModes, "-old") + } + + if cmd.missing { + opModes = append(opModes, "-missing") + } + + if cmd.unused { + opModes = append(opModes, "-unused") + } + + if cmd.modified { + opModes = append(opModes, "-modified") + } + + // 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 diff --git a/cmd/dep/status_test.go b/cmd/dep/status_test.go index 6f0eb3f4..a1fc867e 100644 --- a/cmd/dep/status_test.go +++ b/cmd/dep/status_test.go @@ -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) { @@ -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{unused: 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) + } + }) + } +} From 8f6ef146a928a5ab8a8bd13c054b02e89a899aae Mon Sep 17 00:00:00 2001 From: Sunny Date: Sun, 19 Nov 2017 19:04:11 +0530 Subject: [PATCH 2/2] status: remove modified, unused and detailed flags --- cmd/dep/status.go | 24 ------------------------ cmd/dep/status_test.go | 2 +- 2 files changed, 1 insertion(+), 25 deletions(-) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index 4d90456e..d7988035 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -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 { @@ -234,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, @@ -309,10 +297,6 @@ func (cmd *statusCommand) validateFlags() error { // Operating mode flags. opModes := []string{} - if cmd.detailed { - opModes = append(opModes, "-detailed") - } - if cmd.old { opModes = append(opModes, "-old") } @@ -321,14 +305,6 @@ func (cmd *statusCommand) validateFlags() error { opModes = append(opModes, "-missing") } - if cmd.unused { - opModes = append(opModes, "-unused") - } - - if cmd.modified { - opModes = append(opModes, "-modified") - } - // Check if any other flags are passed with -dot. if cmd.dot { if cmd.template != "" { diff --git a/cmd/dep/status_test.go b/cmd/dep/status_test.go index a1fc867e..b06a4591 100644 --- a/cmd/dep/status_test.go +++ b/cmd/dep/status_test.go @@ -441,7 +441,7 @@ func TestValidateFlags(t *testing.T) { }, { name: "single operating mode", - cmd: statusCommand{unused: true}, + cmd: statusCommand{old: true}, wantErr: nil, }, {