0

我正在尝试向我的 git 存储库添加一些提交挂钩。我想利用 Rspec 并创建每次提交时都会运行的提交消息规范。我已经想出了如何在“spec”命令之外运行 rspec,但我现在遇到了一个有趣的问题。

这是我当前的代码:

.git/hooks/commit-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 不加载文件。我怀疑我需要创建自己的规范运行器。在查看 rspec-1.3.0/lib/spec/runner/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

但是,在我这样做之前,我想要一些反馈。有什么想法吗?

4

1 回答 1

0

您是否真的需要 RSpec 提供的所有特殊内容(should以及各种匹配器)来验证单个文件的内容?对于这个问题来说,这似乎有点矫枉过正。


spec/autorun最终调用Spec::Runner.autorunwhich 解析ARGV,就好像它包含规范命令行的正常参数一样。

当您将裸“规范”文件安装为 Git 钩子时,它将获取适用于正在使用的任何 Git 钩子的参数,而不是规范样式的参数(规范文件名/目录/模式和规范选项)。

您可能可以像这样解决问题:

# 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 
于 2010-07-09T06:53:58.910 回答