From 80cb37418a4c757b40354ddc57d8ef82736b46ff Mon Sep 17 00:00:00 2001 From: Stig Sandbeck Mathisen Date: Sat, 4 Oct 2014 15:38:36 +0200 Subject: [PATCH] Add automated tests --- .travis.yml | 15 ++++++++ t/test.t | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 .travis.yml create mode 100644 t/test.t diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..45098892 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +--- +language: perl +install: + - sudo apt-get install devscripts python ruby php5-cli gawk + # + # Modules used by test script + - cpanm --notest Test::More File::Find Capture::Tiny + # + # Modules used by plugins + - cpanm --notest Asterisk::AMI Cache::Memcached BerkeleyDB DBD::Pg Data::Dump Device::SerialPort FCGI::Client File::ReadBackwards File::Tail::Multi Graphics::ColorNames IPC::Run3 IPC::ShareLite Modern::Perl MooseX::POE Net::Telnet Net::Telnet::Cisco POE Proc::ProcessTable Redis Sys::Virt WWW::Mechanize::TreeBuilder nvidia::ml + # Modules used by plugins, but missing on cpan + # - Sun::Solaris::Kstat + # - VMware::VIRuntime + # - MythTV +script: "prove" diff --git a/t/test.t b/t/test.t new file mode 100644 index 00000000..2d02b413 --- /dev/null +++ b/t/test.t @@ -0,0 +1,107 @@ +# -*- 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 + (?\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" ); + }; + } + elsif ( $interpreter =~ m{/bin/bash} ) { + 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( + [ 'pylint', '--errors-only', '--report=no', $file ] + ), + $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( + [ 'gawk', '--source', "'BEGIN { exit(0) } END { exit(0) }'", + '--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 { + diag($stdout); + diag($stderr); + return; + } +} + +done_testing($num_plugins);