Skip to content

Commit 3b941d1

Browse files
committed
refactor: remove library behavior tests per reedham feedback
Removed 3 tests that verify python-dotenv library behavior: - test_dotenv_with_special_characters - test_dotenv_with_empty_and_whitespace_values - test_get_dotenv_values_direct_with_real_file Keeping 18 tests that verify SAM CLI logic: - File existence checks - Empty file warnings - Merging behavior (dotenv + JSON) - Integration with InvokeContext Tests: 18/18 pass, cleaner test suite focused on SAM CLI logic.
1 parent da11247 commit 3b941d1

1 file changed

Lines changed: 1 addition & 118 deletions

File tree

tests/unit/commands/local/cli_common/test_invoke_context_dotenv.py

Lines changed: 1 addition & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ def test_non_existent_dotenv_file_raises_exception(
423423
finally:
424424
os.unlink(template_path)
425425

426+
426427
@patch("samcli.commands.local.cli_common.invoke_context.InvokeContext._get_container_manager")
427428
@patch("samcli.commands.local.cli_common.invoke_context.SamLocalStackProvider")
428429
@patch("samcli.commands.local.cli_common.invoke_context.SamFunctionProvider")
@@ -460,97 +461,6 @@ def test_empty_dotenv_file_logs_warning(self, MockFunctionProvider, MockStackPro
460461
os.unlink(dotenv_path)
461462
os.unlink(template_path)
462463

463-
@patch("samcli.commands.local.cli_common.invoke_context.InvokeContext._get_container_manager")
464-
@patch("samcli.commands.local.cli_common.invoke_context.SamLocalStackProvider")
465-
@patch("samcli.commands.local.cli_common.invoke_context.SamFunctionProvider")
466-
def test_dotenv_with_special_characters(self, MockFunctionProvider, MockStackProvider, MockGetContainerManager):
467-
"""Should handle special characters in variable values"""
468-
# Create .env file with special characters
469-
with tempfile.NamedTemporaryFile(mode="w", suffix=".env", delete=False) as f:
470-
f.write("PASSWORD=p@ssw0rd!#$%\n")
471-
f.write("URL=https://example.com?param=value&other=123\n")
472-
f.write('JSON_DATA={"key": "value", "nested": {"data": true}}\n')
473-
dotenv_path = f.name
474-
475-
# Create dummy template
476-
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
477-
f.write("AWSTemplateFormatVersion: '2010-09-09'\n")
478-
template_path = f.name
479-
480-
try:
481-
# Mock stack provider
482-
mock_stack = Mock()
483-
MockStackProvider.get_stacks.return_value = ([mock_stack], Mock())
484-
485-
# Mock function provider
486-
mock_function_provider = Mock()
487-
mock_function_provider.get_all.return_value = []
488-
MockFunctionProvider.return_value = mock_function_provider
489-
490-
# Mock container manager
491-
mock_container_manager = Mock()
492-
mock_container_manager.is_docker_reachable = True
493-
MockGetContainerManager.return_value = mock_container_manager
494-
495-
with InvokeContext(template_file=template_path, dotenv_file=dotenv_path, env_vars_file=None) as context:
496-
# Verify special characters are preserved
497-
self.assertIsNotNone(context._env_vars_value)
498-
params = context._env_vars_value["Parameters"]
499-
self.assertEqual(params["PASSWORD"], "p@ssw0rd!#$%")
500-
self.assertEqual(params["URL"], "https://example.com?param=value&other=123")
501-
self.assertEqual(params["JSON_DATA"], '{"key": "value", "nested": {"data": true}}')
502-
finally:
503-
os.unlink(dotenv_path)
504-
os.unlink(template_path)
505-
506-
@patch("samcli.commands.local.cli_common.invoke_context.InvokeContext._get_container_manager")
507-
@patch("samcli.commands.local.cli_common.invoke_context.SamLocalStackProvider")
508-
@patch("samcli.commands.local.cli_common.invoke_context.SamFunctionProvider")
509-
def test_dotenv_with_empty_and_whitespace_values(
510-
self, MockFunctionProvider, MockStackProvider, MockGetContainerManager
511-
):
512-
"""Should handle empty values and whitespace correctly"""
513-
# Create .env file with empty and whitespace values
514-
with tempfile.NamedTemporaryFile(mode="w", suffix=".env", delete=False) as f:
515-
f.write("EMPTY_VAR=\n")
516-
f.write("SPACE_VAR= \n")
517-
f.write("TAB_VAR=\t\n")
518-
f.write("NORMAL_VAR=value\n")
519-
dotenv_path = f.name
520-
521-
# Create dummy template
522-
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
523-
f.write("AWSTemplateFormatVersion: '2010-09-09'\n")
524-
template_path = f.name
525-
526-
try:
527-
# Mock stack provider
528-
mock_stack = Mock()
529-
MockStackProvider.get_stacks.return_value = ([mock_stack], Mock())
530-
531-
# Mock function provider
532-
mock_function_provider = Mock()
533-
mock_function_provider.get_all.return_value = []
534-
MockFunctionProvider.return_value = mock_function_provider
535-
536-
# Mock container manager
537-
mock_container_manager = Mock()
538-
mock_container_manager.is_docker_reachable = True
539-
MockGetContainerManager.return_value = mock_container_manager
540-
541-
with InvokeContext(template_file=template_path, dotenv_file=dotenv_path, env_vars_file=None) as context:
542-
# Verify empty and whitespace values are handled
543-
# Note: python-dotenv strips trailing whitespace, so " " becomes ""
544-
self.assertIsNotNone(context._env_vars_value)
545-
params = context._env_vars_value["Parameters"]
546-
self.assertEqual(params["EMPTY_VAR"], "")
547-
self.assertEqual(params["SPACE_VAR"], "") # python-dotenv strips whitespace
548-
self.assertEqual(params["TAB_VAR"], "") # python-dotenv strips whitespace
549-
self.assertEqual(params["NORMAL_VAR"], "value")
550-
finally:
551-
os.unlink(dotenv_path)
552-
os.unlink(template_path)
553-
554464
def test_get_dotenv_values_static_method_nonexistent_file(self):
555465
"""Test that _get_dotenv_values raises exception for non-existent file"""
556466
from samcli.commands.local.cli_common.invoke_context import (
@@ -660,30 +570,3 @@ def test_merge_container_vars_without_parameters(self):
660570
self.assertEqual(result["DEBUG_VAR1"], "json_override") # JSON wins
661571
self.assertEqual(result["DEBUG_VAR2"], "debug2") # Dotenv preserved
662572
self.assertEqual(result["DEBUG_VAR3"], "json_only") # JSON added
663-
664-
def test_get_dotenv_values_direct_with_real_file(self):
665-
"""Test _get_dotenv_values with real file - no mocking"""
666-
from samcli.commands.local.cli_common.invoke_context import InvokeContext
667-
668-
# Create real .env file
669-
with tempfile.NamedTemporaryFile(mode="w", suffix=".env", delete=False) as f:
670-
f.write("TEST_VAR=test_value\n")
671-
f.write("ANOTHER_VAR=another_value\n")
672-
f.write("# Comment line\n")
673-
f.write("\n")
674-
f.write("LAST_VAR=last_value\n")
675-
dotenv_path = f.name
676-
677-
try:
678-
# Call the actual method - no mocks
679-
result = InvokeContext._get_dotenv_values(dotenv_path)
680-
681-
# Verify actual parsing behavior
682-
self.assertIsNotNone(result)
683-
self.assertEqual(len(result), 3) # Should ignore comments and empty lines
684-
self.assertEqual(result["TEST_VAR"], "test_value")
685-
self.assertEqual(result["ANOTHER_VAR"], "another_value")
686-
self.assertEqual(result["LAST_VAR"], "last_value")
687-
self.assertNotIn("#", result) # Comments not included
688-
finally:
689-
os.unlink(dotenv_path)

0 commit comments

Comments
 (0)