mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-24 05:40:23 -07:00
test/: PEP8 compliancy (#24803)
* test/: PEP8 compliancy - Make PEP8 compliant * Python3 chokes on casting int to bytes (#24952) But if we tell the formatter that the var is a number, it works
This commit is contained in:
parent
31c59ad5f9
commit
4efec414e7
110 changed files with 1702 additions and 1547 deletions
|
@ -24,9 +24,9 @@ from collections import defaultdict
|
|||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import patch, MagicMock
|
||||
|
||||
from ansible.template.safe_eval import safe_eval
|
||||
|
||||
|
||||
class TestSafeEval(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
|
|
@ -99,8 +99,8 @@ class SomeUnsafeClass(AnsibleUnsafe):
|
|||
class TestTemplarTemplate(BaseTemplar, unittest.TestCase):
|
||||
def test_lookup_jinja_dict_key_in_static_vars(self):
|
||||
res = self.templar.template("{'some_static_var': '{{ some_var }}'}",
|
||||
static_vars=['some_static_var'])
|
||||
#self.assertEqual(res['{{ a_keyword }}'], "blip")
|
||||
static_vars=['some_static_var'])
|
||||
# self.assertEqual(res['{{ a_keyword }}'], "blip")
|
||||
print(res)
|
||||
|
||||
def test_templatable(self):
|
||||
|
@ -128,7 +128,7 @@ class TestTemplarTemplate(BaseTemplar, unittest.TestCase):
|
|||
def test_template_convert_bare_unsafe(self):
|
||||
res = self.templar.template('some_unsafe_var', convert_bare=True, bare_deprecated=False)
|
||||
self.assertEqual(res, 'unsafe_blip')
|
||||
#self.assertIsInstance(res, AnsibleUnsafe)
|
||||
# self.assertIsInstance(res, AnsibleUnsafe)
|
||||
self.assertTrue(self.is_unsafe(res), 'returned value from template.template (%s) is not marked unsafe' % res)
|
||||
|
||||
def test_template_convert_bare_filter(self):
|
||||
|
@ -138,7 +138,7 @@ class TestTemplarTemplate(BaseTemplar, unittest.TestCase):
|
|||
def test_template_convert_bare_filter_unsafe(self):
|
||||
res = self.templar.template('some_unsafe_var|capitalize', convert_bare=True, bare_deprecated=False)
|
||||
self.assertEqual(res, 'Unsafe_blip')
|
||||
#self.assertIsInstance(res, AnsibleUnsafe)
|
||||
# self.assertIsInstance(res, AnsibleUnsafe)
|
||||
self.assertTrue(self.is_unsafe(res), 'returned value from template.template (%s) is not marked unsafe' % res)
|
||||
|
||||
def test_template_convert_data(self):
|
||||
|
@ -325,7 +325,7 @@ class TestTemplarLookup(BaseTemplar, unittest.TestCase):
|
|||
def test_lookup_jinja_defined(self):
|
||||
res = self.templar._lookup('list', '{{ some_var }}')
|
||||
self.assertTrue(self.is_unsafe(res))
|
||||
#self.assertIsInstance(res, AnsibleUnsafe)
|
||||
# self.assertIsInstance(res, AnsibleUnsafe)
|
||||
|
||||
def test_lookup_jinja_dict_string_passed(self):
|
||||
self.assertRaisesRegexp(AnsibleError,
|
||||
|
@ -344,7 +344,7 @@ class TestTemplarLookup(BaseTemplar, unittest.TestCase):
|
|||
def test_lookup_jinja_kwargs(self):
|
||||
res = self.templar._lookup('list', 'blip', random_keyword='12345')
|
||||
self.assertTrue(self.is_unsafe(res))
|
||||
#self.assertIsInstance(res, AnsibleUnsafe)
|
||||
# self.assertIsInstance(res, AnsibleUnsafe)
|
||||
|
||||
def test_lookup_jinja_list_wantlist(self):
|
||||
res = self.templar._lookup('list', '{{ some_var }}', wantlist=True)
|
||||
|
@ -362,7 +362,7 @@ class TestTemplarLookup(BaseTemplar, unittest.TestCase):
|
|||
res = self.templar._lookup('list', '{{ some_unsafe_var }}', wantlist=True)
|
||||
for lookup_result in res:
|
||||
self.assertTrue(self.is_unsafe(lookup_result))
|
||||
#self.assertIsInstance(lookup_result, AnsibleUnsafe)
|
||||
# self.assertIsInstance(lookup_result, AnsibleUnsafe)
|
||||
|
||||
# Should this be an AnsibleUnsafe
|
||||
# self.assertIsInstance(res, AnsibleUnsafe)
|
||||
|
@ -371,22 +371,22 @@ class TestTemplarLookup(BaseTemplar, unittest.TestCase):
|
|||
res = self.templar._lookup('list', {'{{ a_keyword }}': '{{ some_var }}'})
|
||||
self.assertEqual(res['{{ a_keyword }}'], "blip")
|
||||
# TODO: Should this be an AnsibleUnsafe
|
||||
#self.assertIsInstance(res['{{ a_keyword }}'], AnsibleUnsafe)
|
||||
#self.assertIsInstance(res, AnsibleUnsafe)
|
||||
# self.assertIsInstance(res['{{ a_keyword }}'], AnsibleUnsafe)
|
||||
# self.assertIsInstance(res, AnsibleUnsafe)
|
||||
|
||||
def test_lookup_jinja_dict_unsafe(self):
|
||||
res = self.templar._lookup('list', {'{{ some_unsafe_key }}': '{{ some_unsafe_var }}'})
|
||||
self.assertTrue(self.is_unsafe(res['{{ some_unsafe_key }}']))
|
||||
#self.assertIsInstance(res['{{ some_unsafe_key }}'], AnsibleUnsafe)
|
||||
# self.assertIsInstance(res['{{ some_unsafe_key }}'], AnsibleUnsafe)
|
||||
# TODO: Should this be an AnsibleUnsafe
|
||||
#self.assertIsInstance(res, AnsibleUnsafe)
|
||||
# self.assertIsInstance(res, AnsibleUnsafe)
|
||||
|
||||
def test_lookup_jinja_dict_unsafe_value(self):
|
||||
res = self.templar._lookup('list', {'{{ a_keyword }}': '{{ some_unsafe_var }}'})
|
||||
self.assertTrue(self.is_unsafe(res['{{ a_keyword }}']))
|
||||
#self.assertIsInstance(res['{{ a_keyword }}'], AnsibleUnsafe)
|
||||
# self.assertIsInstance(res['{{ a_keyword }}'], AnsibleUnsafe)
|
||||
# TODO: Should this be an AnsibleUnsafe
|
||||
#self.assertIsInstance(res, AnsibleUnsafe)
|
||||
# self.assertIsInstance(res, AnsibleUnsafe)
|
||||
|
||||
def test_lookup_jinja_none(self):
|
||||
res = self.templar._lookup('list', None)
|
||||
|
@ -414,14 +414,14 @@ class TestAnsibleContext(BaseTemplar, unittest.TestCase):
|
|||
def test_resolve_unsafe(self):
|
||||
context = self._context(variables={'some_unsafe_key': wrap_var('some_unsafe_string')})
|
||||
res = context.resolve('some_unsafe_key')
|
||||
#self.assertIsInstance(res, AnsibleUnsafe)
|
||||
# self.assertIsInstance(res, AnsibleUnsafe)
|
||||
self.assertTrue(self.is_unsafe(res),
|
||||
'return of AnsibleContext.resolve (%s) was expected to be marked unsafe but was not' % res)
|
||||
|
||||
def test_resolve_unsafe_list(self):
|
||||
context = self._context(variables={'some_unsafe_key': [wrap_var('some unsafe string 1')]})
|
||||
res = context.resolve('some_unsafe_key')
|
||||
#self.assertIsInstance(res[0], AnsibleUnsafe)
|
||||
# self.assertIsInstance(res[0], AnsibleUnsafe)
|
||||
self.assertTrue(self.is_unsafe(res),
|
||||
'return of AnsibleContext.resolve (%s) was expected to be marked unsafe but was not' % res)
|
||||
|
||||
|
@ -437,7 +437,7 @@ class TestAnsibleContext(BaseTemplar, unittest.TestCase):
|
|||
context = self._context(variables={'some_key': 'some_string'})
|
||||
res = context.resolve('some_key')
|
||||
self.assertEqual(res, 'some_string')
|
||||
#self.assertNotIsInstance(res, AnsibleUnsafe)
|
||||
# self.assertNotIsInstance(res, AnsibleUnsafe)
|
||||
self.assertFalse(self.is_unsafe(res),
|
||||
'return of AnsibleContext.resolve (%s) was not expected to be marked unsafe but was' % res)
|
||||
|
||||
|
@ -445,6 +445,6 @@ class TestAnsibleContext(BaseTemplar, unittest.TestCase):
|
|||
context = self._context(variables={'some_key': None})
|
||||
res = context.resolve('some_key')
|
||||
self.assertEqual(res, None)
|
||||
#self.assertNotIsInstance(res, AnsibleUnsafe)
|
||||
# self.assertNotIsInstance(res, AnsibleUnsafe)
|
||||
self.assertFalse(self.is_unsafe(res),
|
||||
'return of AnsibleContext.resolve (%s) was not expected to be marked unsafe but was' % res)
|
||||
|
|
|
@ -27,6 +27,7 @@ from ansible.template import _escape_backslashes, _count_newlines_from_end
|
|||
# These are internal utility functions only needed for templating. They're
|
||||
# algorithmic so good candidates for unittesting by themselves
|
||||
|
||||
|
||||
class TestBackslashEscape(unittest.TestCase):
|
||||
|
||||
test_data = (
|
||||
|
@ -36,7 +37,7 @@ class TestBackslashEscape(unittest.TestCase):
|
|||
intermediate=u"{{ 'test2 %s' | format('\\\\1') }}",
|
||||
expectation=u"test2 \\1",
|
||||
args=dict()
|
||||
),
|
||||
),
|
||||
# Test backslashes inside the jinja2 var itself are double
|
||||
# escaped
|
||||
dict(
|
||||
|
@ -44,7 +45,7 @@ class TestBackslashEscape(unittest.TestCase):
|
|||
intermediate=u"Test 2\\3: {{ '\\\\1 %s' | format('\\\\2') }}",
|
||||
expectation=u"Test 2\\3: \\1 \\2",
|
||||
args=dict()
|
||||
),
|
||||
),
|
||||
# Test backslashes outside of the jinja2 var are not double
|
||||
# escaped
|
||||
dict(
|
||||
|
@ -52,14 +53,14 @@ class TestBackslashEscape(unittest.TestCase):
|
|||
intermediate=u"Test 2\\3: {{ 'test2 %s' | format('\\\\1') }}; \\done",
|
||||
expectation=u"Test 2\\3: test2 \\1; \\done",
|
||||
args=dict()
|
||||
),
|
||||
),
|
||||
# Test backslashes in a variable sent to a filter are handled
|
||||
dict(
|
||||
template=u"{{ 'test2 %s' | format(var1) }}",
|
||||
intermediate=u"{{ 'test2 %s' | format(var1) }}",
|
||||
expectation=u"test2 \\1",
|
||||
args=dict(var1=u'\\1')
|
||||
),
|
||||
),
|
||||
# Test backslashes in a variable expanded by jinja2 are double
|
||||
# escaped
|
||||
dict(
|
||||
|
@ -67,8 +68,9 @@ class TestBackslashEscape(unittest.TestCase):
|
|||
intermediate=u"Test 2\\3: {{ var1 | format('\\\\2') }}",
|
||||
expectation=u"Test 2\\3: \\1 \\2",
|
||||
args=dict(var1=u'\\1 %s')
|
||||
),
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
self.env = jinja2.Environment()
|
||||
|
||||
|
@ -84,6 +86,7 @@ class TestBackslashEscape(unittest.TestCase):
|
|||
args = test['args']
|
||||
self.assertEquals(template.render(**args), test['expectation'])
|
||||
|
||||
|
||||
class TestCountNewlines(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
|
|
@ -18,4 +18,3 @@
|
|||
# Make coding more python3-ish
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue