Shoulda destroys rake tasks (in test environment) with ruby 1.9.3 #223

Closed
jarl-dk opened this Issue Oct 9, 2012 · 9 comments

Projects

None yet

4 participants

@jarl-dk

The following series of commands reproduces the problem from scratch:

$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
$ rails --version
Rails 3.2.8
$rails new basic_app/
$cd basic_app/
$echo "gem 'shoulda'" >> Gemfile
$echo "gem 'therubyracer', :platforms => :ruby" >> Gemfile
$bundle update
$RAILS_ENV=test rake db:create

The last command results in the following stacktrace:

/home/jarl/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: db:create (ArgumentError)
        from /home/jarl/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
        from /home/jarl/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
        from /home/jarl/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
        from /home/jarl/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
        from /home/jarl/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
        from /home/jarl/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
        from /home/jarl/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/test/unit.rb:21:in `run'
        from /home/jarl/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
        from /home/jarl/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
        from /home/jarl/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'

This does not happen without the shoulda gem.

@jarl-dk

Related bug reports:
#207
jimweirich/rake#51

@drapergeek
thoughtbot, inc. member

This should be resolved in 3.3.2 due to a fix in shoulda-context.

@drapergeek drapergeek closed this Oct 19, 2012
@jarl-dk

I still experience this with shoulda 3.3.2 (sholda-context 1.0.1 and shoulda-matchers 1.4.1). Please reopen...

@gabebw gabebw reopened this Oct 29, 2012
@gabebw
thoughtbot, inc. member

I can reproduce this.

Here's what I did on my local machine:

$ rails -v
Rails 3.2.8
$ ruby -v
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.2.0]
$ rails new app-for-shoulda-223
$ cd app-for-shoulda-223
$ <edit Gemfile to look like below>
$ bundle
$ rake db:create:all
db/development.sqlite3 already exists
/Users/gabe/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: db:create:all (ArgumentError)
        from /Users/gabe/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
        from /Users/gabe/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
        from /Users/gabe/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
        from /Users/gabe/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
        from /Users/gabe/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
        from /Users/gabe/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
        from /Users/gabe/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/test/unit.rb:21:in `run'
        from /Users/gabe/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
        from /Users/gabe/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
        from /Users/gabe/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'

Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.8'
gem 'sqlite3'

gem 'shoulda-context', '1.0.1'

Nothing else is changed from a vanilla Rails 3.2.8 install.

@gabebw
thoughtbot, inc. member

I've tracked this down to Test::Unit's at_exit hook, in test/unit.rb:

@@stop_auto_run = false
def self.autorun
  at_exit {
    Test::Unit::RunCount.run_once {
      exit(Test::Unit::Runner.new.run(ARGV) || true)
    } unless @@stop_auto_run
  } unless @@installed_at_exit
  @@installed_at_exit = true
end

The ARGV part means that it treats db:create:all as an argument to the test runner. As proof, adding this to the Rakefile fixes the error:

class Test::Unit::Runner
  @@stop_auto_run = true
end

I'm not sure why including shoulda-context makes it think that db:create:all should be passed to Test::Unit though.

@gabebw
thoughtbot, inc. member

Aha, found it. shoulda-context has this line:

require "test/unit"

which causes the autorunning to kick in. If I change it to require "test/unit/testcase", the error disappears.

@gabebw gabebw referenced this issue in thoughtbot/shoulda-context Dec 18, 2012
Merged

Require only Test::Unit::TestCase #17

@gabebw
thoughtbot, inc. member

The pull request referenced above, thoughtbot/shoulda-context#17, fixes the issue. Once that's in a released version, it should fix your problem.

@travisjeffery

@jarl-dk the fix is released in shoulda-context v1.0.2.

@jarl-dk

👍

I have verified it...

@jarl-dk jarl-dk closed this Dec 20, 2012
@eostrom eostrom added a commit to SouthArts1/ArtsReady that referenced this issue Mar 21, 2014
@eostrom eostrom Upgraded shoulda.
It was preventing rake tasks from working in the test environment.

thoughtbot/shoulda#223

Shoulda Matchers 2.0 removed the `assign_to` and
`respond_with_content_type` matchers. They recommend testing assignments
implicitly via integration tests, but making sure these controller
actions are all covered by our integration tests is a detour I don't
want to make right now.
8211dd5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment