This commit is contained in:
Sunny 2017-10-30 21:46:53 +05:30
Родитель ffac8dc47a
Коммит 54445a6b58
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4322DE32A726C94E
2 изменённых файлов: 111 добавлений и 0 удалений

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

@ -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

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

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