MCPcopy
hub / github.com/spf13/cobra / TestCommandPrintRedirection

Function TestCommandPrintRedirection

command_test.go:2177–2216  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2175}
2176
2177func TestCommandPrintRedirection(t *testing.T) {
2178 errBuff, outBuff := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
2179 root := &Command{
2180 Run: func(cmd *Command, args []string) {
2181
2182 cmd.PrintErr("PrintErr")
2183 cmd.PrintErrln("PrintErr", "line")
2184 cmd.PrintErrf("PrintEr%s", "r")
2185
2186 cmd.Print("Print")
2187 cmd.Println("Print", "line")
2188 cmd.Printf("Prin%s", "t")
2189 },
2190 }
2191
2192 root.SetErr(errBuff)
2193 root.SetOut(outBuff)
2194
2195 if err := root.Execute(); err != nil {
2196 t.Error(err)
2197 }
2198
2199 gotErrBytes, err := io.ReadAll(errBuff)
2200 if err != nil {
2201 t.Error(err)
2202 }
2203
2204 gotOutBytes, err := io.ReadAll(outBuff)
2205 if err != nil {
2206 t.Error(err)
2207 }
2208
2209 if wantErr := []byte("PrintErrPrintErr line\nPrintErr"); !bytes.Equal(gotErrBytes, wantErr) {
2210 t.Errorf("got: '%s' want: '%s'", gotErrBytes, wantErr)
2211 }
2212
2213 if wantOut := []byte("PrintPrint line\nPrint"); !bytes.Equal(gotOutBytes, wantOut) {
2214 t.Errorf("got: '%s' want: '%s'", gotOutBytes, wantOut)
2215 }
2216}
2217
2218func TestFlagErrorFunc(t *testing.T) {
2219 c := &Command{Use: "c", Run: emptyRun}

Callers

nothing calls this directly

Calls 10

SetErrMethod · 0.95
SetOutMethod · 0.95
ExecuteMethod · 0.95
PrintErrMethod · 0.80
PrintErrlnMethod · 0.80
PrintErrfMethod · 0.80
PrintMethod · 0.80
PrintlnMethod · 0.80
PrintfMethod · 0.80
ErrorMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…