Skip to content

Commit c595899

Browse files
authored
Introduce stub_env helper (#210)
1 parent 38acf16 commit c595899

9 files changed

+108
-25
lines changed

spec/command/copy_image_from_upstream_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
let!(:app) { dummy_test_app }
4747

4848
before do
49-
ENV["CPLN_UPSTREAM"] = upstream_app
49+
stub_env("CPLN_UPSTREAM", upstream_app)
5050
end
5151

5252
it "raises error" do
@@ -62,9 +62,9 @@
6262
let!(:app) { dummy_test_app }
6363

6464
before do
65-
ENV["CPLN_UPSTREAM"] = upstream_app
65+
stub_env("CPLN_UPSTREAM", upstream_app)
6666
# Ideally, we should have a different org, but for testing purposes, this works
67-
ENV["CPLN_ORG_UPSTREAM"] = dummy_test_org
67+
stub_env("CPLN_ORG_UPSTREAM", dummy_test_org)
6868

6969
run_cpflow_command!("apply-template", "app", "-a", upstream_app)
7070
run_cpflow_command!("apply-template", "app", "-a", app)
@@ -93,9 +93,9 @@
9393
let!(:app) { dummy_test_app }
9494

9595
before do
96-
ENV["CPLN_UPSTREAM"] = upstream_app
96+
stub_env("CPLN_UPSTREAM", upstream_app)
9797
# Ideally, we should have a different org, but for testing purposes, this works
98-
ENV["CPLN_ORG_UPSTREAM"] = dummy_test_org
98+
stub_env("CPLN_ORG_UPSTREAM", dummy_test_org)
9999

100100
run_cpflow_command!("apply-template", "app", "-a", upstream_app)
101101
run_cpflow_command!("apply-template", "app", "-a", app)

spec/command/deploy_image_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
let!(:app) { dummy_test_app("invalid-release-script") }
6363

6464
before do
65-
ENV["APP_NAME"] = app
65+
stub_env("APP_NAME", app)
6666

6767
allow(Kernel).to receive(:sleep)
6868

@@ -92,7 +92,7 @@
9292
let!(:app) { dummy_test_app("rails-non-app-image", create_if_not_exists: true) }
9393

9494
before do
95-
ENV["APP_NAME"] = app
95+
stub_env("APP_NAME", app)
9696

9797
allow(Kernel).to receive(:sleep)
9898

spec/command/promote_app_from_upstream_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
let!(:app) { dummy_test_app("nothing") }
1010

1111
before do
12-
ENV["CPLN_UPSTREAM"] = upstream_app
12+
stub_env("CPLN_UPSTREAM", upstream_app)
1313
# Ideally, we should have a different org, but for testing purposes, this works
14-
ENV["CPLN_ORG_UPSTREAM"] = dummy_test_org
14+
stub_env("CPLN_ORG_UPSTREAM", dummy_test_org)
1515

1616
run_cpflow_command!("apply-template", "app", "-a", upstream_app)
1717
run_cpflow_command!("apply-template", "app", "rails", "-a", app)
@@ -40,10 +40,10 @@
4040
let!(:app) { dummy_test_app("invalid-release-script") }
4141

4242
before do
43-
ENV["CPLN_UPSTREAM"] = upstream_app
43+
stub_env("CPLN_UPSTREAM", upstream_app)
4444
# Ideally, we should have a different org, but for testing purposes, this works
45-
ENV["CPLN_ORG_UPSTREAM"] = dummy_test_org
46-
ENV["APP_NAME"] = app
45+
stub_env("CPLN_ORG_UPSTREAM", dummy_test_org)
46+
stub_env("APP_NAME", app)
4747

4848
run_cpflow_command!("apply-template", "app", "-a", upstream_app)
4949
run_cpflow_command!("apply-template", "app", "rails", "postgres", "-a", app)
@@ -77,10 +77,10 @@
7777
let!(:app) { dummy_test_app }
7878

7979
before do
80-
ENV["CPLN_UPSTREAM"] = upstream_app
80+
stub_env("CPLN_UPSTREAM", upstream_app)
8181
# Ideally, we should have a different org, but for testing purposes, this works
82-
ENV["CPLN_ORG_UPSTREAM"] = dummy_test_org
83-
ENV["APP_NAME"] = app
82+
stub_env("CPLN_ORG_UPSTREAM", dummy_test_org)
83+
stub_env("APP_NAME", app)
8484

8585
run_cpflow_command!("apply-template", "app", "-a", upstream_app)
8686
run_cpflow_command!("apply-template", "app", "rails", "postgres", "-a", app)

spec/command/setup_app_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@
162162
let!(:app) { dummy_test_app("undefined-org") }
163163

164164
before do
165-
allow(ENV).to receive(:fetch).and_call_original
166-
allow(ENV).to receive(:fetch).with("CPLN_ORG", nil).and_return(nil)
165+
stub_env("CPLN_ORG", nil)
167166
end
168167

169168
after do

spec/core/controlplane_api_direct_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
describe "#api_host" do
99
it "returns correct host for 'api' when CPLN_ENDPOINT is not set" do
10-
allow(ENV).to receive(:fetch).with("CPLN_ENDPOINT", "https://api.cpln.io").and_return("https://api.cpln.io")
10+
stub_env("CPLN_ENDPOINT", nil)
1111

1212
host = described_instance.api_host(:api)
1313

1414
expect(host).to eq("https://api.cpln.io")
1515
end
1616

1717
it "returns correct host for 'api' when CPLN_ENDPOINT is set" do
18-
allow(ENV).to receive(:fetch).with("CPLN_ENDPOINT", "https://api.cpln.io").and_return("http://api.cpln.io")
18+
stub_env("CPLN_ENDPOINT", "http://api.cpln.io")
1919

2020
host = described_instance.api_host(:api)
2121

@@ -39,7 +39,7 @@
3939
end
4040

4141
it "returns token from CPLN_TOKEN" do
42-
allow(ENV).to receive(:fetch).with("CPLN_TOKEN", nil).and_return("token_1")
42+
stub_env("CPLN_TOKEN", "token_1")
4343

4444
result = described_instance.api_token
4545

@@ -48,7 +48,7 @@
4848
end
4949

5050
it "returns token from 'cpln profile token'" do
51-
allow(ENV).to receive(:fetch).with("CPLN_TOKEN", nil).and_return(nil)
51+
stub_env("CPLN_TOKEN", nil)
5252
allow(Shell).to receive(:cmd).with("cpln", "profile", "token").and_return({ output: "token_2" })
5353

5454
result = described_instance.api_token
@@ -58,7 +58,7 @@
5858
end
5959

6060
it "raises error if token is not found" do
61-
allow(ENV).to receive(:fetch).with("CPLN_TOKEN", nil).and_return(nil)
61+
stub_env("CPLN_TOKEN", nil)
6262
allow(Shell).to receive(:cmd).with("cpln", "profile", "token").and_return({ output: "" })
6363

6464
message = "Unknown API token format. " \

spec/core/controlplane_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
let!(:original_cmd) { "cmd" }
2020

2121
before do
22-
allow(ENV).to receive(:fetch).with("HIDE_COMMAND_OUTPUT", nil).and_return(nil)
22+
stub_env("HIDE_COMMAND_OUTPUT", nil)
2323
allow(Shell).to receive(:should_hide_output?).and_return(false)
2424
end
2525

@@ -48,15 +48,15 @@
4848
end
4949

5050
it "hides everything when 'HIDE_COMMAND_OUTPUT' env var is set to 'true'" do
51-
allow(ENV).to receive(:fetch).with("HIDE_COMMAND_OUTPUT", nil).and_return("true")
51+
stub_env("HIDE_COMMAND_OUTPUT", "true")
5252

5353
cmd = described_instance.send(:build_command, original_cmd)
5454

5555
expect(cmd).to eq("#{original_cmd} > /dev/null 2>&1")
5656
end
5757

5858
it "provided 'output_mode' overrides 'HIDE_COMMAND_OUTPUT' env var" do
59-
allow(ENV).to receive(:fetch).with("HIDE_COMMAND_OUTPUT", nil).and_return("true")
59+
stub_env("HIDE_COMMAND_OUTPUT", "true")
6060

6161
cmd = described_instance.send(:build_command, original_cmd, output_mode: :all)
6262

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
require_relative "support/dummy_app_setup"
3232
require_relative "support/command_helpers"
3333
require_relative "support/date_time_helpers"
34+
require_relative "support/stub_env"
3435

3536
# This file was generated by the `rspec --init` command. Conventionally, all
3637
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
@@ -130,6 +131,7 @@
130131

131132
config.include CommandHelpers
132133
config.include DateTimeHelpers
134+
config.include StubENV
133135

134136
config.verbose_retry = true
135137
config.display_try_failure_messages = true

spec/support/stub_env.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
# Copied from https://gitlab.com/gitlab-org/gitlab/-/blob/master/gems/gitlab-rspec/lib/gitlab/rspec/stub_env.rb
4+
module StubENV
5+
# Stub ENV variables
6+
#
7+
# You can provide either a key and value as separate params or both in a Hash format
8+
#
9+
# Keys and values will always be converted to String, to comply with how ENV behaves
10+
#
11+
# @param key_or_hash [String, Hash<String,String>]
12+
# @param value [String]
13+
def stub_env(key_or_hash, value = nil)
14+
init_stub unless env_stubbed?
15+
16+
if key_or_hash.is_a? Hash
17+
key_or_hash.each do |key, value|
18+
add_stubbed_value(key, ensure_env_type(value))
19+
end
20+
else
21+
add_stubbed_value key_or_hash, ensure_env_type(value)
22+
end
23+
end
24+
25+
private
26+
27+
STUBBED_KEY = "__STUBBED__"
28+
29+
def add_stubbed_value(key, value)
30+
allow(ENV).to receive(:[]).with(key).and_return(value)
31+
allow(ENV).to receive(:key?).with(key).and_return(true)
32+
allow(ENV).to receive(:fetch).with(key) do |_|
33+
value || raise(KeyError, "key not found: \"#{key}\"")
34+
end
35+
allow(ENV).to receive(:fetch).with(key, anything) do |_, default_val|
36+
value || default_val
37+
end
38+
end
39+
40+
def env_stubbed?
41+
ENV.fetch(STUBBED_KEY, false)
42+
end
43+
44+
def init_stub
45+
allow(ENV).to receive(:[]).and_call_original
46+
allow(ENV).to receive(:key?).and_call_original
47+
allow(ENV).to receive(:fetch).and_call_original
48+
add_stubbed_value(STUBBED_KEY, true)
49+
end
50+
51+
def ensure_env_type(value)
52+
value.nil? ? value : value.to_s
53+
end
54+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require "support/stub_env"
4+
5+
# Copied from https://gitlab.com/gitlab-org/gitlab/-/blob/master/gems/gitlab-rspec/lib/gitlab/rspec/stub_env.rb
6+
RSpec.describe "StubENV" do
7+
include StubENV
8+
9+
describe "#stub_env" do
10+
it "stubs the environment variable for all ways to read it" do
11+
stub_env("MY_TEST_ENV_VAR", "the value")
12+
13+
expect(ENV["MY_TEST_ENV_VAR"]).to eq("the value") # rubocop:disable Style/FetchEnvVar
14+
expect(ENV.key?("MY_TEST_ENV_VAR")).to be(true)
15+
expect(ENV.fetch("MY_TEST_ENV_VAR")).to eq("the value")
16+
expect(ENV.fetch("MY_TEST_ENV_VAR", "some default")).to eq("the value")
17+
end
18+
19+
context "when stubbed to be nil" do
20+
it "uses a default value for fetch and raises if no default given" do
21+
stub_env("MY_TEST_ENV_VAR", nil)
22+
23+
expect(ENV.fetch("MY_TEST_ENV_VAR", "some default")).to eq("some default")
24+
expect { ENV.fetch("MY_TEST_ENV_VAR") }.to raise_error(KeyError)
25+
end
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)