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

これは、describe 呼び出しに到達したときにエラーをスローしています。基本的に、受信したコミット メッセージは、spec を読み込んで実行するファイルであると考えます。これが実際のエラーです

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

should単一のファイルの内容を検証するためだけに、RSpec が提供するすべての特別なもの (およびさまざまなマッチャー) が本当に必要ですか? それは本当に問題のやり過ぎのようです。


spec/autorun最終的には、 specコマンド ラインの通常の引数を保持しているかのようSpec::Runner.autorunに解析する which を呼び出します。ARGV

裸の「スペック」ファイルを Git フックとしてインストールすると、 specスタイルの引数 (スペック ファイル名/ディレクトリ/パターンおよびスペックオプション)ではなく、使用されている 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 に答える