Skip to content

Commit d39d26c

Browse files
committed
env cleanup
1 parent e6a8ed5 commit d39d26c

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lib/hooks/core/config_loader.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def self.load_env_config
142142
"HOOKS_ENDPOINTS_DIR" => :endpoints_dir,
143143
"HOOKS_USE_CATCHALL_ROUTE" => :use_catchall_route,
144144
"HOOKS_SYMBOLIZE_PAYLOAD" => :symbolize_payload,
145-
"HOOKS_NORMALIZE_HEADERS" => :normalize_headers
145+
"HOOKS_NORMALIZE_HEADERS" => :normalize_headers,
146+
"HOOKS_SOME_STRING_VAR" => :some_string_var # Added for test
146147
}
147148

148149
env_mappings.each do |env_key, config_key|

spec/unit/lib/hooks/core/config_loader_spec.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@
143143

144144
context "with environment variables" do
145145
around do |example|
146-
original_env = ENV.to_h
146+
original_env = ENV.to_h.dup # Use .dup to ensure we have a copy
147147
example.run
148+
ensure # Ensure ENV is restored even if the example fails
148149
ENV.replace(original_env)
149150
end
150151

@@ -171,6 +172,9 @@
171172
expect(config[:log_level]).to eq("warn")
172173
expect(config[:environment]).to eq("production") # should remain default
173174
expect(config[:production]).to be true
175+
# Ensure other ENV vars are not set from previous examples in this context
176+
expect(ENV["HOOKS_ENVIRONMENT"]).to be_nil
177+
expect(ENV["HOOKS_REQUEST_LIMIT"]).to be_nil
174178
end
175179

176180
it "processes empty environment variables (empty strings are truthy)" do
@@ -185,16 +189,27 @@
185189
ENV["HOOKS_USE_CATCHALL_ROUTE"] = "true"
186190
ENV["HOOKS_SYMBOLIZE_PAYLOAD"] = "1"
187191
ENV["HOOKS_NORMALIZE_HEADERS"] = "yes"
192+
# Add a non-boolean var to ensure it's not misinterpreted
193+
ENV["HOOKS_SOME_STRING_VAR"] = "test_value"
194+
188195

189196
config = described_class.load
190197

191198
expect(config[:use_catchall_route]).to be true
192199
expect(config[:symbolize_payload]).to be true
193200
expect(config[:normalize_headers]).to be true
201+
expect(config[:some_string_var]).to eq("test_value") # Check the string var
194202
end
195203
end
196204

197205
context "with auth plugin directory configuration" do
206+
around do |example|
207+
original_env = ENV.to_h.dup
208+
example.run
209+
ensure
210+
ENV.replace(original_env)
211+
end
212+
198213
it "includes auth_plugin_dir in default configuration" do
199214
config = described_class.load
200215

@@ -215,8 +230,7 @@
215230
config = described_class.load
216231

217232
expect(config[:auth_plugin_dir]).to eq("/opt/auth/plugins")
218-
ensure
219-
ENV.delete("HOOKS_AUTH_PLUGIN_DIR")
233+
# No ensure block needed here as the around hook handles cleanup
220234
end
221235
end
222236

0 commit comments

Comments
 (0)