1717package container
1818
1919import (
20+ "bytes"
2021 "errors"
21- "os "
22+ "io "
2223 "strings"
2324 "testing"
2425 "time"
@@ -56,11 +57,9 @@ func TestAttach(t *testing.T) {
5657
5758 testCase .Setup = func (data test.Data , helpers test.Helpers ) {
5859 cmd := helpers .Command ("run" , "--rm" , "-it" , "--name" , data .Identifier (), testutil .CommonImage )
59- cmd .WithPseudoTTY (func (f * os.File ) error {
60- // ctrl+p and ctrl+q (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
61- _ , err := f .Write ([]byte {16 , 17 })
62- return err
63- })
60+ cmd .WithPseudoTTY ()
61+ // ctrl+p and ctrl+q (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
62+ cmd .Feed (bytes .NewReader ([]byte {16 , 17 }))
6463
6564 cmd .Run (& test.Expected {
6665 ExitCode : 0 ,
@@ -74,15 +73,13 @@ func TestAttach(t *testing.T) {
7473 testCase .Command = func (data test.Data , helpers test.Helpers ) test.TestableCommand {
7574 // Run interactively and detach
7675 cmd := helpers .Command ("attach" , data .Identifier ())
77- cmd .WithPseudoTTY ( func ( f * os. File ) error {
78- _ , _ = f . WriteString ( "echo mark${NON}mark \n " )
76+ cmd .Feed ( strings . NewReader ( "echo mark${NON}mark \n " ))
77+ cmd . WithFeeder ( func () io. Reader {
7978 // Interestingly, and unlike with run, on attach, docker (like nerdctl) ALSO needs a pause so that the
8079 // container can read stdin before we detach
8180 time .Sleep (time .Second )
8281 // ctrl+p and ctrl+q (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
83- _ , err := f .Write ([]byte {16 , 17 })
84-
85- return err
82+ return bytes .NewReader ([]byte {16 , 17 })
8683 })
8784
8885 return cmd
@@ -120,10 +117,8 @@ func TestAttachDetachKeys(t *testing.T) {
120117
121118 testCase .Setup = func (data test.Data , helpers test.Helpers ) {
122119 cmd := helpers .Command ("run" , "--rm" , "-it" , "--detach-keys=ctrl-q" , "--name" , data .Identifier (), testutil .CommonImage )
123- cmd .WithPseudoTTY (func (f * os.File ) error {
124- _ , err := f .Write ([]byte {17 })
125- return err
126- })
120+ cmd .WithPseudoTTY ()
121+ cmd .Feed (bytes .NewReader ([]byte {17 }))
127122
128123 cmd .Run (& test.Expected {
129124 ExitCode : 0 ,
@@ -137,15 +132,14 @@ func TestAttachDetachKeys(t *testing.T) {
137132 testCase .Command = func (data test.Data , helpers test.Helpers ) test.TestableCommand {
138133 // Run interactively and detach
139134 cmd := helpers .Command ("attach" , "--detach-keys=ctrl-a,ctrl-b" , data .Identifier ())
140- cmd .WithPseudoTTY (func (f * os.File ) error {
141- _ , _ = f .WriteString ("echo mark${NON}mark\n " )
135+ cmd .WithPseudoTTY ()
136+ cmd .Feed (strings .NewReader ("echo mark${NON}mark\n " ))
137+ cmd .WithFeeder (func () io.Reader {
142138 // Interestingly, and unlike with run, on attach, docker (like nerdctl) ALSO needs a pause so that the
143139 // container can read stdin before we detach
144140 time .Sleep (time .Second )
145- // ctrl+a and ctrl+b (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
146- _ , err := f .Write ([]byte {1 , 2 })
147-
148- return err
141+ // ctrl+p and ctrl+q (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
142+ return bytes .NewReader ([]byte {1 , 2 })
149143 })
150144
151145 return cmd
@@ -179,11 +173,9 @@ func TestAttachForAutoRemovedContainer(t *testing.T) {
179173
180174 testCase .Setup = func (data test.Data , helpers test.Helpers ) {
181175 cmd := helpers .Command ("run" , "--rm" , "-it" , "--detach-keys=ctrl-a,ctrl-b" , "--name" , data .Identifier (), testutil .CommonImage )
182- cmd .WithPseudoTTY (func (f * os.File ) error {
183- // ctrl+a and ctrl+b (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
184- _ , err := f .Write ([]byte {1 , 2 })
185- return err
186- })
176+ cmd .WithPseudoTTY ()
177+ // ctrl+a and ctrl+b (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
178+ cmd .Feed (bytes .NewReader ([]byte {1 , 2 }))
187179
188180 cmd .Run (& test.Expected {
189181 ExitCode : 0 ,
@@ -197,10 +189,8 @@ func TestAttachForAutoRemovedContainer(t *testing.T) {
197189 testCase .Command = func (data test.Data , helpers test.Helpers ) test.TestableCommand {
198190 // Run interactively and detach
199191 cmd := helpers .Command ("attach" , data .Identifier ())
200- cmd .WithPseudoTTY (func (f * os.File ) error {
201- _ , err := f .WriteString ("echo mark${NON}mark\n exit 42\n " )
202- return err
203- })
192+ cmd .WithPseudoTTY ()
193+ cmd .Feed (strings .NewReader ("echo mark${NON}mark\n exit 42\n " ))
204194
205195 return cmd
206196 }
0 commit comments