Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

CPAN test scripts, "run" directory, and test data files

by dallen16 (Sexton)
on Dec 04, 2015 at 01:38 UTC ( #1149356=perlquestion: print w/replies, xml ) Need Help??
dallen16 has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on my first CPAN package and have a question about how, really where the test scripts are run. I'd like to include a data file that is accessed by the test script (eg, 00_core.t). My question is what is the directory where the test scripts are run? Is it reliably the parent directory of the "t" (./t) directory? If I put the test data file in t/data (which some modules seem to do), can I assume that from the test script, the file path would be "./t/data/mytestdata.dat"?

Part B of wisdom seeking question is how careful / involved does the test script need to be to specify the directory path and filename in a portable manner?

Observation... looking at the test scripts in other packages, I noticed that is it common for test scripts to create a temporary file, write test data to the file, close it, then re-open it to use for one or more tests, and then delete (unlink) it. Is this the preferred way to do it? Seems like it might avoid potential problems with say end of line characters, EBCDIC machines, and questions about file directories ...

Many thanks for sharing your experience and wisdom.

  • Comment on CPAN test scripts, "run" directory, and test data files

Replies are listed 'Best First'.
Re: CPAN test scripts, "run" directory, and test data files
by syphilis (Chancellor) on Dec 04, 2015 at 04:03 UTC
    Hi,

    I personally think that creating 't/data/mytesdata.dat' and locating it in the test script as './t/data/mytesdata.dat' is fine.
    However, I've struck at least one person who expects the test scripts to be runnable from both the top-level and the 't' directories.
    I have no time for that viewpoint, though I pander to it if I think of it (and can be bothered).

    IMO, the test suite is designed to be run by 'make test', which means the cwd will be the directory that contains the Makefile (and the Makefile.PL).
    If one subsequently needs to run any test scripts outside of the 'make test' process, then one ought to understand that limitations may exist wrt to the location from which the scripts are run (as would be the case with your proposed layout).

    Rather than unlink any files, I prefer to have the 'make clean' step remove them - which can be achieved by specifying the 'clean' option in WriteMakefile() in the Makefile.PL:
    clean => { FILES => './t/data/out1.dat ./out2.dat' },
    It annoys me when I want to inspect the contents of a certain file, only to find that the test script that created that file has also removed it.
    There also exist on CPAN some annoying test scripts where the attempts to unlink certain files fail (though these files could be successfully removed by 'make clean' if they were specified in the 'clean' option).

    Cheers,
    Rob

      "It annoys me when I want to inspect the contents of a certain file, only to find that the test script that created that file has also removed it."

      That just changed my entire perspective of why and how I'll do things in the future (as opposed to commenting out unlink in my own files).

Re: CPAN test scripts, "run" directory, and test data files
by stevieb (Abbot) on Dec 04, 2015 at 03:45 UTC

    "My question is what is the directory where the test scripts are run?"

    Well, look at it this way... when your module is popped open by the CPAN Testers, they base their operations (executions) of your tests from the top-level root directory of your module, so when tests are run, they typically see lib, t etc. underneath.

    Your unit tests should always be under the t/ directory, and inside of a test file, you should always specify special locations from the location the test is being run *from*... that is, if you have Module/t/01-my_test.t, wanting to grab a baseline file from Module/t/data/baseline.conf, you'd reference it as t/data/baseline.conf.

    It's really that simple. Write your tests from the perspective that they'll be run out of the parent directory of t/, and you'll be fine.

    this is an example test file where I open a baseline comparison file from within t/orig. I do perform unlink in that test file... I could create tempfiles in t/data, lib/MyModule or wherever... just reference them as you're sitting in your module root dir (really, really avoid absolute paths... that's not portable at all).

    To create, then unlink within a unit test, ensure you have any directory eg: t/data dir, then, after you've done your work (eg: open my $wfh, '>', 't/data/scrap.crap' or die $!;) at the end of your unit test (or at the end of the test file) do something like:

    eval { unlink 't/data/scrap.crap' or die; }; is ($@, '', "unlink of scrap.crap ok");

    Again... tests are run from your module root directory. When specifying a directory from within your test, act like you're in the directory above it. When running your unit tests, be in your module root dir, and do: perl t/01-mytest.t or make test. If your directories are off, you'll find out quickly.

Re: CPAN test scripts, "run" directory, and test data files
by Discipulus (Monsignor) on Dec 04, 2015 at 08:23 UTC
    dallen16++ for your question. Only a little though about your's".. specify the directory path and filename in a portable manner": in my little experience (no CPAN contribution, till now) about path portability i saw that Perl works very well in Win32 OSs with linux styled paths. So if you need to interpolate too in the path, instead of having to "c:\\a_lot_\\of\\$backslashes" you can safely use / and you'll "c:/fewer/$keystrokes" (pronounce c:/ like 'see')

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: CPAN test scripts, "run" directory, and test data files
by choroba (Bishop) on Dec 04, 2015 at 12:12 UTC
    To get the path to the t/ directory, you can also use $FindBin::Bin (in core since 5.4) in your test file. That's what I did in Syntax::Construct.
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: CPAN test scripts, "run" directory, and test data files
by Anonymous Monk on Dec 04, 2015 at 01:47 UTC

    My question is what is the directory where the test scripts are run? Is it reliably the parent directory of the "t" (./t) directory? If I put the test data file in t/data (which some modules seem to do), can I assume that from the test script, the file path would be "./t/data/mytestdata.dat"?

    Try it out and see what it is

    See Cwd, File::Spec->rel2abs()

    Observation... looking at the test scripts in other packages, I noticed that is it common for test scripts to create a temporary file, write test data to the file, close it, then re-open it to use for one or more tests, and then delete (unlink) it. Is this the preferred way to do it

    No this is not common if the data is static.

    If the data is static, and if you're not testing the creation/deletion of files, then don't create temporary files

Re: CPAN test scripts, "run" directory, and test data files
by RonW (Vicar) on Dec 04, 2015 at 20:24 UTC

    note that if you use $FindBin::Bin, you should first call FindBin::again() as described under "Known Issues" in the documentation. This because a module you (or the test harness) use could also be using FindBin.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1149356]
Approved by toolic
Front-paged by Discipulus
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-05-23 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?