From 5bf762a25d0694f0fa858289b39d549fc6cf4df0 Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Thu, 17 Oct 2024 13:17:46 +1100 Subject: [PATCH] Use a clearer message for nonexistent template branches --- src/briefcase/commands/base.py | 8 ++++---- src/briefcase/exceptions.py | 7 +++---- tests/commands/base/test_update_cookiecutter_cache.py | 4 ++-- tests/commands/create/test_generate_app_template.py | 6 +++--- tests/commands/new/test_new_app.py | 6 +++--- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/briefcase/commands/base.py b/src/briefcase/commands/base.py index 110bf70fe..94e34bd74 100644 --- a/src/briefcase/commands/base.py +++ b/src/briefcase/commands/base.py @@ -32,10 +32,10 @@ from briefcase.exceptions import ( BriefcaseCommandError, BriefcaseConfigError, + InvalidTemplateBranch, InvalidTemplateRepository, MissingAppMetadata, NetworkFailure, - TemplateUnsupportedVersion, UnsupportedHostError, UnsupportedPythonVersion, ) @@ -1037,7 +1037,7 @@ def update_cookiecutter_cache(self, template: str, branch="master"): head.checkout() except IndexError as e: # No branch exists for the requested version. - raise TemplateUnsupportedVersion(branch) from e + raise InvalidTemplateBranch(template, branch) from e except (ValueError, self.tools.git.exc.GitError) as e: raise BriefcaseCommandError( "Unable to check out template branch.\n" @@ -1094,7 +1094,7 @@ def _generate_template(self, template, branch, output_path, extra_context): raise InvalidTemplateRepository(template) from e except cookiecutter_exceptions.RepositoryCloneFailed as e: # Branch does not exist. - raise TemplateUnsupportedVersion(branch) from e + raise InvalidTemplateBranch(template, branch) from e except cookiecutter_exceptions.UndefinedVariableInTemplate as e: raise BriefcaseConfigError(e.message) from e @@ -1135,7 +1135,7 @@ def generate_template( output_path=output_path, extra_context=extra_context, ) - except TemplateUnsupportedVersion: + except InvalidTemplateBranch: # Only use the main template if we're on a development branch of briefcase # and the user didn't explicitly specify which branch to use. if version.dev is None or branch is not None: diff --git a/src/briefcase/exceptions.py b/src/briefcase/exceptions.py index c5356a15b..53e7d583e 100644 --- a/src/briefcase/exceptions.py +++ b/src/briefcase/exceptions.py @@ -137,11 +137,10 @@ def __init__(self, error_msg): super().__init__(msg=error_msg, skip_logfile=True) -class TemplateUnsupportedVersion(BriefcaseCommandError): - def __init__(self, briefcase_version): - self.briefcase_version = briefcase_version +class InvalidTemplateBranch(BriefcaseCommandError): + def __init__(self, template_repo, branch): super().__init__( - f"Could not find a template branch for Briefcase {briefcase_version}." + f"{branch} did not match any branches in template repository {template_repo}." ) diff --git a/tests/commands/base/test_update_cookiecutter_cache.py b/tests/commands/base/test_update_cookiecutter_cache.py index 2462eeb8b..052f0f50a 100644 --- a/tests/commands/base/test_update_cookiecutter_cache.py +++ b/tests/commands/base/test_update_cookiecutter_cache.py @@ -5,7 +5,7 @@ import pytest from git import exc as git_exceptions -from briefcase.exceptions import BriefcaseCommandError, TemplateUnsupportedVersion +from briefcase.exceptions import BriefcaseCommandError, InvalidTemplateBranch from ...utils import create_file @@ -394,7 +394,7 @@ def test_cached_missing_branch_template(base_command, mock_git): cached_path.mkdir(parents=True) # Generating the template under there conditions raises an error - with pytest.raises(TemplateUnsupportedVersion): + with pytest.raises(InvalidTemplateBranch): base_command.update_cookiecutter_cache( template="https://example.com/magic/special-template.git", branch="invalid", diff --git a/tests/commands/create/test_generate_app_template.py b/tests/commands/create/test_generate_app_template.py index 9e5c9c281..8c05bae02 100644 --- a/tests/commands/create/test_generate_app_template.py +++ b/tests/commands/create/test_generate_app_template.py @@ -12,8 +12,8 @@ from briefcase.exceptions import ( BriefcaseCommandError, BriefcaseConfigError, + InvalidTemplateBranch, NetworkFailure, - TemplateUnsupportedVersion, ) @@ -243,7 +243,7 @@ def test_default_template_dev_explicit_invalid_branch( ) # Generate the template. - with pytest.raises(TemplateUnsupportedVersion): + with pytest.raises(InvalidTemplateBranch): create_command.generate_app_template(myapp) # Cookiecutter was invoked (once) with the expected template name and context. @@ -651,7 +651,7 @@ def test_cached_missing_branch_template(monkeypatch, create_command, myapp): mock_remote.refs.__getitem__.side_effect = IndexError # Generating the template under there conditions raises an error - with pytest.raises(TemplateUnsupportedVersion): + with pytest.raises(InvalidTemplateBranch): create_command.generate_app_template(myapp) diff --git a/tests/commands/new/test_new_app.py b/tests/commands/new/test_new_app.py index 1748a6b07..d389d7922 100644 --- a/tests/commands/new/test_new_app.py +++ b/tests/commands/new/test_new_app.py @@ -10,8 +10,8 @@ from briefcase.console import Console, Log from briefcase.exceptions import ( BriefcaseCommandError, + InvalidTemplateBranch, InvalidTemplateRepository, - TemplateUnsupportedVersion, ) @@ -123,7 +123,7 @@ def test_new_app_missing_template(monkeypatch, new_command, tmp_path): ) # Create the new app, using the default template. This raises an error. - with pytest.raises(TemplateUnsupportedVersion): + with pytest.raises(InvalidTemplateBranch): new_command.new_app() # App context is constructed @@ -397,7 +397,7 @@ def test_new_app_with_invalid_template_branch(monkeypatch, new_command, tmp_path ) # Create a new app, with a specific template; this raises an error - with pytest.raises(TemplateUnsupportedVersion): + with pytest.raises(InvalidTemplateBranch): new_command.new_app(template="https://example.com/other.git") # App context is constructed