easyjo
easyjo

Reputation: 734

Execute tests for another app from a rake file

I'm trying to execute cucumber tests for a project within a rake file in another project.

Currently I am trying this:

system "cd /path/to/project;rvm use --create 1.9.2-p290@test; cucumber features/test.feature"

This works for the cd, and the rvm seems to work if I run which ruby after the rvm use... but the problem is that the cucumber gem seems to be called from the current folder (not the app to test folder).

The error I get is:

cucumber is not part of the bundle. Add it to Gemfile. (Gem::LoadError)

It seems to be using the local gemset version of cucumber rather than the @test gemset.

Any thoughts on this?

Or is there a better way to run cucumber tests for another project that relies on rvm & a different bundle?

Upvotes: 1

Views: 710

Answers (2)

Sujimichi
Sujimichi

Reputation: 466

I've also been trying to do exactly the same thing; run an application's tests (or any rake task) from inside another 'control' application.

Reason: (just so I don't get served with a "why on earth?")

I am trying to build an application (rather like cruisecontrol.rb ) which can monitor, schedule and review the specs for a set of apps.

After some digging around in cruisecontrol's source I found that Bundler provides a solution;

Bundler.with_clean_env do 
  system "rake spec"
end

see line56 of https://github.com/thoughtworks/cruisecontrol.rb/blob/master/lib/platform.rb

That steps out of the bundle and the command is run without the control app's gems.

BUT as is most likely, the command uses bundle exec then this stops working.

Bundler.with_clean_env { system "bundle exec rake spec" }

And you are right back to the exact same problem. This is caused by some bundler variables still existing and being inherited by the sub-shell. Full (very good) explanation here.

The solution is to change the with_clean_env method on bundler like this;

BUNDLER_VARS = %w(BUNDLE_GEMFILE RUBYOPT BUNDLE_BIN_PATH)
module Bundler
  def self.with_clean_env &blk
    bundled_env = ENV.to_hash
    BUNDLER_VARS.each{ |var| ENV.delete(var) }
    yield
  ensure
    ENV.replace(bundled_env.to_hash)     
  end
end

above code from here

I put that in the environment.rb of my control application (it should probably be in a initializer?) and now I can run the specs of another app from within the control app.

#in control app
result = nil
Dir.chdir(test_app_path) #move into test app
Bundler.with_clean_env { result = `bundle exec rake spec` } #run test apps specs
puts result #display result inside control app

Upvotes: 4

pythonandchips
pythonandchips

Reputation: 2195

Changing the ; in your script to && seems to work.

Upvotes: 0

Related Questions