find_comments implementation and AugeasCommentNode creation

This commit is contained in:
Joona Hoikkala 2019-10-22 21:44:23 +03:00
parent 3f36298716
commit f249f97a76
No known key found for this signature in database
GPG key ID: D5AA86BBF9B29A5C
4 changed files with 49 additions and 7 deletions

View file

@ -152,12 +152,21 @@ class AugeasBlockNode(AugeasDirectiveNode):
return nodes
def find_comments(self, comment, exact=False): # pylint: disable=unused-argument
"""Recursive search of DirectiveNodes from the sequence of children"""
new_metadata = {"augeasparser": self.parser}
return [AugeasCommentNode(comment=assertions.PASS,
ancestor=self,
filepath=assertions.PASS,
metadata=new_metadata)]
"""
Recursive search of DirectiveNodes from the sequence of children.
Note that the argument exact is not implemented here, as it's not
currently in use in ApacheConfigurator.
"""
nodes = list()
ownpath = self.metadata.get("augeaspath")
comments = self.parser.find_comments(comment, start=ownpath)
for com in comments:
nodes.append(self._create_commentnode(com))
return nodes
def delete_child(self, child): # pragma: no cover
"""Deletes a ParserNode from the sequence of children"""
@ -167,6 +176,19 @@ class AugeasBlockNode(AugeasDirectiveNode):
"""Returns a list of unsaved filepaths"""
return [assertions.PASS]
def _create_commentnode(self, path):
"""Helper function to create a CommentNode from Augeas path"""
comment = self.parser.aug.get(path)
metadata = {"augeasparser": self.parser, "augeaspath": path}
# Because of the dynamic nature, and the fact that we're not populating
# the complete ParserNode tree, we use the search parent as ancestor
return AugeasCommentNode(comment=comment,
ancestor=self,
filepath=apache_util.get_file_path(path),
metadata=metadata)
def _create_directivenode(self, path):
"""Helper function to create a DirectiveNode from Augeas path"""

View file

@ -245,7 +245,7 @@ class DualBlockNode(DualNodeBase):
if pass_primary and pass_secondary:
# Both unimplemented
new_nodes.append(nodeclass(primary=primary_res[0],
secondary=secondary_res[0]))
secondary=secondary_res[0])) # pragma: no cover
elif pass_primary:
for c in secondary_res:
new_nodes.append(nodeclass(primary=primary_res[0],

View file

@ -63,3 +63,12 @@ class AugeasParserNodeTest(util.ApacheTest):
self.assertEqual(servername[0].parameters[0], "certbot.demo")
found = True
self.assertTrue(found)
def test_find_comments(self):
rootcomment = self.config.parser_root.find_comments(
"This is the main Apache server configuration file. "
)
self.assertEqual(len(rootcomment), 1)
self.assertTrue(rootcomment[0].filepath.endswith(
"debian_apache_2_4/multiple_vhosts/apache2/apache2.conf"
))

View file

@ -161,6 +161,17 @@ class DualParserNodeTest(unittest.TestCase): # pylint: disable=too-many-public-
self.block.primary)
def test_find_comments(self):
pri_comments = [augeasparser.AugeasCommentNode(comment="some comment",
ancestor=self.block,
filepath="/path/to/whatever")]
sec_comments = [augeasparser.AugeasCommentNode(comment=assertions.PASS,
ancestor=self.block,
filepath=assertions.PASS)]
find_coms_primary = mock.MagicMock(return_value=pri_comments)
find_coms_secondary = mock.MagicMock(return_value=sec_comments)
self.block.primary.find_comments = find_coms_primary
self.block.secondary.find_comments = find_coms_secondary
dcoms = self.block.find_comments("comment")
p_dcoms = [d.primary for d in dcoms]
s_dcoms = [d.secondary for d in dcoms]