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

Function TestCopyGuardedContent

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

Source from the content-addressed store, hash-verified

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

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