Skip to content

Commit

Permalink
Make sure that environment variables set by external libraries are taken
Browse files Browse the repository at this point in the history
into account
  • Loading branch information
maxmeyer committed Jan 1, 2016
1 parent 4adc55c commit bc0f98b
Show file tree
Hide file tree
Showing 13 changed files with 327 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Feature: Remove existing environment variable via API-method
Feature: Delete existing environment variable via API-method

It is quite handy to modify the environment of a process. To make this
possible, `aruba` provides several methods. One of these is
Expand Down
68 changes: 68 additions & 0 deletions features/api/environment/set_environment_variable.feature
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,46 @@ Feature: Set environment variable via API-method
When I run `rspec`
Then the specs should all pass

Scenario: Set variable via ENV

Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'Environment command', :type => :aruba do
before(:each) { ENV['REALLY_LONG_LONG_VARIABLE'] = '2' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
it { expect(last_command_started.output).to include 'REALLY_LONG_LONG_VARIABLE=2' }
end
"""
When I run `rspec`
Then the specs should all pass

Scenario: Existing variable set in before block in RSpec

Setting environment variables with `#set_environment_variable('VAR', 'value')` takes
precedence before setting variables with `ENV['VAR'] = 'value'`.

Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'Environment command', :type => :aruba do
before(:each) { set_environment_variable 'REALLY_LONG_LONG_VARIABLE', '1' }
before(:each) { ENV['REALLY_LONG_LONG_VARIABLE'] = '2' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
it { expect(last_command_started.output).to include 'REALLY_LONG_LONG_VARIABLE=1' }
end
"""
When I run `rspec`
Then the specs should all pass

Scenario: Run some ruby code in code with previously set environment

The `#with_environment`-block makes the change environment temporary
Expand Down Expand Up @@ -304,3 +344,31 @@ Feature: Set environment variable via API-method
"""
When I run `rspec`
Then the specs should all pass
Scenario: External ruby file / ruby gem modifying ENV
There are some Rubygems around which need to modify ENV['NODE_PATH'] like
[`ruby-stylus`](https://github.com/forgecrafted/ruby-stylus/blob/e7293362dc8cbf550f7c317d721ba6b9087e8833/lib/stylus.rb#L168).
This is supported by aruba as well.
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'

RSpec.describe 'Environment command', :type => :aruba do
before(:each) do
require_relative File.expand_path('../../lib/my_library.rb', __FILE__)
end

before(:each) { run('env') }
before(:each) { stop_all_commands }

it { expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=1' }
end
"""
And a file named "lib/my_library.rb" with:
"""
ENV['LONG_LONG_VARIABLE'] = '1'
"""
When I run `rspec`
Then the specs should all pass
129 changes: 129 additions & 0 deletions features/configuration/command_runtime_environment.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
Feature: Define default process environment
Say you want to have a default set of environment variables, then use this
code.

~~~ruby
Aruba.configure do |config|
config.command_runtime_environment = { 'LONG_LONG_VARIABLE' => 'x' }
end
~~~

This can be changed via `#set_environment_variable`,
`#append_environment_variable`, `#delete_environment_variable` or
`#prepend_environment_variable`.

Background:
Given I use the fixture "cli-app"

Scenario: Overwrite existing variable with new default value
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
ENV['LONG_LONG_VARIABLE'] = 'y'
Aruba.configure do |config|
config.command_runtime_environment = { 'LONG_LONG_VARIABLE' => 'x' }
end
RSpec.describe 'Environment command', :type => :aruba do
before(:each) { run('env') }
before(:each) { stop_all_commands }
it { expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=x' }
end
"""
When I run `rspec`
Then the specs should all pass

Scenario: Overwrite default value for variable
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
ENV['LONG_LONG_VARIABLE'] = 'y'
Aruba.configure do |config|
config.command_runtime_environment = { 'LONG_LONG_VARIABLE' => 'x' }
end
RSpec.describe 'Environment command', :type => :aruba do
before(:each) { set_environment_variable 'LONG_LONG_VARIABLE', 'z' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
it { expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=z' }
end
"""
When I run `rspec`
Then the specs should all pass

Scenario: Append value to default value
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
ENV['LONG_LONG_VARIABLE'] = 'y'
Aruba.configure do |config|
config.command_runtime_environment = { 'LONG_LONG_VARIABLE' => 'x' }
end
RSpec.describe 'Environment command', :type => :aruba do
before(:each) { append_environment_variable 'LONG_LONG_VARIABLE', 'z' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
it { expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=xz' }
end
"""
When I run `rspec`
Then the specs should all pass

Scenario: Prepend value
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
ENV['LONG_LONG_VARIABLE'] = 'y'
Aruba.configure do |config|
config.command_runtime_environment = { 'LONG_LONG_VARIABLE' => 'x' }
end
RSpec.describe 'Environment command', :type => :aruba do
before(:each) { prepend_environment_variable 'LONG_LONG_VARIABLE', 'z' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
it { expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=zx' }
end
"""
When I run `rspec`
Then the specs should all pass

Scenario: Remove variable from default set of variables
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
ENV['LONG_LONG_VARIABLE'] = 'y'
Aruba.configure do |config|
config.command_runtime_environment = { 'LONG_LONG_VARIABLE' => 'x' }
end
RSpec.describe 'Environment command', :type => :aruba do
before(:each) { delete_environment_variable 'LONG_LONG_VARIABLE' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
it { expect(last_command_started.output).not_to include 'LONG_LONG_VARIABLE' }
end
"""
When I run `rspec`
Then the specs should all pass
5 changes: 3 additions & 2 deletions lib/aruba/api/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def run(*args)
@commands ||= []
@commands << cmd

environment = aruba.environment.to_h
environment = aruba.environment
working_directory = expand_path('.')
event_bus = aruba.event_bus

Expand Down Expand Up @@ -202,7 +202,7 @@ def run(*args)
:exit_timeout => exit_timeout,
:io_wait_timeout => io_wait_timeout,
:working_directory => working_directory,
:environment => environment,
:environment => environment.to_hash,
:main_class => main_class,
:stop_signal => stop_signal,
:startup_wait_time => startup_wait_time,
Expand All @@ -217,6 +217,7 @@ def run(*args)
end

aruba.config.before(:command, self, command)

command.start

aruba.announcer.announce(:stop_signal, command.pid, stop_signal) if stop_signal
Expand Down
2 changes: 2 additions & 0 deletions lib/aruba/api/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ def expand_path(file_name, dir_string = nil)
def with_environment(env = {}, &block)
old_aruba_env = aruba.environment.to_h

# make sure the old environment is really restored in "ENV"
Aruba.platform.with_environment aruba.environment.update(env).to_h, &block
ensure
# make sure the old environment is really restored in "aruba.environment"
aruba.environment.clear
aruba.environment.update old_aruba_env
end
Expand Down
2 changes: 1 addition & 1 deletion lib/aruba/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Configuration < BasicConfiguration
option_accessor :io_wait_timeout, :contract => { Num => Num }, :default => 0.1
option_accessor :startup_wait_time, :contract => { Num => Num }, :default => 0
option_accessor :fixtures_directories, :contract => { Array => ArrayOf[String] }, :default => %w(features/fixtures spec/fixtures test/fixtures fixtures)
option_accessor :command_runtime_environment, :contract => { Hash => Hash }, :default => ENV.to_hash.dup
option_accessor :command_runtime_environment, :contract => { Hash => Hash }, :default => ENV.to_hash
# rubocop:disable Metrics/LineLength
option_accessor(:command_search_paths, :contract => { ArrayOf[String] => ArrayOf[String] }) { |config| [File.join(config.root_directory.value, 'bin')] }
# rubocop:enable Metrics/LineLength
Expand Down
3 changes: 0 additions & 3 deletions lib/aruba/cucumber/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
end

Before do
# this is ENV by default ...
aruba.environment.update aruba.config.command_runtime_environment

# ... so every change needs to be done later
prepend_environment_variable 'PATH', aruba.config.command_search_paths.join(':') + ':'
set_environment_variable 'HOME', aruba.config.home_directory
Expand Down
Loading

0 comments on commit bc0f98b

Please sign in to comment.