blib/lib/Test/CPAN/Health/Check/Examples.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Test/CPAN/Health/Check/DuplicateCode.pm ↑ Summary blib/lib/Test/CPAN/Health/Check/Kwalitee.pm »
1 package Test::CPAN::Health::Check::Examples;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use Carp qw(croak carp);
8 use File::Find::Rule;
9 use File::Spec;
10 use Readonly;
11 use Params::Validate::Strict qw(validate_strict);
12  
13 use parent 'Test::CPAN::Health::Check';
14  
15 our $VERSION = '0.1.0';
16  
17 # Conventional directory names for distribution example scripts.
18 Readonly::Array my @EXAMPLE_DIRS => qw(examples eg example);
19  
20 =head1 NAME
21  
22 Test::CPAN::Health::Check::Examples - Check that example scripts are present
23  
24 =head1 SYNOPSIS
25  
26 use Test::CPAN::Health::Check::Examples;
27  
28 my $check = Test::CPAN::Health::Check::Examples->new;
29 my $result = $check->run($dist);
30  
31 =head1 DESCRIPTION
32  
33 Checks for the presence of an C<examples/>, C<eg/>, or C<example/> directory
34 containing at least one file.
35  
36 Score matrix:
37  
38 =over 4
39  
40 =item * 100 -- examples directory found with at least one file.
41  
42 =item * 50 -- examples directory found but empty.
43  
44 =item * 0 -- no examples directory found.
45  
46 =back
47  
48 =cut
49  
50 sub id { return 'examples' }
51 sub name { return 'Examples' }
52 sub description { return 'Checks that example scripts are present in the distribution' }
53 sub weight { return 2 }
54 sub category { return 'quality' }
55  
56 =head2 run
57  
58 =head3 PURPOSE
59  
60 Locate an examples directory and report whether it contains files.
61  
62 =head3 API SPECIFICATION
63  
64 =head4 INPUT
65  
66 dist Test::CPAN::Health::Distribution required
67 context Hashref optional (unused)
68  
69 =head4 OUTPUT
70  
71 L<Test::CPAN::Health::Result> with:
72  
73 check_id 'examples'
74 status 'pass' | 'warn' | 'fail'
75 score 100 | 50 | 0
76 summary human-readable verdict
77  
78 =head3 MESSAGES
79  
80 Code | Severity | Message | Resolution
81 ------+----------+------------------------------------+---------------------
82 EX001 | FAIL | No examples directory found | Add an examples/ or eg/ directory
83 EX002 | WARN | Examples directory is empty | Add at least one script to examples/
84 EX003 | PASS | Examples directory found |
85  
86 =head3 FORMAL SPECIFICATION
87  
88 -- Z schema (placeholder) --
89 ExamplesOp
90 dist : Distribution
91 score : {0, 50, 100}
92 -------------------------------------------------------
93 no_dir => score = 0
94 dir_empty => score = 50
95 dir_nonempty => score = 100
96  
97 =head3 SIDE EFFECTS
98  
99 Reads directory listings from disk.
100  
101 =head3 USAGE EXAMPLE
102  
103 my $result = Test::CPAN::Health::Check::Examples->new->run($dist);
104  
105 =cut
106  
107 sub run {
108 my ($self, $dist, $context) = @_;
109  
110 croak 'dist must be a Test::CPAN::Health::Distribution'
111 unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution');
112  
113 for my $dir_name (@EXAMPLE_DIRS) {
114 next unless $dist->has_dir($dir_name);
115  
116 my $dir_path = File::Spec->catdir($dist->path, $dir_name);
117 my @files = File::Find::Rule->file->in($dir_path);
118  
119 if (@files) {
120 return $self->_result(
121 status => 'pass',
122 score => 100,
123 summary => sprintf(
124 'Examples directory found: %s/ (%d file(s))',
125 $dir_name, scalar @files,
126 ),
127 data => { name => $self->name, dir => $dir_name, count => scalar @files },
128 );
129 }
130  
131 return $self->_result(
132 status => 'warn',
133 score => 50,
134 summary => sprintf('Examples directory %s/ exists but is empty', $dir_name),
135 details => ['Add at least one example script to the directory'],
136 data => { name => $self->name, dir => $dir_name, count => 0 },
137 );
138 }
139  
140 return $self->_result(
141 status => 'fail',
142 score => 0,
143 summary => 'No examples directory found (examples/, eg/, or example/)',
144 details => [
145 'Create an examples/ or eg/ directory containing short, runnable Perl scripts that demonstrate typical use cases',
146 'Each script should be self-contained: load the module, perform one clear task, and print output so users can run it directly with "perl examples/basic_usage.pl"',
147 'Good examples cover the most common use cases and lower the barrier for new users getting started',
148 ],
149 data => { name => $self->name },
150 );
151 }
152  
153 =head1 AUTHOR
154  
155 Nigel Horne, C<< <njh at nigelhorne.com> >>
156  
157 =head1 LICENSE AND COPYRIGHT
158  
159 Copyright (C) 2025-2026 Nigel Horne.
160  
161 This program is free software; you can redistribute it and/or modify it
162 under the terms of the GNU General Public License as published by the Free
163 Software Foundation; either version 2 of the License, or (at your option)
164 any later version.
165  
166 =cut
167  
168 1;
« blib/lib/Test/CPAN/Health/Check/DuplicateCode.pm ↑ Summary blib/lib/Test/CPAN/Health/Check/Kwalitee.pm »