告诉rspec不加载文件

时间:2010-07-08 18:52:45

标签: ruby git rspec githooks

我正在尝试为我的git repo添加一些提交挂钩。我想利用Rspec并创建每次提交时都会运行的提交消息规范。我已经想出如何在'spec'命令之外运行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

当它进入describe调用时抛出一个错误。基本上,它认为它收到的提交消息是要加载并运行规范的文件。这是实际错误

./.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 / 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

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

1 个答案:

答案 0 :(得分:0)

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


spec/autorun最终调用Spec::Runner.autorun解析ARGV,就像它为 spec 命令行保存了正常参数一样。

当您将裸“规范”文件安装为Git挂钩时, 它将获得适合于使用任何Git钩子的参数, 不是 spec 式参数(spec文件名/目录/模式和 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