Skip to content

Commit

Permalink
Implement run-with-tty test routine
Browse files Browse the repository at this point in the history
Similar to `is-run` except it does magiks to make standard handles
be TTY so you could do TTY-sensitive tests.
  • Loading branch information
zoffixznet committed Oct 24, 2017
1 parent 3802dde commit bcf0f93
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions packages/Test/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,34 @@ sub fails-like (&test, $ex-type, $reason?, *%matcher) is export {
}
}

sub run-with-tty (
$code, $desc, :$in = '', :$status = 0, :$out = '', :$err = ''
) is export {
state $path = make-temp-file.absolute;
# on MacOS, `script` doesn't take the command via `c` arg
state $script = shell(:!out, :!err, 'script -qc ""')
?? script -qc '"$*EXECUTABLE" "$path"'
!! shell(:!out, :!err, script -q /dev/null "$*EXECUTABLE" -e "")
?? script -q /dev/null "$*EXECUTABLE" "$path"
!! do { skip "need `script` command to run test: $desc"; return }

subtest $desc => {
$path.IO.spurt: $code;
if shell :in, :out, :err, $script -> $_ {
plan 3;
# on MacOS, `script` really wants the ending newline...
.in.spurt: "$in\n", :close;
cmp-ok .out.slurp(:close), '~~', $out, 'STDOUT';
cmp-ok .err.slurp(:close), '~~', $err, 'STDERR';
cmp-ok .exitcode, '~~', $status, 'exit code';
}
else {
plan 1;
flunk "Failed to run command; exitcode: $^proc.exitcode()";
}
}
}

=begin pod
=head1 NAME
Expand Down Expand Up @@ -500,6 +528,21 @@ Executes C<&test> and uses C<Test.pm>'s C<isa-ok> to check the return value is a
C<Failure>, then uses C<Test.pm>'s <throws-like> to check that C<Failure>
throws the correct exception when sunk.
=head2 run-with-tty
sub run-with-tty (
$code, $desc, :$in = '', :$status = 0, :$out = '', :$err = ''
)
Puts C<$code> into a file, and runs it with C<$*EXECUTABLE> using C<`script`>
command line utility, if available, or skips the tests if not. Appends C<\n> to
C<$in> (MacOS's C<`script`> seems to require it), and sends it to the program.
Then performs three smartmatch tests against C<$status> (exitcode), C<$out>
(slurped STDOUT content) and C<$err> (slurped STDERR content).
At the time of this writing, on MacOS's STDOUT seems to be prefixed with
STDIN and C<^D\b\b> chars after it when running Rakudo compiler.
=end pod

# vim: ft=perl6

0 comments on commit bcf0f93

Please sign in to comment.