|
| 1 | +#!/usr/bin/env perl |
| 2 | +use 5.010001; |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +our $VERSION = '9999.99.99_99'; # VERSION |
| 7 | + |
| 8 | +use Test::More; |
| 9 | +use Test::Output; |
| 10 | +use File::Temp; |
| 11 | +use File::Spec; |
| 12 | + |
| 13 | +use Rex::CLI; |
| 14 | +## no critic (RequireTrailingCommaAtNewline); |
| 15 | +## no critic (ProhibitPostfixControls, WhileDiamondDefaultAssignment); |
| 16 | +## no critic (ProhibitPunctuationVars, ProhibitPackageVars); |
| 17 | +## no critic (RequireCheckedSyscalls, RequireCheckedClose); |
| 18 | +## no critic (RegularExpressions); |
| 19 | +## no critic (Carping, ProhibitNoWarnings, DuplicateLiteral); |
| 20 | + |
| 21 | +#diag 'create some rexfiles to test...'; |
| 22 | +my $fh = undef; |
| 23 | +my $testdir = File::Temp->newdir( 'rextest.XXXX', TMPDIR => 1, CLEANUP => 1 ); |
| 24 | +while (<DATA>) { |
| 25 | + last if /^__END__$/; |
| 26 | + if (/^@@ *(\S+)$/) { |
| 27 | + |
| 28 | + #diag "prepare file $1"; |
| 29 | + close $fh if $fh; |
| 30 | + open $fh, '>', File::Spec->catfile( $testdir, $1 ) or die $!; |
| 31 | + next; |
| 32 | + } |
| 33 | + print {$fh} $_ if $fh; |
| 34 | +} |
| 35 | +close $fh if $fh; |
| 36 | + |
| 37 | +our $exit_was_called = undef; |
| 38 | + |
| 39 | +# we must disable Rex::CLI::exit() sub imported from Rex::Commands |
| 40 | +no warnings 'redefine'; |
| 41 | +local *Rex::CLI::exit = sub { $exit_was_called = 1 }; |
| 42 | +use warnings 'redefine'; |
| 43 | + |
| 44 | +# |
| 45 | +# enable this to debug! |
| 46 | +# |
| 47 | +$::QUIET = 1; |
| 48 | + |
| 49 | +#$Rex::Logger::no_color = 1; |
| 50 | +my $logfile = File::Spec->catfile( $testdir, 'log' ); |
| 51 | +Rex::Config->set_log_filename($logfile); |
| 52 | + |
| 53 | +# NOW TEST |
| 54 | + |
| 55 | +# No Rexfile warning (via Rex::Logger) |
| 56 | +Rex::CLI::load_rexfile( File::Spec->catfile( $testdir, 'no_Rexfile' ) ); |
| 57 | +my $content = _get_log(); |
| 58 | +like( $content, qr/WARN - No Rexfile found/, |
| 59 | + 'No Rexfile warning (via logger)' ); |
| 60 | + |
| 61 | +# Valid Rexfile |
| 62 | +_reset_test(); |
| 63 | +output_like { |
| 64 | + Rex::CLI::load_rexfile( File::Spec->catfile( $testdir, 'Rexfile_noerror' ) ); |
| 65 | +} |
| 66 | +qr/^$/, qr/^$/, 'No stdout/stderr messages on valid Rexfile'; |
| 67 | +$content = _get_log(); |
| 68 | +is( $content, q{}, 'No warnings on valid Rexfile (via logger)' ); |
| 69 | + |
| 70 | +# Rexfile with warnings |
| 71 | +_reset_test(); |
| 72 | +output_like { |
| 73 | + Rex::CLI::load_rexfile( File::Spec->catfile( $testdir, 'Rexfile_warnings' ) ); |
| 74 | +} |
| 75 | +qr/^$/, qr/^$/, 'No stdout/stderr messages on Rexfile with warnings'; |
| 76 | +$content = _get_log(); |
| 77 | +ok( !$exit_was_called, 'sub load_rexfile() not exit' ); |
| 78 | +like( |
| 79 | + $content, |
| 80 | + qr/WARN - You have some code warnings/, |
| 81 | + 'Code warnings via logger' |
| 82 | +); |
| 83 | +like( $content, qr/This is warning/, 'warn() warning via logger' ); |
| 84 | +like( |
| 85 | + $content, |
| 86 | + qr/Use of uninitialized value \$undef/, |
| 87 | + 'perl warning via logger' |
| 88 | +); |
| 89 | +unlike( |
| 90 | + $content, |
| 91 | + qr#at /loader/0x#, |
| 92 | + 'loader prefix is filtered in warnings report' |
| 93 | +); |
| 94 | + |
| 95 | +# Rexfile with fatal errors |
| 96 | +_reset_test(); |
| 97 | +output_like { |
| 98 | + Rex::CLI::load_rexfile( File::Spec->catfile( $testdir, 'Rexfile_fatal' ) ); |
| 99 | +} |
| 100 | +qr/^$/, qr/^$/, 'No stdout/stderr messages on Rexfile with errors'; |
| 101 | +$content = _get_log(); |
| 102 | +ok( $exit_was_called, 'sub load_rexfile() aborts' ); |
| 103 | +like( $content, qr/ERROR - Compile time errors/, 'Fatal errors via logger' ); |
| 104 | +like( $content, qr/syntax error at/, 'syntax error is fatal error via logger' ); |
| 105 | +unlike( |
| 106 | + $content, |
| 107 | + qr#at /loader/0x#, |
| 108 | + 'loader prefix is filtered in errors report' |
| 109 | +); |
| 110 | + |
| 111 | +# Now print messages to STDERR/STDOUT |
| 112 | +# Valid Rexfile |
| 113 | +_reset_test(); |
| 114 | +output_like { |
| 115 | + Rex::CLI::load_rexfile( |
| 116 | + File::Spec->catfile( $testdir, 'Rexfile_noerror_print' ) ); |
| 117 | +} |
| 118 | +qr/^This is STDOUT message$/, qr/^This is STDERR message$/, |
| 119 | + 'Correct stdout/stderr messages printed from valid Rexfile'; |
| 120 | +$content = _get_log(); |
| 121 | +is( $content, q{}, |
| 122 | + 'No warnings via logger on valid Rexfile that print messages' ); |
| 123 | + |
| 124 | +# Rexfile with warnings |
| 125 | +_reset_test(); |
| 126 | +output_like { |
| 127 | + Rex::CLI::load_rexfile( |
| 128 | + File::Spec->catfile( $testdir, 'Rexfile_warnings_print' ) ); |
| 129 | +} |
| 130 | +qr/^This is STDOUT message$/, qr/^This is STDERR message$/, |
| 131 | + 'Correct stdout/stderr messages printed from Rexfile with warnings'; |
| 132 | +$content = _get_log(); |
| 133 | +like( |
| 134 | + $content, |
| 135 | + qr/WARN - You have some code warnings/, |
| 136 | + 'Code warnings exist via logger' |
| 137 | +); |
| 138 | + |
| 139 | +# Rexfile with fatal errors |
| 140 | +_reset_test(); |
| 141 | +output_like { |
| 142 | + Rex::CLI::load_rexfile( |
| 143 | + File::Spec->catfile( $testdir, 'Rexfile_fatal_print' ) ); |
| 144 | +} |
| 145 | +qr/^$/, qr/^$/, |
| 146 | + 'No stdout/stderr messages printed from Rexfile that has errors'; |
| 147 | +$content = _get_log(); |
| 148 | +ok( $exit_was_called, 'sub load_rexfile() aborts' ); |
| 149 | +like( |
| 150 | + $content, |
| 151 | + qr/ERROR - Compile time errors/, |
| 152 | + 'Fatal errors exist via logger' |
| 153 | +); |
| 154 | + |
| 155 | +done_testing; |
| 156 | + |
| 157 | +# from logger.t |
| 158 | +sub _get_log { |
| 159 | + ## no critic (LocalVars) |
| 160 | + local $/; |
| 161 | + |
| 162 | + open my $fh, '<', $logfile or die $!; |
| 163 | + my $loglines = <$fh>; |
| 164 | + close $fh; |
| 165 | + |
| 166 | + return $loglines; |
| 167 | +} |
| 168 | + |
| 169 | +sub _reset_test { |
| 170 | + $exit_was_called = undef; |
| 171 | + |
| 172 | + # reset log |
| 173 | + open my $fh, '>', $logfile or die $!; |
| 174 | + close $fh; |
| 175 | + |
| 176 | + # reset require |
| 177 | + delete $INC{'__Rexfile__.pm'}; |
| 178 | + |
| 179 | + return; |
| 180 | +} |
| 181 | + |
| 182 | +__DATA__ |
| 183 | +@@ Rexfile_noerror |
| 184 | +use Rex; |
| 185 | +user 'testuser'; |
| 186 | +task test => sub { say "test1" }; |
| 187 | +
|
| 188 | +@@ Rexfile_warnings |
| 189 | +use Rex; |
| 190 | +use warnings; |
| 191 | +warn 'This is warning'; |
| 192 | +my $undef; my $warn = 'warn'.$undef; |
| 193 | +user 'testuser'; |
| 194 | +task test => sub { say "test2" }; |
| 195 | +
|
| 196 | +@@ Rexfile_fatal |
| 197 | +use Rex; |
| 198 | +aaaabbbbcccc |
| 199 | +task test => sub { say "test3" }; |
| 200 | +
|
| 201 | +@@ Rexfile_noerror_print |
| 202 | +use Rex; |
| 203 | +user 'testuser'; |
| 204 | +print STDERR 'This is STDERR message'; |
| 205 | +print STDOUT 'This is STDOUT message'; |
| 206 | +task test2 => sub { say "test4" }; |
| 207 | +
|
| 208 | +@@ Rexfile_warnings_print |
| 209 | +use Rex; |
| 210 | +use warnings; |
| 211 | +warn 'This is warning'; |
| 212 | +my $undef; my $warn = 'warn'.$undef; |
| 213 | +print STDERR 'This is STDERR message'; |
| 214 | +print STDOUT 'This is STDOUT message'; |
| 215 | +user 'testuser'; |
| 216 | +task test2 => sub { say "test5" }; |
| 217 | +
|
| 218 | +@@ Rexfile_fatal_print |
| 219 | +use Rex; |
| 220 | +print STDERR 'This is STDERR message'; |
| 221 | +print STDOUT 'This is STDOUT message'; |
| 222 | +aaaabbbbcccc |
| 223 | +print STDERR 'This is STDERR message'; |
| 224 | +print STDOUT 'This is STDOUT message'; |
| 225 | +task test2 => sub { say "test6" }; |
| 226 | +
|
0 commit comments