2010-07-08 53 views
0

我想添加一些提交钩子到我的git仓库。我想利用Rspec并创建每次提交时都会运行的提交消息规范。我已经想出了如何在'spec'命令之外运行rspec,但是现在我有一个有趣的问题。告诉rspec不加载文件

这里是我当前的代码:

的.git /钩/提交-MSG

#!/usr/bin/env ruby 

require 'rubygems' 
require 'spec/autorun' 

message = File.read(ARGV[0]) 

describe "failing" do 
    it "should fail" do 
     true.should == false 
    end 
end 

当它到达的描述呼叫这是抛出一个错误。基本上,它认为它收到的提交消息是加载和运行规格的文件。这是实际的错误

./.git/COMMIT_EDITMSG:1: undefined local variable or method `commit-message-here' for main:Object (NameError) 
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:15:in `load' 
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:15:in `load_files' 
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:14:in `each' 
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:14:in `load_files' 
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/options.rb:133:in `run_examples' 
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner.rb:61:in `run' 
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner.rb:45:in `autorun' 
from .git/hooks/commit-msg:12 

我正在寻找一种方法来告诉rspec不加载文件。我有一个怀疑,我需要创建我自己的spec跑步者。我得出这一结论在rspec的-1.3.0/lib目录/规格/跑步/ example_group_runner.rb

def load_files(files) 
    $KCODE = 'u' if RUBY_VERSION.to_f < 1.9 
    # It's important that loading files (or choosing not to) stays the 
    # responsibility of the ExampleGroupRunner. Some implementations (like) 
    # the one using DRb may choose *not* to load files, but instead tell 
    # someone else to do it over the wire. 
    files.each do |file| 
     load file 
    end 
    end 

查看这些行之后,但,我想一些反馈我这样做了。有什么想法吗?

回答

0

你甚至真的需要RSpec提供的所有特殊的东西(should和各种匹配器)来验证单个文件的内容吗?这对于这个问题看起来真是太过分了。


spec/autorun最终调用Spec::Runner.autorun其解析ARGV好像掌握正常参数的规范命令行。

当您安装裸“规范”文件作为一个Git挂钩, 它会得到论据是适合于任何的Git钩子正在被使用, 不规范风格的参数(SPEC文件名/目录/模式和规格选项)。

你也许可以破解周围像这样的问题:

# Save original ARGV, replace its elements with spec arguments 
orig_argv = ARGV.dup 
%w(--format nested).inject(ARGV.clear, :<<) 

require 'rubygems' 
require 'spec/autorun' 

# rest of your code/spec 
# NOTE: to refer to the Git hook arguments use orig_argv instead of ARGV 
+0

感谢您的想法克里斯。我想正是因为这个原因使用rspec:匹配器,组织,报告,通过/失败。 我希望能够运行如下检查:“提交消息的第一行应少于51个字符”或“消息应包含错误编号”等等...... rspec给我所有这些功能,加上统一的(和熟悉的)报告输出 – 2010-07-12 03:17:05