Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions iniparse/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ def tidy(cfg: compat.RawConfigParser):
if isinstance(cfg, compat.RawConfigParser):
cfg = cfg.data
cont = cfg._data.contents
i = 1
# start at 0 so that a section at the very top of the file is tidied too
i = 0
while i < len(cont):
if isinstance(cont[i], LineContainer):
tidy_section(cont[i])
i += 1
elif (isinstance(cont[i-1], EmptyLine) and
elif (i > 0 and isinstance(cont[i-1], EmptyLine) and
isinstance(cont[i], EmptyLine)):
del cont[i]
else:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,26 @@ def test_compat(self):

c=3
"""))

def test_first_section_is_tidied(self):
"""
:id: dce50a4b-cb18-4bf7-bf7a-d0260cac1162
:title: tidy() cleans up a section at the start of the file
:description:
Verifies that tidy() collapses consecutive empty lines in the
first section even when the file starts directly with a
section header.
:tags: Tier 2
:steps:
1. Parse a file starting with `[a]` whose options are separated
by several empty lines, followed by a similar section `[b]`.
2. Call tidy() and render the configuration.
:expectedresults:
1. File is parsed.
2. Both sections contain only single empty lines.
"""
cfg = INIConfig(StringIO('[a]\nx = 1\n\n\n\ny = 2\n\n'
'[b]\nz = 3\n\n\n\nw = 4\n'))
tidy(cfg)
self.assertEqual(str(cfg), '[a]\nx = 1\n\ny = 2\n\n'
'[b]\nz = 3\n\nw = 4\n')
Loading