Commit 79f41d9c by Michael DeHaan

This makes the module args parser more functional to eliminate side effects and…

This makes the module args parser more functional to eliminate side effects and eliminiates the 'return None' error path
to make sure we are handling more use cases.  Some paths are not yet complete, including most likely handling of the 'raw'
module.
parent 56b6cb53
...@@ -13,20 +13,17 @@ class TestModArgsDwim(unittest.TestCase): ...@@ -13,20 +13,17 @@ class TestModArgsDwim(unittest.TestCase):
self.m = ModuleArgsParser() self.m = ModuleArgsParser()
pass pass
def _debug(self, mod, args, to):
print "RETURNED module = %s" % mod
print " args = %s" % args
print " to = %s" % to
def tearDown(self): def tearDown(self):
pass pass
def test_action_to_shell(self):
mod, args, to = self.m.parse(dict(action='shell echo hi'))
assert mod == 'command'
assert args == dict(
_raw_params = 'echo hi',
_uses_shell = True,
)
assert to is None
def test_basic_shell(self): def test_basic_shell(self):
mod, args, to = self.m.parse(dict(shell='echo hi')) mod, args, to = self.m.parse(dict(shell='echo hi'))
self._debug(mod, args, to)
assert mod == 'command' assert mod == 'command'
assert args == dict( assert args == dict(
_raw_params = 'echo hi', _raw_params = 'echo hi',
...@@ -36,6 +33,7 @@ class TestModArgsDwim(unittest.TestCase): ...@@ -36,6 +33,7 @@ class TestModArgsDwim(unittest.TestCase):
def test_basic_command(self): def test_basic_command(self):
mod, args, to = self.m.parse(dict(command='echo hi')) mod, args, to = self.m.parse(dict(command='echo hi'))
self._debug(mod, args, to)
assert mod == 'command' assert mod == 'command'
assert args == dict( assert args == dict(
_raw_params = 'echo hi', _raw_params = 'echo hi',
...@@ -44,6 +42,7 @@ class TestModArgsDwim(unittest.TestCase): ...@@ -44,6 +42,7 @@ class TestModArgsDwim(unittest.TestCase):
def test_shell_with_modifiers(self): def test_shell_with_modifiers(self):
mod, args, to = self.m.parse(dict(shell='/bin/foo creates=/tmp/baz removes=/tmp/bleep')) mod, args, to = self.m.parse(dict(shell='/bin/foo creates=/tmp/baz removes=/tmp/bleep'))
self._debug(mod, args, to)
assert mod == 'command' assert mod == 'command'
assert args == dict( assert args == dict(
creates = '/tmp/baz', creates = '/tmp/baz',
...@@ -55,30 +54,35 @@ class TestModArgsDwim(unittest.TestCase): ...@@ -55,30 +54,35 @@ class TestModArgsDwim(unittest.TestCase):
def test_normal_usage(self): def test_normal_usage(self):
mod, args, to = self.m.parse(dict(copy='src=a dest=b')) mod, args, to = self.m.parse(dict(copy='src=a dest=b'))
self._debug(mod, args, to)
assert mod == 'copy' assert mod == 'copy'
assert args == dict(src='a', dest='b') assert args == dict(src='a', dest='b')
assert to is None assert to is None
def test_complex_args(self): def test_complex_args(self):
mod, args, to = self.m.parse(dict(copy=dict(src='a', dest='b'))) mod, args, to = self.m.parse(dict(copy=dict(src='a', dest='b')))
self._debug(mod, args, to)
assert mod == 'copy' assert mod == 'copy'
assert args == dict(src='a', dest='b') assert args == dict(src='a', dest='b')
assert to is None assert to is None
def test_action_with_complex(self): def test_action_with_complex(self):
mod, args, to = self.m.parse(dict(action=dict(module='copy', src='a', dest='b'))) mod, args, to = self.m.parse(dict(action=dict(module='copy', src='a', dest='b')))
self._debug(mod, args, to)
assert mod == 'copy' assert mod == 'copy'
assert args == dict(src='a', dest='b') assert args == dict(src='a', dest='b')
assert to is None assert to is None
def test_action_with_complex_and_complex_args(self): def test_action_with_complex_and_complex_args(self):
mod, args, to = self.m.parse(dict(action=dict(module='copy', args=dict(src='a', dest='b')))) mod, args, to = self.m.parse(dict(action=dict(module='copy', args=dict(src='a', dest='b'))))
self._debug(mod, args, to)
assert mod == 'copy' assert mod == 'copy'
assert args == dict(src='a', dest='b') assert args == dict(src='a', dest='b')
assert to is None assert to is None
def test_local_action_string(self): def test_local_action_string(self):
mod, args, to = self.m.parse(dict(local_action='copy src=a dest=b')) mod, args, to = self.m.parse(dict(local_action='copy src=a dest=b'))
self._debug(mod, args, to)
assert mod == 'copy' assert mod == 'copy'
assert args == dict(src='a', dest='b') assert args == dict(src='a', dest='b')
assert to is 'localhost' assert to is 'localhost'
...@@ -56,7 +56,7 @@ def parse_kv(args, check_raw=False): ...@@ -56,7 +56,7 @@ def parse_kv(args, check_raw=False):
# them to a special option for use later by the shell/command module # them to a special option for use later by the shell/command module
if len(raw_params) > 0: if len(raw_params) > 0:
options['_raw_params'] = ' '.join(raw_params) options['_raw_params'] = ' '.join(raw_params)
return options return options
def _get_quote_state(token, quote_char): def _get_quote_state(token, quote_char):
...@@ -239,4 +239,3 @@ def unquote(data): ...@@ -239,4 +239,3 @@ def unquote(data):
if is_quoted(data): if is_quoted(data):
return data[1:-1] return data[1:-1]
return data return data
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment