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   218529 use 5.012;
  2         16  
6 2     2   10 use warnings;
  2         5  
  2         71  
7              
8 2     2   12 use File::Basename;
  2         3  
  2         160  
9 2     2   482 use Rex 1.013004 -base;
  2         48069  
  2         14  
10 2     2   517490 use Rex::Helper::Run;
  2         11  
  2         131  
11 2     2   18 use Rex::Hook;
  2         7  
  2         109  
12 2     2   24 use Text::Diff 1.44;
  2         20235  
  2         730  
13              
14             our $VERSION = 'v0.3.0.3'; # TRIAL
15              
16             register_function_hooks { before_change => { file => \&show_diff, }, };
17              
18             sub show_diff {
19 12     12 0 937596 my ( $original_file, @opts ) = @_;
20              
21 12         90 my $diff;
22 12         339 my $diff_command = can_run('diff');
23 12         186048 state $managing_windows = is_windows();
24              
25 12 50 33     162408 if ( !$managing_windows && $diff_command ) {
    0          
26 12         227 my $command = join q( ), $diff_command, '-u', involved_files($original_file);
27              
28 12 100       174 if ( $diff = i_run $command, fail_ok => TRUE ) {
29 10         142509 $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       17917 if ( length $diff > 0 ) {
42 10         276 Rex::Commands::say("Diff for: $original_file\n$diff");
43             }
44              
45 12         1709 return;
46             }
47              
48             sub involved_files {
49 15     15 0 13689 my $file = shift;
50              
51 15         297 my $temp_file = Rex::Commands::File::get_tmp_file_name($file);
52 15         5146 my $null = File::Spec->devnull();
53              
54 15 100       287 if ( !is_file($file) ) { $file = $null } # creating file
  4         24635  
55 15 100       5296 if ( !is_file($temp_file) ) { $temp_file = $null } # removing file
  5         1162  
56              
57 15         1751 return ( $file, $temp_file );
58             }
59              
60             1;
61              
62             __END__