Skip to content

Commit

Permalink
Provide more descriptive error message when there is a duplicate line…
Browse files Browse the repository at this point in the history
… to be parsed (#539)
  • Loading branch information
itdependsnetworks authored Jun 27, 2024
1 parent 11f5e88 commit be99b5a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion netutils/config/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,14 @@ def _update_same_line_children_configs(self) -> None:
new_config_lines: t.List[ConfigLine] = []
for line in self.config_lines:
if line in self.same_line_children:
previous_line = new_config_lines[-1]
try:
previous_line = new_config_lines[-1]
except IndexError as error:
raise IndexError(
f"This error is likely from a duplicate line detected at the line `{line.config_line}`, "
"see https://netutils.readthedocs.io/en/latest/dev/dev_config/#duplicate-line-detection "
f"for more details.\nOriginal Error: {error}"
)
previous_config_line = previous_line.config_line
current_parents = previous_line.parents + (previous_config_line,)
line = ConfigLine(line.config_line, current_parents)
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,14 @@ def test_incorrect_banner_ios():
)
with pytest.raises(ValueError):
compliance.parser_map["cisco_ios"](banner_cfg).config_lines # pylint: disable=expression-not-assigned


def test_duplicate_line():
logging = (
"!\n"
"snmp-server community <<REPLACED>> RO SNMP_ACL_RO\n"
"snmp-server community <<REPLACED>> RO SNMP_ACL_RO\n"
"snmp-server community <<REPLACED>> RW SNMP_ACL_RW\n"
)
with pytest.raises(IndexError, match=r".*This error is likely from a duplicate line detected.*"):
compliance.parser_map["cisco_ios"](logging).config_lines # pylint: disable=expression-not-assigned

0 comments on commit be99b5a

Please sign in to comment.