Esempio n. 1
0
 def test_get_kwarg_missing_default(self):
     """ Make sure we can get a default kwarg if it's not set """
     mock_kwarg_1 = Mock()
     mock_kwarg_1.arg = 'arg1'
     mock_kwarg_1.value = 'value1'
     # Grab arg2 and it works
     with patch('shellshock.converters.parse', side_effect=lambda x: x):
         with self.assertRaises(KeyError):
             get_kwarg([mock_kwarg_1], 'notreal')
Esempio n. 2
0
def envvar(call_args, call_kwargs, parseable=None, **kwargs):
    default = get_kwarg(call_kwargs, 'default', default=None)
    var_name = parse(call_args[0], raw=True)
    parseable._known_vars.add(var_name)
    if default is not None:
        return "{var}=${{{var}:-{default}}}".format(var=var_name,
                                                    default=default)
Esempio n. 3
0
    def test_get_kwarg(self):
        """ Make sure we can get a kwarg from a call args list """
        mock_kwarg_1 = Mock()
        mock_kwarg_1.arg = 'arg1'
        mock_kwarg_1.value = 'value1'
        mock_kwarg_2 = Mock()
        mock_kwarg_2.arg = 'arg2'
        mock_kwarg_2.value = 'value2'
        # Grab arg2 and it works
        with patch('shellshock.converters.parse', side_effect=lambda x: x):
            result = get_kwarg([mock_kwarg_1, mock_kwarg_2], 'arg2')
        self.assertEqual(result, 'value2')

        # Grab arg3 (nonexistent) and it works
        with patch('shellshock.converters.parse', side_effect=lambda x: x):
            result = get_kwarg([mock_kwarg_1, mock_kwarg_2], 'arg2')
        self.assertEqual(result, 'value2')
Esempio n. 4
0
 def test_get_kwarg_no_default(self):
     """ Make sure if no default is provided we get an exception """
     mock_kwarg_1 = Mock()
     mock_kwarg_1.arg = 'arg1'
     mock_kwarg_1.value = 'value1'
     # Grab arg2 and it works
     with patch('shellshock.converters.parse', side_effect=lambda x: x):
         result = get_kwarg([mock_kwarg_1], 'notreal', default='test')
     self.assertEqual(result, 'test')
Esempio n. 5
0
 def test_get_kwarg_default(self):
     """ Make sure we can get a default kwarg if it's not set """
     mock_kwarg_1 = Mock()
     mock_kwarg_1.arg = 'arg1'
     mock_kwarg_1.value = 'value1'
     # Grab arg2 and it works
     with patch('shellshock.converters.parse', side_effect=lambda x: x):
         result = get_kwarg([mock_kwarg_1], 'notreal', default='test')
     self.assertEqual(result, 'test')
Esempio n. 6
0
def input(call_args,
          call_kwargs,
          parseable=None,
          assign_to_var=None,
          **kwargs):
    if assign_to_var is not None:
        # See if we are assigning this to a new variable first
        target_var = assign_to_var
    else:
        # If we're not assigning, they better specify the 'target' kwarg
        target_var = get_kwarg(call_kwargs, 'target', raw=True)
    parseable._known_vars.add(target_var)

    return "read -p {prompt} {var} </dev/tty".format(
        prompt=parse(call_args[0]),
        var=target_var,
    )