Skip to content

Commit

Permalink
fix: translate paths in SAR metadata during sam build (aws#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunedan authored and jfuss committed Jan 15, 2019
1 parent 57e67e2 commit 694d2b2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
20 changes: 20 additions & 0 deletions samcli/commands/_utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
from samcli.yamlhelper import yaml_parse, yaml_dump


_METADATA_WITH_LOCAL_PATHS = {
"AWS::ServerlessRepo::Application": ["LicenseUrl", "ReadmeUrl"]
}

_RESOURCES_WITH_LOCAL_PATHS = {
"AWS::Serverless::Function": ["CodeUri"],
"AWS::Serverless::Api": ["DefinitionUri"],
Expand Down Expand Up @@ -132,6 +136,22 @@ def _update_relative_paths(template_dict,
"""

for resource_type, properties in template_dict.get("Metadata", {}).items():

if resource_type not in _METADATA_WITH_LOCAL_PATHS:
# Unknown resource. Skipping
continue

for path_prop_name in _METADATA_WITH_LOCAL_PATHS[resource_type]:
path = properties.get(path_prop_name)

updated_path = _resolve_relative_to(path, original_root, new_root)
if not updated_path:
# This path does not need to get updated
continue

properties[path_prop_name] = updated_path

for _, resource in template_dict.get("Resources", {}).items():
resource_type = resource.get("Type")

Expand Down
37 changes: 34 additions & 3 deletions tests/unit/commands/_utils/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from mock import patch, mock_open
from parameterized import parameterized, param

from samcli.commands._utils.template import get_template_data, _RESOURCES_WITH_LOCAL_PATHS, _update_relative_paths, \
move_template
from samcli.commands._utils.template import get_template_data, _METADATA_WITH_LOCAL_PATHS, \
_RESOURCES_WITH_LOCAL_PATHS, _update_relative_paths, move_template


class Test_get_template_data(TestCase):
Expand Down Expand Up @@ -78,10 +78,41 @@ def setUp(self):

self.expected_result = os.path.join("..", "foo", "bar")

@parameterized.expand(
[(resource_type, props) for resource_type, props in _METADATA_WITH_LOCAL_PATHS.items()]
)
def test_must_update_relative_metadata_paths(self, resource_type, properties):

for propname in properties:
for path in [self.s3path, self.abspath, self.curpath]:
template_dict = {
"Metadata": {
resource_type: {
propname: path
},
"AWS::Ec2::Instance": {
propname: path
}
},
"Parameters": {
"a": "b"
}
}

expected_template_dict = copy.deepcopy(template_dict)
if path == self.curpath:
expected_template_dict["Metadata"][resource_type][propname] = \
self.expected_result

result = _update_relative_paths(template_dict, self.src, self.dest)

self.maxDiff = None
self.assertEquals(result, expected_template_dict)

@parameterized.expand(
[(resource_type, props) for resource_type, props in _RESOURCES_WITH_LOCAL_PATHS.items()]
)
def test_must_update_relative_paths(self, resource_type, properties):
def test_must_update_relative_resource_paths(self, resource_type, properties):

for propname in properties:

Expand Down

0 comments on commit 694d2b2

Please sign in to comment.