<commit_before># -*- coding: utf-8 -*-

# Import python libs
from __future__ import absolute_import
import os

# Import Salt Testing libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase
from tests.support.mock import (
    MagicMock,
    patch
)
# Import salt libs
import salt.states.environ as envstate
import salt.modules.environ as envmodule
import salt.modules.reg as regmodule
import salt.utils as utils


class TestEnvironState(TestCase, LoaderModuleMockMixin):

    def setup_loader_modules(self):
        loader_globals = {
            '__env__': 'base',
            '__opts__': {'test': False},
            '__salt__': {
                'environ.setenv': envmodule.setenv,
                'reg.read_value': regmodule.read_value,
            }
        }
        return {envstate: loader_globals, envmodule: loader_globals}

    def setUp(self):
        patcher = patch.dict(os.environ, {'INITIAL': 'initial'}, clear=True)
        patcher.start()

        def reset_environ(patcher):
            patcher.stop()

        self.addCleanup(reset_environ, patcher)

    def test_setenv(self):
        '''test that a subsequent calls of setenv changes nothing'''

        ret = envstate.setenv('test', 'value')
        self.assertEqual(ret['changes'], {'test': 'value'})

        ret = envstate.setenv('test', 'other')
        self.assertEqual(ret['changes'], {'test': 'other'})

        # once again with the same value
        ret = envstate.setenv('test', 'other')
        self.assertEqual(ret['changes'], {})

    def test_setenv_permanent(self):
        with patch.dict(envmodule.__salt__, {'reg.set_value': MagicMock(), 'reg.delete_value': MagicMock()}), \
                patch('salt.utils.is_windows', MagicMock(return_value=True)):
            ret = envstate.setenv('test', 'value', permanent=True)
            self.assertEqual(ret['changes'], {'test': 'value'})
            envmodule.__salt__['reg.set_value'].assert_called_with("HKCU", "Environment", 'test', 'value')

            ret = envstate.setenv('test', False, false_unsets=True, permanent=True)
            self.assertEqual(ret['changes'], {'test': None})
            envmodule.__salt__['reg.delete_value'].assert_called_with("HKCU", "Environment", 'test')

    def test_setenv_dict(self):
        '''test that setenv can be invoked with dict'''<commit_msg>Skip test that requires pywin32 on *nix platforms<commit_after># -*- coding: utf-8 -*-

# Import python libs
from __future__ import absolute_import
import os

# Import Salt Testing libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase
from tests.support.mock import (
    MagicMock,
    patch
)
# Import salt libs
import salt.states.environ as envstate
import salt.modules.environ as envmodule
import salt.modules.reg as regmodule
import salt.utils as utils


class TestEnvironState(TestCase, LoaderModuleMockMixin):

    def setup_loader_modules(self):
        loader_globals = {
            '__env__': 'base',
            '__opts__': {'test': False},
            '__salt__': {
                'environ.setenv': envmodule.setenv,
                'reg.read_value': regmodule.read_value,
            }
        }
        return {envstate: loader_globals, envmodule: loader_globals}

    def setUp(self):
        patcher = patch.dict(os.environ, {'INITIAL': 'initial'}, clear=True)
        patcher.start()

        def reset_environ(patcher):
            patcher.stop()

        self.addCleanup(reset_environ, patcher)

    def test_setenv(self):
        '''test that a subsequent calls of setenv changes nothing'''

        ret = envstate.setenv('test', 'value')
        self.assertEqual(ret['changes'], {'test': 'value'})

        ret = envstate.setenv('test', 'other')
        self.assertEqual(ret['changes'], {'test': 'other'})

        # once again with the same value
        ret = envstate.setenv('test', 'other')
        self.assertEqual(ret['changes'], {})

    @skipIf(not utils.is_windows(), 'Windows only')
    def test_setenv_permanent(self):
        '''test that we can set perminent environment variables (requires pywin32)'''
        with patch.dict(envmodule.__salt__, {'reg.set_value': MagicMock(), 'reg.delete_value': MagicMock()}):
            ret = envstate.setenv('test', 'value', permanent=True)
            self.assertEqual(ret['changes'], {'test': 'value'})
            envmodule.__salt__['reg.set_value'].assert_called_with("HKCU", "Environment", 'test', 'value')

            ret = envstate.setenv('test', False, false_unsets=True, permanent=True)
            self.assertEqual(ret['changes'], {'test': None})
            envmodule.__salt__['reg.delete_value'].assert_called_with("HKCU", "Environment", 'test')

    def test_setenv_dict(self):
        '''test that setenv can be invoked with dict'''
{'fraction_non_alphanumeric': 0.1325898389095415, 'fraction_numerical': 0.0012391573729863693, 'mean_word_length': 3.0869198312236286, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4020609', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1388, 'n_words': 332}