From 4e59e0d744ecb217af34cdfd6a3c6c92d00e9bbb Mon Sep 17 00:00:00 2001 From: Parya Jafari Date: Thu, 28 Nov 2024 10:02:56 -0500 Subject: [PATCH] Fix error message for mis-formatted version field in manifest.yml (#1904) --- .../cli/_plugins/nativeapp/artifacts.py | 12 ++++-- tests/nativeapp/test_artifacts.py | 43 +++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/snowflake/cli/_plugins/nativeapp/artifacts.py b/src/snowflake/cli/_plugins/nativeapp/artifacts.py index fb212107c8..2ba28a446c 100644 --- a/src/snowflake/cli/_plugins/nativeapp/artifacts.py +++ b/src/snowflake/cli/_plugins/nativeapp/artifacts.py @@ -777,12 +777,16 @@ def find_version_info_in_manifest_file( label: Optional[str] = None version_info = manifest_content.get("version", None) - if version_info: - if name_field in version_info: + if version_info is not None: + if not isinstance(version_info, dict): + raise ClickException( + "Error occurred while reading manifest.yml. Received unexpected version format." + ) + if version_info.get(name_field) is not None: version_name = to_identifier(str(version_info[name_field])) - if patch_field in version_info: + if version_info.get(patch_field) is not None: patch_number = int(version_info[patch_field]) - if label_field in version_info: + if version_info.get(label_field) is not None: label = str(version_info[label_field]) return VersionInfo(version_name, patch_number, label) diff --git a/tests/nativeapp/test_artifacts.py b/tests/nativeapp/test_artifacts.py index e1c46590b1..59f141ad49 100644 --- a/tests/nativeapp/test_artifacts.py +++ b/tests/nativeapp/test_artifacts.py @@ -20,6 +20,7 @@ from typing import Dict, Iterable, List, Optional, Union import pytest +from click import ClickException from snowflake.cli._plugins.nativeapp.artifacts import ( ArtifactError, ArtifactPredicate, @@ -28,6 +29,7 @@ NotInDeployRootError, SourceNotFoundError, TooManyFilesError, + VersionInfo, build_bundle, find_events_definitions_in_manifest_file, find_version_info_in_manifest_file, @@ -39,6 +41,7 @@ from snowflake.cli.api.project.util import to_identifier from yaml import safe_dump +from tests.nativeapp.factories import ManifestFactory from tests.nativeapp.utils import ( assert_dir_snapshot, touch, @@ -1426,6 +1429,46 @@ def test_find_version_info_in_manifest_file(version_name, patch_name, label): assert l == label +@pytest.mark.parametrize("version_info", ["some_name", ["li1", "li2"], "", 4]) +def test_bad_version_info_in_manifest_file_throws_error(version_info): + manifest_contents = ManifestFactory(version=version_info) + + deploy_root_structure = {"manifest.yml": manifest_contents} + with temp_local_dir(deploy_root_structure) as deploy_root: + with pytest.raises(ClickException) as err: + find_version_info_in_manifest_file(deploy_root=deploy_root) + assert ( + err.value.message + == "Error occurred while reading manifest.yml. Received unexpected version format." + ) + + +def test_read_empty_version_info_in_manifest_file(): + manifest_contents = ManifestFactory( + version={"name": None, "patch": None, "label": None} + ) + + deploy_root_structure = {"manifest.yml": manifest_contents} + with temp_local_dir(deploy_root_structure) as deploy_root: + + version_info = find_version_info_in_manifest_file(deploy_root=deploy_root) + assert version_info == VersionInfo( + version_name=None, patch_number=None, label=None + ) + + +def test_read_empty_version_in_manifest_file(): + manifest_contents = ManifestFactory(version=None) + + deploy_root_structure = {"manifest.yml": manifest_contents} + with temp_local_dir(deploy_root_structure) as deploy_root: + + version_info = find_version_info_in_manifest_file(deploy_root=deploy_root) + assert version_info == VersionInfo( + version_name=None, patch_number=None, label=None + ) + + @pytest.mark.parametrize( "configuration_section, expected_output", [