semaperepelitsa / spork-minitest

MiniTest runner for Spork
MIT License
13 stars 25 forks source link

Exception encountered: #<LoadError: no such file to load -- -r> #2

Closed tacomanator closed 12 years ago

tacomanator commented 12 years ago

Not sure where this error is coming from but looking for help to isolate.

I'm trying to add spork (via spork-minitest) to my rails 3.2.3 project which is using minitest and guard. I've added spork-minitest to the gemfile, and generated a spork entry in the guardfile:

guard 'spork', cucumber: false, test_unit: false, minitest: true, :minitest_env => { 'RAILS_ENV' => 'test' } do
  watch('config/application.rb')
  watch('config/environment.rb')
  watch(%r{^config/environments/.+\.rb$})
  watch(%r{^config/initializers/.+\.rb$})
  watch('Gemfile')
  watch('Gemfile.lock')
  watch('test/test_helper.rb') { :minitest }
end

I moved all of the code in my test_helper into the Spork.prefork block:

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] = "test"
  require File.expand_path('../../config/environment', __FILE__)
  require "rails/test_help"
  require "minitest/autorun"
  require "minitest/rails"
  require 'capybara/rails'
  class MiniTest::Rails::Spec
    include Mongoid::Matchers
    include Capybara::DSL
    before :each do
      # ensure a clean slate for each unit test
      Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
    end
  end
end

Spork.each_run do
end

I can then run spork using and see that tests are much faster via time bundle exec rake test:models --drb (complains about --drb option, but it takes it anyway).

I then try to run the same process through guard via bundle exec guard. It starts properly, but errors when running the tests:

$ bundle exec guard
Guard uses Growl to send notifications.
Guard is now watching
Starting Spork for MiniTest
Using MiniTest
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8988!
Spork server for MiniTest successfully started

Running all tests
Running tests with args ["-r", "/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/guard-minitest-0.5.0/lib/guard/minitest/runners/default_runner.rb", "-e", "::GUARD_NOTIFY=true", "test/test_helper.rb", "./test/integration/blog_test.rb", "./test/integration/user_test.rb", "./test/models/blog_test.rb", "./test/test_helper.rb"]...
Exception encountered: #<LoadError: no such file to load -- -r>
backtrace:
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `block in require'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/spork-minitest-0.0.3/lib/spork/test_framework/minitest.rb:13:in `block in run_tests'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/spork-minitest-0.0.3/lib/spork/test_framework/minitest.rb:12:in `each'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/spork-minitest-0.0.3/lib/spork/test_framework/minitest.rb:12:in `run_tests'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/spork-0.9.1/lib/spork/run_strategy/forking.rb:13:in `block in run'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/spork-0.9.1/lib/spork/forker.rb:21:in `block in initialize'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/spork-0.9.1/lib/spork/forker.rb:18:in `fork'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/spork-0.9.1/lib/spork/forker.rb:18:in `initialize'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/spork-0.9.1/lib/spork/run_strategy/forking.rb:9:in `new'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/spork-0.9.1/lib/spork/run_strategy/forking.rb:9:in `run'
/Users/drew/.rvm/gems/ruby-1.9.2-p318/gems/spork-0.9.1/lib/spork/server.rb:48:in `run'
/Users/drew/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/drb/drb.rb:1563:in `perform_without_block'
/Users/drew/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/drb/drb.rb:1523:in `perform'
/Users/drew/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/drb/drb.rb:1601:in `block (2 levels) in main_loop'
/Users/drew/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/drb/drb.rb:1597:in `loop'
/Users/drew/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/drb/drb.rb:1597:in `block in main_loop'
Done.

I'm starting here because spork-minitest is deepest in the stack, and because I verified that guard works properly before attempting to integrate spork.

semaperepelitsa commented 12 years ago

Unfortunately, this gem doesn't work with guard properly. Apparently it works now with guard-spork, since you don't get any errors on the start but guard-minitest is still broken. The issue is that spork-minitest doesn't support any options like -r or -e that the guard is passing.

I have found that it is too complex to fix that and decided to use a Guard MiniTest alternative for testing: Autotest (or ZenTest). Also, I start spork manually. If you still want to use Guard you can try out the original spork-testunit instead of spork-minitest. It is much more complex than mine but should work.

tacomanator commented 12 years ago

Thanks @semaperepelitsa ... I hadn't tried autotest before, I think I'll give it a spin. Wondering if you were able to get minitest/autotest to use spork? Can't quite figure out how to get the drb option passed in correctly.

semaperepelitsa commented 12 years ago

There is a code snippet in the bottom of the readme.

tacomanator commented 12 years ago

Thanks, I missed that before. Did the trick. I also added something to .autotest to run spork if it's not already running. Very kludgy solution I'm sure, but in case anyone is interested (gist: https://gist.github.com/2667886)

require 'socket'
require 'timeout'

def is_port_open?(ip, port)
  begin
    Timeout::timeout(1) do
      begin
        s = TCPSocket.new(ip, port)
        s.close
        return false
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return true
      end
    end
  rescue Timeout::Error
  end

  return true
end

if is_port_open?('localhost', 8988)
  pid = fork do
     `spork`
      exit
  end
  puts "waiting for spork to start"
  while is_port_open?('localhost', 8988) do sleep 0.1 end

end

require 'autotest/restart'
require 'autotest/growl'
require 'autotest/fsevent'
require 'turn/autorun'

Autotest.add_hook :initialize do |at|
  at.testlib = ".minitest"
end

Autotest.add_hook :all_good do |at|
  system "rake rcov_info"
end if ENV['RCOV']

class Autotest
  # run tests over drb server (spork)
  def make_test_cmd files_to_test
    if files_to_test.empty?
      "" # no tests to run
    else
      "testdrb #{files_to_test.keys.join(' ')}"
    end
  end
end
semaperepelitsa commented 12 years ago

Very good! One note: you should use spawn "spork" instead of forking the ruby process and running the command after that.