Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Ruby-based CLIs with Aruba and Bundler

I have an RSpec suite, run via Bundler, that is testing a number of different command-line applications using Aruba. It works fine ... as long as the command being tested is not itself written in Ruby using Bundler. But I cannot figure out how to prevent the RSpec suite's bundler config from interfering with the execution of commands that themselves use Bundler - at least, not without extreme measures.

I have tried various permutations of unset_bundler_env_vars and with_clean_env, to no avail. Here's an example of a technique I thought would work:

describe 'my ruby app' do 
  before :each { unset_bundler_env_vars }
  it 'should work' do
    Bundler.with_clean_env { run_simple ruby_command_name }
  end
end

I also tried unset_bundler_env_vars without with_clean_env, and vice-versa, in case they interfered with each other. No dice.

The only way I've gotten it to work is to massage Aruba's copy of the environment manually, like this:

before :all do
  aruba.environment.tap do |env|
    if env.include? 'BUNDLE_ORIG_PATH' then
      env['PATH'] = env['BUNDLE_ORIG_PATH']
      %w(BUNDLE_BIN_PATH BUNDLE_GEMFILE BUNDLE_ORIG_PATH GEM_HOME RBENV_DIR
         RBENV_HOOK_PATH RUBYLIB RUBYOPT).each do |key|
        env.delete key
      end
    end
  end
end

There must be a better way. Neither the test suite nor the command being tested should know or care what language the other is written in. And my test code that uses Aruba and Bundler should not need to know the details of how bundle exec affects the process environment.

So what am I doing wrong? How should I be doing this?

like image 267
Mark Reed Avatar asked Aug 29 '16 20:08

Mark Reed


1 Answers

It looks like unset_bundler_env_vars is deprecated and replaced by delete_by_environment_variable which requires a string param (source).

You might try before :each { delete_environment_variable('BUNDLE_GEMFILE') } in your spec. If that does not work, you may need to iterate through the PATH variable list to delete each one.

In the deprecation notice, there is a work-around, though I am not sure how brittle that would be moving forward.

unset_bundler_env_vars
aruba.environment.clear.update(ENV) 

Hope this helps.

like image 165
nrako Avatar answered Nov 07 '22 11:11

nrako