-
Notifications
You must be signed in to change notification settings - Fork 221
Fix #1521 lib/Rex/CLI.pm: Use $SIG{__WARN__} when loading Rexfile instead of stderr redirection #1522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix #1521 lib/Rex/CLI.pm: Use $SIG{__WARN__} when loading Rexfile instead of stderr redirection #1522
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
71d6a5f
Add Rexfile loading tests
uralm1 55d6ffb
Remove useless '## no critic' annotation
ferki 71b0c6a
Fix test boilerplate
ferki 5a92105
Fix avoidable package variable
ferki 2aea423
Split out sample Rexfiles
ferki 7fb2543
Tidy up sample Rexfiles
ferki 94191a7
Clear tasks between tests
ferki 74ad07b
Use built-in command to slurp files
ferki 374c9e7
Enable autodie pragma
ferki 04324d4
Compare log content against expected log files
ferki a67d170
Override exit calls cleanly
ferki 1a41ffe
Test exit status
ferki 8265d52
Test console output
ferki 8640d64
Test in a loop
ferki d5ed1d9
Fix handling of warnings during Rexfile loading
uralm1 7e51ca8
Remove unused variable
uralm1 88f8bf8
Make the output more human-friendly
uralm1 fb36281
Restore standard STDERR usage
ferki fbd633f
Mask internal naming in Rexfile loading output
ferki 644cb6d
Prevent empty log lines upon Rexfile warnings
ferki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ Revision history for Rex | |
|
||
[BUG FIXES] | ||
- Detect invalid hostgroup expressions | ||
- Prevent empty log lines upon Rexfile warnings | ||
|
||
[DOCUMENTATION] | ||
- Clarify optional arguments of file commands | ||
|
@@ -20,6 +21,8 @@ Revision history for Rex | |
[NEW FEATURES] | ||
|
||
[REVISION] | ||
- Fix handling of warnings during Rexfile loading | ||
- Mask internal naming in Rexfile loading output | ||
|
||
1.13.4 2021-07-05 Ferenc Erki <[email protected]> | ||
[DOCUMENTATION] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,6 @@ vars\..* | |
passwd | ||
upload | ||
.*\.conf | ||
.*\.log | ||
^images | ||
projects | ||
Rex\-\d+.* | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#!/usr/bin/env perl | ||
|
||
use 5.010001; | ||
use strict; | ||
use warnings; | ||
use autodie; | ||
|
||
our $VERSION = '9999.99.99_99'; # VERSION | ||
|
||
use Test::More; | ||
ferki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use File::Spec; | ||
use File::Temp; | ||
use Rex::CLI; | ||
use Rex::Commands::File; | ||
use Sub::Override; | ||
use Test::Output; | ||
|
||
$Rex::Logger::format = '%l - %s'; | ||
$Rex::Logger::no_color = 1; | ||
|
||
my $testdir = File::Spec->join( 't', 'rexfiles' ); | ||
my $rex_cli_path = $INC{'Rex/CLI.pm'}; | ||
my $empty = q(); | ||
|
||
my ( $dh, $exit_was_called, $expected ); | ||
|
||
my $override = | ||
Sub::Override->new( 'Rex::CLI::exit' => sub { $exit_was_called = 1 } ); | ||
|
||
my $logfile = File::Temp->new->filename; | ||
Rex::Config->set_log_filename($logfile); | ||
|
||
opendir $dh, $testdir; | ||
my @rexfiles = grep { !/[.]/msx } readdir $dh; | ||
closedir $dh; | ||
|
||
push @rexfiles, 'no_Rexfile'; | ||
|
||
plan tests => scalar @rexfiles; | ||
|
||
for my $rexfile (@rexfiles) { | ||
subtest "Testing with $rexfile" => sub { | ||
$rexfile = File::Spec->join( $testdir, $rexfile ); | ||
|
||
_setup_test($rexfile); | ||
|
||
output_is { Rex::CLI::load_rexfile($rexfile); } $expected->{stdout}, | ||
$expected->{stderr}, 'Expected console output'; | ||
|
||
is( $exit_was_called, $expected->{exit}, 'Expected exit status' ); | ||
is( cat($logfile), $expected->{log}, 'Expected log content' ); | ||
}; | ||
} | ||
|
||
sub _setup_test { | ||
my $rexfile = shift; | ||
|
||
Rex::TaskList->create->clear_tasks(); | ||
|
||
$exit_was_called = 0; | ||
|
||
$expected->{exit} = $rexfile =~ qr{fatal}ms ? 1 : 0; | ||
|
||
for my $extension (qw(log stdout stderr)) { | ||
my $file = "$rexfile.$extension"; | ||
my $default_content = $extension eq 'stderr' ? $expected->{log} : $empty; | ||
|
||
$expected->{$extension} = -r $file ? cat($file) : $default_content; | ||
$expected->{$extension} =~ s{%REX_CLI_PATH%}{$rex_cli_path}msx; | ||
} | ||
|
||
# reset log | ||
open my $fh, '>', $logfile; | ||
close $fh; | ||
|
||
# reset require | ||
delete $INC{'__Rexfile__.pm'}; | ||
|
||
return; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use Rex; | ||
|
||
abc | ||
|
||
task 'test', sub { }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ERROR - Compile time errors: | ||
ERROR - syntax error at Rexfile line 5, near "abc | ||
ERROR - | ||
ERROR - task " | ||
ERROR - Compilation failed in require at %REX_CLI_PATH% line 756. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use Rex; | ||
|
||
print STDERR "This is STDERR message\n"; | ||
print STDOUT "This is STDOUT message\n"; | ||
|
||
abc | ||
|
||
print STDERR "This is STDERR message\n"; | ||
print STDOUT "This is STDOUT message\n"; | ||
|
||
task 'test', sub { }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ERROR - Compile time errors: | ||
ERROR - syntax error at Rexfile line 8, near "abc | ||
ERROR - | ||
ERROR - print" | ||
ERROR - Compilation failed in require at %REX_CLI_PATH% line 756. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
use Rex; | ||
|
||
task 'test', sub { }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use Rex; | ||
|
||
print STDERR "This is STDERR message\n"; | ||
print STDOUT "This is STDOUT message\n"; | ||
|
||
task 'test', sub { }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is STDERR message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is STDOUT message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use Rex; | ||
|
||
use warnings; | ||
|
||
warn 'This is warning'; | ||
my $warn = 'warn' . undef; | ||
|
||
task 'test', sub { }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
WARN - You have some code warnings: | ||
WARN - This is warning at Rexfile line 5. | ||
WARN - Use of uninitialized value in concatenation (.) or string at Rexfile line 6. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use Rex; | ||
|
||
use warnings; | ||
|
||
warn 'This is warning'; | ||
my $warn = 'warn' . undef; | ||
|
||
print STDERR "This is STDERR message\n"; | ||
print STDOUT "This is STDOUT message\n"; | ||
|
||
task 'test', sub { }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
WARN - You have some code warnings: | ||
WARN - This is warning at Rexfile line 5. | ||
WARN - Use of uninitialized value in concatenation (.) or string at Rexfile line 6. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This is STDERR message | ||
WARN - You have some code warnings: | ||
WARN - This is warning at Rexfile line 5. | ||
WARN - Use of uninitialized value in concatenation (.) or string at Rexfile line 6. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is STDOUT message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
WARN - No Rexfile found. | ||
WARN - Create a file named 'Rexfile' in this directory, | ||
WARN - or specify the file you want to use with: | ||
WARN - rex -f file_to_use task_to_run |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.