1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
Feature: Set 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
`#set_environment_variable`. Using this sets the value of a
non-existing variable and overwrites an existing 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`
and the like or `#with_environment`. Besides setting a variable globally, you
can set one for a block of code only using `#with_environment`.
Background:
Given I use the fixture "cli-app"
Scenario: Setting a new variable
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'running env', :type => :aruba do
before do
set_environment_variable 'LONG_LONG_VARIABLE', '1'
run_command('env')
stop_all_commands
end
it "outputs the value set and restores the original" do
expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=1'
expect(ENV['LONG_LONG_VARIABLE']).to be_nil
end
end
"""
When I run `rspec`
Then the specs should all pass
Scenario: Overriding a previously set variable
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'running env', :type => :aruba do
before do
set_environment_variable 'LONG_LONG_VARIABLE', '1'
set_environment_variable 'LONG_LONG_VARIABLE', '2'
run_command('env')
stop_all_commands
end
it "outputs the last values set" do
expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=2'
end
end
"""
When I run `rspec`
Then the specs should all pass
Scenario: Updating a variable previously set via ENV
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
ENV['REALLY_LONG_LONG_VARIABLE'] = '1'
RSpec.describe 'running env', :type => :aruba do
before do
set_environment_variable 'REALLY_LONG_LONG_VARIABLE', '2'
run_command('env')
stop_all_commands
end
it "outputs the value set and restores the original" do
expect(last_command_started.output).to include 'REALLY_LONG_LONG_VARIABLE=2'
expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '1'
end
end
"""
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 do
ENV['REALLY_LONG_LONG_VARIABLE'] = '2'
run_command('env')
stop_all_commands
end
it "outputs the value set and keeps it" do
expect(last_command_started.output).to include 'REALLY_LONG_LONG_VARIABLE=2'
expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '2'
end
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 do
set_environment_variable 'REALLY_LONG_LONG_VARIABLE', '1'
ENV['REALLY_LONG_LONG_VARIABLE'] = '2'
run_command('env')
stop_all_commands
end
it "outputs the value set by #set_environment_variable" do
expect(last_command_started.output).to include 'REALLY_LONG_LONG_VARIABLE=1'
end
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 changed environment temporarily
available for the code run within the block.
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'Environment command', :type => :aruba do
before do
ENV['REALLY_LONG_LONG_VARIABLE'] = '1'
set_environment_variable 'REALLY_LONG_LONG_VARIABLE', '2'
end
it do
with_environment do
expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '2'
end
expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '1'
end
end
"""
When I run `rspec`
Then the specs should all pass
Scenario: Run some ruby code with local environment
If you need to set some environment variables only for the given block.
Pass it an `Hash` containing the environment variables.
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'Environment command', :type => :aruba do
before do
ENV['REALLY_LONG_LONG_VARIABLE'] = '1'
set_environment_variable 'REALLY_LONG_LONG_VARIABLE', '2'
end
it do
with_environment 'REALLY_LONG_LONG_VARIABLE' => '3' do
expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '3'
end
expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '1'
end
end
"""
When I run `rspec`
Then the specs should all pass
Scenario: When an error occures the ENV is not polluted
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'Environment command', :type => :aruba do
before do
ENV['REALLY_LONG_LONG_VARIABLE'] = '1'
set_environment_variable 'REALLY_LONG_LONG_VARIABLE', '2'
end
it do
begin
with_environment 'REALLY_LONG_LONG_VARIABLE' => '3' do
fail
end
rescue StandardError
end
expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '1'
end
end
"""
When I run `rspec`
Then the specs should all pass
Scenario: Run some ruby code with nested environment blocks
It is possible to use a `#with_environment`-block with a
`#with_environment`-block. Each previously set variable is available with
the most inner block.
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'Environment command', :type => :aruba do
before do
ENV['LONG_LONG_VARIABLE'] = '1'
ENV['REALLY_LONG_LONG_VARIABLE'] = '1'
end
it do
with_environment 'REALLY_LONG_LONG_VARIABLE' => 2 do
with_environment 'LONG_LONG_VARIABLE' => 3 do
expect(ENV['LONG_LONG_VARIABLE']).to eq '3'
expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '2'
end
end
expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '1'
end
end
"""
When I run `rspec`
Then the specs should all pass
@unsupported-on-platform-windows
Scenario: Mixed-Case variable
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'Environment command', :type => :aruba do
before { set_environment_variable 'long_LONG_VARIABLE', '1' }
before { run_command('env') }
before { stop_all_commands }
it { expect(last_command_started.output).to include 'long_LONG_VARIABLE=1' }
end
"""
When I run `rspec`
Then the specs should all pass
@unsupported-on-platform-unix
@unsupported-on-platform-mac
Scenario: Mixed-Case variable
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'Environment command', :type => :aruba do
before { set_environment_variable 'long_LONG_VARIABLE', '1' }
before { run_command('env') }
before { stop_all_commands }
it { expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=1' }
end
"""
When I run `rspec`
Then the specs should all pass
|