File Coverage

blib/lib/Test/CheckGitStatus.pm
Criterion Covered Total %
statement 15 31 48.3
branch 3 14 21.4
condition 1 3 33.3
subroutine 5 7 71.4
pod 0 1 0.0
total 24 56 42.8


line stmt bran cond sub pod time code
1             # ABSTRACT: Check git status after every test
2             package Test::CheckGitStatus;
3 2     2   367094 use strict;
  2         4  
  2         86  
4 2     2   10 use warnings;
  2         5  
  2         142  
5 2     2   13 use Config;
  2         4  
  2         2116  
6              
7             our $VERSION = 'v0.1.2'; # VERSION
8              
9             my $CHECK_GIT_STATUS = $ENV{CHECK_GIT_STATUS};
10              
11             # prevent subsequent perl processes to check the status
12             if ($ENV{HARNESS_ACTIVE} or $ENV{TEST_ACTIVE} or $ENV{TEST2_ACTIVE}) {
13             $ENV{CHECK_GIT_STATUS} = 0;
14             }
15             else {
16             $CHECK_GIT_STATUS = 0;
17             }
18              
19             # Prevent forked processes to check the status
20             my $pid = $$;
21              
22             my $cwd;
23              
24             if ($CHECK_GIT_STATUS) {
25             require Test::More;
26             require Cwd;
27             $cwd = Cwd::cwd();
28             }
29              
30             sub check_status {
31 0     0 0 0 my @lines = _get_status();
32 0 0       0 if (@lines > 0) {
33 0         0 Test::More::diag("Error: modified or untracked files\n" . join '', @lines);
34 0         0 $? = 1;
35             }
36             }
37              
38             sub _check_git {
39 1     1   262750 require File::Which;
40 1         1713 my $git = File::Which::which('git');
41 1 50       521 return unless $git;
42 1 50       31 if ($Config{osname} eq 'MSWin32') {
43 0         0 $git = qq{"$git"};
44             }
45 1         7 return $git;
46             }
47              
48             sub _get_status {
49 0     0   0 local $?;
50 0         0 chdir $cwd;
51 0         0 my $git = _check_git();
52 0 0       0 return unless $git;
53 0         0 my $cmd = qq{$git rev-parse --git-dir};
54 0         0 my $out = qx{$cmd};
55 0 0       0 return if $? != 0;
56 0         0 $cmd = qq{$git status --porcelain=v1 2>&1};
57 0         0 my @lines = qx{$cmd};
58 0 0       0 die "Problem running '$git':\n" . join '', @lines if $? != 0;
59 0         0 return @lines;
60             }
61              
62             END {
63             # Check $pid - don't run this in forked processes
64 1 50 33 1   2467040 check_status() if $$ == $pid and $CHECK_GIT_STATUS;
65             }
66              
67             1;
68              
69             __END__