Fix with_ini example and unittest

* Fix example in ini.py
* Fix unittest in test_ini.py to pass CI as latest ansible returns list in
  different order. To prevent such issues in future results are sorted
* PEP8 E501 styling improvements

Co-Authored-By: Sergii Golovatiuk <sgolovat@redhat.com>
pull/49262/head
Yannig Perré 7 years ago committed by Toshio Kuratomi
parent 1e4ab5a038
commit 6b41588e93

@ -49,10 +49,7 @@ EXAMPLES = """
- debug: - debug:
msg: "{{ item }}" msg: "{{ item }}"
with_ini: with_ini:
- value[1-2] - '.* section=section1 file=test.ini re=True'
- section: section1
- file: "lookup.ini"
- re: true
""" """
RETURN = """ RETURN = """
@ -130,13 +127,15 @@ class LookupModule(LookupBase):
for param in params[1:]: for param in params[1:]:
name, value = param.split('=') name, value = param.split('=')
if name not in paramvals: if name not in paramvals:
raise AnsibleAssertionError('%s not in paramvals' % name) raise AnsibleAssertionError('%s not in paramvals' %
name)
paramvals[name] = value paramvals[name] = value
except (ValueError, AssertionError) as e: except (ValueError, AssertionError) as e:
raise AnsibleError(e) raise AnsibleError(e)
# Retrieve file path # Retrieve file path
path = self.find_file_in_search_path(variables, 'files', paramvals['file']) path = self.find_file_in_search_path(variables, 'files',
paramvals['file'])
# Create StringIO later used to parse ini # Create StringIO later used to parse ini
config = StringIO() config = StringIO()
@ -147,12 +146,14 @@ class LookupModule(LookupBase):
# Open file using encoding # Open file using encoding
contents, show_data = self._loader._get_file_contents(path) contents, show_data = self._loader._get_file_contents(path)
contents = to_text(contents, errors='surrogate_or_strict', encoding=paramvals['encoding']) contents = to_text(contents, errors='surrogate_or_strict',
encoding=paramvals['encoding'])
config.write(contents) config.write(contents)
config.seek(0, os.SEEK_SET) config.seek(0, os.SEEK_SET)
self.cp.readfp(config) self.cp.readfp(config)
var = self.get_value(key, paramvals['section'], paramvals['default'], paramvals['re']) var = self.get_value(key, paramvals['section'],
paramvals['default'], paramvals['re'])
if var is not None: if var is not None:
if isinstance(var, MutableSequence): if isinstance(var, MutableSequence):
for v in var: for v in var:

@ -31,27 +31,27 @@ class TestINILookup(unittest.TestCase):
# Simple case # Simple case
dict( dict(
term=u'keyA section=sectionA file=/path/to/file', term=u'keyA section=sectionA file=/path/to/file',
expected=[u'keyA', u'section=sectionA', u'file=/path/to/file'], expected=[u'file=/path/to/file', u'keyA', u'section=sectionA'],
), ),
dict( dict(
term=u'keyB section=sectionB with space file=/path/with/embedded spaces and/file', term=u'keyB section=sectionB with space file=/path/with/embedded spaces and/file',
expected=[u'keyB', u'section=sectionB with space', u'file=/path/with/embedded spaces and/file'], expected=[u'file=/path/with/embedded spaces and/file', u'keyB', u'section=sectionB with space'],
), ),
dict( dict(
term=u'keyC section=sectionC file=/path/with/equals/cn=com.ansible', term=u'keyC section=sectionC file=/path/with/equals/cn=com.ansible',
expected=[u'keyC', u'section=sectionC', u'file=/path/with/equals/cn=com.ansible'], expected=[u'file=/path/with/equals/cn=com.ansible', u'keyC', u'section=sectionC'],
), ),
dict( dict(
term=u'keyD section=sectionD file=/path/with space and/equals/cn=com.ansible', term=u'keyD section=sectionD file=/path/with space and/equals/cn=com.ansible',
expected=[u'keyD', u'section=sectionD', u'file=/path/with space and/equals/cn=com.ansible'], expected=[u'file=/path/with space and/equals/cn=com.ansible', u'keyD', u'section=sectionD'],
), ),
dict( dict(
term=u'keyE section=sectionE file=/path/with/unicode/くらとみ/file', term=u'keyE section=sectionE file=/path/with/unicode/くらとみ/file',
expected=[u'keyE', u'section=sectionE', u'file=/path/with/unicode/くらとみ/file'], expected=[u'file=/path/with/unicode/くらとみ/file', u'keyE', u'section=sectionE'],
), ),
dict( dict(
term=u'keyF section=sectionF file=/path/with/utf 8 and spaces/くらとみ/file', term=u'keyF section=sectionF file=/path/with/utf 8 and spaces/くらとみ/file',
expected=[u'keyF', u'section=sectionF', u'file=/path/with/utf 8 and spaces/くらとみ/file'], expected=[u'file=/path/with/utf 8 and spaces/くらとみ/file', u'keyF', u'section=sectionF'],
), ),
) )
@ -65,4 +65,5 @@ class TestINILookup(unittest.TestCase):
for testcase in self.old_style_params_data: for testcase in self.old_style_params_data:
# print(testcase) # print(testcase)
params = _parse_params(testcase['term']) params = _parse_params(testcase['term'])
params.sort()
self.assertEqual(params, testcase['expected']) self.assertEqual(params, testcase['expected'])

Loading…
Cancel
Save