From 0e41613e9f547ab97365329572555936e84f77b2 Mon Sep 17 00:00:00 2001 From: Gavin Carr Date: Thu, 8 Feb 2007 05:18:06 +0000 Subject: Add initial --default-opts implementation to Nagios::Plugin::Getopt (II). git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1617 f882894a-f735-0410-b71e-b25c423dba1c --- t/Nagios-Plugin-Getopt-03.t | 99 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 t/Nagios-Plugin-Getopt-03.t (limited to 't/Nagios-Plugin-Getopt-03.t') diff --git a/t/Nagios-Plugin-Getopt-03.t b/t/Nagios-Plugin-Getopt-03.t new file mode 100644 index 0000000..9dc39da --- /dev/null +++ b/t/Nagios-Plugin-Getopt-03.t @@ -0,0 +1,99 @@ +# Nagios::Plugin::Getopt --default-opts tests + +use strict; +use File::Spec; +use File::Basename; +use IO::File; + +use Test::More qw(no_plan); +BEGIN { use_ok('Nagios::Plugin::Getopt') }; + +my $tdir = 'npg03'; +if (! -d $tdir) { + my $ttdir = File::Spec->catdir('t', $tdir); + die "missing '$tdir' directory\n" unless -d $ttdir; + $tdir = $ttdir; +} + +# Load expected files +my %EXPECTED = (); +for my $efile (glob File::Spec->catfile($tdir, 'expected', '*')) { + my $fh = IO::File->new($efile, 'r') or die "Cannot open input file '$efile': $!"; + if (my $cmd = $fh->getline()) { # First line only! + chomp $cmd; + $cmd =~ s/^\s+//; + $cmd =~ s/\s+$//; + $EXPECTED{ basename($efile) } = $cmd; + } +} + +$Nagios::Plugin::Getopt::DEFAULT_CONFIG_FILE = File::Spec->catfile($tdir, 'plugins.cfg'); + +my %PARAM = ( + version => '0.01', + blurb => 'This plugin tests various stuff.', + usage => "Usage: %s -H -w + -c ", +); + +sub ng_setup +{ + my $arg = shift; + + # Instantiate object + my $ng = Nagios::Plugin::Getopt->new(%PARAM); + + if (ref $arg eq 'ARRAY' && @$arg) { + $ng->arg(%$_) foreach @$arg; + } + + return $ng; +} + +# Setup our Nagios::Plugin::Getopt object +my $ng; +my $arg = [ + { spec => 'S', help => '-S' }, + { spec => 'H=s', help => '-H' }, + { spec => 'p=s@', help => '-p' }, + { spec => 'username|u=s', help => '--username' }, + { spec => 'password=s', help => '--password' }, + { spec => 'critical=i', help => '--critical' }, + { spec => 'warning=i', help => '--warning' }, + { spec => 'expect=s', help => '--expect' }, +]; + +my %SKIP = map { $_ => 1 } qw(05_singlechar1 07_singlechar3); + +# Process all test cases in $tdir/input +my $glob = $ARGV[0] || '*'; +for my $infile (glob File::Spec->catfile($tdir, 'input', $glob)) { + $ng = ng_setup($arg); + + my $fh = IO::File->new($infile, 'r') or die "Cannot open input file '$infile': $!"; + $infile = basename($infile); + + if (my $cmd = $fh->getline()) { # First line only! + $cmd =~ s/^\s+//; + my ($plugin, @args) = split /\s+/, $cmd; + + # Fake out the plugin name + $ng->{_attr}->{plugin} = $plugin; + + # Parse the options + SKIP: { + skip "Still discussing how overrides with multiple arguments should work ...", 1 if $SKIP{$infile}; + + @ARGV = @args; + eval { $ng->getopts }; + if ($@) { + chomp $@; + ok($infile =~ m/_dies?$/, "$infile ($@)"); + } + else { + is($plugin . ' ' . $ng->_cmdline, $EXPECTED{$infile}, $infile); + } + } + } +} + -- cgit v1.2.3-74-g34f1