mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Teaching objects to load themselves, making the JSON/YAML parsing ambidexterous.
This commit is contained in:
parent
c75aeca435
commit
56b6cb5328
12 changed files with 180 additions and 59 deletions
|
@ -0,0 +1 @@
|
|||
|
85
test/v2/parsing/test_general.py
Normal file
85
test/v2/parsing/test_general.py
Normal file
|
@ -0,0 +1,85 @@
|
|||
# TODO: header
|
||||
|
||||
import unittest
|
||||
from ansible.parsing import load
|
||||
from ansible.errors import AnsibleParserError
|
||||
|
||||
import json
|
||||
|
||||
class MockFile(file):
|
||||
|
||||
def __init__(self, ds, method='json'):
|
||||
self.ds = ds
|
||||
self.method = method
|
||||
|
||||
def read(self):
|
||||
if method == 'json':
|
||||
return json.dumps(ds)
|
||||
elif method == 'yaml':
|
||||
return yaml.dumps(ds)
|
||||
elif method == 'fail':
|
||||
return """
|
||||
AAARGGGGH
|
||||
THIS WON'T PARSE !!!
|
||||
NOOOOOOOOOOOOOOOOOO
|
||||
"""
|
||||
else:
|
||||
raise Exception("untestable serializer")
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
class TestGeneralParsing(unittest.TestCase):
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def parse_json_from_string(self):
|
||||
input = """
|
||||
{
|
||||
"asdf" : "1234",
|
||||
"jkl" : 5678
|
||||
}
|
||||
"""
|
||||
output = load(input)
|
||||
assert output['asdf'] == '1234'
|
||||
assert output['jkl'] == 5678
|
||||
|
||||
def parse_json_from_file(self):
|
||||
output = load(MockFile(dict(a=1,b=2,c=3)),'json')
|
||||
assert ouput == dict(a=1,b=2,c=3)
|
||||
|
||||
def parse_yaml_from_dict(self):
|
||||
input = """
|
||||
asdf: '1234'
|
||||
jkl: 5678
|
||||
"""
|
||||
output = load(input)
|
||||
assert output['asdf'] == '1234'
|
||||
assert output['jkl'] == 5678
|
||||
|
||||
def parse_yaml_from_file(self):
|
||||
output = load(MockFile(dict(a=1,b=2,c=3),'yaml'))
|
||||
assert output == dict(a=1,b=2,c=3)
|
||||
|
||||
def parse_fail(self):
|
||||
input = """
|
||||
TEXT
|
||||
***
|
||||
NOT VALID
|
||||
"""
|
||||
self.failUnlessRaises(load(input), AnsibleParserError)
|
||||
|
||||
def parse_fail_from_file(self):
|
||||
self.failUnlessRaises(load(MockFile(None,'fail')), AnsibleParserError)
|
||||
|
||||
def parse_fail_invalid_type(self):
|
||||
self.failUnlessRaises(3000, AnsibleParsingError)
|
||||
self.failUnlessRaises(dict(a=1,b=2,c=3), AnsibleParserError)
|
||||
|
|
@ -5,10 +5,14 @@ import unittest
|
|||
|
||||
class TestModArgsDwim(unittest.TestCase):
|
||||
|
||||
# TODO: add tests that construct ModuleArgsParser with a task reference
|
||||
# TODO: verify the AnsibleError raised on failure knows the task
|
||||
# and the task knows the line numbers
|
||||
|
||||
def setUp(self):
|
||||
self.m = ModuleArgsParser()
|
||||
pass
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
|
@ -77,5 +81,4 @@ class TestModArgsDwim(unittest.TestCase):
|
|||
mod, args, to = self.m.parse(dict(local_action='copy src=a dest=b'))
|
||||
assert mod == 'copy'
|
||||
assert args == dict(src='a', dest='b')
|
||||
assert to is 'localhost'
|
||||
|
||||
assert to is 'localhost'
|
||||
|
|
|
@ -1,2 +1 @@
|
|||
# TODO: header
|
||||
|
||||
|
|
|
@ -16,13 +16,13 @@ class TestTask(unittest.TestCase):
|
|||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_construct_empty_task(self):
|
||||
t = Task()
|
||||
|
||||
|
||||
def test_construct_task_with_role(self):
|
||||
pass
|
||||
|
||||
|
@ -57,15 +57,13 @@ class TestTask(unittest.TestCase):
|
|||
pass
|
||||
|
||||
def test_can_load_module_complex_form(self):
|
||||
pass
|
||||
pass
|
||||
|
||||
def test_local_action_implies_delegate(self):
|
||||
pass
|
||||
|
||||
pass
|
||||
|
||||
def test_local_action_conflicts_with_delegate(self):
|
||||
pass
|
||||
pass
|
||||
|
||||
def test_delegate_to_parses(self):
|
||||
pass
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue