1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Hook up the general parsing unittests and get them passing

This commit is contained in:
Toshio Kuratomi 2014-10-21 16:06:40 -04:00
parent 0a8c91a812
commit b3452de8a7
2 changed files with 37 additions and 39 deletions

View file

@ -23,23 +23,23 @@ import json
from yaml import YAMLError from yaml import YAMLError
from ansible.errors import AnsibleError, AnsibleInternalError from ansible.errors import AnsibleParserError, AnsibleInternalError
from ansible.parsing.vault import VaultLib from ansible.parsing.vault import VaultLib
from ansible.parsing.yaml import safe_load from ansible.parsing.yaml import safe_load
def load(data): def load(data):
if isinstance(data, file): if hasattr(data, 'read') and hasattr(data.read, '__call__'):
fd = open(f) data = data.read()
data = fd.read()
fd.close()
if isinstance(data, basestring): if isinstance(data, basestring):
try: try:
return json.loads(data) try:
return json.loads(data)
except:
return safe_load(data)
except: except:
return safe_load(data) raise AnsibleParserError("data was not valid yaml")
raise AnsibleInternalError("expected file or string, got %s" % type(data)) raise AnsibleInternalError("expected file or string, got %s" % type(data))

View file

@ -20,10 +20,11 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
from ansible.compat.tests import unittest from ansible.compat.tests import unittest
from ansible.errors import AnsibleParserError from ansible.errors import AnsibleInternalError, AnsibleParserError
from ansible.parsing import load from ansible.parsing import load
import json import json
import yaml
from io import FileIO from io import FileIO
@ -34,13 +35,14 @@ class MockFile(FileIO):
self.method = method self.method = method
def read(self): def read(self):
if method == 'json': if self.method == 'json':
return json.dumps(ds) return json.dumps(self.ds)
elif method == 'yaml': elif self.method == 'yaml':
return yaml.dumps(ds) return yaml.dump(self.ds)
elif method == 'fail': elif self.method == 'fail':
return """ return """
AAARGGGGH AAARGGGGH:
*****
THIS WON'T PARSE !!! THIS WON'T PARSE !!!
NOOOOOOOOOOOOOOOOOO NOOOOOOOOOOOOOOOOOO
""" """
@ -51,56 +53,52 @@ class MockFile(FileIO):
pass pass
class TestGeneralParsing(unittest.TestCase): class TestGeneralParsing(unittest.TestCase):
def __init__(self):
pass
def setUp(self): def setUp(self):
pass pass
def tearDown(self): def tearDown(self):
pass pass
def parse_json_from_string(self): def test_parse_json_from_string(self):
input = """ data = """
{ {
"asdf" : "1234", "asdf" : "1234",
"jkl" : 5678 "jkl" : 5678
} }
""" """
output = load_data(input) output = load(data)
self.assertEqual(output['asdf'], '1234') self.assertEqual(output['asdf'], '1234')
self.assertEqual(output['jkl'], 5678) self.assertEqual(output['jkl'], 5678)
def parse_json_from_file(self): def test_parse_json_from_file(self):
output = load_data(MockFile(dict(a=1,b=2,c=3)),'json') output = load(MockFile(dict(a=1,b=2,c=3), 'json'))
self.assertEqual(ouput, dict(a=1,b=2,c=3)) self.assertEqual(output, dict(a=1,b=2,c=3))
def parse_yaml_from_dict(self): def test_parse_yaml_from_dict(self):
input = """ data = """
asdf: '1234' asdf: '1234'
jkl: 5678 jkl: 5678
""" """
output = load_data(input) output = load(data)
self.assertEqual(output['asdf'], '1234') self.assertEqual(output['asdf'], '1234')
self.assertEqual(output['jkl'], 5678) self.assertEqual(output['jkl'], 5678)
def parse_yaml_from_file(self): def test_parse_yaml_from_file(self):
output = load_data(MockFile(dict(a=1,b=2,c=3),'yaml')) output = load(MockFile(dict(a=1,b=2,c=3),'yaml'))
self.assertEqual(output, dict(a=1,b=2,c=3)) self.assertEqual(output, dict(a=1,b=2,c=3))
def parse_fail(self): def test_parse_fail(self):
input = """ data = """
TEXT TEXT:
*** ***
NOT VALID NOT VALID
""" """
self.assertRaises(load_data(input), AnsibleParserError) self.assertRaises(AnsibleParserError, load, data)
def parse_fail_from_file(self): def test_parse_fail_from_file(self):
self.assertRaises(load_data(MockFile(None,'fail')), AnsibleParserError) self.assertRaises(AnsibleParserError, load, MockFile(None,'fail'))
def parse_fail_invalid_type(self): def test_parse_fail_invalid_type(self):
self.assertRaises(3000, AnsibleParsingError) self.assertRaises(AnsibleInternalError, load, 3000)
self.assertRaises(dict(a=1,b=2,c=3), AnsibleParserError) self.assertRaises(AnsibleInternalError, load, dict(a=1,b=2,c=3))