Merge pull request #1330 from darkowlzz/status-flag-validation

status: add validateFlags()
This commit is contained in:
sam boyer 2017-12-18 13:32:14 -05:00 коммит произвёл GitHub
Родитель 67c45f4686 8f6ef146a9
Коммит f2e423e3f2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 99 добавлений и 12 удалений

Просмотреть файл

@ -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 {
@ -223,6 +217,10 @@ func (out *templateOutput) MissingLine(ms *MissingStatus) error {
}
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
@ -242,16 +240,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,
@ -313,6 +305,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

Просмотреть файл

@ -18,6 +18,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) {
@ -421,3 +422,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)
}
})
}
}