1#! /usr/bin/env perl -w
2BEGIN {
3  if ($ENV{PERL_CORE}) {
4    @INC = ('t', '../../lib');
5  } else {
6    unshift @INC, 't';
7  }
8  require Config;
9  if ($ENV{PERL_CORE} and ($Config::Config{'extensions'} !~ /\bB\b/) ){
10    print "1..0 # Skip -- Perl configured without B module\n";
11    exit 0;
12  }
13  require TestBC;
14}
15
16use strict;
17use Config;
18use File::Spec;
19use File::Path;
20
21my $path = File::Spec->catdir( 'lib', 'B' );
22unless (-d $path) {
23  mkpath( $path ) or skip_all( 'Cannot create fake module path' );
24}
25
26my $file = File::Spec->catfile( $path, 'success.pm' );
27local *OUT;
28open(OUT, '>', $file) or skip_all( 'Cannot write fake backend module');
29print OUT while <DATA>;
30close *OUT;
31
32plan tests => 9; # And someone's responsible.
33
34# use() makes it difficult to avoid O::import()
35require_ok( 'O' );
36
37my @args = ('-Ilib', '-MO=success,foo,bar', $ENV{HARNESS_PERL_SWITCHES} || '', '-e', '1' );
38my @lines = get_lines( @args );
39
40is( $lines[0], 'Compiling!', 'Output should not be saved without -q switch' );
41is( $lines[1], '(foo) <bar>', 'O.pm should call backend compile() method' );
42is( $lines[2], '[]', 'Nothing should be in $O::BEGIN_output without -q' );
43SKIP: {
44  skip( 'stderr redirection to stdout does not work on Win32 cmd', 1)
45    if $^O eq 'MSWin32';
46  is( $lines[3], '-e syntax OK', 'O.pm should not munge perl output without -qq');
47}
48
49$args[1] = '-MO=-q,success,foo,bar';
50@lines = get_lines( @args );
51isnt( $lines[1], 'Compiling!', 'Output should not be printed with -q switch' );
52
53SKIP: {
54  skip( '-q redirection does not work without PerlIO', 2)
55    unless $Config{useperlio};
56  is( $lines[1], "[Compiling!", '... but should be in $O::BEGIN_output' );
57
58  $args[1] = '-MO=-qq,success,foo,bar';
59  @lines = get_lines( @args );
60  is( scalar @lines, 3, '-qq should suppress even the syntax OK message' );
61}
62
63SKIP: {
64  skip( 'Wrong O.pm die eval message with 5.6', 1) if $] < 5.007;
65  skip( 'stderr redirection to stdout does not work on Win32 cmd', 1)
66    if $^O eq 'MSWin32';
67
68  $args[1] = '-MO=success,fail';
69  @lines = get_lines( @args );
70  like( $lines[1], qr/fail at .eval/,
71        'O.pm should die if backend compile() does not return a subref' );
72}
73
74sub get_lines {
75  split(/[\r\n]+/, runperl( args => [ @_ ], stderr => 1 ));
76}
77
78END {
79  1 while unlink($file);
80  rmdir($path); # not "1 while" since there might be more in there
81}
82
83__END__
84package B::success;
85
86$| = 1;
87print "Compiling!\n";
88
89sub compile {
90  return 'fail' if ($_[0] eq 'fail');
91  print "($_[0]) <$_[1]>\n";
92  return sub { print "[$O::BEGIN_output]\n" };
93}
94
951;
96