Skip to content

Commit ee7ef88

Browse files
committed
[spec/] Avoid ENV overwrite in specs
The APPLIANCE_PG_DATA and APPLIANCE_PG_SERVICE environment variables are used by both the database_replication_spec.rb and postgres_admin_spec.rb specs. The previous implementation, specs in both database_replication_spec.rb and postgres_admin_spec.rb would delete/overwrite the ENV variables that were assumed to be set for the other to run it's processes. In this case, it is simpler to store the variable in a tmp variable before overwriting it for the purposes of the spec in question, and then reset the var to it's original value when the spec is finished. That way other specs will be able to use it with out issue, regardless of what order the specs are triggered from.
1 parent 92f6479 commit ee7ef88

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

spec/database_replication_spec.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@
7777
end
7878

7979
before do
80-
ENV["APPLIANCE_PG_DATA"] = "/var/lib/pgsql"
80+
@appliance_pg_data_old = ENV["APPLIANCE_PG_DATA"]
81+
@appliance_pg_service_old = ENV["APPLIANCE_PG_DATA"]
82+
ENV["APPLIANCE_PG_DATA"] = "/var/lib/pgsql"
8183
ENV["APPLIANCE_PG_SERVICE"] = "postgresql-9.5"
8284
end
8385

8486
after do
85-
ENV.delete("APPLIANCE_PG_DATA")
86-
ENV.delete("APPLIANCE_PG_SERVICE")
87+
ENV["APPLIANCE_PG_DATA"] = @appliance_pg_data_old
88+
ENV["APPLIANCE_PG_SERVICE"] = @appliance_pg_service_old
8789
end
8890

8991
it "returns the correct contents" do

spec/postgres_admin_spec.rb

+19-13
Original file line numberDiff line numberDiff line change
@@ -274,23 +274,24 @@
274274
end
275275

276276
context "ENV dependent" do
277-
after do
278-
ENV.delete_if { |k, _| k.start_with?("APPLIANCE") }
279-
end
280-
281277
[%w(data_directory APPLIANCE_PG_DATA /some/path true),
282278
%w(service_name APPLIANCE_PG_SERVICE postgresql ),
283279
%w(package_name APPLIANCE_PG_PACKAGE_NAME postgresql-server ),
284280
%w(template_directory APPLIANCE_TEMPLATE_DIRECTORY /some/path true),
285281
%w(mount_point APPLIANCE_PG_MOUNT_POINT /mount/point true)
286282
].each do |method, var, value, pathname_required|
287283
it method.to_s do
288-
ENV[var] = value
289-
result = described_class.public_send(method)
290-
if pathname_required
291-
expect(result.join("abc/def").to_s).to eql "#{value}/abc/def"
292-
else
293-
expect(result).to eql value
284+
begin
285+
old_var = ENV[var]
286+
ENV[var] = value
287+
result = described_class.public_send(method)
288+
if pathname_required
289+
expect(result.join("abc/def").to_s).to eql "#{value}/abc/def"
290+
else
291+
expect(result).to eql value
292+
end
293+
ensure
294+
ENV[var] = old_var
294295
end
295296
end
296297
end
@@ -301,9 +302,14 @@
301302

302303
context "with a data directory" do
303304
around do |example|
304-
Dir.mktmpdir do |dir|
305-
ENV["APPLIANCE_PG_DATA"] = dir
306-
example.run
305+
begin
306+
old_appliance_pg_data = ENV["APPLIANCE_PG_DATA"]
307+
Dir.mktmpdir do |dir|
308+
ENV["APPLIANCE_PG_DATA"] = dir
309+
example.run
310+
end
311+
ensure
312+
ENV["APPLIANCE_PG_DATA"] = old_appliance_pg_data
307313
end
308314
end
309315

0 commit comments

Comments
 (0)