In working with Vanilla Perl on Win32, I keep finding modules that fail their tests due to hardcoded slashes in their test scripts. Even modules that are pretty good about using File::Spec in the module itself seem to slip up during testing. For example:

use Test::More tests => 1; use My::Module qw/get_a_filename/; # gives a path with backslashes my $expected = "some/file/path/here"; is( get_a_filename(), $expected );

I considered writing Test::FilePath that would run arguments through File::Spec->canonpath() automatically:

use Test::FilePath tests => 1; filepath_is( "some/path", "some\path" );

That works for Win32, since canonpath() changes forward-slashes to back-slashes, but I don't think it works for other OSes like MacOS (Classic) that use things like ":" for separators.

Getting a truly platform-independent filepath comparison to a hard-coded test expectation seems like it would require one of these approaches:

  1. Hardcode the path components separately and assemble them at runtime using File::Spec.

    my @expected = qw{ some file path here }; is( get_a_filename(), File::Spec->join( @expected ) );
  2. Use a convention that expected filepaths are always in Unix-style, and use a test helper to split and remap to the local directory separator convention.

    my $expected = "some/file/path/here"; filepath_is( "some:file:path:here", $expected );

Do you prefer one over the other? How have you approached this problem? Are there other ideas you'd suggest? What edge cases do you anticipate? Would you use something like Test::FilePath (despite the extra dependency)?

Your input is greatly appreciated.

P.S. And if you do have hardcoded slashes in some module test on CPAN, please go fix that!

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.


In reply to Portable filename comparison for test files by xdg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.