如何在TextMate中运行为验证表单设计的测试perl脚本?

时间:2012-01-21 00:18:53

标签: perl textmate modulino

我正在使用TextMate 1.5.10(Mac OSX 10.7.2)编写perl modulino application。为了验证功能,我使用的是设计为使用prove命令行工具运行的测试脚本。

我正在使用的目录结构的示例如下所示:

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

001_load_test.t文件如下所示:

#!/usr/bin/perl 

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

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

done_testing();

当我在“text_mate_test”目录中运行proveprove -v时,一切都按预期传递。

我希望能够在TextMate中设置一个热键,允许我运行测试文件,而不必跳转到终端。目前,如果我使用Cmd + R直接从TextMate内部运行“001_load_test.t”,它会说“无法在@INC中找到MyModule.pm”。这是预期的,因为测试脚本不是为直接运行而设计的。 (我对编写测试文件还很陌生,但我相信这是设置它们的正确方法。)

假设我不想更改测试文件本身,有没有办法设置热键,以便我可以从TextMate中准确运行文件?

3 个答案:

答案 0 :(得分:2)

我已经想出了一个更好的方法来做到这一点。

在TextMate Bundle Editor(菜单栏 - > Bundles - > Bundle Editor - > Show Bundle Editor)中,我已将默认的“Perl - > Run Script”软件包更新为:

#!/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

以下是它在Bundle Editor中的外观截图。

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

这样做的好处是你可以使用相同的热键(默认为Cmd + r)来运行带有perl的普通脚本和带有证明的测试脚本。

这就是我想要的。


更新:当我第一次开发它时,我在“t”目录中只有一个测试脚本。在我添加其他测试脚本之前,我没有注意到这个答案的原始版本中的代码将在所有脚本中运行。不只是正在进行的工作。为了回到预期的行为,我更新了捆绑代码,以便证明只能在活动脚本上运行。

答案 1 :(得分:1)

我想出了一个解决方案。创建一个名为“Run Script with prove”的新Perl包,并将其与Shift-Cmd-R相关联。捆绑包的代码是:

#!/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\/[^\/]+)$/ )

    ### Use 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]);

else

    error_string = "This script's filepath doesn't end with /t/.*\.t\n"
    error_string += "That is required for the 'Perl -> Run Script with prove' bundle to work.\n"

    TextMate::Executor.run("echo", :script_args => [error_string]);

end

注意:这是一系列试错黑客的结果。我不知道这样做是否“正确”,但这对我有用。除最后两行之外的所有内容都是TextMate附带的原始“Run Script”包中的副本。基于此,似乎这应该是非常安全的。

更新:当我第一次构建时,我在“t”目录中只有一个测试文件。当我添加更多内容时,我发现该捆绑包的原始版本正在运行所有测试文件。此代码表示仅运行您正在处理的测试脚本的预期行为的更新。由于我最终这样做的方式,因此有必要添加后备。如果您尝试运行与标准测试文件路径格式不匹配的脚本,则会显示错误消息。

答案 2 :(得分:0)

如果添加

,它将使程序能够找到您的模块
use lib '..';

到代码顶部(use MyModule之前)。这会将text_mate_test目录添加到@INC并启用Perl来查找模块,但是您可能会遇到直接运行程序的其他问题。

相关问题