Search code examples
rubyrspecrubygemsrakebundler

Missing mutant-rspec?


I'm trying to set up a new Ruby project on a newly installed Debian 8.5 using the ruby 2.1.5+deb8u2 an ruby-dev 2.1.5+deb8u2 packages from the repository. I want to include mutation testing in my project. I added mutant-rspec to my Gemfile:

source 'https://rubygems.org'

gem 'rake'
gem 'rspec', require: 'spec'
gem 'mutant-rspec'

Bundler (1.13.0) seems to install the gem just fine

ytg@debian-vm:~/projects/new_project$ bundle install
Fetching gem metadata from https://rubygems.org/..........
Fetching version metadata from https://rubygems.org/.
Using rake 11.2.2
....
Using rspec-core 3.5.3
Using rspec-expectations 3.5.0
Using rspec-mocks 3.5.0
...
Using rspec 3.5.0
...
Installing mutant 0.8.11
Installing mutant-rspec 0.8.11
Bundle complete! 5 Gemfile dependencies, 34 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

Now if I try to run mutant, using rake mutant --trace with the rake file

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
require 'mutant'

RSpec::Core::RakeTask.new :spec

task default: :spec

RuboCop::RakeTask.new

task :mutant do
  result = Mutant::CLI.run %w(--since problem9 -Ilib -Ispec
                              --use rspec MyProject*)
  raise 'Mutation testing failed' unless result
end

I get an error message:

warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.8-compliant syntax, but you are running 2.1.5.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
** Invoke mutant (first_time)
** Execute mutant
Could not load integration "rspec" (you may want to try installing the gem mutant-rspec)
rake aborted!
Mutation testing failed
/home/ytg/projects/new_project/Rakefile:15:in `block in <top (required)>'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/task.rb:248:in `call'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/task.rb:248:in `block in execute'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/task.rb:243:in `each'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/task.rb:243:in `execute'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/usr/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/task.rb:173:in `invoke'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/application.rb:152:in `invoke_task'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/application.rb:108:in `block (2 levels) in top_level'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/application.rb:108:in `each'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/application.rb:108:in `block in top_level'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/application.rb:117:in `run_with_threads'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/application.rb:102:in `top_level'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/application.rb:80:in `block in run'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/application.rb:178:in `standard_exception_handling'
/var/lib/gems/2.1.0/gems/rake-11.2.2/lib/rake/application.rb:77:in `run'
/var/lib/gems/2.1.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/usr/local/bin/rake:23:in `load'
/usr/local/bin/rake:23:in `<main>'
Tasks: TOP => mutant

The same message appears if I try to do it directly

ytg@debian-vm:~/projects/new_project$ mutant -I lib/ --use rspec MyProject*
warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.8-compliant syntax, but you are running 2.1.5.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
Could not load integration "rspec" (you may want to try installing the gem mutant-rspec)

As I understand this is because of some loading issue. But what loading issue exactly? How can I run mutant on my project?

(I also tried to install the gem with sudo gem install mutant-rspec just in case, but it didn't help, I get the same error message.)


Solution

  • The mutant you are running is likely not executing in the right environment. Try bundle exec mutant instead, which will ensure the one specified in your Gemfile is being used alongside the right set of gems.

    To help debug, use which mutant to figure out which version is getting called. sudo gem install should not be a solution, I would recommend sudo gem uninstall to revert back to where you were, otherwise it may cause problems later.

    But I think all of that is a red herring.

    For your Rakefile, it isn't actually shelling out to the mutant script. You're bypassing it and going direct to Mutant::CLI. The problem here is that nothing is setting up the load path so that it would include mutant-rspec:

    task :mutant do
      puts $LOAD_PATH # debug: see it's missing load path for mutant-rspec
      require 'bundler/setup' # This line is new
      puts $LOAD_PATH # debug: see how it changes
    
      result = Mutant::CLI.run %w(--since problem9 -Ilib -Ispec
                                  --use rspec MyProject*)
      raise 'Mutation testing failed' unless result
    end
    

    Or alternatively, bundle exec rake mutant should also do the same thing. I'd recommend always using bundle exec (or binstubs), rather than whatever rake happens to be on your path. Otherwise it's hard to reason about.