Implement add_child_comment (#7518)

This commit is contained in:
Joona Hoikkala
2019-11-18 21:21:51 +02:00
committed by GitHub
parent b70f9c4744
commit ac1a60ff0b
3 changed files with 32 additions and 15 deletions
+16 -6
View File
@@ -243,14 +243,24 @@ class AugeasBlockNode(AugeasDirectiveNode):
metadata=new_metadata)
return new_dir
def add_child_comment(self, comment="", position=None): # pylint: disable=unused-argument
def add_child_comment(self, comment="", position=None):
"""Adds a new CommentNode to the sequence of children"""
new_metadata = {"augeasparser": self.parser, "augeaspath": assertions.PASS}
new_comment = AugeasCommentNode(comment=assertions.PASS,
ancestor=self,
filepath=assertions.PASS,
insertpath, realpath, before = self._aug_resolve_child_position(
"#comment",
position
)
new_metadata = {"augeasparser": self.parser, "augeaspath": realpath}
# Create the new comment
self.parser.aug.insert(insertpath, "#comment", before)
# Set the comment content
self.parser.aug.set(realpath, comment)
new_comment = AugeasCommentNode(comment=comment,
ancestor=assertions.PASS,
filepath=apache_util.get_file_path(realpath),
metadata=new_metadata)
self.children += (new_comment,)
return new_comment
def find_blocks(self, name, exclude=True): # pylint: disable=unused-argument
@@ -144,6 +144,16 @@ class AugeasParserNodeTest(util.ApacheTest): # pylint: disable=too-many-public-
found = True
self.assertTrue(found)
def test_add_child_comment(self):
newc = self.config.parser_root.primary.add_child_comment("The content")
comments = self.config.parser_root.find_comments("The content")
self.assertEqual(len(comments), 1)
self.assertEqual(
newc.metadata["augeaspath"],
comments[0].primary.metadata["augeaspath"]
)
self.assertEqual(newc.comment, comments[0].comment)
def test_delete_child(self):
listens = self.config.parser_root.primary.find_directives("Listen")
self.assertEqual(len(listens), 1)
@@ -6,7 +6,6 @@ import mock
from certbot_apache import assertions
from certbot_apache import augeasparser
from certbot_apache import dualparser
from certbot_apache import interfaces
class DualParserNodeTest(unittest.TestCase): # pylint: disable=too-many-public-methods
@@ -151,15 +150,13 @@ class DualParserNodeTest(unittest.TestCase): # pylint: disable=too-many-public-
self.assertTrue(mock_second.called)
def test_add_child_comment(self):
self.assertEqual(len(self.block.primary.children), 0)
self.assertEqual(len(self.block.secondary.children), 0)
mock_first = mock.MagicMock(return_value=self.comment.primary)
mock_second = mock.MagicMock(return_value=self.comment.secondary)
self.block.primary.add_child_comment = mock_first
self.block.secondary.add_child_comment = mock_second
self.block.add_child_comment("Comment")
self.assertEqual(len(self.block.primary.children), 1)
self.assertEqual(len(self.block.secondary.children), 1)
self.assertTrue(isinstance(self.block.primary.children[0],
interfaces.CommentNode))
self.assertEqual(self.block.primary.children[0].ancestor,
self.block.primary)
self.assertTrue(mock_first.called)
self.assertTrue(mock_second.called)
def test_find_comments(self):
pri_comments = [augeasparser.AugeasCommentNode(comment="some comment",