root / t / test.t @ 92e7aaf8
Historique | Voir | Annoter | Télécharger (3,04 ko)
| 1 |
# -*- perl -*- |
|---|---|
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More; |
| 7 |
use File::Find (); |
| 8 |
use Capture::Tiny ':all'; |
| 9 |
|
| 10 |
use vars qw/*name *dir *prune/; |
| 11 |
*name = *File::Find::name; |
| 12 |
*dir = *File::Find::dir; |
| 13 |
*prune = *File::Find::prune; |
| 14 |
my $num_plugins = 0; |
| 15 |
|
| 16 |
sub wanted {
|
| 17 |
my ( $dev, $ino, $mode, $nlink, $uid, $gid, $interpreter ); |
| 18 |
|
| 19 |
( ( $dev, $ino, $mode, $nlink, $uid, $gid ) = lstat($_) ) |
| 20 |
&& -f _ |
| 21 |
&& ( $interpreter = hashbang("$_") )
|
| 22 |
&& ++$num_plugins |
| 23 |
&& process_file( $_, $name, $interpreter ); |
| 24 |
} |
| 25 |
|
| 26 |
File::Find::find( { wanted => \&wanted }, 'plugins' );
|
| 27 |
|
| 28 |
sub hashbang {
|
| 29 |
my ($filename) = @_; |
| 30 |
open my $file, '<', $filename; |
| 31 |
my $firstline = <$file>; |
| 32 |
close $file; |
| 33 |
|
| 34 |
$firstline =~ m{ ^\#! # hashbang
|
| 35 |
\s* # optional space |
| 36 |
(?:/usr/bin/env\s+)? # optional /usr/bin/env |
| 37 |
(?<interpreter>\S+) # interpreter |
| 38 |
}xm; |
| 39 |
|
| 40 |
return $+{interpreter};
|
| 41 |
} |
| 42 |
|
| 43 |
sub process_file {
|
| 44 |
my ( $file, $filename, $interpreter ) = @_; |
| 45 |
use v5.10.1; |
| 46 |
|
| 47 |
if ( $interpreter =~ m{/bin/sh} ) {
|
| 48 |
subtest $filename => sub {
|
| 49 |
plan tests => 2; |
| 50 |
ok( check_file_with( [ 'sh', '-n', $file ] ), "sh syntax check" ); |
| 51 |
ok( check_file_with( [ 'checkbashisms', $file ] ), |
| 52 |
"checkbashisms" ); |
| 53 |
}; |
| 54 |
} |
| 55 |
elsif ( $interpreter =~ m{/bin/ksh} ) {
|
| 56 |
ok( check_file_with( [ 'ksh', '-n', $file ] ), |
| 57 |
$filename . " ksh syntax check" ); |
| 58 |
} |
| 59 |
elsif ( $interpreter =~ m{bash} ) {
|
| 60 |
ok( check_file_with( [ 'bash', '-n', $file ] ), |
| 61 |
$filename . " bash syntax check" ); |
| 62 |
} |
| 63 |
elsif ( $interpreter =~ m{perl} ) {
|
| 64 |
ok( check_file_with( [ 'perl', '-cw', $file ] ), |
| 65 |
$filename . " perl syntax check" ); |
| 66 |
} |
| 67 |
elsif ( $interpreter =~ m{python} ) {
|
| 68 |
ok( check_file_with( |
| 69 |
[ 'pylint', '--rcfile=/dev/null', '--errors-only', '--report=no', $file ] |
| 70 |
), |
| 71 |
$filename . " python syntax check" |
| 72 |
); |
| 73 |
} |
| 74 |
elsif ( $interpreter =~ m{php} ) {
|
| 75 |
ok( check_file_with( [ 'php', '-l', $file ] ), |
| 76 |
$filename . " php syntax check" ); |
| 77 |
} |
| 78 |
elsif ( $interpreter =~ m{j?ruby} ) {
|
| 79 |
ok( check_file_with( [ 'ruby', '-cw', $file ] ), |
| 80 |
$filename . " ruby syntax check" ); |
| 81 |
} |
| 82 |
elsif ( $interpreter =~ m{gawk} ) {
|
| 83 |
ok( check_file_with( |
| 84 |
[ 'gawk', '--source', "'BEGIN { exit(0) } END { exit(0) }'",
|
| 85 |
'--file', $file |
| 86 |
] |
| 87 |
), |
| 88 |
$filename . " gawk syntax check" |
| 89 |
); |
| 90 |
} |
| 91 |
else {
|
| 92 |
fail( $filename . " unknown interpreter " . $interpreter ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
sub check_file_with {
|
| 97 |
my ($check_command) = @_; |
| 98 |
my ( $stdout, $stderr, $exit ) = capture {
|
| 99 |
system( @{$check_command} );
|
| 100 |
}; |
| 101 |
if ( $exit == 0 ) {
|
| 102 |
return 1; |
| 103 |
} |
| 104 |
else {
|
| 105 |
diag(sprintf("\nCommand: %s\n\nSTDOUT:\n\n%s\n\nSTDERR:\n\n%s\n\n", join(" ", @{$check_command}), $stdout, $stderr));
|
| 106 |
return; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
done_testing($num_plugins); |
