<commit_before># -*- coding: utf-8 -*-
"""
Tests for the win_pkg module
"""

# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals

# Import Salt Libs
import salt.modules.config as config
import salt.modules.pkg_resource as pkg_resource
import salt.modules.win_pkg as win_pkg
import salt.utils.data
import salt.utils.platform
import salt.utils.win_reg as win_reg
from salt.exceptions import MinionError

# Import 3rd Party Libs
from salt.ext import six

# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase, skipIf


@skipIf(not salt.utils.platform.is_windows(), "Must be on Windows!")
class WinPkgInstallTestCase(TestCase, LoaderModuleMockMixin):
    """
    Test cases for salt.modules.win_pkg
    """

    def setup_loader_modules(self):
        pkg_info = {
            "3.03": {
                "full_name": "Nullsoft Install System",
                "installer": "http://download.sourceforge.net/project/nsis/NSIS%203/3.03/nsis-3.03-setup.exe",
                "install_flags": "/S",
                "uninstaller": "%PROGRAMFILES(x86)%\\NSIS\\uninst-nsis.exe",
                "uninstall_flags": "/S",
                "msiexec": False,
                "reboot": False,
            },
            "3.02": {
                "full_name": "Nullsoft Install System",
                "installer": "http://download.sourceforge.net/project/nsis/NSIS%203/3.02/nsis-3.02-setup.exe",
                "install_flags": "/S",
                "uninstaller": "%PROGRAMFILES(x86)%\\NSIS\\uninst-nsis.exe",
                "uninstall_flags": "/S",
                "msiexec": False,
                "reboot": False,
            },
        }

        return {
            win_pkg: {
                "_get_latest_package_version": MagicMock(return_value="3.03"),
                "_get_package_info": MagicMock(return_value=pkg_info),
                "__salt__": {
                    "pkg_resource.add_pkg": pkg_resource.add_pkg,
                    "pkg_resource.parse_targets": pkg_resource.parse_targets,
                    "pkg_resource.sort_pkglist": pkg_resource.sort_pkglist,
                    "pkg_resource.stringify": pkg_resource.stringify,
                    "config.valid_fileproto": config.valid_fileproto,
                },
                "__utils__": {
                    "reg.key_exists": win_reg.key_exists,
                    "reg.list_keys": win_reg.list_keys,
                    "reg.read_value": win_reg.read_value,
                    "reg.value_exists": win_reg.value_exists,
                },
            },
            pkg_resource: {"__grains__": {"os": "Windows"}},
        }

    def test_pkg__get_reg_software(self):
        result = win_pkg._get_reg_software()
        self.assertTrue(isinstance(result, dict))
        found_python = False
        search = "Python 2" if six.PY2 else "Python 3"
        for key in result:
            if search in key:
                found_python = True
        self.assertTrue(found_python)

    def test_pkg_install_not_found(self):
        """
        Test pkg.install when the Version is NOT FOUND in the Software
        Definition
        """
        ret_reg = {"Nullsoft Install System": "3.03"}
        # The 2nd time it's run 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):
            expected = {"nsis": {"not found": "3.01"}}
            result = win_pkg.install(name="nsis", version="3.01")
            self.assertDictEqual(expected, result)

    def test_pkg_install_rollback(self):
        """
        test pkg.install rolling back to a previous version
        """
        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"]}, {"nsis": "3.02"}]<commit_msg>Drop Py2 and six on tests/unit/modules/test_win_pkg.py<commit_after>"""
Tests for the win_pkg module
"""

import salt.modules.config as config
import salt.modules.pkg_resource as pkg_resource
import salt.modules.win_pkg as win_pkg
import salt.utils.data
import salt.utils.platform
import salt.utils.win_reg as win_reg
from salt.exceptions import MinionError
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase, skipIf


@skipIf(not salt.utils.platform.is_windows(), "Must be on Windows!")
class WinPkgInstallTestCase(TestCase, LoaderModuleMockMixin):
    """
    Test cases for salt.modules.win_pkg
    """

    def setup_loader_modules(self):
        pkg_info = {
            "3.03": {
                "full_name": "Nullsoft Install System",
                "installer": "http://download.sourceforge.net/project/nsis/NSIS%203/3.03/nsis-3.03-setup.exe",
                "install_flags": "/S",
                "uninstaller": "%PROGRAMFILES(x86)%\\NSIS\\uninst-nsis.exe",
                "uninstall_flags": "/S",
                "msiexec": False,
                "reboot": False,
            },
            "3.02": {
                "full_name": "Nullsoft Install System",
                "installer": "http://download.sourceforge.net/project/nsis/NSIS%203/3.02/nsis-3.02-setup.exe",
                "install_flags": "/S",
                "uninstaller": "%PROGRAMFILES(x86)%\\NSIS\\uninst-nsis.exe",
                "uninstall_flags": "/S",
                "msiexec": False,
                "reboot": False,
            },
        }

        return {
            win_pkg: {
                "_get_latest_package_version": MagicMock(return_value="3.03"),
                "_get_package_info": MagicMock(return_value=pkg_info),
                "__salt__": {
                    "pkg_resource.add_pkg": pkg_resource.add_pkg,
                    "pkg_resource.parse_targets": pkg_resource.parse_targets,
                    "pkg_resource.sort_pkglist": pkg_resource.sort_pkglist,
                    "pkg_resource.stringify": pkg_resource.stringify,
                    "config.valid_fileproto": config.valid_fileproto,
                },
                "__utils__": {
                    "reg.key_exists": win_reg.key_exists,
                    "reg.list_keys": win_reg.list_keys,
                    "reg.read_value": win_reg.read_value,
                    "reg.value_exists": win_reg.value_exists,
                },
            },
            pkg_resource: {"__grains__": {"os": "Windows"}},
        }

    def test_pkg__get_reg_software(self):
        result = win_pkg._get_reg_software()
        self.assertTrue(isinstance(result, dict))
        found_python = False
        search = "Python 3"
        for key in result:
            if search in key:
                found_python = True
        self.assertTrue(found_python)

    def test_pkg_install_not_found(self):
        """
        Test pkg.install when the Version is NOT FOUND in the Software
        Definition
        """
        ret_reg = {"Nullsoft Install System": "3.03"}
        # The 2nd time it's run 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):
            expected = {"nsis": {"not found": "3.01"}}
            result = win_pkg.install(name="nsis", version="3.01")
            self.assertDictEqual(expected, result)

    def test_pkg_install_rollback(self):
        """
        test pkg.install rolling back to a previous version
        """
        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"]}, {"nsis": "3.02"}]
{'fraction_non_alphanumeric': 0.11821852132223064, 'fraction_numerical': 0.01450921019429725, 'mean_word_length': 2.0796425796425795, 'pattern_counts': {'":': 76, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2536972', 'n_tokens_mistral': 2506, 'n_tokens_neox': 2335, 'n_words': 479}