diff --git a/tests/unit/lib/iac/test_iac_implementations.py b/tests/unit/lib/iac/test_iac_implementations.py index e7c3e8d8a8..ec59e14de1 100644 --- a/tests/unit/lib/iac/test_iac_implementations.py +++ b/tests/unit/lib/iac/test_iac_implementations.py @@ -1,8 +1,11 @@ from unittest import TestCase -from unittest.mock import Mock, MagicMock +from unittest.mock import MagicMock +import tempfile +import os +import yaml -from samcli.lib.iac.cdk.cdk_iac import CdkIacImplementation from samcli.lib.iac.cfn.cfn_iac import CfnIacImplementation +from samcli.lib.iac.plugins_interfaces import SamCliContext, LookupPath # TODO: Implement these tests when the classes are fully implemented @@ -11,14 +14,52 @@ class TestImplementations(TestCase): def test_cfn_implementation(self): impl = CfnIacImplementation(MagicMock()) impl.get_iac_file_patterns() - impl.update_packaged_locations(Mock()) - impl.write_project(Mock(), Mock()) + impl.update_packaged_locations(MagicMock()) + impl.write_project(MagicMock(), MagicMock()) self.assertTrue(True) def test_cdk_implementation(self): - impl = CdkIacImplementation(Mock()) + impl = CfnIacImplementation(MagicMock()) impl.get_iac_file_patterns() - impl.read_project(Mock()) - impl.update_packaged_locations(Mock()) - impl.write_project(Mock(), Mock()) + impl.update_packaged_locations(MagicMock()) + impl.write_project(MagicMock(), MagicMock()) self.assertTrue(True) + + def test_read_project_minimal_template(self): + # Create a minimal CloudFormation template + minimal_template = { + "AWSTemplateFormatVersion": "2010-09-09", + "Resources": { + "MyFunction": { + "Type": "AWS::Serverless::Function", + "Properties": { + "Handler": "index.handler", + "Runtime": "python3.8", + "CodeUri": "./src" + } + } + } + } + with tempfile.TemporaryDirectory() as tmpdir: + template_path = os.path.join(tmpdir, "template.yaml") + with open(template_path, "w") as f: + yaml.dump(minimal_template, f) + + context = SamCliContext( + command_options_map={"template_file": template_path}, + sam_command_name="test", + is_guided=False, + is_debugging=False, + profile=None, + region=None + ) + impl = CfnIacImplementation(context) + lookup_paths = [LookupPath(tmpdir)] + project = impl.read_project(lookup_paths) + + # Assert the project contains one stack with the expected resource + self.assertEqual(len(project.stacks), 1) + stack = project.stacks[0] + self.assertIn("Resources", stack) + resources = stack["Resources"].section_items + self.assertTrue(any(r.item_id == "MyFunction" for r in resources)) \ No newline at end of file