cucumber / aruba

Test command-line applications with Cucumber-Ruby, RSpec or Minitest.
MIT License
948 stars 163 forks source link

with_env behaviour with multiple environment variables #223

Closed jennifersmith closed 9 years ago

jennifersmith commented 9 years ago

I see that recently @dg-ratiodata added a helpful method with_env to Aruba::API (#188) that allows you to run a block with environment variables set which are then restored back to original values.

However, the current implementation seems to run the block once for each env var you pass into the hash.

    def with_env(env = {}, &block)
      env.each do |k,v|
        set_env k, v
        block.call
        restore_env
      end
    end

Is this the intended behaviour?

Context: I want to set a series of env vars before running my process all together,

I can achieve this by calling the underlying methods directly, but I wondered whether you would be happy for me to change this method to set all the vars just once and then call the block. Something like:

    def with_env(env = {}, &block)
      env.each do |k,v|
        set_env k, v
      end

      block.call
      restore_env
    end

Appreciate that this breaks existing behaviour and so might be a breaking change for some people - so I would be happy to rename this method or use an alternative approach.

ghost commented 9 years ago

Good question, why I implemented it this way. :smile: But I think your implementation was the one I had in mind.

The only thing I would change is the style of the each-loop.

env.each { |k,v| set_env k, v }

@jarl-dk Should he provide a PR?

ghost commented 9 years ago

@jarl-dk Should he provide a PR?

@jarl-dk Should she provide a PR? Sorry for that @jennifersmith

jarl-dk commented 9 years ago

Thanks for this.

jennifersmith commented 9 years ago

Ha no worries on both counts, thanks for addressing this (and for a very useful library) On 15 Jan 2015 00:37, "Jarl Friis" notifications@github.com wrote:

Thanks for this.

— Reply to this email directly or view it on GitHub https://github.com/cucumber/aruba/issues/223#issuecomment-69916469.