Skip to content
Myr

Proper handling of flag error messages. Fixing test that wasn't passing.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by spf13 · committed by spf13 · parent 62fb674 · Browse files · 2 files+14 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+14 added−10 removed

  • other:+14 added−10 removed

2 files+14 added−10 removed

modified:cobra_test.go+9 added-6 removed
213 unmodified lines
214 t.Errorf("unset flag should have default value, expecting 123, given %d", flagi1)215 }216217 // Testing with flag only existing on child218 buf2 := new(bytes.Buffer)219 c = initialize()220 c.SetOutput(buf2)221 cmdEcho.AddCommand(cmdTimes)222 c.AddCommand(cmdPrint, cmdEcho)223 c.SetArgs(strings.Split("echo -j 99 -i77 one two", " "))224 err := c.Execute()223 _ = err224 //c.DebugFlags()225226 // TODO figure out why this isn't passing227 //if err == nil {228 //t.Errorf("invalid flag should generate error")229 //}226 if err == nil {227 t.Errorf("invalid flag should generate error")228 }229230 if !strings.Contains(buf2.String(), "intone=123") {231 t.Errorf("Wrong error message displayed, \n %s", buf.String())232 }233234}235236func TestPersistentFlags(t *testing.T) {237 c := initialize()
33 unmodified lines
modified:command.go+5 added-4 removed
334 unmodified lines
335 err = c.Flags().Parse(args)336 if err != nil {337 return err338 }339 if c.flagErrorBuf != nil {340 //fmt.Println(c.flagErrorBuf.String())341 return nil342 //return fmt.Errorf("%s", c.flagErrorBuf.String())339 // The upstream library adds spaces to the error340 // response regardless of success.341 // Handling it here until fixing upstream342 if len(strings.TrimSpace(c.flagErrorBuf.String())) > 1 {343 return fmt.Errorf("%s", c.flagErrorBuf.String())344 }345 return nil346}347
22 unmodified lines