Merge pull request #705 from ddalcino/topic/ddalcino/cli/warn-ext7zr

Log a warning when aqtinstall falls back to an external 7z extraction tool
This commit is contained in:
Hiroshi Miura
2023-09-12 07:35:30 +09:00
committed by GitHub
2 changed files with 68 additions and 20 deletions

View File

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