From f37b732fa2a9f4c4e31c602fb3309bc882400ab9 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 5 Mar 2012 00:36:07 -0600 Subject: cleanups, dzil stuff, etc --- .gitignore | 17 ++++++++++++ Changes | 18 ++++++------ dist.ini | 10 +++---- lib/IO/Pty/Easy.pm | 70 +++++++++++++++++------------------------------ t/000-load.t | 7 ----- t/001-open-close.t | 9 ------ t/002-spawn.t | 16 ----------- t/003-subprocess.t | 21 -------------- t/004-undefined-program.t | 15 ---------- t/010-read-write.t | 48 -------------------------------- t/100-system.t | 28 ------------------- t/open-close.t | 11 ++++++++ t/read-write.t | 50 +++++++++++++++++++++++++++++++++ t/spawn.t | 18 ++++++++++++ t/subprocess.t | 23 ++++++++++++++++ t/system.t | 30 ++++++++++++++++++++ t/undefined-program.t | 17 ++++++++++++ 17 files changed, 205 insertions(+), 203 deletions(-) create mode 100644 .gitignore delete mode 100644 t/000-load.t delete mode 100644 t/001-open-close.t delete mode 100644 t/002-spawn.t delete mode 100644 t/003-subprocess.t delete mode 100644 t/004-undefined-program.t delete mode 100644 t/010-read-write.t delete mode 100644 t/100-system.t create mode 100644 t/open-close.t create mode 100644 t/read-write.t create mode 100644 t/spawn.t create mode 100644 t/subprocess.t create mode 100644 t/system.t create mode 100644 t/undefined-program.t diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc6fde8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +cover_db +META.* +MYMETA.* +Makefile +blib +inc +pm_to_blib +MANIFEST +Makefile.old +nytprof.out +MANIFEST.bak +*.sw[po] +.DS_Store +.build +IO-Pty-Easy-* +*.bs +*.o diff --git a/Changes b/Changes index 711c55f..95f676f 100644 --- a/Changes +++ b/Changes @@ -1,10 +1,12 @@ Revision history for IO-Pty-Easy -0.08 10/10/2009 +{{$NEXT}} + +0.08 2009-10-10 - Fixed some circular references, should fix global destruction issues (reported by kbrint, RT#50373) -0.07 07/07/2009 +0.07 2009-07-07 - Add a constructor option 'raw' (default true) to configure whether the pty should be set to raw mode on spawn. - Don't ever automatically set the master side of the pty to raw, since @@ -12,7 +14,7 @@ Revision history for IO-Pty-Easy anything?) - Build system changed to Dist::Zilla -0.06 07/06/2009 +0.06 2009-07-06 - Localize $@ and $? in the destructor - Convert the module to use the actual pty object as the class instance, rather than hiding it away in the hashref - now things like fileno($pty) @@ -20,26 +22,26 @@ Revision history for IO-Pty-Easy you have been digging around in the hash prior to this. - Add a few accessors for the object state -0.05 02/04/2009 +0.05 2009-02-04 - Fix read() returning undef on timeout -0.04 02/03/2009 +0.04 2009-02-03 - Don't mess with SIGCHLD, it breaks system() and `` - Don't die if $pty->close is called multiple times (and close on DESTROY) - Don't mess with SIGWINCH if we weren't the ones that set it up - Sleep while waiting for a process to die, rather than spinning - FreeBSD should pass all tests now (sorear) -0.03 08/20/2007 +0.03 2007-08-20 - Make sure stdin/out are connected to a tty before trying to clone winsize from them - Fix the subprocess test so that it waits for the read before the subprocess dies -0.02 08/17/2007 +0.02 2007-08-17 - Made calls which could possibly terminate the subprocess blocking by default - A few other minor bug fixes, doc fixes, and general cleanups -0.01 08/17/2007 +0.01 2007-08-17 - Initial release diff --git a/dist.ini b/dist.ini index 42610d5..1bdd191 100644 --- a/dist.ini +++ b/dist.ini @@ -1,12 +1,10 @@ name = IO-Pty-Easy -version = 0.08 author = Jesse Luehrs license = Perl_5 copyright_holder = Jesse Luehrs -abstract = Easy interface to IO::Pty -[@Classic] +[@DOY] +dist = IO-Pty-Easy +repository = github -[Prereq] -IO::Pty = 0 -Scalar::Util = 0 +[AutoPrereqs] diff --git a/lib/IO/Pty/Easy.pm b/lib/IO/Pty/Easy.pm index 904fbd5..9439332 100644 --- a/lib/IO/Pty/Easy.pm +++ b/lib/IO/Pty/Easy.pm @@ -1,14 +1,13 @@ package IO::Pty::Easy; use warnings; use strict; -use base 'IO::Pty'; +# ABSTRACT: Easy interface to IO::Pty + use Carp; use POSIX (); use Scalar::Util qw(weaken); -=head1 NAME - -IO::Pty::Easy - Easy interface to IO::Pty +use base 'IO::Pty'; =head1 SYNOPSIS @@ -41,11 +40,7 @@ portability restrictions from that module. =cut -=head1 CONSTRUCTOR - -=cut - -=head2 new() +=method new(%params) The C constructor initializes the pty and returns a new C object. The constructor recognizes these parameters: @@ -97,11 +92,7 @@ sub new { return $self; } -=head1 METHODS - -=cut - -=head2 spawn() +=method spawn(@argv) Fork a new subprocess, with stdin/stdout/stderr tied to the pty. @@ -185,7 +176,7 @@ sub spawn { } } -=head2 read() +=method read($timeout, $length) Read data from the process running on the pty. @@ -223,7 +214,7 @@ sub read { return $buf; } -=head2 write() +=method write($buf, $timeout) Writes a string to the pty. @@ -251,7 +242,7 @@ sub write { return $nchars; } -=head2 is_active() +=method is_active Returns whether or not a subprocess is currently running on the pty. @@ -289,7 +280,7 @@ sub is_active { return $active; } -=head2 kill() +=method kill($sig, $non_blocking) Sends a signal to the process currently running on the pty (if any). Optionally blocks until the process dies. @@ -315,7 +306,7 @@ sub kill { return $kills; } -=head2 close() +=method close Kills any subprocesses and closes the pty. No other operations are valid after this call. @@ -329,10 +320,10 @@ sub close { $self->kill; } -=head2 handle_pty_size() +=method handle_pty_size Read/write accessor for the C option documented in -L. +L. =cut @@ -342,10 +333,10 @@ sub handle_pty_size { ${*{$self}}{io_pty_easy_handle_pty_size}; } -=head2 def_max_read_chars() +=method def_max_read_chars Read/write accessor for the C option documented in -L. +L. =cut @@ -355,7 +346,7 @@ sub def_max_read_chars { ${*{$self}}{io_pty_easy_def_max_read_chars}; } -=head2 pid() +=method pid Returns the pid of the process currently running in the pty, or undef if no process is running. @@ -380,20 +371,6 @@ sub DESTROY { $self->close; } -=head1 SEE ALSO - -L - -L - -L - -=head1 AUTHOR - -Jesse Luehrs, C<< >> - -This module is based heavily on the F script bundled with L. - =head1 BUGS No known bugs. @@ -402,6 +379,16 @@ Please report any bugs through RT: email C, or browse to L. +=head1 SEE ALSO + +L + +(This module is based heavily on the F script bundled with L.) + +L + +L + =head1 SUPPORT You can find this documentation for this module with the perldoc command. @@ -430,13 +417,6 @@ L =back -=head1 COPYRIGHT AND LICENSE - -Copyright 2007-2009 Jesse Luehrs. - -This program is free software; you can redistribute it and/or modify it -under the same terms as Perl itself. - =cut 1; diff --git a/t/000-load.t b/t/000-load.t deleted file mode 100644 index 982a1ef..0000000 --- a/t/000-load.t +++ /dev/null @@ -1,7 +0,0 @@ -#!perl -use strict; -use warnings; -use Test::More tests => 1; - -use_ok 'IO::Pty::Easy'; - diff --git a/t/001-open-close.t b/t/001-open-close.t deleted file mode 100644 index 1c9eed1..0000000 --- a/t/001-open-close.t +++ /dev/null @@ -1,9 +0,0 @@ -#!perl -use strict; -use warnings; -use Test::More tests => 1; -use IO::Pty::Easy; - -my $pty = IO::Pty::Easy->new; -$pty->close; -ok(!$pty->opened, "closing a pty before a spawn"); diff --git a/t/002-spawn.t b/t/002-spawn.t deleted file mode 100644 index 620a57a..0000000 --- a/t/002-spawn.t +++ /dev/null @@ -1,16 +0,0 @@ -#!perl -use strict; -use warnings; -use Test::More tests => 5; -use IO::Pty::Easy; - -my $pty = IO::Pty::Easy->new; -$pty->spawn("$^X -ple ''"); -ok($pty->is_active, "spawning a subprocess"); -ok($pty->kill(0, 1), "subprocess actually exists"); -$pty->kill; -ok(!$pty->is_active, "killing a subprocess"); -$pty->spawn("$^X -ple ''"); -$pty->close; -ok(!$pty->is_active, "auto-killing a pty with close()"); -ok(!$pty->opened, "closing a pty after a spawn"); diff --git a/t/003-subprocess.t b/t/003-subprocess.t deleted file mode 100644 index cc87025..0000000 --- a/t/003-subprocess.t +++ /dev/null @@ -1,21 +0,0 @@ -#!perl -use strict; -use warnings; -use Test::More tests => 2; -use IO::Pty::Easy; - -my $pty = IO::Pty::Easy->new; -my $script = << 'EOF'; -$| = 1; -if (-t *STDIN && -t *STDOUT) { print "ok" } -else { print "failed" } -EOF - -my $outside_of_pty = `$^X -e '$script'`; -unlike($outside_of_pty, qr/ok/, "running outside of pty fails -t checks"); - -# we need to keep the script alive until we can read the output from it -$script .= "sleep 1 while 1;"; -$pty->spawn("$^X -e '$script'"); -like($pty->read, qr/ok/, "runs subprocess in a pty"); -$pty->close; diff --git a/t/004-undefined-program.t b/t/004-undefined-program.t deleted file mode 100644 index 335a415..0000000 --- a/t/004-undefined-program.t +++ /dev/null @@ -1,15 +0,0 @@ -#!perl -use strict; -use warnings; -use Test::More tests => 2; -use IO::Pty::Easy; - -my $pty = IO::Pty::Easy->new; -eval { - local $SIG{ALRM} = sub { die "alarm\n" }; - alarm 5; - $pty->spawn("missing_program_io_pty_easy"); - alarm 0; -}; -like($@, qr/Cannot exec\(missing_program_io_pty_easy\)/); -ok(!$pty->is_active, "pty isn't active if program doesn't exist"); diff --git a/t/010-read-write.t b/t/010-read-write.t deleted file mode 100644 index 80f5f1e..0000000 --- a/t/010-read-write.t +++ /dev/null @@ -1,48 +0,0 @@ -#!perl -use strict; -use warnings; -use Test::More tests => 5; -use IO::Pty::Easy; - -my $pty = IO::Pty::Easy->new; - -$pty->spawn("$^X -ple ''"); -$pty->write("testing\n"); -like($pty->read, qr/testing/, "basic read/write testing"); -is($pty->read(0.1), undef, "read returns undef on timeout"); -$pty->kill; - -$pty->spawn("$^X -e 'sleep(1) while 1'"); -eval { - local $SIG{ALRM} = sub { - is($pty->write("should fail", 0.1), undef, - "write returns undef on timeout"); - $SIG{ALRM} = 'DEFAULT'; - alarm 1; - }; - alarm 1; - $pty->write('a'x(1024*1024)); -}; -$pty->kill; -$pty->close; - -# create an entirely new pty to clear the input buffer -$pty = IO::Pty::Easy->new; -$pty->spawn("$^X -e 'sleep(1) while 1'"); -my $result = "wrong"; -$result = eval { - local $SIG{ALRM} = sub { die "alarm\n" }; - alarm 2; - my $write_result = $pty->write('a'x(1024*1024), 0.1); - defined($write_result) ? "wrong" : "right"; -}; -TODO: { - local $TODO = "need to figure this one out"; - is($result, "right", "write times out properly even on the first call"); - isnt($@, "alarm\n", "write times out properly even on the first call"); -} - -# if the perl script ends with a subprocess still running, the test will exit -# with the exit status of the signal that the subprocess dies with, so we have -# to kill the subprocess before exiting. -$pty->close; diff --git a/t/100-system.t b/t/100-system.t deleted file mode 100644 index 0be6006..0000000 --- a/t/100-system.t +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env perl -use strict; -use warnings; -use Test::More tests => 4; -use IO::Pty::Easy; - -my $pty = IO::Pty::Easy->new; -$pty->spawn("$^X -ple ''"); -my $output; -eval { - local $SIG{ALRM} = sub { die "alarm\n" }; - alarm 5; - $output = `$^X -e 'print "foo"'`; - alarm 0; -}; -isnt($@, "alarm\n", "system() didn't time out"); -is($output, "foo", "system() got the right value"); -$pty->kill; -undef $output; -eval { - local $SIG{ALRM} = sub { die "alarm2\n" }; - alarm 5; - $output = `$^X -e 'print "bar"'`; - alarm 0; -}; -isnt($@, "alarm2\n", "system() didn't time out (after kill)"); -is($output, "bar", "system() got the right value (after kill)"); -$pty->close; diff --git a/t/open-close.t b/t/open-close.t new file mode 100644 index 0000000..6140821 --- /dev/null +++ b/t/open-close.t @@ -0,0 +1,11 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; +use IO::Pty::Easy; + +my $pty = IO::Pty::Easy->new; +$pty->close; +ok(!$pty->opened, "closing a pty before a spawn"); + +done_testing; diff --git a/t/read-write.t b/t/read-write.t new file mode 100644 index 0000000..21fe729 --- /dev/null +++ b/t/read-write.t @@ -0,0 +1,50 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; +use IO::Pty::Easy; + +my $pty = IO::Pty::Easy->new; + +$pty->spawn("$^X -ple ''"); +$pty->write("testing\n"); +like($pty->read, qr/testing/, "basic read/write testing"); +is($pty->read(0.1), undef, "read returns undef on timeout"); +$pty->kill; + +$pty->spawn("$^X -e 'sleep(1) while 1'"); +eval { + local $SIG{ALRM} = sub { + is($pty->write("should fail", 0.1), undef, + "write returns undef on timeout"); + $SIG{ALRM} = 'DEFAULT'; + alarm 1; + }; + alarm 1; + $pty->write('a'x(1024*1024)); +}; +$pty->kill; +$pty->close; + +# create an entirely new pty to clear the input buffer +$pty = IO::Pty::Easy->new; +$pty->spawn("$^X -e 'sleep(1) while 1'"); +my $result = "wrong"; +$result = eval { + local $SIG{ALRM} = sub { die "alarm\n" }; + alarm 2; + my $write_result = $pty->write('a'x(1024*1024), 0.1); + defined($write_result) ? "wrong" : "right"; +}; +TODO: { + local $TODO = "need to figure this one out"; + is($result, "right", "write times out properly even on the first call"); + isnt($@, "alarm\n", "write times out properly even on the first call"); +} + +# if the perl script ends with a subprocess still running, the test will exit +# with the exit status of the signal that the subprocess dies with, so we have +# to kill the subprocess before exiting. +$pty->close; + +done_testing; diff --git a/t/spawn.t b/t/spawn.t new file mode 100644 index 0000000..9bdc498 --- /dev/null +++ b/t/spawn.t @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; +use IO::Pty::Easy; + +my $pty = IO::Pty::Easy->new; +$pty->spawn("$^X -ple ''"); +ok($pty->is_active, "spawning a subprocess"); +ok($pty->kill(0, 1), "subprocess actually exists"); +$pty->kill; +ok(!$pty->is_active, "killing a subprocess"); +$pty->spawn("$^X -ple ''"); +$pty->close; +ok(!$pty->is_active, "auto-killing a pty with close()"); +ok(!$pty->opened, "closing a pty after a spawn"); + +done_testing; diff --git a/t/subprocess.t b/t/subprocess.t new file mode 100644 index 0000000..d889074 --- /dev/null +++ b/t/subprocess.t @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; +use IO::Pty::Easy; + +my $pty = IO::Pty::Easy->new; +my $script = << 'EOF'; +$| = 1; +if (-t *STDIN && -t *STDOUT) { print "ok" } +else { print "failed" } +EOF + +my $outside_of_pty = `$^X -e '$script'`; +unlike($outside_of_pty, qr/ok/, "running outside of pty fails -t checks"); + +# we need to keep the script alive until we can read the output from it +$script .= "sleep 1 while 1;"; +$pty->spawn("$^X -e '$script'"); +like($pty->read, qr/ok/, "runs subprocess in a pty"); +$pty->close; + +done_testing; diff --git a/t/system.t b/t/system.t new file mode 100644 index 0000000..c6fb29e --- /dev/null +++ b/t/system.t @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; +use IO::Pty::Easy; + +my $pty = IO::Pty::Easy->new; +$pty->spawn("$^X -ple ''"); +my $output; +eval { + local $SIG{ALRM} = sub { die "alarm\n" }; + alarm 5; + $output = `$^X -e 'print "foo"'`; + alarm 0; +}; +isnt($@, "alarm\n", "system() didn't time out"); +is($output, "foo", "system() got the right value"); +$pty->kill; +undef $output; +eval { + local $SIG{ALRM} = sub { die "alarm2\n" }; + alarm 5; + $output = `$^X -e 'print "bar"'`; + alarm 0; +}; +isnt($@, "alarm2\n", "system() didn't time out (after kill)"); +is($output, "bar", "system() got the right value (after kill)"); +$pty->close; + +done_testing; diff --git a/t/undefined-program.t b/t/undefined-program.t new file mode 100644 index 0000000..f53d065 --- /dev/null +++ b/t/undefined-program.t @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; +use IO::Pty::Easy; + +my $pty = IO::Pty::Easy->new; +eval { + local $SIG{ALRM} = sub { die "alarm\n" }; + alarm 5; + $pty->spawn("missing_program_io_pty_easy"); + alarm 0; +}; +like($@, qr/Cannot exec\(missing_program_io_pty_easy\)/); +ok(!$pty->is_active, "pty isn't active if program doesn't exist"); + +done_testing; -- cgit v1.2.3-54-g00ecf