Cucumber / Aruba / Docs / 04 aruba api / Environment
Prepend environment variable
It is quite handy to modify the environment of a process. To make this possible, aruba provides several methods. One of these is #prepend_environment_variable. Using this variable prepends a given value to an existing one. If the variable does not exist, it is created with the given value.
Each variable name and each value is converted to a string. Otherwise ruby would complain about an invalid argument. To make use of a variable you can either use #run_command and the like or #with_environment.
Background:
- Given I use the fixture "
cli-app"
Scenario: Non-existing variable
- Given a file named "
spec/environment_spec.rb" with:rubyrequire 'spec_helper' RSpec.describe 'Long running command', :type => :aruba do before { prepend_environment_variable 'LONG_LONG_VARIABLE', 'a' } before { run_command('env') } before { stop_all_commands } it { expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=a' } end - When I run `rspec`
- Then the specs should all pass
Scenario: Existing inner variable
- Given a file named "
spec/environment_spec.rb" with:rubyrequire 'spec_helper' RSpec.describe 'Long running command', :type => :aruba do before { prepend_environment_variable 'LONG_LONG_VARIABLE', 'a' } before { prepend_environment_variable 'LONG_LONG_VARIABLE', 'b' } before { run_command('env') } before { stop_all_commands } it { expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=ba' } end - When I run `rspec`
- Then the specs should all pass
Scenario: Existing outer variable
- Given a file named "
spec/environment_spec.rb" with:rubyrequire 'spec_helper' ENV['REALLY_LONG_LONG_VARIABLE'] = 'a' RSpec.describe 'Long running command', :type => :aruba do before { prepend_environment_variable 'REALLY_LONG_LONG_VARIABLE', 'b' } before { run_command('env') } before { stop_all_commands } it { expect(last_command_started.output).to include 'REALLY_LONG_LONG_VARIABLE=ba' } # Has no effect here, is not in block and not a command `run` it { expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq 'a' } end - When I run `rspec`
- Then the specs should all pass
Scenario: Run some ruby code with previously set environment
- Given a file named "
spec/environment_spec.rb" with:rubyrequire 'spec_helper' ENV['REALLY_LONG_LONG_VARIABLE'] = 'a' RSpec.describe 'Long running command', :type => :aruba do before { prepend_environment_variable 'REALLY_LONG_LONG_VARIABLE', 'b' } before { run_command('env') } before { stop_all_commands } it do with_environment do expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq 'ba' end end # Has no effect here, is not in block and not a command `run` it { expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq 'a' } end - When I run `rspec`
- Then the specs should all pass
Scenario: Run some ruby code with local environment
If you pass the same variable to the block it will not be prepended, but overwrites the variable
- Given a file named "
spec/environment_spec.rb" with:rubyrequire 'spec_helper' ENV['REALLY_LONG_LONG_VARIABLE'] = 'a' RSpec.describe 'Long running command', :type => :aruba do before { prepend_environment_variable 'REALLY_LONG_LONG_VARIABLE', 'b' } before { run_command('env') } before { stop_all_commands } it do with_environment 'REALLY_LONG_LONG_VARIABLE' => 'a' do expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq 'a' end end # Has no effect here, is not in block and not a command `run` it { expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq 'a' } end - When I run `rspec`
- Then the specs should all pass
Source: features/04_aruba_api/environment/prepend_environment_variable.feature at commit c39310a7, Aruba (MIT license).