Skip to content

Commit

Permalink
Run Black Pre-Commit Step
Browse files Browse the repository at this point in the history
  • Loading branch information
vps-eric committed May 2, 2023
1 parent 24a8893 commit c23fe80
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
16 changes: 9 additions & 7 deletions salt/states/pip_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def _check_if_installed(
index_url,
extra_index_url,
pip_list=False,
**kwargs
**kwargs,
):
"""
Takes a package name and version specification (if any) and checks it is
Expand Down Expand Up @@ -367,7 +367,9 @@ def _pep440_version_cmp(pkg1, pkg2, ignore_epoch=False):
if pkg_resources.parse_version(pkg1) > pkg_resources.parse_version(pkg2):
return 1
except Exception as exc: # pylint: disable=broad-except
logger.exception(f'Comparison of package versions "{pkg1}" and "{pkg2}" failed: {exc}')
logger.exception(
f'Comparison of package versions "{pkg1}" and "{pkg2}" failed: {exc}'
)
return None


Expand Down Expand Up @@ -418,7 +420,7 @@ def installed(
cache_dir=None,
no_binary=None,
extra_args=None,
**kwargs
**kwargs,
):
"""
Make sure the package is installed
Expand Down Expand Up @@ -853,7 +855,7 @@ def installed(
# If we fail, then just send False, and we'll try again in the next function call
except Exception as exc: # pylint: disable=broad-except
logger.exception(
f'Pre-caching of PIP packages during states.pip.installed failed by exception from pip.list: {exc}'
f"Pre-caching of PIP packages during states.pip.installed failed by exception from pip.list: {exc}"
)
pip_list = False

Expand All @@ -874,7 +876,7 @@ def installed(
index_url,
extra_index_url,
pip_list,
**kwargs
**kwargs,
)
# If _check_if_installed result is None, something went wrong with
# the command running. This way we keep stateful output.
Expand Down Expand Up @@ -980,7 +982,7 @@ def installed(
no_cache_dir=no_cache_dir,
extra_args=extra_args,
disable_version_check=True,
**kwargs
**kwargs,
)

if pip_install_call and pip_install_call.get("retcode", 1) == 0:
Expand Down Expand Up @@ -1045,7 +1047,7 @@ def installed(
user=user,
cwd=cwd,
env_vars=env_vars,
**kwargs
**kwargs,
)
)

Expand Down
61 changes: 31 additions & 30 deletions tests/pytests/unit/states/test_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@

@pytest.fixture
def configure_loader_modules():
return {
pip_state: {
'__env__': 'base',
'__opts__': {
'test': False
}
}
}
return {pip_state: {"__env__": "base", "__opts__": {"test": False}}}


def test_issue_64169(caplog):
pkg_to_install = 'nonexistent_package'
exception_message = 'Invalid JSON (test_issue_64169)'
pkg_to_install = "nonexistent_package"
exception_message = "Invalid JSON (test_issue_64169)"

mock_pip_list = MagicMock(side_effect=[
CommandExecutionError(exception_message), # pre-cache the pip list (preinstall)
{}, # Checking if the pkg is already installed
{pkg_to_install: '100.10.1'} # Confirming successful installation
])
mock_pip_version = MagicMock(return_value='100.10.1')
mock_pip_list = MagicMock(
side_effect=[
CommandExecutionError(
exception_message
), # pre-cache the pip list (preinstall)
{}, # Checking if the pkg is already installed
{pkg_to_install: "100.10.1"}, # Confirming successful installation
]
)
mock_pip_version = MagicMock(return_value="100.10.1")
mock_pip_install = MagicMock(return_value={"retcode": 0, "stdout": ""})

with patch.dict(pip_state.__salt__, {
"pip.list": mock_pip_list,
"pip.version": mock_pip_version,
"pip.install": mock_pip_install
}):
with patch.dict(
pip_state.__salt__,
{
"pip.list": mock_pip_list,
"pip.version": mock_pip_version,
"pip.install": mock_pip_install,
},
):
with caplog.at_level(logging.WARNING):
# Call pip.installed with a specifically 'broken' pip.list.
# pip.installed should continue, but log the exception from pip.list.
Expand All @@ -47,24 +47,25 @@ def test_issue_64169(caplog):
try:
pip_state.installed(
name=pkg_to_install,
use_wheel=False, # Set False to simplify testing
use_wheel=False, # Set False to simplify testing
no_use_wheel=False, # '
no_binary=False, # '
log=None # Regression will cause this function call to throw
# an AttributeError
no_binary=False, # '
log=None, # Regression will cause this function call to throw an AttributeError
)
except AttributeError as exc:
# Observed behavior in #64169
pytest.fail(
'Regression on #64169: pip_state.installed seems to be throwing an unexpected AttributeException: '
f'{exc}'
"Regression on #64169: pip_state.installed seems to be throwing an unexpected AttributeException: "
f"{exc}"
)

# Take 64169 further and actually confirm that the exception from pip.list got logged.
assert 'Pre-caching of PIP packages during states.pip.installed failed by exception ' \
f'from pip.list: {exception_message}' in caplog.messages
assert (
"Pre-caching of PIP packages during states.pip.installed failed by exception "
f"from pip.list: {exception_message}" in caplog.messages
)

# Confirm that the state continued to install the package as expected.
# Only check the 'pkgs' parameter of pip.install
mock_install_call_args, mock_install_call_kwargs = mock_pip_install.call_args
assert mock_install_call_kwargs['pkgs'] == pkg_to_install
assert mock_install_call_kwargs["pkgs"] == pkg_to_install

0 comments on commit c23fe80

Please sign in to comment.