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,15 @@ 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+ 
77+ 		cmd .WithPseudoTTY ()
78+ 		cmd .Feed (strings .NewReader ("echo mark${NON}mark\n " ))
79+ 		cmd .WithFeeder (func () io.Reader  {
7980			// Interestingly, and unlike with run, on attach, docker (like nerdctl) ALSO needs a pause so that the 
8081			// container can read stdin before we detach 
8182			time .Sleep (time .Second )
8283			// 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 
84+ 			return  bytes .NewReader ([]byte {16 , 17 })
8685		})
8786
8887		return  cmd 
@@ -120,10 +119,8 @@ func TestAttachDetachKeys(t *testing.T) {
120119
121120	testCase .Setup  =  func (data  test.Data , helpers  test.Helpers ) {
122121		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- 		})
122+ 		cmd .WithPseudoTTY ()
123+ 		cmd .Feed (bytes .NewReader ([]byte {17 }))
127124
128125		cmd .Run (& test.Expected {
129126			ExitCode : 0 ,
@@ -137,15 +134,14 @@ func TestAttachDetachKeys(t *testing.T) {
137134	testCase .Command  =  func (data  test.Data , helpers  test.Helpers ) test.TestableCommand  {
138135		// Run interactively and detach 
139136		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 " )
137+ 		cmd .WithPseudoTTY ()
138+ 		cmd .Feed (strings .NewReader ("echo mark${NON}mark\n " ))
139+ 		cmd .WithFeeder (func () io.Reader  {
142140			// Interestingly, and unlike with run, on attach, docker (like nerdctl) ALSO needs a pause so that the 
143141			// container can read stdin before we detach 
144142			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 
143+ 			// ctrl+p and ctrl+q (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes) 
144+ 			return  bytes .NewReader ([]byte {1 , 2 })
149145		})
150146
151147		return  cmd 
@@ -179,11 +175,9 @@ func TestAttachForAutoRemovedContainer(t *testing.T) {
179175
180176	testCase .Setup  =  func (data  test.Data , helpers  test.Helpers ) {
181177		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- 		})
178+ 		cmd .WithPseudoTTY ()
179+ 		// ctrl+a and ctrl+b (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes) 
180+ 		cmd .Feed (bytes .NewReader ([]byte {1 , 2 }))
187181
188182		cmd .Run (& test.Expected {
189183			ExitCode : 0 ,
@@ -197,10 +191,8 @@ func TestAttachForAutoRemovedContainer(t *testing.T) {
197191	testCase .Command  =  func (data  test.Data , helpers  test.Helpers ) test.TestableCommand  {
198192		// Run interactively and detach 
199193		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- 		})
194+ 		cmd .WithPseudoTTY ()
195+ 		cmd .Feed (strings .NewReader ("echo mark${NON}mark\n exit 42\n " ))
204196
205197		return  cmd 
206198	}
0 commit comments