Go back to hasattr and add a test.

This commit is contained in:
Jacob Hoffman-Andrews 2015-10-11 12:19:39 -07:00
parent 06c85d6b5a
commit f16489f762
2 changed files with 13 additions and 3 deletions

View file

@ -131,12 +131,14 @@ class NginxParserTest(util.NginxTest):
ssl_re = re.compile(r'\n\s+ssl_certificate /etc/ssl/cert.pem')
dump = nginxparser.dumps(nparser.parsed[nparser.abs_path('nginx.conf')])
self.assertEqual(1, len(re.findall(ssl_re, dump)))
nparser.add_server_directives(nparser.abs_path('server.conf'),
server_conf = nparser.abs_path('server.conf')
nparser.add_server_directives(server_conf,
set(['alias', 'another.alias',
'somename']),
[['foo', 'bar'], ['ssl_certificate',
'/etc/ssl/cert2.pem']])
self.assertEqual(nparser.parsed[nparser.abs_path('server.conf')],
self.assertEqual(nparser.parsed[server_conf],
[['ssl_certificate', '/etc/ssl/cert2.pem'],
['foo', 'bar'],
['server_name', 'somename alias another.alias']])
@ -152,6 +154,12 @@ class NginxParserTest(util.NginxTest):
self.assertTrue(util.contains_at_depth(root, ['http'], 1))
self.assertTrue(util.contains_at_depth(root, block, 2))
# Check that our server block got inserted first among all server
# blocks.
http_block = filter(lambda x: x[0] == ['http'], root)[0][1]
server_blocks = filter(lambda x: x[0] == ['server'], http_block)
self.assertEqual(server_blocks[0], block)
def test_replace_server_directives(self):
nparser = parser.NginxParser(self.config_path, self.ssl_options)
target = set(['.example.com', 'example.*'])

View file

@ -93,7 +93,9 @@ def contains_at_depth(haystack, needle, n):
Return true if the needle is present in one of the sub-iterables in haystack
at depth n. Haystack must be an iterable.
"""
if not isinstance(haystack, collections.Iterable):
# Specifically use hasattr rather than isinstance(..., collections.Iterable)
# because we want to include lists but reject strings.
if not hasattr(haystack, '__iter__'):
return False
if n == 0:
return needle in haystack