Skip to content

Commit

Permalink
More actionable error message for snow init (#1488)
Browse files Browse the repository at this point in the history
* More actionable error message for snow init

* fix integration tests

* review fixes
  • Loading branch information
sfc-gh-pczajka committed Aug 27, 2024
1 parent 939e059 commit ff742a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/snowflake/cli/plugins/init/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def _path_argument_callback(path: str) -> str:
show_default=False,
)
SourceOption = typer.Option(
default=DEFAULT_SOURCE,
DEFAULT_SOURCE,
"--template-source",
help=f"local path to template directory or URL to git repository with templates.",
)
VariablesOption = variables_option(
Expand Down Expand Up @@ -132,13 +133,13 @@ def _fetch_remote_template(
return template_root


def _read_template_metadata(template_root: SecurePath) -> Template:
def _read_template_metadata(template_root: SecurePath, args_error_msg: str) -> Template:
"""Parse template.yml file."""
template_metadata_path = template_root / TEMPLATE_METADATA_FILE_NAME
log.debug("Reading template metadata from %s", template_metadata_path.path)
if not template_metadata_path.exists():
raise InvalidTemplate(
f"Template does not have {TEMPLATE_METADATA_FILE_NAME} file."
f"File {TEMPLATE_METADATA_FILE_NAME} not found. {args_error_msg}"
)
with template_metadata_path.open(read_file_limit_mb=DEFAULT_SIZE_LIMIT_MB) as fd:
yaml_contents = yaml.safe_load(fd) or {}
Expand Down Expand Up @@ -203,6 +204,7 @@ def init(
is_remote = any(
template_source.startswith(prefix) for prefix in ["git@", "http://", "https://"] # type: ignore
)
args_error_msg = f"Check whether {TemplateOption.param_decls[0]} and {SourceOption.param_decls[0]} arguments are correct."

# copy/download template into tmpdir, so it is going to be removed in case command ends with an error
with SecurePath.temporary_directory() as tmpdir:
Expand All @@ -217,7 +219,9 @@ def init(
destination=tmpdir,
)

template_metadata = _read_template_metadata(template_root)
template_metadata = _read_template_metadata(
template_root, args_error_msg=args_error_msg
)
if template_metadata.minimum_cli_version:
_validate_cli_version(template_metadata.minimum_cli_version)

Expand Down
6 changes: 5 additions & 1 deletion tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ def test_error_missing_template_yml(runner, test_projects_path, temp_dir):
]
)
assert result.exit_code == 1
assert "Template does not have template.yml file." in result.output
assert (
"File template.yml not found. Check whether --template and --template-source"
in result.output
)
assert "arguments are correct." in result.output
assert not Path(project_name).exists()


Expand Down
6 changes: 5 additions & 1 deletion tests_integration/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ def test_missing_template_yml(runner, temporary_working_directory):
result = runner.invoke(["init", path, "--template-source", url])
assert result.exit_code == 1
assert "Error" in result.output
assert f"Template does not have template.yml file." in result.output
assert (
"File template.yml not found. Check whether --template and --template-source"
in result.output
)
assert "arguments are correct" in result.output


@pytest.mark.integration
Expand Down

0 comments on commit ff742a5

Please sign in to comment.