-
Notifications
You must be signed in to change notification settings - Fork 0
/
efm_perl.pl
executable file
·402 lines (277 loc) · 8.87 KB
/
efm_perl.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env perl
# # efm debug
# # efm skipall
# # efm modules CGI
# # efm skip unused pedantic-maybe_const lint-context
#---------------------------------------------------
# Any line that begins with no spaces and looks like
#
# ## efm skip circular unused lint
#
# will skip the listed tests. Currently known tests are stored in the @tests
# variable in the get_directives sub.
#
# If you have
#
# ## efm skipall
#
# then all tests will be skipped.
#---------------------------------------------------
# Any line that begins with no spaces and looks like
#
# ## efm debug
#
# will turn on debugging messages. Anything after debug
# will be ignored.
#---------------------------------------------------
# Any line that begins with no spaces and looks like
#
# ## efm modules
#
# will add the listed modules to the -c call. E.g.,
#
# ## efm modules Module::Name
#
# will add -MModule::Name to the -c call.
#---------------------------------------------------
# Any line that begins with no spaces and looks like
#
# ## efm libs
#
# will add the listed directories to the -c call. E.g.,
#
# ## efm includes path/to/lib
#
# will add -I path/to/lib to the -c call.
# Thanks to:
#
# http://blogs.perl.org/users/ovid/2011/01/warningsunused-versus-ppi.html for
# the 'warnings::unused' trick.
#
# https://github.com/Ovid/DB--Color.git for the 'circular::require' trick
use strict;
use warnings FATAL => 'all';
use autodie;
use Cwd;
use IPC::Run3::Simple;
use Try::Tiny;
die "Too many arguments!\n"
if scalar @ARGV > 1;
my $file = shift @ARGV
or die "No filename to check!\n";
known_tests()
if $file eq 'dumptests';
die "$file is not a file\n"
unless -f $file;
die "$file is not readable\n"
unless -r _;
our $DEBUG = 0;
##############################################################################
my ( $includes, $modules ) = get_directives();
my $cmd = [ perl(), @$includes, @$modules, '-c', $file ];
run3( {
'cmd' => $cmd,
'stdout' => \my $err,
'stderr' => \my @errors,
'syserr' => \my $syserr,
} );
$DEBUG && warn "@$cmd at efm_perl.pl line 1\n";
my $skip = skip_error_messages();
for my $error ( @errors ) {
# Skip any errors reported that aren't in the file we're checking.
next unless $error =~ /$file/;
# Skip any errors we want to ignore.
next if $error =~ /$skip/;
print "$error\n";
}
##############################################################################
# Make external call
sub call {
my ( $cmd ) = @_;
my ( $result, $err, $syserr ) = run3( $cmd );
my $msg;
$msg .= "Unexpected error: $syserr\n"
if $syserr;
$msg .= "Error: $err\n"
if $err;
die $msg if $msg;
return $result;
} ## end sub call
# Grab perldoc location.
{
my $perldoc;
sub perldoc { return $perldoc ||= call( [qw( which perldoc )] ) }
}
# Grab perl to be used based on environment.
{
my $perl;
sub perl { return $perl ||= call( [qw( which perl )] ) }
}
# Check if module is installed
sub module_installed {
my ( $module ) = @_;
try {
my $found_module = call( [ perldoc(), '-l', $module ] );
$DEBUG && warn "$module is installed ($found_module)";
return 1;
}
catch {
if ( /No documentation/ ) {
$DEBUG && warn "$module is not installed";
return;
}
die "$_\n";
}
} ## end sub module_installed
# Error messages to be skipped.
sub skip_error_messages {
return join q{|}, ( '"DB::single" used only once: possible typo', 'BEGIN failed--compilation aborted', );
}
# Display known tests.
sub known_tests {
my @tests = keys %{ tests() };
my @lint_tests = keys %{ lint_tests() };
my @pedantic_tests = keys %{ pedantic_tests() };
print "$_\n" for sort ( @tests, @lint_tests, @pedantic_tests );
exit;
}
##############################################################################
sub tests {
return {
circular => '-circular::require',
indirect => '-indirect',
lint => 'B::Lint',
lintsubs => 'B::LintSubs',
method => 'warnings::method',
pedantic => 'warnings::pedantic',
regexrecompile => 'warnings::regex::compile',
uninit => 'uninit',
unused => 'warnings::unused',
};
}
##############################################################################
sub lint_tests {
my @lint_tests = qw(
bare-subs context dollar-underscore implicit-read implicit-write
magic-diamond oo private-names regexp-variables undefined-subs
);
@lint_tests = map { "lint-$_" } @lint_tests;
my %lint_test; @lint_test{ @lint_tests } = ( 1 ) x @lint_tests;
return \%lint_test;
}
sub check_lint_tests {
my ( $lint_module, $tests ) = @_;
if ( module_installed( $lint_module ) ) {
# If B::LintStrictOO is not installed, remove lint-oo from available tests.
if ( exists $tests->{ 'lint-oo' } ) {
delete $tests->{ 'lint-oo' }
unless module_installed( 'B::LintStrictOO' );
}
my $use_oo = delete $tests->{ 'lint-oo' };
my $remaining_tests = scalar keys %$tests;
my $total_tests = scalar keys %{ lint_tests() };
$total_tests--; # Account for having removed lint-oo.
# If all tests remain in $tests, then use all.
$tests = { all => 1 }
if $remaining_tests == $total_tests;
# If no tests remain in $tests, then use none.
$tests = { none => 1 }
if $remaining_tests <= 1;
$tests->{ 'lint-oo' } = 1
if $use_oo;
my $options = join ',', map { s/lint-//; $_ } sort keys %$tests;
return "O=Lint,$options";
} ## end if ( module_installed...)
} ## end sub check_lint_tests
##############################################################################
sub pedantic_tests {
my @pedantic_tests = qw(
maybe_const ref_assignment sort_prototype void_close void_grep void_print
);
@pedantic_tests = map { "pedantic-$_" } @pedantic_tests;
my %pedantic_test; @pedantic_test{ @pedantic_tests } = ( 1 ) x @pedantic_tests;
return \%pedantic_test;
}
sub check_pedantic_tests {
my ( $module, $tests ) = @_;
if ( module_installed( $module ) ) {
# If all tests remain in %pedantic_test then no need for explicitly
# calling tests.
# If no tests remain in %pedantic_test then we can skip this test
# entirely.
my $remaining_tests = scalar keys %$tests;
my $total_tests = scalar keys %{ pedantic_tests() };
return $module
if $remaining_tests == $total_tests;
if ( $remaining_tests != 0 ) {
my $options = join ',', map { s/pedantic-//; $_ } sort keys %$tests;
return "$module=$options";
}
} ## end if ( module_installed...)
return;
} ## end sub check_pedantic_tests
##############################################################################
##############################################################################
# Grab custom instructions for this file from the file.
sub get_directives {
my $efm = '';
try {
$efm = call( [ 'grep', '^## efm ', $file ] );
}
catch {
/Error:/ ? $DEBUG && "No efm lines found.\n" : do { die "$_\n" };
};
my $test = tests();
my $lint_test = lint_tests();
my $pedantic_test = pedantic_tests();
# Naive attempt to include default working module libraries.
my @includes = qw( ./lib ./lib/auto );
my ( @modules, @check_modules );
for my $line ( split /\n+/, $efm ) {
if ( $line =~ /^## efm\s+debug/ ) {
$DEBUG++;
} elsif ( $line =~ s/^## efm\s+skipall// ) {
$test = {};
} elsif ( $line =~ s/^## efm\s+skip\s+// ) {
for my $skip ( split /\s+/, $line ) {
delete $test->{ $skip };
delete $lint_test->{ $skip };
delete $pedantic_test->{ $skip };
}
} elsif ( $line =~ s/^## efm\s+modules\s+// ) {
push @check_modules, split /\s+/, $line;
} elsif ( $line =~ s/^## efm\s+includes\s+// ) {
push @includes, split /\s+/, $line;
}
} ## end for my $line ( split...)
# Special handling for lint test ...
if ( exists $test->{ lint } ) {
my $lint = check_lint_tests( delete $test->{ lint }, $lint_test );
push @modules, $lint
if defined $lint;
} ## end if ( exists $test...)
# Special handling for pedantics tests ..
if ( exists $test->{ pedantic } ) {
my $pedantic = check_pedantic_tests( delete $test->{ pedantic }, $pedantic_test );
push @modules, $pedantic
if defined $pedantic;
}
# uninit is not included in 5.10 and later
delete $test->{ uninit }
if exists $test->{ uninit } && $] >= 5.010;
# Check if test modules are installed ...
for my $m ( sort keys %$test ) {
my $module = $test->{ $m };
( my $check_module = $module ) =~ s/^-?//;
push @modules, $module
if module_installed( $check_module );
}
# Check if requested modules are installed ...
for my $module ( @check_modules ) {
push @modules, $module
if module_installed( $module );
}
@includes = map { ( '-I', $_ ) } @includes;
@modules = map { "-M$_" } @modules;
return \@includes, \@modules;
} ## end sub get_directives