in reply to How do I test a script that doesn't have a .pl extension

So require_ok is broken :) Try ok( require '...' );
  • Comment on Re: How do I test a script that doesn't have a .pl extension

Replies are listed 'Best First'.
Re^2: How do I test a script that doesn't have a .pl extension
by ten8ciousb (Novice) on Nov 02, 2012 at 13:14 UTC

    well this is embarassing. first time seeking wisdom and, er... how does that go?
    TMTOWTDI
    yes.

    require_ok('foo') ### fails foo not found in @INC ok(require 'foo') ### passes
    did work for me. thank you.

      That test will crash instead of failing, if foo does not exist, or is empty:

      >perl -MTest::More=tests,2 -e "ok(require 'empty.pl'); print ok 1" 1..2 empty.pl did not return a true value at -e line 1. # Looks like your test exited with 255 before it could output anything +.
      Q:\>perl -MTest::More=tests,2 -e "ok(require 'nonexistent.pl'); print +ok 1" 1..2 Can't locate nonexistent.pl in @INC (@INC contains: ...) at -e line 1. # Looks like your test exited with 2 before it could output anything.

      You may want to let your tests just crash, because in my experience, use_ok and require_ok are just useless anyway, as no subsequent test will pass if they fail.

        thanks. you've got a good point there. if the test can't load the script that I want to run tests against, I want it to crash and just doing
        require 'foo';
        will do that.
        but I prefer using the ok() or require_ok() with a die or croak (which I didn't include in my example) because then in a test suite I get better diagnostic:
        not ok 2 - require foo; # at /source/t/foo.t line 30. # Tried to require 'foo'. # Error: Can't locate foo.pm in @INC (@INC contains: /source/bin +... at (eval 17) line 2. Died at /source/t/foo.t line 30.
Re^2: How do I test a script that doesn't have a .pl extension
by ten8ciousb (Novice) on Nov 02, 2012 at 21:25 UTC
    Update: stepped through the execution and found why it fails.
    In require_ok
    # Try to determine if we've been given a module name or file. # Module names must be barewords, files not. $module = qq['$module'] unless _is_module_name($module);
    the problem is in _is_module_name
    foo is being treated as a module because of this
    $module =~ s/\b::\b//g; return $module =~ /^[a-zA-Z]\w*$/ ? 1 : 0;
    with "foo.pl" it returns 0
    with "foo" it returns 1
    changing it to
    # $module =~ s/\b::\b//g; return $module =~ /^[a-zA-Z]\w*$/ ? 1 : 0 if $module =~ s/\b::\b//g;
    resolves the problem and works for these tested scenarios
    require_ok("foo"); require_ok('foo'); require_ok("foo.pl"); require_ok('foo.pl'); require_ok("Net::FTP"); require_ok('Net::FTP');