Skip to content

Commit d7230f5

Browse files
Leading spaces (#657)
* Catching leading spaces at config start * spacing tests * after black * remoing wrong variable assigment * adding parametrize * reforming * black formatting * Update tests/unit/test_parser.py Co-authored-by: Ken Celenza <[email protected]> * disable=redefined-outer-name * fromatting... --------- Co-authored-by: Ken Celenza <[email protected]>
1 parent 91b7ab9 commit d7230f5

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

netutils/config/parser.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,11 @@ def build_config_relationship(self) -> t.List[ConfigLine]:
312312
... ]
313313
True
314314
"""
315-
for line in self.generator_config:
316-
if not line[0].isspace():
315+
for index, line in enumerate(self.generator_config):
316+
current_spaces = self.get_leading_space_count(line) if line[0].isspace() else 0
317+
if index == 0 and line[0].isspace():
318+
self._current_parents = self._remove_parents(line, current_spaces)
319+
elif not line[0].isspace():
317320
self._current_parents = ()
318321
if self.is_banner_start(line):
319322
line = self._build_banner(line) # type: ignore
@@ -988,8 +991,11 @@ def build_config_relationship(self) -> t.List[ConfigLine]:
988991
... ]
989992
True
990993
"""
991-
for line in self.generator_config:
992-
if not line[0].isspace():
994+
for index, line in enumerate(self.generator_config):
995+
current_spaces = self.get_leading_space_count(line) if line[0].isspace() else 0
996+
if index == 0 and line[0].isspace():
997+
self._current_parents = self._remove_parents(line, current_spaces)
998+
elif not line[0].isspace():
993999
self._current_parents = ()
9941000
else:
9951001
previous_config = self.config_lines[-1]
@@ -1386,7 +1392,11 @@ def build_config_relationship(self) -> t.List[ConfigLine]:
13861392
... ]
13871393
True
13881394
"""
1389-
for line in self.generator_config:
1395+
for index, line in enumerate(self.generator_config):
1396+
current_spaces = self.get_leading_space_count(line) if line[0].isspace() else 0
1397+
1398+
if index == 0 and line[0].isspace():
1399+
self._current_parents = self._remove_parents(line, current_spaces)
13901400
if not line[0].isspace():
13911401
self._current_parents = ()
13921402
if self.is_banner_start(line):

tests/unit/test_parser.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,20 @@ def test_duplicate_line():
7878
)
7979
with pytest.raises(IndexError, match=r".*This error is likely from a duplicate line detected.*"):
8080
compliance.parser_map["cisco_ios"](logging).config_lines # pylint: disable=expression-not-assigned
81+
82+
83+
@pytest.mark.parametrize("network_os", ["cisco_ios", "arista_eos", "cisco_iosxr"])
84+
def test_leading_spaces_config_start(network_os): # pylint: disable=redefined-outer-name
85+
logging = (
86+
"! Command: show running-config\n"
87+
" 24.1.4\n"
88+
"!\n"
89+
"no aaa root\n"
90+
"!\n"
91+
"management api http-commands\n"
92+
" no shutdown\n"
93+
"no service interface inactive port-id allocation disabled\n"
94+
"transceiver qsfp default-mode 4x10G\n"
95+
)
96+
with pytest.raises(IndexError, match=r".*Validate the first line does not begin with a space.*"):
97+
compliance.parser_map[network_os](logging).config_lines # pylint: disable=expression-not-assigned

0 commit comments

Comments
 (0)