Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Local library for module test script

by mikosullivan (Novice)
on Dec 03, 2014 at 02:53 UTC ( [id://1109061]=perlquestion: print w/replies, xml ) Need Help??

mikosullivan has asked for the wisdom of the Perl Monks concerning the following question:

I recently uploaded a new module to CPAN, JSON::Relaxed. For the tests I decided it would be cool to put all my testing subroutines into a separate file because, y'know, that's what you do with subs that you intend to use elsewhere.

So in test.t I put this line:

require './lib.pm';
Well, the testers weren't too happy. They gave this error:
Can't locate ./lib.pm in @INC
So, apparently I can't require a file in a test script the way I can in the real world. Is there a way to include lib.pm (which is in the same directory as test.t for use in my tests?

Replies are listed 'Best First'.
Re: Local library for module test script
by syphilis (Archbishop) on Dec 03, 2014 at 03:31 UTC
    During 'make test' the current working directory is the top-level folder ... so I'm thinking you want require './t/lib.pm';.
    Best to check that on your own machine before uploading to CPAN.

    Cheers,
    Rob

      cwd can change, best not to rely on it at all ... I'm frequently inside cwd() of "t" when trying to fix/debug some module, so relying on require "./t/..." would fail

      I also often do perl dist-name-version/t/foo.t :) relying on cwd is weak sauce, it can work very often, but its weak practice

        cwd can change

        Sure, and I have no objection to the approach you suggested.
        However, I write my test suites under the assumption that they're going to be run from "one directory up". If someone complained that they couldn't be run from the 't' directory then I'd likely suggest "stop doing that".

        Cheers,
        Rob
Re: Local library for module test script (updated FindBin)
by LanX (Saint) on Dec 03, 2014 at 03:55 UTC
    use lib ".";

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    update

    see FindBin, it's core.

    use FindBin qw($Bin); use lib "$Bin";

    or

    use FindBin qw($Bin); require $Bin.'/lib.pm';
      LanX :) a relative path is relative, with cwd() usually being dist-name-version, and the "module.pm" living in dist-name-version/t/ adding "." wouldn't change anything , require lib.pm would still fail
        Anonymous Monk, I tested  use lib '.'!

        (at least I thought :)

        I supposed that lib takes the file's path as base.

        The perldoc wasn't clear¹, so I tested by changing the working directory with chdir before use and it seemed to work fine.

        But now I am realizing that I should have put the chdir into a BEGIN block, otherwise it happens too late.

        So your point is valid!!!

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

        ¹) but led me now to FindBin

Re: Local library for module test script ( @INC cwd pwd chdir )
by Anonymous Monk on Dec 03, 2014 at 03:27 UTC

    relative paths are relative, and unless you use chdir, they're often wrong

    Also, @INC is @INC

    Also, lib is a core module

    So don't rely on cwd() being "t" or "..\t" because thats weak sauce, one way

    BEGIN { use File::Spec; use File::Basename(); our $thisf = File::Spec->rel2abs( $0 ); our $thisd = File::Basename::dirname($thisf); }
    then you can  require "$thisd/lib.pm" or chdir $thisd; ...
      That worked very nicely. Thanks!
Re: Local library for module test script
by Khen1950fx (Canon) on Dec 04, 2014 at 04:36 UTC
    The require is the least of your problems. More importantly, the slurp() keeps popping up as undefined.

    Update: Solved. My bad, it was my system. I used require "t/test-lib.pm". Passed all tests.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1109061]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 13:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found