From 5e7a8c9dcd60f38b48e3cd20a6f76d16ee2ead44 Mon Sep 17 00:00:00 2001 From: steve Date: Thu, 22 Jun 2006 14:52:58 +0000 Subject: [PATCH] 2006-06-22 14:52:58 by steve New test - make sure that every command line argument which is offered on the POD text, output with --help, is actually processed. --- tests/getopt.t | 132 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 tests/getopt.t diff --git a/tests/getopt.t b/tests/getopt.t new file mode 100644 index 0000000..dfe2e11 --- /dev/null +++ b/tests/getopt.t @@ -0,0 +1,132 @@ +#!/usr/bin/perl -w +# +# Test that every perl script accepts and processes each of the options +# documented in its POD. +# +# Cute test :) +# +# Steve +# -- +# $Id: getopt.t,v 1.1 2006-06-22 14:52:58 steve Exp $ + + +use strict; +use File::Find; +use Test::More qw( no_plan ); + + +# +# Test each file +# +foreach my $file ( sort( glob "./bin/*-*" ) ) +{ + testFile( $file ); +} + + +# +# Check a file. +# +# If this is a perl file then call "perl -c $name", otherwise +# return +# +sub testFile +{ + my ($file ) = (@_); + is( -e $file, 1, "File exists: $file" ); + is( -x $file, 1, "File is executable" ); + + # + # Run the file with "--help" and capture the output. + # + my $output = `$file --help`; + + # + # Parse out the options we accept + # + my @documented = (); + + foreach my $line ( split( /\n/, $output ) ) + { + if ( $line =~ /[ \t]*--([a-z-]+)/ ) + { + push @documented, $1; + } + } + + # + # Test we got some options + # + ok( $#documented > 1, "We found some options documented." ); + + # + # Now read the input file. + # + open( IN, "<", $file ) or die "Failed to open file for reading $file - $!"; + my @LINES = ; + close( IN ); + + # + # Options accepted + # + my %accepted; + + # + # Do minimal parsing to find the options we process with + # Getopt::Long; + # + my $complete = join( "\n", @LINES ); + if ( $complete =~ /GetOptions\(([^\)]+)\)/mi ) + { + my $opt = $1; + + # + # Process each one. + # + foreach my $o ( split( /\n/, $opt ) ) + { + # + # Strip trailing comments. + # + if ( $o =~ /([^#]+)#/ ) + { + $o = $1; + } + + # + # Remove "" from around it. + # + if ( $o =~ /"([^"]+)"/ ) + { + $o = $1; + } + + # + # Discard anything after "=", or " " + # + if ( $o =~ /(.*)[ \t=]+(.*)/ ) + { + $o = $1; + } + + # + # Now avoid blank lines. + # + next if ( $o =~ /^[ \t]*$/ ); + + + # + # Phew. Now we're done. + # + $accepted{$o} = 1; + } + } + + # + # Now we want to find an option that is not documented. + # + foreach my $argument ( @documented ) + { + is( $accepted{$argument}, 1, "Option '--$argument' accepted" ); + } +}