diff --git a/changelog/68102.added.md b/changelog/68102.added.md new file mode 100644 index 00000000000..648a906edab --- /dev/null +++ b/changelog/68102.added.md @@ -0,0 +1,2 @@ +Added a new `force` option to pkg.install on Windows to force the installer +to run even if the package is already installed diff --git a/salt/modules/win_pkg.py b/salt/modules/win_pkg.py index d4bee486e0d..cd7cb54a81b 100644 --- a/salt/modules/win_pkg.py +++ b/salt/modules/win_pkg.py @@ -1471,6 +1471,14 @@ def install(name=None, refresh=False, pkgs=None, **kwargs): .. versionadded:: 2016.11.0 + force (bool): + If ``True``, the installation will run whether the package is + already installed or not. If ``False``, the installation will not + run if the correct version of the package is already installed. + Default is ``False``. + + .. versionadded:: 3006.15 + Returns: dict: Return a dict containing the new package names and versions. If the package is already installed, an empty dict is returned. @@ -1603,12 +1611,13 @@ def install(name=None, refresh=False, pkgs=None, **kwargs): # If the version was not passed, version_num will be None if not version_num: if pkg_name in old: - log.debug( - "pkg.install: '%s' version '%s' is already installed", - pkg_name, - old[pkg_name][0], - ) - continue + if not kwargs.get("force", False): + log.debug( + "pkg.install: '%s' version '%s' is already installed", + pkg_name, + old[pkg_name][0], + ) + continue # Get the most recent version number available from winrepo.p # May also return `latest` or an empty string version_num = _get_latest_pkg_version(pkginfo) @@ -1621,12 +1630,13 @@ def install(name=None, refresh=False, pkgs=None, **kwargs): # Check if the version is already installed if version_num in old.get(pkg_name, []): # Desired version number already installed - log.debug( - "pkg.install: '%s' version '%s' is already installed", - pkg_name, - version_num, - ) - continue + if not kwargs.get("force", False): + log.debug( + "pkg.install: '%s' version '%s' is already installed", + pkg_name, + version_num, + ) + continue # If version number not installed, is the version available? elif version_num != "latest" and version_num not in pkginfo: log.error("Version %s not found for package %s", version_num, pkg_name) diff --git a/tests/pytests/unit/modules/test_win_pkg.py b/tests/pytests/unit/modules/test_win_pkg.py index dd35d8f6521..b056a13a8ff 100644 --- a/tests/pytests/unit/modules/test_win_pkg.py +++ b/tests/pytests/unit/modules/test_win_pkg.py @@ -193,6 +193,29 @@ def test_pkg_install_existing(): assert expected == result +def test_pkg_install_existing_force_true(): + """ + test pkg.install when the package is already installed + and force=True + """ + ret_reg = {"Nullsoft Install System": "3.03"} + # The 2nd time it's run, pkg.list_pkgs uses with stringify + se_list_pkgs = {"nsis": ["3.03"]} + with patch.object(win_pkg, "list_pkgs", return_value=se_list_pkgs), patch.object( + win_pkg, "_get_reg_software", return_value=ret_reg + ), patch.dict( + win_pkg.__salt__, + { + "cmd.run_all": MagicMock(return_value={"retcode": 0}), + "cp.cache_file": MagicMock(return_value="C:\\fake\\path.exe"), + "cp.is_cached": MagicMock(return_value=True), + }, + ): + expected = {"nsis": {"install status": "success"}} + result = win_pkg.install(name="nsis", force=True) + assert expected == result + + def test_pkg_install_latest(): """ test pkg.install when the package is already installed
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: