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

# Import Python Libs
from __future__ import absolute_import
import os
import textwrap

# Import Salt Testing libs
from salttesting.helpers import ensure_in_syspath

ensure_in_syspath('../../')

# Import Salt Libs
import integration
import salt.utils


STATE_DIR = os.path.join(integration.FILES, 'file', 'base')


class EnabledTest(integration.ModuleCase):
    '''
    validate the use of shell processing for cmd.run on the salt command line
    and in templating
    '''
    cmd = ("printf '%s\n' first second third | wc -l ; "
           "export SALTY_VARIABLE='saltines' && echo $SALTY_VARIABLE ; "
           "echo duh &> /dev/null")

    def test_shell_default_enabled(self):
        '''
        ensure that python_shell defaults to True for cmd.run
        '''
        enabled_ret = '3\nsaltines'  # the result of running self.cmd in a shell
        ret = self.run_function('cmd.run', [self.cmd])
        self.assertEqual(ret.strip(), enabled_ret)

    def test_shell_disabled(self):
        '''
        test shell disabled output for cmd.run
        '''
        disabled_ret = ('first\nsecond\nthird\n|\nwc\n-l\n;\nexport\nSALTY_VARIABLE=saltines'
                        '\n&&\necho\n$SALTY_VARIABLE\n;\necho\nduh\n&>\n/dev/null')
        ret = self.run_function('cmd.run', [self.cmd], python_shell=False)
        self.assertEqual(ret, disabled_ret)

    def test_template_default_enabled(self):
        '''
        ensure that python_shell defaults to True for templates
        '''
        state_name = 'template_shell_enabled'
        state_filename = state_name + '.sls'
        state_file = os.path.join(STATE_DIR, state_filename)

        enabled_ret = '3 saltines'  # the result of running self.cmd in a shell
        ret_key = 'test_|-shell_enabled_|-{0}_|-configurable_test_state'.format(enabled_ret)

        try:
            with salt.utils.fopen(state_file, 'w') as fp_:
                fp_.write(textwrap.dedent('''\
                {{% set shell_enabled = salt['cmd.run']("{0}").strip() %}}

                shell_enabled:
                  test.configurable_test_state:
                    - name: '{{{{ shell_enabled }}}}'
                '''.format(self.cmd)))

            ret = self.run_function('state.sls', [state_name])
            self.assertEqual(ret[ret_key]['name'], enabled_ret)
        finally:
            os.remove(state_file)

    def test_template_disabled(self):
        '''
        test shell disabled output for templates
        '''
        state_name = 'template_shell_disabled'
        state_filename = state_name + '.sls'
        state_file = os.path.join(STATE_DIR, state_filename)

        # the result of running self.cmd not in a shell
        disabled_ret = ('first second third | wc -l ; export SALTY_VARIABLE=saltines '
                        '&& echo $SALTY_VARIABLE ; echo duh &> /dev/null')
        ret_key = 'test_|-shell_enabled_|-{0}_|-configurable_test_state'.format(disabled_ret)

        try:
            with salt.utils.fopen(state_file, 'w') as fp_:
                fp_.write(textwrap.dedent('''\
                {{% set shell_disabled = salt['cmd.run']("{0}", python_shell=False) %}}

                shell_enabled:
                  test.configurable_test_state:<commit_msg>Update shell enabled integration tests with cmd.run aliasing removed<commit_after># -*- coding: utf-8 -*-

# Import Python Libs
from __future__ import absolute_import
import os
import textwrap

# Import Salt Testing libs
from salttesting.helpers import ensure_in_syspath

ensure_in_syspath('../../')

# Import Salt Libs
import integration
import salt.utils


STATE_DIR = os.path.join(integration.FILES, 'file', 'base')


class EnabledTest(integration.ModuleCase):
    '''
    validate the use of shell processing for cmd.run on the salt command line
    and in templating
    '''
    cmd = ("printf '%s\n' first second third | wc -l ; "
           "export SALTY_VARIABLE='saltines' && echo $SALTY_VARIABLE ; "
           "echo duh &> /dev/null")

    def test_shell_default_enabled(self):
        '''
        ensure that python_shell defaults to True for cmd.run
        '''
        enabled_ret = '3\nsaltines'  # the result of running self.cmd in a shell
        ret = self.run_function('cmd.run', [self.cmd])
        self.assertEqual(ret.strip(), enabled_ret)

    def test_shell_disabled(self):
        '''
        test shell disabled output for cmd.run
        '''
        disabled_ret = ('first\nsecond\nthird\n|\nwc\n-l\n;\nexport\nSALTY_VARIABLE=saltines'
                        '\n&&\necho\n$SALTY_VARIABLE\n;\necho\nduh\n&>\n/dev/null')
        ret = self.run_function('cmd.run', [self.cmd], python_shell=False)
        self.assertEqual(ret, disabled_ret)

    def test_template_shell(self):
        '''
        Test cmd.shell works correctly when using a template.

        Note: This test used to test that python_shell defaulted to True for templates
        in releases before Nitrogen. The cmd.run --> cmd.shell aliasing was removed in
        Nitrogen. Templates should now be using cmd.shell.
        '''
        state_name = 'template_shell_enabled'
        state_filename = state_name + '.sls'
        state_file = os.path.join(STATE_DIR, state_filename)

        enabled_ret = '3 saltines'  # the result of running self.cmd in a shell
        ret_key = 'test_|-shell_enabled_|-{0}_|-configurable_test_state'.format(enabled_ret)

        try:
            with salt.utils.fopen(state_file, 'w') as fp_:
                fp_.write(textwrap.dedent('''\
                {{% set shell_enabled = salt['cmd.shell']("{0}").strip() %}}

                shell_enabled:
                  test.configurable_test_state:
                    - name: '{{{{ shell_enabled }}}}'
                '''.format(self.cmd)))

            ret = self.run_function('state.sls', [state_name])
            self.assertEqual(ret[ret_key]['name'], enabled_ret)
        finally:
            os.remove(state_file)

    def test_template_default_disabled(self):
        '''
        test shell disabled output for templates (python_shell=False is the default
        beginning with the Nitrogen release).
        '''
        state_name = 'template_shell_disabled'
        state_filename = state_name + '.sls'
        state_file = os.path.join(STATE_DIR, state_filename)

        # the result of running self.cmd not in a shell
        disabled_ret = ('first second third | wc -l ; export SALTY_VARIABLE=saltines '
                        '&& echo $SALTY_VARIABLE ; echo duh &> /dev/null')
        ret_key = 'test_|-shell_enabled_|-{0}_|-configurable_test_state'.format(disabled_ret)

        try:
            with salt.utils.fopen(state_file, 'w') as fp_:
                fp_.write(textwrap.dedent('''\
                {{% set shell_disabled = salt['cmd.run']("{0}") %}}

                shell_enabled:
                  test.configurable_test_state:
{'fraction_non_alphanumeric': 0.1175875036158519, 'fraction_numerical': 0.00202487706103558, 'mean_word_length': 2.3698830409356724, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '2958036', 'n_tokens_mistral': 2132, 'n_tokens_neox': 2053, 'n_words': 476}