1
0
mirror of https://github.com/certbot/certbot.git synced 2026-01-26 07:41:33 +03:00

Implement add_child_directive (#7517)

This commit is contained in:
Joona Hoikkala
2019-11-13 00:19:02 +02:00
committed by ohemorange
parent 578ca1c6af
commit bdf24d2bed
2 changed files with 40 additions and 6 deletions

View File

@@ -221,12 +221,26 @@ class AugeasBlockNode(AugeasDirectiveNode):
# pylint: disable=unused-argument
def add_child_directive(self, name, parameters=None, position=None): # pragma: no cover
"""Adds a new DirectiveNode to the sequence of children"""
new_metadata = {"augeasparser": self.parser, "augeaspath": assertions.PASS}
new_dir = AugeasDirectiveNode(name=assertions.PASS,
ancestor=self,
filepath=assertions.PASS,
if not parameters:
raise errors.PluginError("Directive requires parameters and none were set.")
insertpath, realpath, before = self._aug_resolve_child_position(
"directive",
position
)
new_metadata = {"augeasparser": self.parser, "augeaspath": realpath}
# Create the new directive
self.parser.aug.insert(insertpath, "directive", before)
# Set the directive key
self.parser.aug.set(realpath, name)
new_dir = AugeasDirectiveNode(name=name,
parameters=parameters,
ancestor=assertions.PASS,
filepath=apache_util.get_file_path(realpath),
metadata=new_metadata)
self.children += (new_dir,)
return new_dir
def add_child_comment(self, comment="", position=None): # pylint: disable=unused-argument

View File

@@ -9,7 +9,7 @@ from certbot_apache import assertions
from certbot_apache.tests import util
class AugeasParserNodeTest(util.ApacheTest):
class AugeasParserNodeTest(util.ApacheTest): # pylint: disable=too-many-public-methods
"""Test AugeasParserNode using available test configurations"""
def setUp(self): # pylint: disable=arguments-differ
@@ -216,6 +216,7 @@ class AugeasParserNodeTest(util.ApacheTest):
AugeasBlockNode,
**parameters
)
def test_node_init_error_missing_augeaspath(self):
from certbot_apache.augeasparser import AugeasBlockNode
parameters = {
@@ -231,3 +232,22 @@ class AugeasParserNodeTest(util.ApacheTest):
AugeasBlockNode,
**parameters
)
def test_add_child_directive(self):
self.config.parser_root.primary.add_child_directive(
"ThisWasAdded",
["with", "parameters"],
position=0
)
dirs = self.config.parser_root.find_directives("ThisWasAdded")
self.assertEqual(len(dirs), 1)
self.assertEqual(dirs[0].parameters, ("with", "parameters"))
# The new directive was added to the very first line of the config
self.assertTrue(dirs[0].metadata["augeaspath"].endswith("[1]"))
def test_add_child_directive_exception(self):
self.assertRaises(
errors.PluginError,
self.config.parser_root.primary.add_child_directive,
"ThisRaisesErrorBecauseMissingParameters"
)