2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00
contrib-munin/t/test.t

120 lines
3.2 KiB
Perl
Raw Normal View History

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 {
my ( $dev, $ino, $mode, $nlink, $uid, $gid, $interpreter );
( ( $dev, $ino, $mode, $nlink, $uid, $gid ) = lstat($_) )
&& -f _
&& ( $interpreter = hashbang("$_") )
&& ++$num_plugins
&& process_file( $_, $name, $interpreter );
}
File::Find::find( { wanted => \&wanted }, 'plugins' );
sub hashbang {
my ($filename) = @_;
open my $file, '<', $filename;
my $firstline = <$file>;
close $file;
$firstline =~ m{ ^\#! # hashbang
\s* # optional space
(?:/usr/bin/env\s+)? # optional /usr/bin/env
(?<interpreter>\S+) # interpreter
}xm;
return $+{interpreter};
}
sub process_file {
my ( $file, $filename, $interpreter ) = @_;
use v5.10.1;
if ( $interpreter =~ m{/bin/sh} ) {
subtest $filename => sub {
plan tests => 2;
ok( check_file_with( [ 'sh', '-n', $file ] ), "sh syntax check" );
ok( check_file_with( [ 'checkbashisms', $file ] ),
"checkbashisms" );
};
}
2014-10-04 19:32:33 +02:00
elsif ( $interpreter =~ m{/bin/ksh} ) {
ok( check_file_with( [ 'ksh', '-n', $file ] ),
$filename . " ksh syntax check" );
}
elsif ( $interpreter =~ m{bash} ) {
2014-10-04 15:38:36 +02:00
ok( check_file_with( [ 'bash', '-n', $file ] ),
$filename . " bash syntax check" );
}
elsif ( $interpreter =~ m{perl} ) {
ok( check_file_with( [ 'perl', '-cw', $file ] ),
$filename . " perl syntax check" );
}
elsif ( $interpreter =~ m{python} ) {
ok( check_file_with(
2014-10-04 20:17:52 +02:00
[ 'pylint', '--rcfile=/dev/null',
'--errors-only', '--report=no',
$file
]
2014-10-04 15:38:36 +02:00
),
$filename . " python syntax check"
);
}
elsif ( $interpreter =~ m{php} ) {
ok( check_file_with( [ 'php', '-l', $file ] ),
$filename . " php syntax check" );
}
elsif ( $interpreter =~ m{j?ruby} ) {
ok( check_file_with( [ 'ruby', '-cw', $file ] ),
$filename . " ruby syntax check" );
}
elsif ( $interpreter =~ m{gawk} ) {
ok( check_file_with(
2014-10-04 20:16:59 +02:00
[ 'gawk', '--source', 'BEGIN { exit(0) } END { exit(0) }',
2014-10-04 15:38:36 +02:00
'--file', $file
]
),
$filename . " gawk syntax check"
);
}
else {
fail( $filename . " unknown interpreter " . $interpreter );
}
}
sub check_file_with {
my ($check_command) = @_;
my ( $stdout, $stderr, $exit ) = capture {
system( @{$check_command} );
};
if ( $exit == 0 ) {
return 1;
}
else {
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
return;
}
}
done_testing($num_plugins);