Skip to content

Commit

Permalink
Satisfy perltidy
Browse files Browse the repository at this point in the history
  • Loading branch information
uralm1 committed Feb 4, 2022
1 parent 808d0ca commit 2f632ad
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 35 deletions.
1 change: 1 addition & 0 deletions lib/Rex/CLI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ sub load_rexfile {
if (@warnings) {
Rex::Logger::info( "You have some code warnings:", 'warn' );
for (@warnings) {

# remove /loader/.../ prefix before filename
s|/loader/[^/]+/||;
Rex::Logger::info( "\t$_", 'warn' );
Expand Down
108 changes: 73 additions & 35 deletions t/load_rexfile.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ use File::Spec;
use Rex::CLI;

#diag 'create some rexfiles to test...';
my $fh = undef;
my $testdir = File::Temp->newdir('rextest.XXXX', TMPDIR => 1, CLEANUP => 1);
my $fh = undef;
my $testdir = File::Temp->newdir( 'rextest.XXXX', TMPDIR => 1, CLEANUP => 1 );
while (<DATA>) {
last if /^__END__$/;
if (/^@@ *(\S+)$/) {

#diag "prepare file $1";
close($fh) if $fh;
open($fh, '>', File::Spec->catfile($testdir, $1)) or die $!;
open( $fh, '>', File::Spec->catfile( $testdir, $1 ) ) or die $!;
next;
}
print $fh $_ if $fh;
Expand All @@ -36,78 +37,113 @@ use warnings 'redefine';
$::QUIET = 1;

#$Rex::Logger::no_color = 1;
my $logfile = File::Spec->catfile($testdir, 'log');
my $logfile = File::Spec->catfile( $testdir, 'log' );
Rex::Config->set_log_filename($logfile);


# NOW TEST

# No Rexfile warning (via Rex::Logger)
Rex::CLI::load_rexfile(File::Spec->catfile($testdir, 'no_Rexfile'));
Rex::CLI::load_rexfile( File::Spec->catfile( $testdir, 'no_Rexfile' ) );
my $content = _get_log();
like($content, qr/WARN - No Rexfile found/, 'No Rexfile warning (via logger)');
like( $content, qr/WARN - No Rexfile found/,
'No Rexfile warning (via logger)' );

# Valid Rexfile
_reset_test();
output_like {
Rex::CLI::load_rexfile(File::Spec->catfile($testdir, 'Rexfile_noerror'));
} qr/^$/, qr/^$/, 'No stdout/stderr messages on valid Rexfile';
Rex::CLI::load_rexfile( File::Spec->catfile( $testdir, 'Rexfile_noerror' ) );
}
qr/^$/, qr/^$/, 'No stdout/stderr messages on valid Rexfile';
$content = _get_log();
is($content, '', 'No warnings on valid Rexfile (via logger)');
is( $content, '', 'No warnings on valid Rexfile (via logger)' );

# Rexfile with warnings
_reset_test();
output_like {
Rex::CLI::load_rexfile(File::Spec->catfile($testdir, 'Rexfile_warnings'));
} qr/^$/, qr/^$/, 'No stdout/stderr messages on Rexfile with warnings';
Rex::CLI::load_rexfile( File::Spec->catfile( $testdir, 'Rexfile_warnings' ) );
}
qr/^$/, qr/^$/, 'No stdout/stderr messages on Rexfile with warnings';
$content = _get_log();
ok(!$exit_was_called, 'sub load_rexfile() not exit');
like($content, qr/WARN - You have some code warnings/, 'Code warnings via logger');
like($content, qr/This is warning/, 'warn() warning via logger');
like($content, qr/Use of uninitialized value \$undef/, 'perl warning via logger');
unlike($content, qr#at /loader/0x#, 'loader prefix is filtered in warnings report');
ok( !$exit_was_called, 'sub load_rexfile() not exit' );
like(
$content,
qr/WARN - You have some code warnings/,
'Code warnings via logger'
);
like( $content, qr/This is warning/, 'warn() warning via logger' );
like(
$content,
qr/Use of uninitialized value \$undef/,
'perl warning via logger'
);
unlike(
$content,
qr#at /loader/0x#,
'loader prefix is filtered in warnings report'
);

# Rexfile with fatal errors
_reset_test();
output_like {
Rex::CLI::load_rexfile(File::Spec->catfile($testdir, 'Rexfile_fatal'));
} qr/^$/, qr/^$/, 'No stdout/stderr messages on Rexfile with errors';
Rex::CLI::load_rexfile( File::Spec->catfile( $testdir, 'Rexfile_fatal' ) );
}
qr/^$/, qr/^$/, 'No stdout/stderr messages on Rexfile with errors';
$content = _get_log();
ok($exit_was_called, 'sub load_rexfile() aborts');
like($content, qr/ERROR - Compile time errors/, 'Fatal errors via logger');
like($content, qr/syntax error at/, 'syntax error is fatal error via logger');
unlike($content, qr#at /loader/0x#, 'loader prefix is filtered in errors report');
ok( $exit_was_called, 'sub load_rexfile() aborts' );
like( $content, qr/ERROR - Compile time errors/, 'Fatal errors via logger' );
like( $content, qr/syntax error at/, 'syntax error is fatal error via logger' );
unlike(
$content,
qr#at /loader/0x#,
'loader prefix is filtered in errors report'
);

# Now print messages to STDERR/STDOUT
# Valid Rexfile
_reset_test();
output_like {
Rex::CLI::load_rexfile(File::Spec->catfile($testdir, 'Rexfile_noerror_print'));
} qr/^This is STDOUT message$/, qr/^This is STDERR message$/, 'Correct stdout/stderr messages printed from valid Rexfile';
Rex::CLI::load_rexfile(
File::Spec->catfile( $testdir, 'Rexfile_noerror_print' ) );
}
qr/^This is STDOUT message$/, qr/^This is STDERR message$/,
'Correct stdout/stderr messages printed from valid Rexfile';
$content = _get_log();
is($content, '', 'No warnings via logger on valid Rexfile that print messages');
is( $content, '',
'No warnings via logger on valid Rexfile that print messages' );

# Rexfile with warnings
_reset_test();
output_like {
Rex::CLI::load_rexfile(File::Spec->catfile($testdir, 'Rexfile_warnings_print'));
} qr/^This is STDOUT message$/, qr/^This is STDERR message$/, 'Correct stdout/stderr messages printed from Rexfile with warnings';
Rex::CLI::load_rexfile(
File::Spec->catfile( $testdir, 'Rexfile_warnings_print' ) );
}
qr/^This is STDOUT message$/, qr/^This is STDERR message$/,
'Correct stdout/stderr messages printed from Rexfile with warnings';
$content = _get_log();
like($content, qr/WARN - You have some code warnings/, 'Code warnings exist via logger');
like(
$content,
qr/WARN - You have some code warnings/,
'Code warnings exist via logger'
);

# Rexfile with fatal errors
_reset_test();
output_like {
Rex::CLI::load_rexfile(File::Spec->catfile($testdir, 'Rexfile_fatal_print'));
} qr/^$/, qr/^$/, 'No stdout/stderr messages printed from Rexfile that has errors';
Rex::CLI::load_rexfile(
File::Spec->catfile( $testdir, 'Rexfile_fatal_print' ) );
}
qr/^$/, qr/^$/,
'No stdout/stderr messages printed from Rexfile that has errors';
$content = _get_log();
ok($exit_was_called, 'sub load_rexfile() aborts');
like($content, qr/ERROR - Compile time errors/, 'Fatal errors exist via logger');

ok( $exit_was_called, 'sub load_rexfile() aborts' );
like(
$content,
qr/ERROR - Compile time errors/,
'Fatal errors exist via logger'
);

done_testing;


# from logger.t
sub _get_log {
local $/;
Expand All @@ -121,9 +157,11 @@ sub _get_log {

sub _reset_test {
$exit_was_called = undef;

# reset log
open my $fh, '>', $logfile or die $!;
close $fh;

# reset require
delete $INC{'__Rexfile__.pm'};
}
Expand Down

0 comments on commit 2f632ad

Please sign in to comment.