forked from RexOps/Rex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_rexfile.t
executable file
·98 lines (71 loc) · 2.22 KB
/
load_rexfile.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env perl
use v5.12.5;
use warnings;
use autodie;
our $VERSION = '9999.99.99_99'; # VERSION
use Test::More;
use English qw(-no_match_vars);
use File::Spec;
use File::Temp;
use Rex::CLI;
use Rex::Commands::File;
use Sub::Override;
use Test::Output;
use constant FIRST_PERL_VERSION_WITH_EXTRA_ERROR_MESSAGE => v5.37.9;
$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;
if ( -r $file ) {
if ( $PERL_VERSION lt FIRST_PERL_VERSION_WITH_EXTRA_ERROR_MESSAGE
&& $expected->{exit} == 1 )
{
my @output_lines = split qr{^}msx, cat($file);
pop @output_lines;
$expected->{$extension} = join $empty, @output_lines;
}
else {
$expected->{$extension} = cat($file);
}
}
else {
$expected->{$extension} = $default_content;
}
$expected->{$extension} =~ s{%REXFILE_PATH%}{$rexfile}gmsx;
}
# reset log
open my $fh, '>', $logfile;
close $fh;
# reset require
delete $INC{'__Rexfile__.pm'};
return;
}