2014-10-04 15:38:36 +02:00
|
|
|
# -*- perl -*-
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Test::More;
|
|
|
|
use File::Find ();
|
|
|
|
use Capture::Tiny ':all';
|
|
|
|
|
|
|
|
use vars qw/*name *dir *prune/;
|
|
|
|
*name = *File::Find::name;
|
|
|
|
*dir = *File::Find::dir;
|
|
|
|
*prune = *File::Find::prune;
|
|
|
|
my $num_plugins = 0;
|
|
|
|
|
|
|
|
sub wanted {
|
2014-10-05 00:17:23 +02:00
|
|
|
my ( $dev, $ino, $mode, $nlink, $uid, $gid, $interpreter, $arguments );
|
2014-10-04 15:38:36 +02:00
|
|
|
|
|
|
|
( ( $dev, $ino, $mode, $nlink, $uid, $gid ) = lstat($_) )
|
|
|
|
&& -f _
|
2014-10-05 00:17:23 +02:00
|
|
|
&& ( ( $interpreter, $arguments ) = hashbang("$_") )
|
|
|
|
&& ($interpreter)
|
2014-10-04 15:38:36 +02:00
|
|
|
&& ++$num_plugins
|
2014-10-05 00:17:23 +02:00
|
|
|
&& process_file( $_, $name, $interpreter, $arguments );
|
2014-10-04 15:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
File::Find::find( { wanted => \&wanted }, 'plugins' );
|
|
|
|
|
|
|
|
sub hashbang {
|
|
|
|
my ($filename) = @_;
|
|
|
|
open my $file, '<', $filename;
|
|
|
|
my $firstline = <$file>;
|
|
|
|
close $file;
|
|
|
|
|
2014-10-05 00:17:23 +02:00
|
|
|
$firstline =~ m{ ^\#! # hashbang
|
|
|
|
\s* # optional space
|
|
|
|
(?:/usr/bin/env\s+)? # optional /usr/bin/env
|
|
|
|
(?<interpreter>\S+) # interpreter
|
|
|
|
(?:\s+
|
|
|
|
(?<arguments>[^\n]*) # optional interpreter arguments
|
|
|
|
)?
|
|
|
|
}xms;
|
2014-10-04 15:38:36 +02:00
|
|
|
|
2014-10-05 11:23:53 +02:00
|
|
|
return ( $+{interpreter}, $+{arguments} );
|
2014-10-04 15:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub process_file {
|
2014-10-05 00:17:23 +02:00
|
|
|
my ( $file, $filename, $interpreter, $arguments ) = @_;
|
2014-10-04 15:38:36 +02:00
|
|
|
use v5.10.1;
|
|
|
|
|
2018-03-08 23:33:33 +01:00
|
|
|
if ( ! -x $file ) {
|
|
|
|
# missing executable flag
|
|
|
|
diag(
|
|
|
|
sprintf("\nFile '%s' lacks executable permission bits. Maybe try 'chmod +x $file'?\n",
|
|
|
|
$file)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
elsif ( $interpreter =~ m{/bin/sh} ) {
|
2014-10-04 15:38:36 +02:00
|
|
|
subtest $filename => sub {
|
|
|
|
plan tests => 2;
|
2014-10-04 23:52:40 +02:00
|
|
|
run_check(
|
|
|
|
{ command => [ 'sh', '-n', $file ],
|
|
|
|
description => 'sh syntax check'
|
|
|
|
}
|
|
|
|
);
|
2018-03-08 04:17:18 +01:00
|
|
|
my $checkbashisms_location = `command -v checkbashisms 2>/dev/null`;
|
|
|
|
chomp($checkbashisms_location);
|
|
|
|
my $command;
|
|
|
|
if ($checkbashisms_location ne "") {
|
|
|
|
# monkey-patch "checkbashisms" in order to allow "command -v"
|
|
|
|
# see https://unix.stackexchange.com/a/85250: "command -v" vs. which/hash/...
|
|
|
|
# see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=733511
|
|
|
|
my $run_modified_checkbashisms = q/sed 's#command\\\s+-\[\^p\]#command\s+-[^pvV]#'/
|
|
|
|
. " '$checkbashisms_location' | perl - '$file'";
|
|
|
|
$command = [ 'sh', '-c', $run_modified_checkbashisms ];
|
|
|
|
} else {
|
|
|
|
# make sure that the non-confusing "checkbashisms not found" message is displayed
|
|
|
|
$command = [ 'checkbashisms', $file ];
|
|
|
|
}
|
2014-10-04 23:52:40 +02:00
|
|
|
run_check(
|
2018-03-08 04:17:18 +01:00
|
|
|
{ command => $command,
|
2014-10-04 23:52:40 +02:00
|
|
|
description => 'checkbashisms'
|
|
|
|
}
|
|
|
|
);
|
2014-10-04 15:38:36 +02:00
|
|
|
};
|
|
|
|
}
|
2014-10-04 19:32:33 +02:00
|
|
|
elsif ( $interpreter =~ m{/bin/ksh} ) {
|
2018-03-09 05:33:37 +01:00
|
|
|
subtest $filename => sub {
|
|
|
|
plan tests => 2;
|
|
|
|
run_check(
|
|
|
|
{ command => [ 'ksh', '-n', $file ],
|
|
|
|
description => 'ksh syntax check',
|
|
|
|
filename => $filename
|
|
|
|
}
|
|
|
|
);
|
|
|
|
run_check(
|
|
|
|
{ command => [ 'shellcheck', $file ],
|
|
|
|
description => 'shellcheck'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-10-17 02:21:16 +02:00
|
|
|
}
|
2014-10-04 19:43:08 +02:00
|
|
|
elsif ( $interpreter =~ m{bash} ) {
|
2014-10-04 23:52:40 +02:00
|
|
|
run_check(
|
|
|
|
{ command => [ 'bash', '-n', $file ],
|
|
|
|
description => 'bash syntax check',
|
|
|
|
filename => $filename
|
|
|
|
}
|
|
|
|
);
|
2014-10-04 15:38:36 +02:00
|
|
|
}
|
2015-09-26 17:25:54 +02:00
|
|
|
elsif ( $interpreter =~ m{/bin/zsh} ) {
|
|
|
|
run_check(
|
|
|
|
{ command => [ 'zsh', '-n', $file ],
|
|
|
|
description => 'zsh syntax check',
|
|
|
|
filename => $filename
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2014-10-04 15:38:36 +02:00
|
|
|
elsif ( $interpreter =~ m{perl} ) {
|
2014-10-05 00:17:23 +02:00
|
|
|
my $command;
|
2014-10-05 11:23:53 +02:00
|
|
|
if ( $arguments =~ m{-.*T}mx ) {
|
2014-10-05 00:17:23 +02:00
|
|
|
$command = [ 'perl', '-cwT', $file ];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$command = [ 'perl', '-cw', $file ];
|
|
|
|
}
|
2014-10-04 23:52:40 +02:00
|
|
|
run_check(
|
2014-10-05 00:17:23 +02:00
|
|
|
{ command => $command,
|
2014-10-04 23:52:40 +02:00
|
|
|
description => 'perl syntax check',
|
|
|
|
filename => $filename
|
|
|
|
}
|
|
|
|
);
|
2014-10-04 15:38:36 +02:00
|
|
|
}
|
2015-10-30 06:09:12 +01:00
|
|
|
elsif ( $interpreter =~ m{python3} ) {
|
|
|
|
run_check(
|
|
|
|
{ command => [ 'python3', '-m', 'py_compile', $file ],
|
|
|
|
description => 'python3 compile',
|
|
|
|
filename => $filename
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2014-10-04 15:38:36 +02:00
|
|
|
elsif ( $interpreter =~ m{python} ) {
|
2014-10-05 11:24:14 +02:00
|
|
|
run_check(
|
|
|
|
{ command => [ 'python', '-m', 'py_compile', $file ],
|
|
|
|
description => 'python compile',
|
|
|
|
filename => $filename
|
|
|
|
}
|
|
|
|
);
|
2014-10-04 15:38:36 +02:00
|
|
|
}
|
|
|
|
elsif ( $interpreter =~ m{php} ) {
|
2014-10-04 23:52:40 +02:00
|
|
|
run_check(
|
|
|
|
{ command => [ 'php', '-l', $file ],
|
|
|
|
description => 'php syntax check',
|
|
|
|
filename => $filename
|
|
|
|
}
|
|
|
|
);
|
2014-10-04 15:38:36 +02:00
|
|
|
}
|
|
|
|
elsif ( $interpreter =~ m{j?ruby} ) {
|
2014-10-04 23:52:40 +02:00
|
|
|
run_check(
|
|
|
|
{ command => [ 'ruby', '-cw', $file ],
|
|
|
|
description => 'ruby syntax check',
|
|
|
|
filename => $filename
|
|
|
|
}
|
|
|
|
);
|
2014-10-04 15:38:36 +02:00
|
|
|
}
|
|
|
|
elsif ( $interpreter =~ m{gawk} ) {
|
2014-10-04 23:52:40 +02:00
|
|
|
run_check(
|
|
|
|
{ command => [
|
|
|
|
'gawk', '--source', 'BEGIN { exit(0) } END { exit(0) }',
|
2014-10-04 15:38:36 +02:00
|
|
|
'--file', $file
|
2014-10-04 23:52:40 +02:00
|
|
|
],
|
|
|
|
description => 'gawk syntax check',
|
|
|
|
filename => $filename
|
|
|
|
}
|
2014-10-04 15:38:36 +02:00
|
|
|
);
|
|
|
|
}
|
2014-10-04 21:49:45 +02:00
|
|
|
elsif ( $interpreter =~ m{expect} ) {
|
2014-10-04 23:52:40 +02:00
|
|
|
SKIP: {
|
|
|
|
skip 'no idea how to check expect scripts', 1;
|
|
|
|
pass("No pretending everything is ok");
|
|
|
|
}
|
2014-10-04 21:49:45 +02:00
|
|
|
}
|
2014-10-04 15:38:36 +02:00
|
|
|
else {
|
|
|
|
fail( $filename . " unknown interpreter " . $interpreter );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-04 23:52:40 +02:00
|
|
|
sub run_check {
|
|
|
|
my ($args) = @_;
|
|
|
|
my $check_command = $args->{command};
|
|
|
|
my $description = $args->{description};
|
|
|
|
my $filename = $args->{filename};
|
|
|
|
|
|
|
|
my $message;
|
|
|
|
|
|
|
|
if ($filename) {
|
|
|
|
$message = sprintf( '%s: %s', $filename, $description );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$message = $description;
|
|
|
|
}
|
|
|
|
|
2014-10-04 15:38:36 +02:00
|
|
|
my ( $stdout, $stderr, $exit ) = capture {
|
|
|
|
system( @{$check_command} );
|
|
|
|
};
|
2014-10-04 23:52:40 +02:00
|
|
|
|
|
|
|
ok( ( $exit == 0 ), $message );
|
|
|
|
|
|
|
|
if ($exit) {
|
2014-10-04 20:17:52 +02:00
|
|
|
diag(
|
|
|
|
sprintf(
|
|
|
|
"\nCommand: %s\n\nSTDOUT:\n\n%s\n\nSTDERR:\n\n%s\n\n",
|
|
|
|
join( " ", @{$check_command} ),
|
|
|
|
$stdout, $stderr
|
|
|
|
)
|
|
|
|
);
|
2014-10-04 15:38:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done_testing($num_plugins);
|