Remove fallback parameter

This commit is contained in:
Dave Dalcino
2023-08-27 15:07:47 -07:00
parent 1eeebfb44d
commit 85037194fe
2 changed files with 15 additions and 19 deletions

View File

@@ -233,10 +233,11 @@ class Cli:
"This may not install properly, but we will try our best." "This may not install properly, but we will try our best."
) )
def _set_sevenzip(self, external: Optional[str], fallback: str, is_p7zr_missing: bool) -> Optional[str]: def _set_sevenzip(self, external: Optional[str]) -> Optional[str]:
sevenzip = external sevenzip = external
fallback = Settings.zipcmd
if not sevenzip: if not sevenzip:
if is_p7zr_missing: if EXT7Z:
self.logger.warning(f"The py7zr module failed to load. Falling back to '{fallback}' for .7z extraction.") self.logger.warning(f"The py7zr module failed to load. Falling back to '{fallback}' for .7z extraction.")
self.logger.warning(f"You can use the '--external | -E' flags to select your own extraction tool.") self.logger.warning(f"You can use the '--external | -E' flags to select your own extraction tool.")
sevenzip = fallback sevenzip = fallback
@@ -363,9 +364,6 @@ class Cli:
timeout = (Settings.connection_timeout, Settings.response_timeout) timeout = (Settings.connection_timeout, Settings.response_timeout)
modules = args.modules modules = args.modules
sevenzip = self._set_sevenzip(args.external) sevenzip = self._set_sevenzip(args.external)
if EXT7Z and sevenzip is None:
# override when py7zr is not exist
sevenzip = self._set_sevenzip("7z")
if args.base is not None: if args.base is not None:
if not self._check_mirror(args.base): if not self._check_mirror(args.base):
raise CliInputError( raise CliInputError(
@@ -480,9 +478,6 @@ class Cli:
else: else:
timeout = (Settings.connection_timeout, Settings.response_timeout) timeout = (Settings.connection_timeout, Settings.response_timeout)
sevenzip = self._set_sevenzip(args.external) sevenzip = self._set_sevenzip(args.external)
if EXT7Z and sevenzip is None:
# override when py7zr is not exist
sevenzip = self._set_sevenzip(Settings.zipcmd)
modules = getattr(args, "modules", None) # `--modules` is invalid for `install-src` modules = getattr(args, "modules", None) # `--modules` is invalid for `install-src`
archives = args.archives archives = args.archives
all_extra = True if modules is not None and "all" in modules else False all_extra = True if modules is not None and "all" in modules else False
@@ -554,9 +549,6 @@ class Cli:
else: else:
base_dir = output_dir base_dir = output_dir
sevenzip = self._set_sevenzip(args.external) sevenzip = self._set_sevenzip(args.external)
if EXT7Z and sevenzip is None:
# override when py7zr is not exist
sevenzip = self._set_sevenzip(Settings.zipcmd)
version = getattr(args, "version", None) version = getattr(args, "version", None)
if version is not None: if version is not None:
Cli._validate_version_str(version, allow_minus=True) Cli._validate_version_str(version, allow_minus=True)

View File

@@ -7,6 +7,7 @@ from typing import Dict, List, Optional, Union
import pytest import pytest
from aqt.exceptions import CliInputError from aqt.exceptions import CliInputError
from aqt.helper import Settings
from aqt.installer import Cli from aqt.installer import Cli
from aqt.metadata import MetadataFactory, SimpleSpec, Version from aqt.metadata import MetadataFactory, SimpleSpec, Version
@@ -395,18 +396,19 @@ def test_cli_set_7zip_nonexistent(monkeypatch):
def test_set_7zip_checks_external_tool_when_specified(monkeypatch, capsys, external_tool_exists: bool): def test_set_7zip_checks_external_tool_when_specified(monkeypatch, capsys, external_tool_exists: bool):
cli = Cli() cli = Cli()
cli._setup_settings() cli._setup_settings()
external, fallback = "my_7z_extractor", "7zipper" external = "my_7z_extractor"
def mock_subprocess_run(args, **kwargs): def mock_subprocess_run(args, **kwargs):
assert args[0] == external assert args[0] == external
if not external_tool_exists: if not external_tool_exists:
raise FileNotFoundError() raise FileNotFoundError()
monkeypatch.setattr("aqt.installer.subprocess.run", mock_subprocess_run) monkeypatch.setattr("aqt.installer.subprocess.run", mock_subprocess_run)
monkeypatch.setattr("aqt.installer.EXT7Z", False)
if external_tool_exists: if external_tool_exists:
assert external == cli._set_sevenzip(external, fallback, is_p7zr_missing=False) assert external == cli._set_sevenzip(external)
else: else:
with pytest.raises(CliInputError) as err: with pytest.raises(CliInputError) as err:
cli._set_sevenzip(external, fallback, is_p7zr_missing=False) cli._set_sevenzip(external)
assert format(err.value) == format(f"Specified 7zip command executable does not exist: '{external}'") assert format(err.value) == format(f"Specified 7zip command executable does not exist: '{external}'")
assert capsys.readouterr()[1] == '' assert capsys.readouterr()[1] == ''
@@ -415,18 +417,19 @@ def test_set_7zip_checks_external_tool_when_specified(monkeypatch, capsys, exter
def test_set_7zip_uses_fallback_when_py7zr_missing(monkeypatch, capsys, fallback_exists: bool): def test_set_7zip_uses_fallback_when_py7zr_missing(monkeypatch, capsys, fallback_exists: bool):
cli = Cli() cli = Cli()
cli._setup_settings() cli._setup_settings()
external, fallback = None, "7zipper" external, fallback = None, Settings.zipcmd
def mock_subprocess_run(args, **kwargs): def mock_subprocess_run(args, **kwargs):
assert args[0] == fallback assert args[0] == fallback
if not fallback_exists: if not fallback_exists:
raise FileNotFoundError() raise FileNotFoundError()
monkeypatch.setattr("aqt.installer.subprocess.run", mock_subprocess_run) monkeypatch.setattr("aqt.installer.subprocess.run", mock_subprocess_run)
monkeypatch.setattr("aqt.installer.EXT7Z", True)
if fallback_exists: if fallback_exists:
assert fallback == cli._set_sevenzip(external, fallback, is_p7zr_missing=True) assert fallback == cli._set_sevenzip(external)
else: else:
with pytest.raises(CliInputError) as err: with pytest.raises(CliInputError) as err:
cli._set_sevenzip(external, fallback, is_p7zr_missing=True) cli._set_sevenzip(external)
assert format(err.value) == format(f"Fallback 7zip command executable does not exist: '{fallback}'") assert format(err.value) == format(f"Fallback 7zip command executable does not exist: '{fallback}'")
assert f"Falling back to '{fallback}'" in capsys.readouterr()[1] assert f"Falling back to '{fallback}'" in capsys.readouterr()[1]
@@ -435,13 +438,14 @@ def test_set_7zip_uses_fallback_when_py7zr_missing(monkeypatch, capsys, fallback
def test_set_7zip_chooses_p7zr_when_ext_missing(monkeypatch, capsys, fallback_exists: bool): def test_set_7zip_chooses_p7zr_when_ext_missing(monkeypatch, capsys, fallback_exists: bool):
cli = Cli() cli = Cli()
cli._setup_settings() cli._setup_settings()
external, fallback = None, "7zipper" external = None
def mock_subprocess_run(args, **kwargs): def mock_subprocess_run(args, **kwargs):
assert False, "Should not try to run anything" assert False, "Should not try to run anything"
monkeypatch.setattr("aqt.installer.subprocess.run", mock_subprocess_run) monkeypatch.setattr("aqt.installer.subprocess.run", mock_subprocess_run)
assert cli._set_sevenzip(external, fallback, is_p7zr_missing=False) is None monkeypatch.setattr("aqt.installer.EXT7Z", False)
assert cli._set_sevenzip(external) is None
assert capsys.readouterr()[1] == '' assert capsys.readouterr()[1] == ''