Skip to content

Commit

Permalink
Support backslash for windows path (#1485)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-melnacouzi authored Aug 26, 2024
1 parent 6708d2e commit 87eef93
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
17 changes: 16 additions & 1 deletion src/snowflake/cli/api/project/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,25 @@ class SchemaValidationError(ClickException):
def __init__(self, error: ValidationError):
errors = error.errors()
message = f"During evaluation of {error.title} in project definition following errors were encountered:\n"

def calculate_location(e):
if e["loc"] is None:
return None

# show numbers as list indexes and strings as dictionary keys. Example: key1[0].key2
result = "".join(
f"[{item}]" if isinstance(item, int) else f".{item}"
for item in e["loc"]
)

# remove leading dot from the string if any:
return result[1:] if result.startswith(".") else result

message += "\n".join(
[
self.message_templates.get(e["type"], self.generic_message).format(
**e, location=".".join(e["loc"]) if e["loc"] is not None else None
**e,
location=calculate_location(e),
)
for e in errors
]
Expand Down
30 changes: 17 additions & 13 deletions src/snowflake/cli/plugins/nativeapp/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from snowflake.cli.api.rendering.sql_templates import (
get_sql_cli_jinja_env,
)
from snowflake.cli.api.secure_path import UNLIMITED, SecurePath
from snowflake.cli.api.sql_execution import SqlExecutionMixin
from snowflake.cli.plugins.connection.util import make_snowsight_url
from snowflake.cli.plugins.nativeapp.artifacts import (
Expand Down Expand Up @@ -577,7 +578,10 @@ def create_app_package(self) -> None:
)

def _expand_script_templates(
self, env: jinja2.Environment, jinja_context: dict[str, Any], scripts: List[str]
self,
env: jinja2.Environment,
jinja_context: dict[str, Any],
scripts: List[str],
) -> List[str]:
"""
Input:
Expand All @@ -589,20 +593,22 @@ def _expand_script_templates(
Size of the return list is the same as the size of the input scripts list.
"""
scripts_contents = []
for relpath in scripts:
for script in scripts:
full_path = SecurePath(self.project_root) / script
try:
template = env.get_template(relpath)
template_content = full_path.read_text(file_size_limit_mb=UNLIMITED)
template = env.from_string(template_content)
result = template.render(**jinja_context)
scripts_contents.append(result)

except jinja2.TemplateNotFound as e:
raise MissingScriptError(e.name) from e
except FileNotFoundError as e:
raise MissingScriptError(script) from e

except jinja2.TemplateSyntaxError as e:
raise InvalidScriptError(e.name, e, e.lineno) from e
raise InvalidScriptError(script, e, e.lineno) from e

except jinja2.UndefinedError as e:
raise InvalidScriptError(relpath, e) from e
raise InvalidScriptError(script, e) from e

return scripts_contents

Expand All @@ -618,7 +624,7 @@ def _apply_package_scripts(self) -> None:
)

env = jinja2.Environment(
loader=jinja2.loaders.FileSystemLoader(self.project_root),
loader=jinja2.BaseLoader(),
keep_trailing_newline=True,
undefined=jinja2.StrictUndefined,
)
Expand Down Expand Up @@ -668,19 +674,17 @@ def _execute_post_deploy_hooks(
if not post_deploy_hooks:
return

with cc.phase(f"Executing {deployed_object_type} post-deploy actions"):
with cc.phase(f"Executing {deployed_object_type} post_deploy actions"):
sql_scripts_paths = []
for hook in post_deploy_hooks:
if hook.sql_script:
sql_scripts_paths.append(hook.sql_script)
else:
raise ValueError(
f"Unsupported {deployed_object_type} post-deploy hook type: {hook}"
f"Unsupported {deployed_object_type} post_deploy hook type: {hook}"
)

env = get_sql_cli_jinja_env(
loader=jinja2.loaders.FileSystemLoader(self.project_root)
)
env = get_sql_cli_jinja_env()
scripts_content_list = self._expand_script_templates(
env, cli_context.template_context, sql_scripts_paths
)
Expand Down
2 changes: 1 addition & 1 deletion tests/nativeapp/test_post_deploy_for_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_invalid_hook_type(

with pytest.raises(ValueError) as err:
processor.execute_app_post_deploy_hooks()
assert "Unsupported application post-deploy hook type" in str(err)
assert "Unsupported application post_deploy hook type" in str(err)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 87eef93

Please sign in to comment.