Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: catch error in _relevant_cdk_files_are_present #7210

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions samcli/lib/iac/cdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
Utils for CDK-based projects
"""

import logging
import os
from typing import Dict

LOG = logging.getLogger(__name__)
CDK_METADATA_TYPE_VALUE = "AWS::CDK::Metadata"
CDK_PATH_METADATA_KEY = "aws:cdk:path"

Expand Down Expand Up @@ -70,5 +72,9 @@ def _relevant_cdk_files_are_present() -> bool:
relevant_cdk_files = ["manifest.json", "tree.json", "cdk.json"]
# In case a customer runs `cdk synth --no-staging > template.yaml` the template
# will be in the project root and it's entirely possible to find the cdk.json there
project_files = os.listdir(os.getcwd())
return any(cdk_file in project_files for cdk_file in relevant_cdk_files)
try:
project_files = os.listdir(os.getcwd())
return any(cdk_file in project_files for cdk_file in relevant_cdk_files)
except FileNotFoundError as e:
LOG.debug("Error listing CWD: %s", e)
return False
20 changes: 12 additions & 8 deletions tests/unit/lib/iac/cdk/test_cdk_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ def test_cfn_templates(self, expected_is_cdk, template_name):

@parameterized.expand(
[
(True, ["lib", "app.py", "requirements.txt", "cdk.json"]),
(True, ["Stack.template.json", "manifest.json", "asset"]),
(True, ["Stack.template.json", "tree.json", "asset"]),
(True, ["Stack.template.json", "manifest.json", "tree.json"]),
(False, ["lib", "app.py", "requirements.txt"]),
(False, []),
(True, ["lib", "app.py", "requirements.txt", "cdk.json"], None),
(True, ["Stack.template.json", "manifest.json", "asset"], None),
(True, ["Stack.template.json", "tree.json", "asset"], None),
(True, ["Stack.template.json", "manifest.json", "tree.json"], None),
(False, ["lib", "app.py", "requirements.txt"], None),
(False, [], None),
(False, [], FileNotFoundError),
]
)
def test_cdk_project_files(self, expected_is_cdk, project_files):
def test_cdk_project_files(self, expected_is_cdk, project_files, side_effect):
with patch("samcli.lib.iac.cdk.utils.os") as mock_os:
mock_os.listdir.return_value = project_files
if side_effect:
mock_os.listdir.side_effect = side_effect
else:
mock_os.listdir.return_value = project_files
is_cdk = is_cdk_project({})
self.assertEqual(is_cdk, expected_is_cdk)
Loading