わすれっぽいきみえ

みらいのじぶんにやさしくしてやる

vagrant1.5.1でbundle execを使ったら`initialize': undefined method `new' for Bundler::UI:Module (NoMethodError)と出るときの対処法

本題

serverspecを
Ruby - serverspecの使い方 - Qiita
を参考に導入し、vagrantインスタンスの設定テストを走らせてみたら 以下のようなエラーが出たので、その対処法についてまとめておく。

$ bundle exec rake spec
/Users/kimikimi714/.rbenv/versions/2.0.0-p451/bin/ruby -S rspec spec/web_server/httpd_spec.rb
/Applications/Vagrant/embedded/gems/gems/vagrant-1.5.1/lib/vagrant/bundler.rb:31:in `initialize': undefined method `new' for Bundler::UI:Module (NoMethodError)
    from /Applications/Vagrant/embedded/gems/gems/vagrant-1.5.1/lib/vagrant/bundler.rb:17:in `new'
    from /Applications/Vagrant/embedded/gems/gems/vagrant-1.5.1/lib/vagrant/bundler.rb:17:in `instance'
    from /Applications/Vagrant/bin/../embedded/gems/gems/vagrant-1.5.1/lib/vagrant/pre-rubygems.rb:22:in `<main>'
/Applications/Vagrant/embedded/gems/gems/vagrant-1.5.1/lib/vagrant/bundler.rb:31:in `initialize': undefined method `new' for Bundler::UI:Module (NoMethodError)
    from /Applications/Vagrant/embedded/gems/gems/vagrant-1.5.1/lib/vagrant/bundler.rb:17:in `new'
    from /Applications/Vagrant/embedded/gems/gems/vagrant-1.5.1/lib/vagrant/bundler.rb:17:in `instance'
    from /Applications/Vagrant/bin/../embedded/gems/gems/vagrant-1.5.1/lib/vagrant/pre-rubygems.rb:22:in `<main>'
......

Finished in 0.90539 seconds
6 examples, 0 failures

答えは
Embedded bundler clashing with my bundler · Issue #3193 · mitchellh/vagrant · GitHub
OS X用vagrant1.5.0もしくはvagrant1.5.1(私の環境)ではbundle execがうまく動いてくれない、というもの。
何が原因でちゃんと動かないのかコードとかまともに読んでないので分からないけども、少なくとも 1.5.2以上でfixされているので、vagrantのバージョンをあげてやればいい。(今だと1.5.4になる)

余談

で、vagrantのバージョンをあげてやったら今度は次のようなwarningをはいた。

$ bundle exec rake spec
/Users/kimikimi714/.rbenv/versions/2.0.0-p451/bin/ruby -S rspec spec/web_server/httpd_spec.rb
A Vagrant environment is required to run this command. Run `vagrant init`
to set one up in this directory, or change to a directory with a
Vagrantfile and try again.
A Vagrant environment is required to run this command. Run `vagrant init`
to set one up in this directory, or change to a directory with a
Vagrantfile and try again.
......

Finished in 2.62 seconds
6 examples, 0 failures

今度はVagrantfileがないという。 確かにvangrantのインスタンスを立ち上げられるディレクトリとserverspecのテストコードをおいているディレクトリは異なるので serverspec-initで自動的に生成されるspec_helper.rbではWarningが出てしまう。
というわけでspec_helper.rbを書き換える。

diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index f2e6e2e..a243a58 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -25,21 +25,6 @@ RSpec.configure do |c|
       c.host  = host
       options = Net::SSH::Config.for(c.host)
       user    = options[:user] || Etc.getlogin
-      vagrant_up = `vagrant up web_server`
-      config = `vagrant ssh-config web_server`
-      if config != ''
-        config.each_line do |line|
-          if match = /HostName (.*)/.match(line)
-            host = match[1]
-          elsif  match = /User (.*)/.match(line)
-            user = match[1]
-          elsif match = /IdentityFile (.*)/.match(line)
-            options[:keys] =  [match[1].gsub(/"/,'')]
-          elsif match = /Port (.*)/.match(line)
-            options[:port] = match[1]
-          end
-        end
-      end
       c.ssh   = Net::SSH.start(host, user, options)
     end
   end

vagrant upコマンドやvagrant ssh-configコマンドがWarningの原因になっている。
serverspecのトップ

Serverspec with SSH backend logs in to target servers as a user configured in ~/.ssh/config or a current user. If you'd like to change the user, please edit the below line in spec/spec_helper.rb.

と書いてあり、~/.ssh/configにはすでにchefのレシピを流し込むようにvagrantインスタンスの設定を記載してあったので、 それが使えるように書き換えた。

すると

$ bundle exec rake spec
/Users/kimikimi714/.rbenv/versions/2.0.0-p451/bin/ruby -S rspec spec/web_server/httpd_spec.rb
......

Finished in 0.52625 seconds
6 examples, 0 failures

Warningも出ないでテストが走るようになった。

failureは自分のテストの書き方かサーバの設定が悪いので自分で何とかしてね!