Skip to content

Commit 4881ecc

Browse files
committed
Add tests for template hot reload configuration in Azu::Configuration
- Introduced tests to verify the default behavior of template hot reloading across different environments (development, test, pipeline, and production). - Added functionality to override the hot reload setting via the TEMPLATE_HOT_RELOAD environment variable. - Included runtime modification tests for the template hot reload setting and ensured it propagates correctly to templates. These enhancements improve the test coverage for template management, aligning with the project's focus on performance and maintainability.
1 parent ee2a934 commit 4881ecc

3 files changed

Lines changed: 199 additions & 2 deletions

File tree

spec/azu/configuration_spec.cr

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,155 @@ describe Azu::Configuration do
129129
config.log.should be_a(Log)
130130
end
131131
end
132+
133+
describe "template hot reload configuration" do
134+
it "defaults to true for development environment" do
135+
# Save original environment
136+
original_env = ENV["CRYSTAL_ENV"]?
137+
ENV.delete("TEMPLATE_HOT_RELOAD") if ENV.has_key?("TEMPLATE_HOT_RELOAD")
138+
139+
ENV["CRYSTAL_ENV"] = "development"
140+
config = Azu::Configuration.new
141+
142+
config.template_hot_reload.should be_true
143+
144+
# Restore original environment
145+
if original_env
146+
ENV["CRYSTAL_ENV"] = original_env
147+
else
148+
ENV.delete("CRYSTAL_ENV")
149+
end
150+
end
151+
152+
it "defaults to true for test environment" do
153+
# Save original environment
154+
original_env = ENV["CRYSTAL_ENV"]?
155+
ENV.delete("TEMPLATE_HOT_RELOAD") if ENV.has_key?("TEMPLATE_HOT_RELOAD")
156+
157+
ENV["CRYSTAL_ENV"] = "test"
158+
config = Azu::Configuration.new
159+
160+
config.template_hot_reload.should be_true
161+
162+
# Restore original environment
163+
if original_env
164+
ENV["CRYSTAL_ENV"] = original_env
165+
else
166+
ENV.delete("CRYSTAL_ENV")
167+
end
168+
end
169+
170+
it "defaults to true for pipeline environment" do
171+
# Save original environment
172+
original_env = ENV["CRYSTAL_ENV"]?
173+
ENV.delete("TEMPLATE_HOT_RELOAD") if ENV.has_key?("TEMPLATE_HOT_RELOAD")
174+
175+
ENV["CRYSTAL_ENV"] = "pipeline"
176+
config = Azu::Configuration.new
177+
178+
config.template_hot_reload.should be_true
179+
180+
# Restore original environment
181+
if original_env
182+
ENV["CRYSTAL_ENV"] = original_env
183+
else
184+
ENV.delete("CRYSTAL_ENV")
185+
end
186+
end
187+
188+
it "defaults to false for production environment" do
189+
# Save original environment
190+
original_env = ENV["CRYSTAL_ENV"]?
191+
ENV.delete("TEMPLATE_HOT_RELOAD") if ENV.has_key?("TEMPLATE_HOT_RELOAD")
192+
193+
ENV["CRYSTAL_ENV"] = "production"
194+
config = Azu::Configuration.new
195+
196+
config.template_hot_reload.should be_false
197+
198+
# Restore original environment
199+
if original_env
200+
ENV["CRYSTAL_ENV"] = original_env
201+
else
202+
ENV.delete("CRYSTAL_ENV")
203+
end
204+
end
205+
206+
it "can be overridden via TEMPLATE_HOT_RELOAD environment variable" do
207+
# Save original environment
208+
original_env = ENV["CRYSTAL_ENV"]?
209+
original_hot_reload = ENV["TEMPLATE_HOT_RELOAD"]?
210+
211+
# Test override in production (normally false)
212+
ENV["CRYSTAL_ENV"] = "production"
213+
ENV["TEMPLATE_HOT_RELOAD"] = "true"
214+
config = Azu::Configuration.new
215+
216+
config.template_hot_reload.should be_true
217+
218+
# Test override in development (normally true)
219+
ENV["CRYSTAL_ENV"] = "development"
220+
ENV["TEMPLATE_HOT_RELOAD"] = "false"
221+
config = Azu::Configuration.new
222+
223+
config.template_hot_reload.should be_false
224+
225+
# Restore original environment
226+
if original_env
227+
ENV["CRYSTAL_ENV"] = original_env
228+
else
229+
ENV.delete("CRYSTAL_ENV")
230+
end
231+
232+
if original_hot_reload
233+
ENV["TEMPLATE_HOT_RELOAD"] = original_hot_reload
234+
else
235+
ENV.delete("TEMPLATE_HOT_RELOAD")
236+
end
237+
end
238+
239+
it "can be modified at runtime" do
240+
config = Azu::Configuration.new
241+
242+
config.template_hot_reload = true
243+
config.template_hot_reload.should be_true
244+
245+
config.template_hot_reload = false
246+
config.template_hot_reload.should be_false
247+
end
248+
249+
it "passes hot reload setting to templates" do
250+
# Save original environment
251+
original_env = ENV["CRYSTAL_ENV"]?
252+
original_hot_reload = ENV["TEMPLATE_HOT_RELOAD"]?
253+
254+
# Test with hot reload enabled
255+
ENV["CRYSTAL_ENV"] = "development"
256+
ENV.delete("TEMPLATE_HOT_RELOAD") if ENV.has_key?("TEMPLATE_HOT_RELOAD")
257+
config = Azu::Configuration.new
258+
259+
config.template_hot_reload.should be_true
260+
config.templates.@hot_reload_enabled.should be_true
261+
262+
# Test with hot reload disabled via environment override
263+
ENV["TEMPLATE_HOT_RELOAD"] = "false"
264+
config = Azu::Configuration.new
265+
266+
config.template_hot_reload.should be_false
267+
config.templates.@hot_reload_enabled.should be_false
268+
269+
# Restore original environment
270+
if original_env
271+
ENV["CRYSTAL_ENV"] = original_env
272+
else
273+
ENV.delete("CRYSTAL_ENV")
274+
end
275+
276+
if original_hot_reload
277+
ENV["TEMPLATE_HOT_RELOAD"] = original_hot_reload
278+
else
279+
ENV.delete("TEMPLATE_HOT_RELOAD")
280+
end
281+
end
282+
end
132283
end

spec/azu/templates_spec.cr

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ describe Azu::Templates do
249249
end
250250
end
251251

252-
describe "hot reloading optimization" do
252+
describe "hot reloading optimization" do
253253
it "detects development environment correctly" do
254254
# Save original environment
255255
original_env = ENV["CRYSTAL_ENV"]?
@@ -272,6 +272,51 @@ describe Azu::Templates do
272272
end
273273
end
274274

275+
it "accepts hot reload parameter in constructor" do
276+
# Test explicit true
277+
templates = Azu::Templates.new(["spec/test_templates"], "spec/test_errors", hot_reload: true)
278+
templates.@hot_reload_enabled.should be_true
279+
280+
# Test explicit false
281+
templates = Azu::Templates.new(["spec/test_templates"], "spec/test_errors", hot_reload: false)
282+
templates.@hot_reload_enabled.should be_false
283+
284+
# Test nil (should use environment detection)
285+
original_env = ENV["CRYSTAL_ENV"]?
286+
ENV["CRYSTAL_ENV"] = "development"
287+
templates = Azu::Templates.new(["spec/test_templates"], "spec/test_errors", hot_reload: nil)
288+
templates.@hot_reload_enabled.should be_true
289+
290+
# Restore original environment
291+
if original_env
292+
ENV["CRYSTAL_ENV"] = original_env
293+
else
294+
ENV.delete("CRYSTAL_ENV")
295+
end
296+
end
297+
298+
it "overrides environment detection when hot reload parameter is provided" do
299+
# Save original environment
300+
original_env = ENV["CRYSTAL_ENV"]?
301+
302+
# Test override in production environment (force enable)
303+
ENV["CRYSTAL_ENV"] = "production"
304+
templates = Azu::Templates.new(["spec/test_templates"], "spec/test_errors", hot_reload: true)
305+
templates.@hot_reload_enabled.should be_true
306+
307+
# Test override in development environment (force disable)
308+
ENV["CRYSTAL_ENV"] = "development"
309+
templates = Azu::Templates.new(["spec/test_templates"], "spec/test_errors", hot_reload: false)
310+
templates.@hot_reload_enabled.should be_false
311+
312+
# Restore original environment
313+
if original_env
314+
ENV["CRYSTAL_ENV"] = original_env
315+
else
316+
ENV.delete("CRYSTAL_ENV")
317+
end
318+
end
319+
275320
it "allows manual hot reload configuration" do
276321
templates = Azu::Templates.new(["spec/test_templates"], "spec/test_errors")
277322

src/azu/configuration.cr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Azu
2020
# c.port_reuse = true
2121
# c.log = Log.for("My Awesome App")
2222
# c.env = Environment::Development
23+
# c.template_hot_reload = true
2324
# c.template.path = "./templates"
2425
# c.template.error_path = "./error_template"
2526
# c.upload.max_file_size = 10.megabytes
@@ -39,7 +40,7 @@ module Azu
3940
property env : Environment = Environment.parse(ENV.fetch("CRYSTAL_ENV", "development"))
4041
def self.hot_reload_default
4142
env = ENV.fetch("CRYSTAL_ENV", "development").downcase
42-
env == "development" || env == "test" || env == "ci"
43+
env == "development" || env == "test" || env == "pipeline"
4344
end
4445

4546
property template_hot_reload : Bool = ENV.fetch("TEMPLATE_HOT_RELOAD", Configuration.hot_reload_default.to_s) == "true"

0 commit comments

Comments
 (0)