Skip to content

Commit ae3fb23

Browse files
committed
Add unit test for RT #132008
Term::ReadLine generates empty &STDERR files GH #1
1 parent d763d2f commit ae3fb23

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

dist/Term-ReadLine/lib/Term/ReadLine.pm

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,17 @@ sub readline {
229229
}
230230
sub addhistory {}
231231

232+
# used for testing purpose
233+
sub devtty { return '/dev/tty' }
234+
232235
sub findConsole {
233236
my $console;
234237
my $consoleOUT;
235238

236-
if ($^O ne 'MSWin32' and -e "/dev/tty") {
237-
$console = "/dev/tty";
239+
my $devtty = devtty();
240+
241+
if ($^O ne 'MSWin32' and -e $devtty) {
242+
$console = $devtty;
238243
} elsif ($^O eq 'MSWin32' or $^O eq 'msys' or -e "con") {
239244
$console = 'CONIN$';
240245
$consoleOUT = 'CONOUT$';
@@ -248,7 +253,7 @@ sub findConsole {
248253

249254
$consoleOUT = $console unless defined $consoleOUT;
250255
$console = "&STDIN" unless defined $console;
251-
if ($console eq "/dev/tty" && !open(my $fh, "<", $console)) {
256+
if ($console eq $devtty && !open(my $fh, "<", $console)) {
252257
$console = "&STDIN";
253258
undef($consoleOUT);
254259
}
@@ -266,9 +271,6 @@ sub new {
266271
if (@_==2) {
267272
my($console, $consoleOUT) = $_[0]->findConsole;
268273

269-
270-
271-
272274
# the Windows CONIN$ needs GENERIC_WRITE mode to allow
273275
# a SetConsoleMode() if we end up using Term::ReadKey
274276
open FIN, (( $^O eq 'MSWin32' && $console eq 'CONIN$' ) ? '+<' : '<' ), $console;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!./perl -w
2+
use strict;
3+
4+
use Test::More;
5+
6+
## unit test for RT 132008 - https://rt.perl.org/Ticket/Display.html?id=132008
7+
8+
if ( $^O eq 'MSWin32' || !-e q{/dev/tty} ) {
9+
plan skip_all => "Test not tested on windows or when /dev/tty do not exists";
10+
}
11+
else {
12+
plan tests => 9;
13+
}
14+
15+
if ( -e q[&STDERR] ) {
16+
note q[Removing existing file &STDERR];
17+
unlink q[&STDERR] or die q{Cannot remove existign file &STDERR [probably created from a previous run]};
18+
}
19+
20+
use_ok('Term::ReadLine');
21+
can_ok( 'Term::ReadLine::Stub', qw{new devtty findConsole} );
22+
23+
is( Term::ReadLine->devtty(), q{/dev/tty} );
24+
my @out = Term::ReadLine::Stub::findConsole();
25+
is_deeply \@out, [ q{/dev/tty}, q{/dev/tty} ], "findConsole is using /dev/tty";
26+
27+
{
28+
no warnings 'redefine';
29+
my $donotexist = q[/this/should/not/exist/hopefully];
30+
31+
ok !-e $donotexist, "File $donotexist does not exist";
32+
local *Term::ReadLine::Stub::devtty = sub { $donotexist };
33+
is( Term::ReadLine->devtty(), $donotexist, "devtty mocked" );
34+
35+
my @out = Term::ReadLine::Stub::findConsole();
36+
is_deeply \@out, [ q{&STDIN}, q{&STDERR} ], "findConsole is using /dev/tty" or diag explain \@out;
37+
38+
ok !-e q[&STDERR], 'file &STDERR do not exist before Term::ReadLine call';
39+
my $tr = Term::ReadLine->new('whatever');
40+
ok !-e q[&STDERR], 'file &STDERR was not created by mistake';
41+
}

0 commit comments

Comments
 (0)