From 32eb779a3923f0f88e8fd9edd7e8e4c352a28948 Mon Sep 17 00:00:00 2001 From: Sergio Schvezov Date: Fri, 6 Oct 2023 09:33:58 -0300 Subject: [PATCH] fix(remote-build): warn instead of error when using unsupported bases Signed-off-by: Sergio Schvezov --- snapcraft/commands/remote.py | 26 ++++++++++++++++++++- snapcraft/errors.py | 1 + tests/unit/commands/test_remote.py | 36 ++++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/snapcraft/commands/remote.py b/snapcraft/commands/remote.py index 61c765a38a..a5bc7ea384 100644 --- a/snapcraft/commands/remote.py +++ b/snapcraft/commands/remote.py @@ -135,7 +135,12 @@ def run(self, parsed_args: argparse.Namespace) -> None: self._snapcraft_yaml = yaml_utils.get_snap_project().project_file self._parsed_args = parsed_args # pylint: enable=attribute-defined-outside-init - base = self._get_effective_base() + try: + base = self._get_effective_base() + except MaintenanceBase as base_err: + base = base_err.base + emit.progress(_get_esm_warning_for_base(base), permanent=True) + self._run_new_or_fallback_remote_build(base) def _run_new_or_fallback_remote_build(self, base: str) -> None: @@ -279,3 +284,22 @@ def _get_effective_base(self) -> str: ) return base + + +def _get_esm_warning_for_base(base: str) -> str: + """Return a warning appropriate for the base under ESM.""" + channel: Optional[str] = None + match base: + case "core": + channel = "4.x" + version = "4" + case "core18": + channel = "7.x" + version = "7" + case _: + raise RuntimeError(f"Unmatched base {base!r}") + + return ( + f"WARNING: base {base!r} was last supported on Snapcraft {version} available " + f"on the {channel!r} channel." + ) diff --git a/snapcraft/errors.py b/snapcraft/errors.py index 18ae86ff93..37148a05ea 100644 --- a/snapcraft/errors.py +++ b/snapcraft/errors.py @@ -138,6 +138,7 @@ def __init__(self, base: str) -> None: resolution=resolution, docs_url="https://snapcraft.io/docs/base-snaps", ) + self.base = base class StoreCredentialsUnauthorizedError(SnapcraftError): diff --git a/tests/unit/commands/test_remote.py b/tests/unit/commands/test_remote.py index f03ee82e0e..35403c475f 100644 --- a/tests/unit/commands/test_remote.py +++ b/tests/unit/commands/test_remote.py @@ -220,7 +220,7 @@ def test_get_effective_base_with_build_base( @pytest.mark.usefixtures("mock_argv", "mock_confirm") -@pytest.mark.parametrize("base", CURRENT_BASES | LEGACY_BASES) +@pytest.mark.parametrize("base", CURRENT_BASES | LEGACY_BASES | ESM_BASES) def test_get_effective_base_type( base, snapcraft_yaml, mock_run_new_or_fallback_remote_build ): @@ -255,15 +255,37 @@ def test_get_effective_base_none(capsys, snapcraft_yaml): @pytest.mark.usefixtures("mock_argv", "mock_confirm") -@pytest.mark.parametrize("base", ESM_BASES) -def test_get_effective_base_esm(base, capsys, snapcraft_yaml): - """Raise an error if an ESM base is used.""" - snapcraft_yaml(base=base) +def test_get_effective_base_core_esm_warning( + emitter, snapcraft_yaml, mock_run_new_or_fallback_remote_build +): + """Warn if core, an ESM base, is used.""" + snapcraft_yaml(base="core") cli.run() - _, err = capsys.readouterr() - assert f"{base!r} is not supported on this version of Snapcraft." in err + mock_run_new_or_fallback_remote_build.assert_called_once_with("core") + emitter.assert_progress( + "WARNING: base 'core' was last supported on Snapcraft 4 available on the " + "'4.x' channel.", + permanent=True, + ) + + +@pytest.mark.usefixtures("mock_argv", "mock_confirm") +def test_get_effective_base_core18_esm_warning( + emitter, snapcraft_yaml, mock_run_new_or_fallback_remote_build +): + """Warn if core18, an ESM base, is used.""" + snapcraft_yaml(base="core18") + + cli.run() + + mock_run_new_or_fallback_remote_build.assert_called_once_with("core18") + emitter.assert_progress( + "WARNING: base 'core18' was last supported on Snapcraft 7 available on the " + "'7.x' channel.", + permanent=True, + ) #######################