From: Adrian Müller Date: Wed, 21 Aug 2024 13:35:35 +0000 (+0200) Subject: Fix reading of nested cnfvars from string X-Git-Tag: v1.7.3~1^2~2 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=10de90684cfc2e59ddd87acbd88fc8ae6a10a802;p=pyi2ncommon Fix reading of nested cnfvars from string The function parse_cnf_children previously disregarded the children received from a recursive parse_cnf_children call if the state was None after the recursive function call. Hence, the leftover children would be omitted instead of attached to the corresponding parent cnfvar. This lead to the following configuration omitting the _IP and _NETMASK entries: 1 FIREWALL_NETGROUP,99: "QA host IP" 2 (1) FIREWALL_NETGROUP_NETWORK,0: "" 3 (2) FIREWALL_NETGROUP_NETWORK_IP,0: "192.168.1.254" 4 (2) FIREWALL_NETGROUP_NETWORK_NETMASK,0: "255.255.255.255" --- diff --git a/src/cnfvar/string.py b/src/cnfvar/string.py index 5a8ba24..b59058c 100644 --- a/src/cnfvar/string.py +++ b/src/cnfvar/string.py @@ -553,9 +553,9 @@ def parse_cnf_children(state, parent): # parent is further down in hierarchy -> new level (state, children, new_parent) = \ parse_cnf_children (state, new_parent) + cnf_line["children"] = children if state is None: break - cnf_line["children"] = children current = get(state) new_parent = get_parent(current) if new_parent is None: diff --git a/test/cnfvar/test_store.py b/test/cnfvar/test_store.py index 1b48732..0da57a8 100644 --- a/test/cnfvar/test_store.py +++ b/test/cnfvar/test_store.py @@ -84,6 +84,19 @@ class TestStore(unittest.TestCase): self.assertEqual(str(cnf_input).splitlines(), expected_input.splitlines()) + def test_correct_parsing_of_nested_cnf_from_string(self): + """Test that grandchildren cnfvars aren't disregarded when no other child cnfvar follows them.""" + input = dedent("""\ + 1 FIREWALL_NETGROUP,99: "QA host IP" + 2 (1) FIREWALL_NETGROUP_NETWORK,0: "" + 3 (2) FIREWALL_NETGROUP_NETWORK_IP,0: "192.168.1.254" + 4 (2) FIREWALL_NETGROUP_NETWORK_NETMASK,0: "255.255.255.255" + """) + + cnf = CnfList.from_cnf_string(input) + + self.assertEqual(str(cnf).splitlines(), input.splitlines()) + def test_deleting_cnfvars(self): """Test that we can delete top-level cnfvars.""" # To avoid creating long dummy objects, we can convert our test file