Skip to content

Commit

Permalink
fix: pretty error msg when init location is invalid (#1606)
Browse files Browse the repository at this point in the history
* Pretty error msg when init location is invalid

* stringify path for windows
  • Loading branch information
sanathkr authored Nov 26, 2019
1 parent c2cdfab commit 1c7ea72
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
19 changes: 17 additions & 2 deletions samcli/local/init/arbitrary_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pathlib import Path
from cookiecutter import repository
from cookiecutter import exceptions
from cookiecutter import config

from samcli.lib.utils import osutils
Expand All @@ -17,6 +18,16 @@
LOG = logging.getLogger(__name__)


BAD_LOCATION_ERROR_MSG = (
"Please verify your location. The following types of location are supported:"
"\n\n* Github: gh:user/repo (or) https://github.com/user/repo (or) [email protected]:user/repo.git"
"\n For Git repositories, you must use location of the root of the repository."
"\n\n* Mercurial: hg+ssh://[email protected]/repo"
"\n\n* Http(s): https://example.com/code.zip"
"\n\n* Local Path: /path/to/code.zip"
)


def generate_non_cookiecutter_project(location, output_dir):
"""
Uses Cookiecutter APIs to download a project at given ``location`` to the ``output_dir``.
Expand Down Expand Up @@ -65,9 +76,13 @@ def generate_non_cookiecutter_project(location, output_dir):
download_fn = functools.partial(repository.clone, repo_url=location, no_input=no_input)

else:
raise ArbitraryProjectDownloadFailed(msg="Unsupported location {location}".format(location=location))
raise ArbitraryProjectDownloadFailed(msg=BAD_LOCATION_ERROR_MSG)

return _download_and_copy(download_fn, output_dir)
try:
return _download_and_copy(download_fn, output_dir)
except exceptions.RepositoryNotFound:
# Download failed because the zip or the repository was not found
raise ArbitraryProjectDownloadFailed(msg=BAD_LOCATION_ERROR_MSG)


def _download_and_copy(download_fn, output_dir):
Expand Down
2 changes: 1 addition & 1 deletion samcli/local/init/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class GenerateProjectFailedError(InitErrorException):


class ArbitraryProjectDownloadFailed(InitErrorException):
fmt = "An error occurred when downloading this project: {msg}"
fmt = "{msg}"
13 changes: 13 additions & 0 deletions tests/integration/init/test_init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ def test_arbitrary_project(self, project_name):
self.assertEqual(os.listdir(str(expected_output_folder)), ["test.txt"])
self.assertEqual(Path(expected_output_folder, "test.txt").read_text(), "hello world")

def test_zip_not_exists(self):
with tempfile.TemporaryDirectory() as temp:
args = [_get_command(), "init", "--location", str(Path("invalid", "zip", "path")), "-o", temp]

process = Popen(args)
try:
process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise

self.assertEqual(process.returncode, 1)


def _get_command():
command = "sam"
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/local/init/test_arbitrary_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pathlib import Path

from cookiecutter.exceptions import RepositoryNotFound
from samcli.local.init.arbitrary_project import generate_non_cookiecutter_project, repository
from samcli.local.init.exceptions import ArbitraryProjectDownloadFailed

Expand Down Expand Up @@ -51,3 +52,12 @@ def test_must_fail_on_local_folders(self):

with self.assertRaises(ArbitraryProjectDownloadFailed):
generate_non_cookiecutter_project(location, self.output_dir)

def test_must_fail_when_repo_not_found(self):
location = str(Path("my", "folder"))

with patch.object(repository, "unzip") as unzip_mock:
unzip_mock.side_effect = RepositoryNotFound("repo")

with self.assertRaises(ArbitraryProjectDownloadFailed):
generate_non_cookiecutter_project(location, self.output_dir)

0 comments on commit 1c7ea72

Please sign in to comment.