@@ -176,50 +176,112 @@ test "run: with env_map" {
176176// var proc = try cmdtest.spawn(.{ .argv = argv });
177177// defer proc.deinit();
178178
179- // try proc.writeToStdin ("PING\n"); // I expect this to be fail as process exit with 42 as code
179+ // try proc.write ("PING\n"); // I expect this to be fail as process exit with 42 as code
180180// try proc.expectStdout("TEST");
181181
182- // try proc.writeToStdin ("ECHO works\n");
182+ // try proc.write ("ECHO works\n");
183183// try testing.expectEqualStrings("works", try proc.readLineFromStdout());
184184
185- // try proc.writeToStdin ("EXIT\n");
185+ // try proc.write ("EXIT\n");
186186
187187// const term = try proc.child.wait();
188188// try testing.expectEqual(@as(u8, 0), term.Exited);
189189// }
190190
191- test "writeToStdin : running process that accepts stdin" {
191+ test "write : running process that accepts stdin" {
192192 const argv = &[_ ][]const u8 {"cat" };
193193 var proc = try cmdtest .spawn (.{ .argv = argv });
194194 defer proc .deinit ();
195195
196196 // This write should succeed without error.
197- try proc .writeToStdin ("data\n " );
197+ try proc .write ("data\n " );
198198 try proc .expectStdout ("data" );
199199}
200200
201- test "writeToStdin : process confirmed exited" {
201+ test "write : process confirmed exited" {
202202 const argv = &[_ ][]const u8 { "echo" , "42" };
203203 var proc = try cmdtest .spawn (.{ .argv = argv });
204204 defer proc .deinit ();
205205 _ = try proc .child .wait ();
206- try testing .expectError (error .ProcessExited , proc .writeToStdin ("data\n " ));
206+ try testing .expectError (error .ProcessExited , proc .write ("data\n " ));
207207}
208208
209- test "writeToStdin : running process that ignores stdin" {
209+ test "write : running process that ignores stdin" {
210210 const argv = &[_ ][]const u8 { "sleep" , "1" };
211211 var proc = try cmdtest .spawn (.{ .argv = argv });
212212 defer proc .deinit ();
213- try proc .writeToStdin ("this is ignored\n " );
213+ try proc .write ("this is ignored\n " );
214214}
215215
216- test "writeToStdin : process died unexpectedly" {
216+ test "write : process died unexpectedly" {
217217 // NOTE: skipped for now, this race condition is known issue
218218 if (builtin .is_test ) return error .SkipZigTest ;
219219
220220 const argv = &[_ ][]const u8 {"ls" };
221221 var proc = try cmdtest .spawn (.{ .argv = argv });
222222 defer proc .deinit ();
223- try testing .expectError (error .ProcessExited , proc .writeToStdin ("data\n " ));
224- // TODO: handle this
223+ try testing .expectError (error .ProcessExited , proc .write ("data\n " ));
225224}
225+
226+ test "readLineFromStdout: happy path" {
227+ const argv = &[_ ][]const u8 { "cmdtest" , "--interactive" };
228+ var proc = try cmdtest .spawn (.{ .argv = argv });
229+ defer proc .deinit ();
230+
231+ try proc .write ("PING\n " );
232+ try testing .expectEqualStrings ("PONG" , try proc .readLineFromStdout ());
233+ }
234+
235+ test "readLineFromStdout: multiple lines" {
236+ const argv = &[_ ][]const u8 { "cmdtest" , "--interactive" };
237+ var proc = try cmdtest .spawn (.{ .argv = argv });
238+ defer proc .deinit ();
239+
240+ try proc .write ("ECHO line one\n " );
241+ try testing .expectEqualStrings ("line one" , try proc .readLineFromStdout ());
242+
243+ try proc .write ("ECHO line two\n " );
244+ try testing .expectEqualStrings ("line two" , try proc .readLineFromStdout ());
245+ }
246+
247+ test "readLineFromStdout: handles CRLF endings" {
248+ const argv = &[_ ][]const u8 { "cmdtest" , "--interactive" };
249+ var proc = try cmdtest .spawn (.{ .argv = argv });
250+ defer proc .deinit ();
251+
252+ try proc .write ("ECHO line with cr\r \n " );
253+ try testing .expectEqualStrings ("line with cr" , try proc .readLineFromStdout ());
254+ }
255+
256+ test "readLineFromStdout: empty line" {
257+ const argv = &[_ ][]const u8 { "cmdtest" , "--interactive" };
258+ var proc = try cmdtest .spawn (.{ .argv = argv });
259+ defer proc .deinit ();
260+
261+ try proc .write ("ECHO \n " );
262+ try testing .expectEqualStrings ("" , try proc .readLineFromStdout ());
263+ }
264+
265+ test "readLineFromStdout: process exited before read" {
266+ const argv = &[_ ][]const u8 { "echo" , "ok" };
267+ var proc = try cmdtest .spawn (.{ .argv = argv });
268+ defer proc .deinit ();
269+
270+ // Wait for the process to fully exit. Its stdout pipe will be closed.
271+ _ = try proc .child .wait ();
272+
273+ // Attempting to read from a closed pipe should fail
274+ try testing .expectError (error .ProcessExited , proc .readLineFromStdout ());
275+ }
276+
277+ // test "readLineFromStdout: child writes no newline" {
278+ // // `printf` is a good tool for writing output without a trailing newline
279+ // const argv = &[_][]const u8{ "printf", "no-newline" };
280+ // var proc = try cmdtest.spawn(.{ .argv = argv });
281+ // defer proc.deinit();
282+
283+ // // The read will consume "no-newline", hit EOF, and since it never found a
284+ // // '\n' delimiter, it will return an error (EndOfStream), which our
285+ // // function maps to ReadFailed.
286+ // try testing.expectError(error.ReadFailed, proc.readLineFromStdout());
287+ // }
0 commit comments