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

标签 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 内部准确地运行该文件?

最佳答案

我找到了一个更好的方法来做到这一点。

在 TextMate bundle 编辑器(菜单栏 -> bundle -> bundle 编辑器 -> 显示 bundle 编辑器)中,我已将默认的“Perl -> 运行脚本” bundle 更新为:

#!/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 编辑器中的外观的屏幕截图。

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

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

这就是我一直在寻找的东西。


更新:当我第一次开发这个时,我在“t”目录中只有一个测试脚本。直到我添加其他测试脚本后,我才注意到该答案的原始版本中的代码将在所有脚本中运行证明。不仅仅是正在研究的那个。为了恢复预期的行为,我更新了捆绑代码,以便 Prove 只能在事件脚本上运行。

关于perl - 如何在 TextMate 中运行专为证明表单而设计的测试 perl 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8949575/

相关文章:

system - 如何在 Perl6 中制作模数?

raku - 如果需要该文件,Perl 6 是否应该运行 MAIN?

python - 如何判断mac和mac程序使用的是哪个python

ruby - TextMate 中的 ⌃⇧H 到 'Tidy' HTML 导致 NoMethodError

python - 相当于 Perl Modulino for Ruby, Python?

perl - 如何确定Perl中变量是否为数字?

ruby-on-rails - 如何让 TextMate 默认使用 Ruby on Rails?

json - 逐 block 解析 JSON

html - 在perl中刷新没有 "Confirm Form Resubmission"的浏览器

regex - 匹配 Perl 中的最后一个正则表达式模式