File: stdin_tell_bug.t

package info (click to toggle)
libfilehandle-unget-perl 0.1634-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 324 kB
  • sloc: perl: 2,160; makefile: 2
file content (103 lines) | stat: -rwxr-xr-x 2,275 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use strict;
use FileHandle::Unget;
use File::Spec::Functions qw(:ALL);
use Test::More tests => 3;
use Config;
use File::Temp;
use File::Slurper qw(read_text);

# -------------------------------------------------------------------------------

use vars qw( %PROGRAMS $single_quote $command_separator $set_env );

if ($^O eq 'MSWin32')
{
  $set_env = 'set';
  $single_quote = '"';
  $command_separator = '&';
}
else
{
  $set_env = '';
  $single_quote = "'";
  $command_separator = '';
}

# -------------------------------------------------------------------------------

my $path_to_perl = $Config{perlpath};

my $test_program;
{
  my $fh;
  ($fh, $test_program) = File::Temp::tempfile(UNLINK => 1);
  print $fh do { local $/; <DATA> };
  close $fh;
}

# Note: No space before the pipe because on Windows it is passed to the test
# program
my $test = "echo hello| $path_to_perl $test_program";
my $expected_stdout = qr/Starting at position (-1|0)\ngot: hello\ngot: world\n/;
my $expected_stderr = '';

{
  my @standard_inc = split /###/, `$path_to_perl -e "\$\\" = '###';print \\"\@INC\\""`;
  my @extra_inc;
  foreach my $inc (@INC)
  {
    push @extra_inc, "$single_quote$inc$single_quote"
      unless grep { /^\Q$inc\E$/ } @standard_inc;
  }

  my $test_program_pattern = $test_program;
  $test_program_pattern =~ s/\\/\\\\/g;
  if (@extra_inc)
  {
    local $" = ' -I';
    $test =~ s#\b\Q$path_to_perl\E\b#$path_to_perl -I@extra_inc#g;
  }
}

my ($test_stdout, $test_stderr);
{
  my $fh;
  ($fh, $test_stdout) = File::Temp::tempfile(UNLINK => 1);
  close $fh;
  ($fh, $test_stderr) = File::Temp::tempfile(UNLINK => 1);
  close $fh;
}

system "$test 1>$test_stdout 2>$test_stderr";

#1
ok(!$?,'Executing external program');

my $actual_stdout = read_text($test_stdout, undef, 1);
my $actual_stderr = read_text($test_stderr, undef, 1);

#2
like($actual_stdout,$expected_stdout,'Output matches');

#3
is($actual_stderr,$expected_stderr,'Stderr matches');

exit;

# -------------------------------------------------------------------------------

__DATA__
use strict;
use FileHandle::Unget;

my $fh = new FileHandle::Unget(\*STDIN);

print 'Starting at position ', tell($fh), "\n";

# 1
print "got: ", scalar <$fh>;

$fh->ungets("world\n");

# 2
print "got: ", scalar <$fh>;