Search code examples
rubyrspecbundlezshchruby

How not to have to type "bundle exec" using Bundler binstubs & chruby?


I'm using chruby to manage my Ruby versions, and Bundler's binstubs to avoid having to type "bundle exec". I'm running into issues I don't totally understand. Here's what I get when I try to run the test suite in our (Rails 4, Ruby 2.2.2) application:

$ rspec             
/Users/duncanmalashock/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- simplecov (LoadError)
    from /Users/duncanmalashock/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/duncanmalashock/ruby_projects/platform/spec/spec_helper.rb:3:in `<top (required)>'
    from /Users/duncanmalashock/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/duncanmalashock/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/duncanmalashock/ruby_projects/platform/spec/controllers/admin/admin_controller_spec.rb:1:in `<top (required)>'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/bin/rspec:23:in `load'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/bin/rspec:23:in `<main>'

When I run $ bundle exec rspec, the suite runs correctly. I've been looking into postmodern's gem_home, and have installed it, but it's not giving me the results I'm looking for. I'm using zsh on OSX Yosemite.

in .zshrc:

source /usr/local/share/chruby/chruby.sh
source '/usr/local/share/chruby/auto.sh'
source /usr/local/share/gem_home/gem_home.sh

export PATH=./.bundle/bin:$PATH
alias b='bundle install --path .bundle/gems --binstubs .bundle/bin'

Solution

  • Just install the simplecov gem in your system (simply do: gem install simplecov) and run your test again. It will work.

    But, it's a better practice to use bundle exec to run the rake or rspec commands. Because that way, you would ensure that the command is running in the context of your Gemfile.

    You can add these aliases to your .zshrc file:

    alias ber="bundle exec rspec"
    alias be="bundle exec"
    

    And, then run your rspec tests like this:

    ber
    

    or,

    be rspec