Re: Testing a Script Distributed with a Module

2005-07-12 Thread James E Keenan

James E Keenan wrote:


Michael G Schwern wrote:




Oh yeah, forgot about that.  Its not in your path so you have to give 
it the full path to the program.



The directories in blib have no relation to where the file came from.
Non-binary executables always go into blib/script.  Binary executables
go into blib/bin.


One other curiosum:  As a result of my Phalanx work, I've gotten in the 
habit of using File::Temp to create 'anonymous' directories and files in 
which to conduct testing.  I tried that here:


my $tdir = tempdir( CLEANUP = 1);
ok(chdir $tdir, 'changed to temp directory for testing');

but, once again, 'blib/' couldn't be located.  So I had to resort to 
manually constructing, populating and cleaning up a 'tmp/' directory at 
the same level as 'blib/' or 't/'.





What finally seems to be working is this:

my $cwd = cwd();
my $tdir;
{
$tdir = tempdir( CLEANUP = 1);
ok(chdir $tdir, 'changed to temp directory for testing');

ok(!
  system($^X -I$cwd/blib/lib $cwd/blib/script/modulemaker -Icn 
XYZ::ABC),

able to call modulemaker utility);
...

jimk


Re: Testing a Script Distributed with a Module

2005-07-09 Thread Michael G Schwern
On Fri, Jul 08, 2005 at 11:17:44PM -0400, James E Keenan wrote:
 One other curiosum:  As a result of my Phalanx work, I've gotten in the 
 habit of using File::Temp to create 'anonymous' directories and files in 
 which to conduct testing.  I tried that here:
 
 my $tdir = tempdir( CLEANUP = 1);
 ok(chdir $tdir, 'changed to temp directory for testing');
 
 but, once again, 'blib/' couldn't be located.  So I had to resort to 
 manually constructing, populating and cleaning up a 'tmp/' directory at 
 the same level as 'blib/' or 't/'.

Since you moved down a directory its ../blib.  To simplify things you're
best of using an absolute path to blib.  Use File::Spec-rel2abs to get
one.


-- 
Michael G Schwern [EMAIL PROTECTED] http://www.pobox.com/~schwern
Don't try the paranormal until you know what's normal.
-- Lords and Ladies by Terry Prachett


Testing a Script Distributed with a Module

2005-07-08 Thread James E Keenan
I am having trouble figuring out how to test a Perl script which 
functions as a command-line utility and which is included with a 
CPAN-style distribution.


For purpose of discussion, let's call the distribution XYZ and the 
script xyz.pl.  My distribution has the following standard structure:


README
Makefile.PL
MANIFEST
lib/
XYZ.pm
scripts/
xyz.pl
t/
01_module.t
02_script.t

xyz.pl is a Perl script, intended for use at the command-line, which 
parses some options and then calls XYZ-new() with those options:


eval 'exec /usr/local/bin/perl  -S $0 ${1+$@}'
  if 0;# not running under some shell

use strict;
use warnings;
use Getopt::Std;
use XYZ;

getopts( Vh, \%opts );
die Usage() if ( $opts{h} );

my $mod   = XYZ-new(
VERBOSE= ( ( $opts{V} ) ? 1 : 0 ),
...
);

I would like to be able to write tests which call xyz.pl with different 
combinations of options and examine the results.  I assume that I would 
have to call the utility with something like this:


system(xyz.pl -V);

The problem I'm facing is that the version of XYZ that I want xyz.pl to 
'use' is the version I am currently developing, namely, the one that 
will be placed in 'blib/lib' by 'make'.  I *don't* want xyz.pl to 'use' 
the *older* version of XYZ which I have installed on disk (presumably at 
/usr/local/lib/perl5/5.8.4/XYZ.pm).


However, it seems that when I call 'system(xyz.pl -V)' from inside file 
t/02_script.t, it goes for the *installed* (and now incorrect) version 
of XYZ.pm rather than the one in the development tree.  (I have tried 
adding 'use blib;' to xyz.pl, but (a) it doesn't locate blib properly 
and (b) I don't want that in the version of xyz.pl to be distributed to 
customers or on CPAN.


So, how do I structure the distribution and write the test so that I can 
test the command-line utility, not just the Perl module?


TIA

Jim Keenan


Re: Testing a Script Distributed with a Module

2005-07-08 Thread Michael G Schwern
On Fri, Jul 08, 2005 at 07:47:42PM -0400, James E Keenan wrote:
 scripts/
 xyz.pl

Make sure MakeMaker is told about that script via EXE_FILES or it won't know
to do anything with it (like install it).


 I would like to be able to write tests which call xyz.pl with different 
 combinations of options and examine the results.  I assume that I would 
 have to call the utility with something like this:
 
 system(xyz.pl -V);
 
 The problem I'm facing is that the version of XYZ that I want xyz.pl to 
 'use' is the version I am currently developing, namely, the one that 
 will be placed in 'blib/lib' by 'make'.

Do the same thing you'd do from the command line.

perl -Mblib xyz.pl -V

which translates into:

# $^X is the current perl.
system($^X -Mblib xyz.pl -V);


-- 
Michael G Schwern [EMAIL PROTECTED] http://www.pobox.com/~schwern
Reality is that which, when you stop believing in it, doesn't go away.
-- Phillip K. Dick


Re: Testing a Script Distributed with a Module

2005-07-08 Thread James E Keenan

Michael G Schwern wrote:




Make sure MakeMaker is told about that script via EXE_FILES or it won't know
to do anything with it (like install it).




Check.  In Makefile.PL, I already had:

 EXE_FILES= [
'scripts/modulemaker',
],


[snip] 


Do the same thing you'd do from the command line.

perl -Mblib xyz.pl -V

which translates into:

# $^X is the current perl.
system($^X -Mblib xyz.pl -V);



This *seems* to be DWIMming:

system($^X -Mblib blib/script/xyz.pl -V);

which, wrapped inside a test, becomes:

ok(! system($^X -Mblib blib/script/xyz.pl -V),
able to call utility);

Note two things:  (1) I had to specify the path from the cwd to the blib 
version of the utility to get it to work.  Simply calling 'xyz.pl' 
didn't work (error said script could not be found).  (2) Though in my 
directory structure the utility is found in directory 'scripts/' -- and 
that's how it's listed in Makefile.PL's EXE_FILES key -- in the blib 
structure it's called 'script/' -- no final 's'.


I suspect I don't yet understand this properly, but since it seems to be 
working, I'll proceed with testing.


Thanks once again, Michael!

jimk


Re: Testing a Script Distributed with a Module

2005-07-08 Thread James E Keenan

Michael G Schwern wrote:




Oh yeah, forgot about that.  Its not in your path so you have to give it 
the full path to the program.



The directories in blib have no relation to where the file came from.
Non-binary executables always go into blib/script.  Binary executables
go into blib/bin.


One other curiosum:  As a result of my Phalanx work, I've gotten in the 
habit of using File::Temp to create 'anonymous' directories and files in 
which to conduct testing.  I tried that here:


my $tdir = tempdir( CLEANUP = 1);
ok(chdir $tdir, 'changed to temp directory for testing');

but, once again, 'blib/' couldn't be located.  So I had to resort to 
manually constructing, populating and cleaning up a 'tmp/' directory at 
the same level as 'blib/' or 't/'.


jimk