<commit_before># -*- coding: utf-8 -*-
'''
    tests.unit.states.pip_test
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    :codeauthor: :email:`<NAME> (<EMAIL>)`
    :copyright: © 2013 by the SaltStack Team, see AUTHORS for more details.
    :license: Apache 2.0, see LICENSE for more details.
'''

# Import python libs
import warnings

# Import Salt Testing libs
from salttesting import skipIf, TestCase
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')

# Import salt libs
import integration
from salt.states import pip_state as pip
from salt.exceptions import CommandExecutionError

# Import 3rd-party libs
try:
    from mock import MagicMock, patch
    HAS_MOCK = True
except ImportError:
    HAS_MOCK = False

pip.__opts__ = {'test': False}
pip.__salt__ = {'cmd.which_bin': lambda _: 'pip'}


@skipIf(HAS_MOCK is False, 'mock python module is unavailable')
class PipStateTest(TestCase, integration.SaltReturnAssertsMixIn):

    def test_installed_deprecated_runas(self):
        # We *always* want *all* warnings thrown on this module
        warnings.resetwarnings()
        warnings.filterwarnings('always', '', DeprecationWarning, __name__)

        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        pip_list = MagicMock(return_value=[])
        pip_install = MagicMock(return_value={'retcode': 0})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock,
                                       'pip.list': pip_list,
                                       'pip.install': pip_install}):
            with warnings.catch_warnings(record=True) as w:
                ret = pip.installed('pep8', runas='me!')<commit_msg>Adapted `unit.states.pip_test` to use `salttesting.mock`<commit_after># -*- coding: utf-8 -*-
'''
    tests.unit.states.pip_test
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    :codeauthor: :email:`<NAME> (<EMAIL>)`
    :copyright: © 2013 by the SaltStack Team, see AUTHORS for more details.
    :license: Apache 2.0, see LICENSE for more details.
'''

# Import python libs
import warnings

# Import Salt Testing libs
from salttesting import skipIf, TestCase
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
ensure_in_syspath('../../')

# Import salt libs
import integration
from salt.states import pip_state as pip
from salt.exceptions import CommandExecutionError

pip.__opts__ = {'test': False}
pip.__salt__ = {'cmd.which_bin': lambda _: 'pip'}


@skipIf(NO_MOCK, NO_MOCK_REASON)
class PipStateTest(TestCase, integration.SaltReturnAssertsMixIn):

    def test_installed_deprecated_runas(self):
        # We *always* want *all* warnings thrown on this module
        warnings.resetwarnings()
        warnings.filterwarnings('always', '', DeprecationWarning, __name__)

        mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
        pip_list = MagicMock(return_value=[])
        pip_install = MagicMock(return_value={'retcode': 0})
        with patch.dict(pip.__salt__, {'cmd.run_all': mock,
                                       'pip.list': pip_list,
                                       'pip.install': pip_install}):
            with warnings.catch_warnings(record=True) as w:
                ret = pip.installed('pep8', runas='me!')
{'fraction_non_alphanumeric': 0.12966360856269113, 'fraction_numerical': 0.006422018348623854, 'mean_word_length': 3.2591145833333335, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1337513', 'n_tokens_mistral': 1023, 'n_tokens_neox': 965, 'n_words': 255}