Skip to content

Commit d350510

Browse files
committed
Added load_rexfile() test
1 parent 7fd51ec commit d350510

File tree

1 file changed

+223
-0
lines changed

1 file changed

+223
-0
lines changed

t/load_rexfile.t

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

0 commit comments

Comments
 (0)