On Thu, Mar 31, 2005 at 04:46:22PM +0000, Mark Stosberg wrote: > We used to have tests that looked like this: > > like(`echo y | darcs command`,qr/$re/); > > That would run the command and answer "y" to the first and only question > it asked. It worked well enough, but I looked for for a pure Perl > solution in the spirit of "being more portable". > > I came up with this: > > { > open2(*READ, *WRITE, "$DARCS unpull -p add"); > print WRITE "a\n"; > like( (<READ>)[4], qr/really unpull/i, "additional confirmation is given > when 'all' option is selected"); > close(WRITE); > close(READ); > # (We never confirmed, so those patches are still there ) > } > > This is more time consuming to write, because not only is more verbose, but I > know exactly how many lines to read on STDERR. Already for two people > something > got slightly off, causing the test to hang indefinitely.
*snip* > I'm I missing an easier and less fragile way to test interactive > commands with Perl? When using open2 you have to be careful to close WRITE before you READ so the program does not hang waiting for more input. Once you've fixed that the technique above should be just fine. sub echo { my($input, $command) = @_; local(*READ, *WRITE); open2(*READ, *WRITE, "$DARCS $command"; print WRITE "a\n"; close WRITE; my $output = join '', <READ>; close READ; return $output; } echo("y", "command"); Or if the internals worry you you can at least consolodate all those shell calls into a function so it can be handily dealt with in one place later. sub echo { my($input, $command) = @_; return `echo "y" | $DARCS $command`; } Otherwise I don't know of anything to do this easily. There's Test::Cmd but its... kind of wierd.