Skip to content

Commit 1736d97

Browse files
fix: check before truncating new line (#107)
**Fix:** updated the logic to remove duplicate newlines only when there is content (default stanzas and others) as they are adding two new lines. **Test:** added test cases for all the scenarios based on the `write()` function.
1 parent 9762c65 commit 1736d97

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

addonfactory_splunk_conf_parser_lib.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ def _read(self, fp, fpname):
155155
if isinstance(val, list):
156156
options[name] = "\n".join(val)
157157

158-
# As the type of fp is not defined in RawConfigParser which is the parent class so we have to go with Any.
158+
# As the type of `fp` is not defined in RawConfigParser (parent class)
159+
# we define the type of `fp` as Any
159160
def write(self, fp: Any, *args) -> None:
160161
"""
161162
Override the write() method to write comments
@@ -193,9 +194,12 @@ def write(self, fp: Any, *args) -> None:
193194
# write the separator line for stanza
194195
fp.write("\n")
195196

196-
# remove the trailing lines in a file, as the content should be written as-is
197-
fp.seek(fp.tell() - 1, SEEK_SET)
198-
fp.truncate()
197+
# multiple lines are added when there are stanzas/sections and its properties,
198+
# hence we check if there are stanzas/sections before truncating the trailing newline
199+
if self._defaults or self._sections:
200+
# remove the trailing lines in a file, as the content should be written as-is
201+
fp.seek(fp.tell() - 1, SEEK_SET)
202+
fp.truncate()
199203

200204
def optionxform(self, optionstr):
201205
return optionstr

test_addonfactory_splunk_conf_parser_lib.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,54 @@ def test_write(self):
7878
7979
[eventtype=some_eventtype]
8080
tag = enabled
81+
"""
82+
parser = conf_parser.TABConfigParser()
83+
parser.read_string(conf)
84+
output = io.StringIO()
85+
parser.write(output)
86+
self.assertEqual(conf, output.getvalue())
87+
88+
def test_write_empty(self):
89+
conf = ""
90+
parser = conf_parser.TABConfigParser()
91+
parser.read_string(conf)
92+
output = io.StringIO()
93+
parser.write(output)
94+
self.assertEqual(conf, output.getvalue())
95+
96+
def test_write_only_comments(self):
97+
conf = """
98+
#
99+
# some comment here
100+
#
101+
"""
102+
parser = conf_parser.TABConfigParser()
103+
parser.read_string(conf)
104+
output = io.StringIO()
105+
parser.write(output)
106+
self.assertEqual(conf, output.getvalue())
107+
108+
def test_write_only_default_section(self):
109+
conf = """
110+
#
111+
# some comment here
112+
#
113+
[DEFAULT]
114+
attrib_one = value_one
115+
attrib_two = value_two
116+
"""
117+
parser = conf_parser.TABConfigParser()
118+
parser.read_string(conf)
119+
output = io.StringIO()
120+
parser.write(output)
121+
self.assertEqual(conf, output.getvalue())
122+
123+
def test_write_fields_outside(self):
124+
conf = """
125+
#
126+
#
127+
out_field = out_value
128+
out_field_two = out_value_two
81129
"""
82130
parser = conf_parser.TABConfigParser()
83131
parser.read_string(conf)

0 commit comments

Comments
 (0)