Search code examples
perltextmatemodulino

How can I run a test perl script designed for prove form inside TextMate?


I'm using TextMate 1.5.10 (Mac OSX 10.7.2) to write a perl modulino application. To verify the functionality, I'm using test scripts designed to be run with the prove command line tool.

An example of the directory structure I'm using looks like this:

text_mate_test/MyModule.pm
text_mate_test/t/001_load_test.t

The 001_load_test.t file looks like this:

#!/usr/bin/perl 

use Modern::Perl;
use Test::More;
use MyModule;

my $testObj = new_ok("MyModule", undef, "Initial load test.");

done_testing();

When I run prove or prove -v in the "text_mate_test" directory, everything passes as expected.

I'd like to be able to setup a hotkey in TextMate that allows me to run the test file without having to jump over to the terminal. Currently, if I run "001_load_test.t" directly from inside TextMate with Cmd+R, it chokes saying "Can't locate MyModule.pm in @INC". That's expected since the test script isn't designed to run directly. (I'm still pretty new to writing test files, but I believe that's the proper way to set them up.)

Running off the assumption that I don't want to change the test file itself, is there a way to setup a hotkey so I can run the file accurately from inside TextMate?


Solution

  • I've figured out an even better way to do this.

    In the TextMate Bundle Editor (Menubar -> Bundles -> Bundle Editor -> Show Bundle Editor), I've updated the default "Perl -> Run Script" bundle to this:

    #!/usr/bin/env ruby
    
    require "#{ENV["TM_SUPPORT_PATH"]}/lib/tm/executor"
    require "#{ENV["TM_SUPPORT_PATH"]}/lib/tm/save_current_document"
    
    TextMate.save_current_document
    TextMate::Executor.make_project_master_current_document
    
    ### If it's a ".t" test script in a "t" directory, run prove
    if ( ENV["TM_FILEPATH"] =~ /^.*\/(t\/[^\/]+)$/ )
    
        ### Grab the relative file path for more legible output
        relative_file_path = $1
    
        ### Jump up one directory so prove will work
        Dir.chdir("../");
    
        ### Call prove with args to run only the file you are working on.
        TextMate::Executor.run("prove", :script_args => ["-v", relative_file_path]);
    
    ### Otherwise, run with perl
    else
        TextMate::Executor.run(ENV["TM_PERL"] || "perl", "-I#{ENV["TM_BUNDLE_SUPPORT"]}", 
            "-Mexception_handler", ENV["TM_FILEPATH"], 
            :version_args => ["-e", 'printf "Perl v%vd", $^V;'])
    end
    

    Here's a screenshot of how it looks in the Bundle Editor.

    TextMate Bundle Editor Screen Shot for using prove to run perl test scripts

    The benefit of this is that you can use the same hot key (Cmd+r by default) to run your normal scripts with perl and your test scripts with prove.

    This is what I was looking for.


    UPDATED: When I first developed this, I only had one test script in the "t" directory. I didn't notice until I added other test scripts that the code in the original version of this answer would run prove across all the scripts. Not just the one being worked on. To get back to the expected behavior, I've update the bundle code so that prove will only run on the active script.