Skip to content

Commit

Permalink
Make pip state use pip.normalize
Browse files Browse the repository at this point in the history
Similarly to `pip.list`, normalize package names before
processing them. In this case most of the work is already done -
most functions invoke `_check_package_version_format` and use
the processed package name that it returns. Thus, to use the same
normalization algorithm everywhere, make that function use
`pip.normalize`, too.

This, among other things, deals with some "There was no error
installing package... although it does not show..." warnings when
the package name contains underscores, since now we look for
the correctly normalized name and we can find it.

Fixes saltstack#66988
  • Loading branch information
avelin committed Oct 23, 2024
1 parent f57a3d2 commit d1c27d1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 3 additions & 2 deletions salt/states/pip_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,12 @@ def _check_pkg_version_format(pkg):
ret["version_spec"] = []
else:
ret["result"] = True
normalize = __salt__["pip.normalize"]
try:
ret["prefix"] = install_req.req.project_name
ret["prefix"] = normalize(install_req.req.project_name)
ret["version_spec"] = install_req.req.specs
except Exception: # pylint: disable=broad-except
ret["prefix"] = re.sub("[^A-Za-z0-9.]+", "-", install_req.name)
ret["prefix"] = normalize(install_req.name)
if hasattr(install_req, "specifier"):
specifier = install_req.specifier
else:
Expand Down
2 changes: 2 additions & 0 deletions tests/pytests/unit/states/test_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest

import salt.modules.pip as pip_module
import salt.states.pip_state as pip_state
from salt.exceptions import CommandExecutionError
from tests.support.mock import MagicMock, patch
Expand Down Expand Up @@ -38,6 +39,7 @@ def test_issue_64169(caplog):
"pip.list": mock_pip_list,
"pip.version": mock_pip_version,
"pip.install": mock_pip_install,
"pip.normalize": pip_module.normalize,
},
):
with caplog.at_level(logging.WARNING):
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/states/test_pip_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest

import salt.modules.pip as pip_module
import salt.states.pip_state as pip_state
import salt.utils.path
import salt.version
Expand All @@ -25,7 +26,10 @@ def setup_loader_modules(self):
pip_state: {
"__env__": "base",
"__opts__": {"test": False},
"__salt__": {"cmd.which_bin": lambda _: "pip"},
"__salt__": {
"cmd.which_bin": lambda _: "pip",
"pip.normalize": pip_module.normalize,
},
}
}

Expand Down

0 comments on commit d1c27d1

Please sign in to comment.