File Coverage

lib/Rex/Hook/File/Diff.pm
Criterion Covered Total %
statement 39 42 92.8
branch 9 12 75.0
condition 1 3 33.3
subroutine 9 9 100.0
pod 0 2 0.0
total 58 68 85.2


line stmt bran cond sub pod time code
1             package Rex::Hook::File::Diff;
2              
3             # ABSTRACT: show diff of changes for files managed by Rex
4              
5 2     2   171401 use 5.012;
  2         13  
6 2     2   9 use warnings;
  2         3  
  2         45  
7              
8 2     2   8 use File::Basename;
  2         4  
  2         118  
9 2     2   465 use Rex 1.013004 -base;
  2         44251  
  2         13  
10 2     2   429943 use Rex::Helper::Run;
  2         10  
  2         122  
11 2     2   10 use Rex::Hook;
  2         10  
  2         70  
12 2     2   18 use Text::Diff 1.44;
  2         18232  
  2         639  
13              
14             our $VERSION = 'v0.3.0.4'; # TRIAL
15              
16             register_function_hooks { before_change => { file => \&show_diff, }, };
17              
18             sub show_diff {
19 12     12 0 486086 my ( $original_file, @opts ) = @_;
20              
21 12         110 my $diff;
22 12         233 my $diff_command = can_run('diff');
23 12         72542 state $managing_windows = is_windows();
24              
25 12 50 33     43615 if ( !$managing_windows && $diff_command ) {
    0          
26 12         213 my $command = join q( ), $diff_command, '-u', involved_files($original_file);
27              
28 12 100       162 if ( $diff = i_run $command, fail_ok => TRUE ) {
29 10         55001 $diff .= "\n";
30             }
31             }
32             elsif ( Rex::is_local() ) {
33 0         0 $diff = diff( involved_files($original_file) );
34             }
35             else {
36 0         0 Rex::Logger::debug(
37             'Skipping File::Diff hook due to remote operation without the diff utility');
38 0         0 return;
39             }
40              
41 12 100       8849 if ( length $diff > 0 ) {
42 10         250 Rex::Commands::say("Diff for: $original_file\n$diff");
43             }
44              
45 12         1453 return;
46             }
47              
48             sub involved_files {
49 15     15 0 12685 my $file = shift;
50              
51 15         223 my $temp_file = Rex::Commands::File::get_tmp_file_name($file);
52 15         3258 my $null = File::Spec->devnull();
53              
54 15 100       188 if ( !is_file($file) ) { $file = $null } # creating file
  4         24218  
55 15 100       3981 if ( !is_file($temp_file) ) { $temp_file = $null } # removing file
  5         1005  
56              
57 15         1634 return ( $file, $temp_file );
58             }
59              
60             1;
61              
62             __END__