MCPcopy Create free account
hub / github.com/cli/cli / TestCopyGuardedContent

Function TestCopyGuardedContent

pkg/iostreams/content_test.go:58–159  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

56}
57
58func TestCopyGuardedContent(t *testing.T) {
59 png := append([]byte("\x89PNG\r\n\x1a\n"), make([]byte, 16)...)
60 readErr := errors.New("boom")
61
62 tests := []struct {
63 name string
64 content []byte
65 // reader overrides content for cases a byte slice cannot express, such
66 // as a mid-stream read failure.
67 reader io.Reader
68 isTTY bool
69 wantOut []byte
70 // wantErrIs matches a sentinel error with errors.Is; wantErrAs matches a
71 // typed error (e.g. BinaryTerminalError, which carries a MIME field) with
72 // errors.As, so its value must be a pointer to that error type.
73 wantErrIs error
74 wantErrAs any
75 }{
76 {
77 name: "clean text is written",
78 content: []byte("hello world\n"),
79 isTTY: true,
80 wantOut: []byte("hello world\n"),
81 },
82 {
83 name: "text with escape is refused",
84 content: []byte("danger\x1b[31mtext"),
85 isTTY: true,
86 wantErrIs: ErrEscapeSequence,
87 },
88 {
89 name: "text with escape is refused when piped",
90 content: []byte("danger\x1b[31mtext"),
91 isTTY: false,
92 wantErrIs: ErrEscapeSequence,
93 },
94 {
95 name: "text with C1 control is refused",
96 content: []byte("danger\u009b31mtext"),
97 isTTY: true,
98 wantErrIs: ErrEscapeSequence,
99 },
100 {
101 name: "binary to terminal is refused",
102 content: png,
103 isTTY: true,
104 wantErrAs: &BinaryTerminalError{},
105 },
106 {
107 name: "binary when piped is written raw",
108 content: png,
109 isTTY: false,
110 wantOut: png,
111 },
112 {
113 name: "empty content writes nothing",
114 content: []byte{},
115 isTTY: true,

Callers

nothing calls this directly

Calls 4

CopyGuardedContentFunction · 0.85
EmptyMethod · 0.80
EqualMethod · 0.80
RunMethod · 0.65

Tested by

no test coverage detected