Got this question forwarded on to me.  Hope the talk of .t files
doesn't go over everyone's heads.


On Tue, Jun 26, 2001 at 03:40:06AM -0500, Me wrote:
> > I'm writing a package that uses another module. This latter module may
> > change somewhat erratically and unreliably, outside of my control.  As
> > a result, I want to have the package test itself and die if it notices
> > that the other module has changed in an incompatible way.  What's a
> > good way of doing this?

Depends on how far out of your control that other package is.  If you
can prevent it from being upgraded, then simply note the current
version and puke if you don't get that...

        use Flakey;
        die unless $Flakey::VERSION == $SafeVersion;


> > I'm currently doing this by wrapping the entire package in a BEGIN
> > block, and then adding on some tests in a CHECK block.  My current
> > package looks like:
> > #!/usr/bin/perl
> >
> > BEGIN { ##### Compilation of packages before testing
> >
> > package MyPackage;
> >
> > use strict;
> > use warnings;
> > use Flakey;
> > .....
> > } ##### End of compilation of packages before testing

Note: 'use' already happens at compile time, so that BEGIN block
doesn't do much.

> > CHECK { ###### Testing of packages
> >
> > package MyPackage::Test;
> > use MyPackage;
> >
> > # Various tests to ensure compatibility
> > die "Flakey is not compatible" if ...
> > .....
> > } ##### End of testing of packages
> >
> > 1;
> > __END__
> >
> > This works well in that any code using MyPackage will properly load
> > the package if Flakey is compatible and will die if Flakey is or
> > becomes not compatible.

> > However, some warnings are issued ("Subroutine foo redefined...")
> > because I "use MyPackage" in MyPackage::Test.

Why are you doing that?  Are there two tests going on here?  One is
testing if Flakey is safe to use, that should happen as part of the
module loading up since its so unreliable.  The other appears to be
MyPackage testing itself.  That only has to happen when MyPackage is
installed, so that should do into a seperate .t file.  Look at
Test::Simple.  If you're interested in embedding your tests in your
source code, have a look at Pod::Tests or SelfTest.


As for Flakey, this should do...

    BEGIN {
        package MyPackage::Testing::Flakey;
        use Flakey;
        die unless ...;
    }

    package MyPackage;

    use Flakey;
    ...whatever you do normally...



> > Is there a better way to do this sort of thing?  Long term, I plan to
> > find a better module than Flakey or incorporate its compatible parts
> > into MyPackage to manage this problem, but I'm looking for a
> > short-term fix for now.

Yes, rapidly dump that module.  You also might want to contact the
author and tell them of your problems.  If Flakey is in-house, sounds
like you've got some social issues to work out.  Perhaps even provide
them with a ready-made regression test (a .t file written with
Test::Simple or some such) they can start using.


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <[EMAIL PROTECTED]>       Kwalitee Is Job One
How can I stoop so low?  Years of practise, that's how. It's been hard
going but now I can stoop lower than a pygmy limbo dancer.
        -- BOFH

Reply via email to