I’ve found that using systems like cucumber to be quite handy when testing software, and I was hoping for a lightweight Perl solution that I could run on my UNIX systems without having to install a bunch of dependancies.
Enter testadmin.pl.
The files are stored in this github gist with a couple of simple examples. One tests if a user is logged in:
123
As a silly system administrator
When I run who
Then user pblair must be logged in
In order to satisfy this test, we look in w.step
Example step file - w.step
1234567891011121314151617
our@output;Whenqr{I run (\S.*)}=>sub {my($regex,$capture)=@_;@output=`$capture->[0]`;chompfor@output;};Thenqr{user (\S+) must be logged in}=>sub {my($regex,$capture)=@_;my$user=$capture->[0];my@l=grep{/^$user/}@output;dieifscalar@l==0;scalar@l;};
We see that it captures the name of a user (supplied by the feature file) and checks the @output array for the given username. It returns the number of found users. The calling function will return a warning if we return 0, so as long as there’s 1 user, then this function will cause the hilighting of the definition file to be displayed as green.
When writing Perl modules, it makes sense to pass the heavy lifting of the admin work off to established tools, namely MakeMaker, dh-make-perl and debuild.
I’ve written a couple of handler scripts that facilitate the auto building of packages from upstream HTTP source.
#!/usr/bin/perlusestrict;#my $regex = 'http://slowpoke.internal.tucows.com/~pblair/perl/Tucows-OPS-Cloud-(\d+(?:\.\d+)).tar.gz';my$WGET=`which wget`||die'Must install either wget for this to work';chomp$WGET;die'wget must be installed and in the PATH'if$WGET=~ /^\s*$/;$WGET.=' -O - 2>/dev/null';@ARGV=map{/^http/?"$WGET $_ |":$_}@ARGV;while(<>){chomp;s/#.*//g;nextif/^\s*$/;nextunless/^(\S+?):\/\//;my$regex=$_;my$versions={};my@a=reverse(split( /\//,$regex));my$r=shift(@a);my$url=join("/",reverse(@a));open(FD,"$WGET -O - $url 2>/dev/null |");while(<FD>){chomp;if( /a href="(.*?)"/){my$link=$1;if($1=~ /$r/){$versions->{$1}=$link;}}}my@latest=sortkeys%$versions;print$url."/".$versions->{$latest[-1]}."\n";}
Which in turn requires that a regex file, or string be presented to it that looks like:
I’ve just written a Perl CPAN module called Net::Domain::Regex that can be used to parse free form text and extract a series of domain name matches that are broken down into their components.
Parse an email to discover all domains within the headers and body
123456789101112131415161718192021222324
#!/usr/bin/perl -wusestrict;useNet::Domain::Regex;useData::Dumper;my$r=Net::Domain::Regex->new;my$email=do{local$/;openFD,"</tmp/google.email";<FD>;};# Filter out some of the domains from the headers etc.my@results=grep{!($_->{tld}eq'com'and$_->{domain}=~ /google|tucows|hostedemail/)}$r->match($email);# Map it so that we get the FQDN and the Domain namemy$r={};for(@results){print$_->{match},"\n";$r->{$_->{domain}.".".$_->{tld}}->{$_->{match}}++;}print"Parsed Domains:",Dumper($r),"\n";
This will result in the following:
Parse an email to discover all domains within the headers and body
I’ve maintained a blog at petermblair.com which has grown to include a lot of programming examples, snippets, etc., but I felt that I wanted to have a place dedicated to programming without having to trim out all of the non programming stuff from my personal site (which I believe should remain to service my personal ‘brand’). As such, I’m starting a new blog here which will attempt to be strictly programming: Perl, Python, C++, etc.