blib/lib/App/Project/Doctor/Check/Tests.pm

filter: Statementstmt 100% 62 / 62 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 100% 90 / 90 CC 5 SCAR 6.9
CC
5
Cov
100%
check
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.0
« blib/lib/App/Project/Doctor/Finding.pm ↑ Summary /usr/local/bin/prove »
1 package App::Project::Doctor::Check::Tests;
2  
3 # This check verifies that the distribution has a working test suite.
4 # It runs in three stages: (1) does t/ exist? (2) are there .t files?
5 # (3) does 'prove -l' exit 0? The first two failures are auto-fixable.
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Inherit the standard interface from Check::Base.
12 4 ▶use parent -norequire, 'App::Project::Doctor::Check::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # croak reports errors at the caller's location, not inside this module.
15 4 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 # File::Path::make_path creates directory trees in the fix closure.
17 4 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 # File::Spec builds cross-platform file paths (forward slashes on Windows too).
19 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 # Readonly makes constants truly immutable at runtime.
21 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 our $VERSION = '0.02';
24  
25 # The prove command to run. --nocolor avoids ANSI codes in the captured output.
26 4 Readonly::Scalar my $PROVE_CMD => 'prove -l --nocolor 2>&1';
27  
28 # Short name used in Finding.check_name and report column headings.
29 3 ▶sub name { 'Tests' }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
30 # One-line description shown in verbose / help output.
31 1 ▶sub description { 'Test suite exists, contains .t files, and passes cleanly.' }
Subroutine description: called CC=1 SCAR=0.0
Pod documented
32 # This check can offer an automated fix (scaffold a smoke test).
33 2 ▶sub can_fix { 1 }
Subroutine can_fix: called CC=1 SCAR=0.0
Pod documented
34 # Run first; everything else depends on a working test suite.
35 5 ▶sub order { 10 }
Subroutine order: called CC=1 SCAR=0.0
Pod documented
36  
37 8 ▶sub check {
Subroutine check: called CC=5 SCAR=16.1
38 8 my ($self, $ctx) = @_;
39 # Require a Context object so we can use its filesystem helpers.
40 8 ▶ croak 'check requires an App::Project::Doctor::Context' unless ref $ctx;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ctx 1 7 8
41  
42 # -----------------------------------------------------------------------
43 # Stage 1: the t/ directory must exist.
44 # -----------------------------------------------------------------------
45 7 ▶ unless ($ctx->has_file('t')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctx->has_file("t")) 4 3 7
46 # No t/ at all -- return immediately with a fixable error.
47 4 return _f(
48 severity => 'error',
49 message => 'No t/ directory -- distribution has no test suite.',
50 fix => _fix_scaffold($ctx),
51 );
52 }
53  
54 # -----------------------------------------------------------------------
55 # Stage 2: at least one .t file must be in t/.
56 # -----------------------------------------------------------------------
57 3 my $test_files = $ctx->test_files;
58 3 ▶ unless (@{$test_files}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$test_files;}) 1 2 3
59 # The directory exists but is empty of test files.
60 1 return _f(
61 severity => 'error',
62 message => 't/ directory exists but contains no .t files.',
63 fix => _fix_scaffold($ctx),
64 );
65 }
66  
67 # -----------------------------------------------------------------------
68 # Stage 3: run prove and check the exit status.
69 # -----------------------------------------------------------------------
70 # We use Perl's chdir instead of shell 'cd && prove' because quotemeta
71 # on a Windows path (C:\Tmp) produces C\:\\Tmp which cmd.exe rejects.
72 2 my $root = $ctx->root;
73 2 require Cwd;
74 2 my $orig = Cwd::cwd(); # Save the current directory so we can restore it.
75 2 chdir $root;
76 2 my $output = qx{$PROVE_CMD}; # Capture all prove output for the detail field.
77 2 my $status = $?; # $? holds the child process exit status.
78 2 chdir $orig; # Always restore the working directory.
79  
80 2 ▶ if ($status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 0) 1 1 2
81 # prove reported failures; include the captured output as detail.
82 return _f(
83 severity => 'error',
84 1 message => sprintf('Test suite FAILED (%d file(s) with failures).', scalar @{$test_files}),
85 detail => $output,
86 );
87 }
88  
89 # All stages passed -- report success with the file count.
90 return _f(
91 severity => 'pass',
92 1 message => sprintf('%d test file(s) found -- all pass.', scalar @{$test_files}),
93 );
94 }
95  
96 # ---------------------------------------------------------------------------
97 # Private helpers
98 # ---------------------------------------------------------------------------
99  
100 # Purpose: Build a Finding with check_name pre-filled to 'Tests'.
101 # Entry: %args is a valid Finding constructor argument list.
102 # Exit: App::Project::Doctor::Finding object.
103 # Side effects: None.
104 7 ▶sub _f {
Subroutine _f: called CC=1 SCAR=0.0
105 7 require App::Project::Doctor::Finding;
106 7 return App::Project::Doctor::Finding->new(check_name => 'Tests', @_);
107 }
108  
109 # Purpose: Return a coderef that creates a minimal t/00-smoke.t scaffold.
110 # Entry: $ctx is the current App::Project::Doctor::Context.
111 # Exit: Coderef ($ctx) -> void; creates t/ and t/00-smoke.t on disk.
112 # Side effects: Creates directories and files under $ctx->root.
113 5 ▶sub _fix_scaffold {
Subroutine _fix_scaffold: called CC=1 SCAR=0.0
114 5 my $ctx = shift;
115 # Return the fix as a closure so it captures $ctx without running now.
116 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 # Build the absolute path to the t/ directory.
118 1 my $t_dir = File::Spec->catdir($ctx->root, 't');
119 # make_path creates the directory and any missing parent directories.
120 1 File::Path::make_path($t_dir);
121 # The smoke test file that will be written.
122 1 my $smoke = File::Spec->catfile($t_dir, '00-smoke.t');
123 1 open my $fh, '>', $smoke;
124 # Write a minimal but valid Test::More script that always passes.
125 1 print {$fh} <<'END_SMOKE';
126 use strict;
127 use warnings;
128 use Test::More;
129  
130 ok(1, 'module loads');
131 done_testing;
132 END_SMOKE
133 1 close $fh;
134 5 };
135 }
136  
137 4 1;
138  
139 __END__
« blib/lib/App/Project/Doctor/Finding.pm ↑ Summary /usr/local/bin/prove »