@@ -220,6 +220,99 @@ def test_parse_env_template_mixed_placeholders(self, tmp_path, mocker):
220220 "COMPOSE_VAR=${COMPOSE_VAR}"
221221 )
222222
223+ def test_parse_env_template_variable_with_default (self , tmp_path , mocker ):
224+ """Test parsing {{ VARIABLE | default }} when variable is not set."""
225+ # Ensure the variable is not in environment
226+ mocker .patch .dict (os .environ , {}, clear = False )
227+ if "TEST_VAR" in os .environ :
228+ del os .environ ["TEST_VAR" ]
229+
230+ template = tmp_path / ".env.example"
231+ template .write_text ("TEST_VAR={{ TEST_VAR | localhost }}" )
232+
233+ result = parse_env_template (template )
234+ assert result == "TEST_VAR=localhost"
235+
236+ def test_parse_env_template_variable_with_default_env_set (self , tmp_path , mocker ):
237+ """Test that environment variable overrides default value."""
238+ mocker .patch .dict (os .environ , {"TEST_VAR" : "custom_value" })
239+
240+ template = tmp_path / ".env.example"
241+ template .write_text ("TEST_VAR={{ TEST_VAR | localhost }}" )
242+
243+ result = parse_env_template (template )
244+ assert result == "TEST_VAR=custom_value"
245+
246+ def test_parse_env_template_variable_with_empty_default (self , tmp_path , mocker ):
247+ """Test parsing {{ VARIABLE | }} with empty string as default."""
248+ mocker .patch .dict (os .environ , {}, clear = False )
249+ if "EMPTY_VAR" in os .environ :
250+ del os .environ ["EMPTY_VAR" ]
251+
252+ template = tmp_path / ".env.example"
253+ template .write_text ("EMPTY_VAR={{ EMPTY_VAR | }}" )
254+
255+ result = parse_env_template (template )
256+ assert result == "EMPTY_VAR="
257+
258+ def test_parse_env_template_variable_whitespace_trimming (self , tmp_path , mocker ):
259+ """Test that whitespace is trimmed from default values."""
260+ mocker .patch .dict (os .environ , {}, clear = False )
261+ if "TEST_VAR" in os .environ :
262+ del os .environ ["TEST_VAR" ]
263+
264+ template = tmp_path / ".env.example"
265+ template .write_text ("TEST_VAR={{ TEST_VAR | localhost }}" )
266+
267+ result = parse_env_template (template )
268+ assert result == "TEST_VAR=localhost"
269+
270+ def test_parse_env_template_branch_with_default (self , tmp_path ):
271+ """Test parsing {{ branch() | default }} when branch_name is None."""
272+ template = tmp_path / ".env.example"
273+ template .write_text ("BRANCH={{ branch() | main }}" )
274+
275+ result = parse_env_template (template , branch_name = None )
276+ assert result == "BRANCH=main"
277+
278+ def test_parse_env_template_branch_with_default_branch_set (self , tmp_path ):
279+ """Test that branch_name overrides default value."""
280+ template = tmp_path / ".env.example"
281+ template .write_text ("BRANCH={{ branch() | main }}" )
282+
283+ result = parse_env_template (template , branch_name = "feature-xyz" )
284+ assert result == "BRANCH=feature-xyz"
285+
286+ def test_parse_env_template_auto_port_with_default (self , tmp_path , mocker ):
287+ """Test {{ auto_port() | default }} - default is captured but not used."""
288+ mocker .patch ("sprout.utils.find_available_port" , return_value = 9000 )
289+
290+ template = tmp_path / ".env.example"
291+ template .write_text ("PORT={{ auto_port() | 8080 }}" )
292+
293+ # auto_port() always generates a port, so default is not used
294+ result = parse_env_template (template )
295+ assert result == "PORT=9000"
296+
297+ def test_parse_env_template_mixed_with_defaults (self , tmp_path , mocker ):
298+ """Test parsing mixed placeholders with default values."""
299+ mocker .patch ("sprout.utils.find_available_port" , return_value = 8080 )
300+ mocker .patch .dict (os .environ , {"SET_VAR" : "from_env" }, clear = False )
301+ if "UNSET_VAR" in os .environ :
302+ del os .environ ["UNSET_VAR" ]
303+
304+ template = tmp_path / ".env.example"
305+ template .write_text (
306+ "SET_VAR={{ SET_VAR | default1 }}\n "
307+ "UNSET_VAR={{ UNSET_VAR | default2 }}\n "
308+ "PORT={{ auto_port() | 3000 }}\n "
309+ "BRANCH={{ branch() | develop }}\n "
310+ "EMPTY={{ EMPTY | }}"
311+ )
312+
313+ result = parse_env_template (template , branch_name = None )
314+ assert result == ("SET_VAR=from_env\n UNSET_VAR=default2\n PORT=8080\n BRANCH=develop\n EMPTY=" )
315+
223316 def test_parse_env_template_file_not_found (self , tmp_path ):
224317 """Test error when template file doesn't exist."""
225318 template = tmp_path / "nonexistent.env"
0 commit comments