This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
forked from cucumber/aruba
-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_environment_variable.feature
376 lines (282 loc) · 11.3 KB
/
set_environment_variable.feature
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
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: Non-existing variable
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 'LONG_LONG_VARIABLE', '1' }
before(:each) { run('env') }
before(:each) { 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
Scenario: Existing variable set from within the test
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 'LONG_LONG_VARIABLE', '1' }
before(:each) { set_environment_variable 'LONG_LONG_VARIABLE', '2' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
it { expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=2' }
end
"""
When I run `rspec`
Then the specs should all pass
Scenario: Existing variable set by some outer parent process
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
ENV['REALLY_LONG_LONG_VARIABLE'] = '1'
RSpec.describe 'Environment command', :type => :aruba do
before(:each) { set_environment_variable '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' }
it { expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '1' }
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(: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
avaiable for the code run within the block.
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
ENV['REALLY_LONG_LONG_VARIABLE'] = '1'
RSpec.describe 'Environment command', :type => :aruba do
before(:each) { set_environment_variable 'REALLY_LONG_LONG_VARIABLE', '2' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
it do
with_environment do
expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '2'
end
end
it { expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '1' }
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'
ENV['REALLY_LONG_LONG_VARIABLE'] = '1'
RSpec.describe 'Environment command', :type => :aruba do
before(:each) { set_environment_variable 'REALLY_LONG_LONG_VARIABLE', '2' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
it do
with_environment 'REALLY_LONG_LONG_VARIABLE' => '3' do
expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '3'
end
end
it { expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '1' }
end
"""
When I run `rspec`
Then the specs should all pass
Scenario: Nested setup with rspec
It doesn't matter if you define an environment variable in some outer
scope, when you are using `RSpec`.
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 'LONG_LONG_VARIABLE', '1' }
describe 'Method XX' do
before(:each) { run('env') }
before(:each) { stop_all_commands }
it { expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=1' }
end
describe 'Method YY' do
before(:each) { set_environment_variable 'LONG_LONG_VARIABLE', '2' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
it { expect(last_command_started.output).to include 'LONG_LONG_VARIABLE=2' }
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'
ENV['REALLY_LONG_LONG_VARIABLE'] = '1'
RSpec.describe 'Environment command', :type => :aruba do
before(:each) { set_environment_variable 'REALLY_LONG_LONG_VARIABLE', '2' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
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
it { expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '1' }
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'
ENV['LONG_LONG_VARIABLE'] = '1'
ENV['REALLY_LONG_LONG_VARIABLE'] = '1'
RSpec.describe 'Environment command', :type => :aruba do
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
end
it { expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '1' }
end
"""
When I run `rspec`
Then the specs should all pass
Scenario: Re-use `#with_environment` for multiple `RSpec`-`it`-blocks
If you chose to run wrap examples via `RSpec`'s `around`-hook, make sure you
use `before(:context) {}` instead of `before(:each)` to set an environment
variable. Only then the `before`-hook is run before the `around`-hook is
run.
Given a file named "spec/environment_spec.rb" with:
"""ruby
require 'spec_helper'
RSpec.describe 'Environment command', :type => :aruba do
# Please mind :context. This is run BEFORE the `around`-hook
before(:context) { set_environment_variable 'REALLY_LONG_LONG_VARIABLE', '1' }
context 'when no arguments are given' do
around(:each) do |example|
with_environment do
example.run
end
end
it { expect(ENV['REALLY_LONG_LONG_VARIABLE']).to eq '1' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
it { expect(last_command_started.output).to include 'REALLY_LONG_LONG_VARIABLE=1' }
end
context 'when arguments given' do
around(:each) do |example|
with_environment 'LONG_LONG_VARIABLE' => 2 do
example.run
end
end
it { expect(ENV['LONG_LONG_VARIABLE']).to eq '2' }
before(:each) { run('env') }
before(:each) { stop_all_commands }
it { expect(last_command_started.output).to include 'REALLY_LONG_LONG_VARIABLE=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(:each) { set_environment_variable 'long_LONG_VARIABLE', '1' }
before(:each) { run('env') }
before(:each) { 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(:each) { set_environment_variable 'long_LONG_VARIABLE', '1' }
before(:each) { run('env') }
before(:each) { 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
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'
$LOAD_PATH << File.expand_path('../../lib', __FILE__)
RSpec.describe 'Environment command', :type => :aruba do
before(:each) do
require 'my_library'
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