Skip to content

Commit

Permalink
Use Rails-recommended binstub for RSpec
Browse files Browse the repository at this point in the history
Some Ruby gems provide an executable to run its contained Ruby program.
We most commonly use `rails`, `rspec`, and `rake`.

When we type `rspec` within the root of our project, Rubygems will use
the latest version of the RSpec gem, not the version specified in the
project's `Gemfile`. That's a problem that Bundler's `bundle exec`
solves.

It's tedious to type `bundle exec`, however. We previously solved that
problem via a directory convention for Bundler's binstubs and adding
that directory to our shell's `PATH`.

Running `bundle binstub rspec` generates a binstub file in the `bin`
directory. Running `./bin/rspec` is now the same thing as running
`bundle exec rspec`.

Adding `export PATH=".git/safe/../../bin:$PATH"` and running `mkdir
.git/safe` in the root of repositories you trust lets us just type
`rspec`, have the binstub be invoked, and therefore the correct version
of RSpec be used for the project.

rbenv/rbenv#309

We previously used `./bin/stubs` as our binstubs directory convention
and ignored the directory in version control. We used that convention
instead of `./bin` because we didn't want to gitignore the
already-existing `./bin` directory and we didn't want to replace the
critical `bin/rails`.

The community is moving towards a convention of using `./bin`:

* Rails is using `./bin` instead of `./script` starting with Rails 4.
* The default Bundler behavior is to use `./bin`.
  • Loading branch information
Dan Croak committed Sep 12, 2013
1 parent 66a26d3 commit 87a471f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/suspenders/app_builder.rb
Expand Up @@ -134,6 +134,11 @@ def configure_rspec
copy_file 'spec_helper.rb', 'spec/spec_helper.rb'
end

def use_rspec_binstub
run 'bundle binstub rspec-core'
run 'rm bin/autospec'
end

def configure_background_jobs_for_rspec
copy_file 'background_jobs_rspec.rb', 'spec/support/background_jobs.rb'
run 'rails g delayed_job:active_record'
Expand Down
3 changes: 2 additions & 1 deletion lib/suspenders/generators/app_generator.rb
Expand Up @@ -49,7 +49,7 @@ def remove_files_we_dont_need
def customize_gemfile
build :replace_gemfile
build :set_ruby_to_version_being_used
bundle_command 'install --binstubs=bin/stubs'
bundle_command 'install'
end

def setup_database
Expand All @@ -76,6 +76,7 @@ def setup_test_environment
build :test_factories_first
build :generate_rspec
build :configure_rspec
build :use_rspec_binstub
build :configure_background_jobs_for_rspec
build :enable_database_cleaner
build :configure_capybara_webkit
Expand Down
1 change: 0 additions & 1 deletion templates/suspenders_gitignore
Expand Up @@ -4,7 +4,6 @@
*.swp
.bundle
.sass-cache/
bin/stubs
coverage/*
db/*.sqlite3
db/schema.rb
Expand Down

0 comments on commit 87a471f

Please sign in to comment.