o.t 2.5 KB

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