Skip to content

Commit

Permalink
Fixed patch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-bdufour committed Sep 19, 2024
1 parent 277fd70 commit 3b3185a
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/snowflake/cli/_plugins/nativeapp/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ def find_setup_script_file(deploy_root: Path) -> Path:

def find_version_info_in_manifest_file(
deploy_root: Path,
) -> Tuple[Optional[str], Optional[str]]:
) -> Tuple[Optional[str], Optional[int]]:
"""
Find version and patch, if available, in the manifest.yml file.
"""
Expand All @@ -745,13 +745,13 @@ def find_version_info_in_manifest_file(
manifest_content = find_and_read_manifest_file(deploy_root=deploy_root)

version_name: Optional[str] = None
patch_name: Optional[str] = None
patch_number: Optional[int] = None

version_info = manifest_content.get("version", None)
if version_info:
if name_field in version_info:
version_name = to_identifier(str(version_info[name_field]))
if patch_field in version_info:
patch_name = to_identifier(str(version_info[patch_field]))
patch_number = int(version_info[patch_field])

return version_name, patch_name
return version_name, patch_number
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def process(
)

# Check if --patch needs to throw a bad option error, either if application package does not exist or if version does not exist
if patch:
if patch is not None:
try:
if not self.get_existing_version_info(version):
raise BadOptionUsage(
Expand Down
8 changes: 4 additions & 4 deletions tests/nativeapp/test_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1365,9 +1365,9 @@ def test_symlink_or_copy_with_symlinks_in_project_root(os_agnostic_snapshot):
assert_dir_snapshot(Path("./output/deploy"), os_agnostic_snapshot)


@pytest.mark.parametrize("patch_name", [None, "1", "42", "foo"])
@pytest.mark.parametrize("patch_name", [None, "1", 42])
@pytest.mark.parametrize(
"version_name", [None, "v1", "1", "1.2", "1.2.3", "0.x", "foo", "abc def"]
"version_name", [None, "v1", "1", 2, "1.2", 1.3, "1.2.3", "0.x", "foo", "abc def"]
)
def test_find_version_info_in_manifest_file(version_name, patch_name):
manifest_contents = {"manifest_version": 1, "version": {}}
Expand All @@ -1386,9 +1386,9 @@ def test_find_version_info_in_manifest_file(version_name, patch_name):
if version_name is None:
assert v is None
else:
assert v == to_identifier(version_name)
assert v == to_identifier(str(version_name))

if patch_name is None:
assert p is None
else:
assert p == to_identifier(patch_name)
assert p == int(patch_name)
106 changes: 106 additions & 0 deletions tests_integration/nativeapp/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,42 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from shlex import split
from typing import Any, Optional

from yaml import safe_dump, safe_load

from snowflake.cli.api.project.util import (
is_valid_unquoted_identifier,
to_identifier,
unquote_identifier,
)
from tests.project.fixtures import *
from tests_integration.test_utils import contains_row_with, row_from_snowflake_session


def set_version_in_app_manifest(manifest_path: Path, version: Any, patch: Any = None):
with open(manifest_path, "r") as f:
manifest = safe_load(f)

version_info = manifest.setdefault("version", {})
version_info["name"] = version
if patch is not None:
version_info["patch"] = patch
else:
version_info.pop("patch", None)

with open(manifest_path, "w") as f:
f.write(safe_dump(manifest))


def normalize_identifier(identifier: str) -> str:
id_str = str(identifier)
if is_valid_unquoted_identifier(str(id_str)):
return id_str.upper()
else:
return unquote_identifier(to_identifier(id_str))


# Tests a simple flow of an existing project, executing snow app version create, drop and teardown, all with distribution=internal
@pytest.mark.integration
@pytest.mark.parametrize(
Expand Down Expand Up @@ -329,3 +360,78 @@ def test_nativeapp_version_create_package_no_magic_comment(
# Remove date field
row.pop("created_on", None)
assert actual.json == snapshot


# Tests a simple flow of an existing project, executing snow app version create, drop and teardown, all with distribution=internal
@pytest.mark.integration
@pytest.mark.parametrize(
"test_project",
[
"napp_init_v1",
"napp_init_v2",
],
)
def test_nativeapp_version_create_and_drop_from_manifest(
runner,
snowflake_session,
default_username,
resource_suffix,
nativeapp_project_directory,
test_project,
):
project_name = "myapp"
with nativeapp_project_directory(test_project) as project_dir:
# not using pytest parameterization here because we need
# to guarantee that the initial version gets created before the patches

VERSIONS = [7, "v1", "1.0", "version 1"]
PATCHES = [None, 10, "12"]

for version_name in VERSIONS:
manifest_path = project_dir / "app/manifest.yml"
set_version_in_app_manifest(manifest_path, version_name)

result_create = runner.invoke_with_connection_json(
["app", "version", "create", "--force", "--skip-git-check"]
)
assert result_create.exit_code == 0

# app package contains version v1
actual = runner.invoke_with_connection_json(["app", "version", "list"])
assert contains_row_with(
actual.json, {"version": normalize_identifier(version_name), "patch": 0}
)

result_drop = runner.invoke_with_connection_json(
["app", "version", "drop", "--force"]
)
assert result_drop.exit_code == 0
actual = runner.invoke_with_connection_json(["app", "version", "list"])
assert len(actual.json) == 0

version_name = "V2"
for patch_name in PATCHES:
manifest_path = project_dir / "app/manifest.yml"
set_version_in_app_manifest(manifest_path, version_name, patch_name)

result_create = runner.invoke_with_connection_json(
["app", "version", "create", "--force", "--skip-git-check"]
)
assert result_create.exit_code == 0

# app package contains version v1
actual = runner.invoke_with_connection_json(["app", "version", "list"])
assert contains_row_with(
actual.json,
{
"version": version_name,
"patch": int(patch_name) if patch_name else 0,
},
)

result_drop = runner.invoke_with_connection_json(
["app", "version", "drop", version_name, "--force"]
)
assert result_drop.exit_code == 0
actual = runner.invoke_with_connection_json(["app", "version", "list"])
assert len(actual.json) == 0

0 comments on commit 3b3185a

Please sign in to comment.