lib/Rex/Hook/File/Diff.pm

filter: Statementstmt 93.3% 42 / 45 Branchbran 75.0% 9 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 83.5% 61 / 73 CC 8 SCAR 13.1
CC
8
Cov
87%
show_diff
7.2
involved_files
3.0
BEGIN
1.0
CRAP 3.7
↑ Summary
1 package Rex::Hook::File::Diff;
2  
3 # ABSTRACT: show diff of changes for files managed by Rex
4  
5 2 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Rex 1.013004 -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Rex::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Text::Diff 1.44;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = 'v0.4.0';
15  
16 2 register_function_hooks { before_change => { file => \&show_diff, }, };
17  
18 12 ▶sub show_diff {
Subroutine show_diff: called CC=6 SCAR=19.7
Pod undocumented
19 12 my ( $original_file, @opts ) = @_;
20  
21 12 my $diff;
22 12 my $diff_command = can_run('diff');
23 12 state $managing_windows = is_windows();
24  
25 12 ▶ if ( !$managing_windows && $diff_command ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $managing_windows and $diff_command) { } 12 0 12
elsif (Rex::is_local()) { } 0 0 0
Condition33%1/3
not $managing_windows and $diff_command
!ll&&!rl&&r
0012
MC/DC0%0/2
not $managing_windows and $diff_command
not $managing_windows $diff_command
Truth table33%1/3
not(A) and B
A= not $managing_windowsB= $diff_command
ABresult
0X0
100
111
26 12 my $command = join q( ), $diff_command, '-u', involved_files($original_file);
27  
28 12 ▶ if ( $diff = i_run $command, fail_ok => TRUE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff = i_run($command, "fail_ok", TRUE())) 10 2 12
29 10 $diff .= "\n";
30 }
31 }
32 elsif ( Rex::is_local() ) {
33 0 $diff = diff( involved_files($original_file) );
34 }
35 else {
36 0 Rex::Logger::debug(
37 'Skipping File::Diff hook due to remote operation without the diff utility');
38 0 return;
39 }
40  
41 12 ▶ if ( length $diff > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $diff > 0) 10 2 12
42 10 Rex::Commands::say("Diff for: $original_file\n$diff");
43 }
44  
45 12 return;
46 }
47  
48 15 ▶sub involved_files {
Subroutine involved_files: called CC=3 SCAR=11.0
Pod undocumented
49 15 my $file = shift;
50  
51 15 my $temp_file = Rex::Commands::File::get_tmp_file_name($file);
52 15 my $null = File::Spec->devnull();
53  
54 15 ▶ if ( !is_file($file) ) { $file = $null } # creating file
Branch: 1 branch
BranchTrueFalseTotal
unless (is_file($file)) 4 11 15
55 15 ▶ if ( !is_file($temp_file) ) { $temp_file = $null } # removing file
Branch: 1 branch
BranchTrueFalseTotal
unless (is_file($temp_file)) 5 10 15
56  
57 15 return ( $file, $temp_file );
58 }
59  
60 2 1;
61  
62 __END__
↑ Summary