Is this handled the same on Linux and Windows

This commit is contained in:
twangboy 2017-09-05 16:32:43 -06:00
parent 21ab306ef4
commit 85997391f1
2 changed files with 64 additions and 57 deletions

View file

@ -318,17 +318,18 @@ class _Section(OrderedDict):
yield '{0}[{1}]{0}'.format(os.linesep, self.name)
sections_dict = OrderedDict()
for name, value in six.iteritems(self):
# Handle Comment Lines
if com_regx.match(name):
yield '{0}{1}'.format(value, os.linesep)
# Handle Sections
elif isinstance(value, _Section):
sections_dict.update({name: value})
# Key / Value pairs
# Adds spaces between the separator
else:
yield '{0}{1}{2}{3}'.format(
name,
(
' {0} '.format(self.sep) if self.sep != ' '
else self.sep
),
' {0} '.format(self.sep) if self.sep != ' ' else self.sep,
value,
os.linesep
)

View file

@ -15,38 +15,42 @@ import salt.modules.ini_manage as ini
class IniManageTestCase(TestCase):
TEST_FILE_CONTENT = '''\
# Comment on the first line
# First main option
option1=main1
# Second main option
option2=main2
[main]
# Another comment
test1=value 1
test2=value 2
[SectionB]
test1=value 1B
# Blank line should be above
test3 = value 3B
[SectionC]
# The following option is empty
empty_option=
'''
TEST_FILE_CONTENT = os.linesep.join([
'# Comment on the first line',
'',
'# First main option',
'option1 = main1',
'',
'# Second main option',
'option2 = main2',
'',
'',
'[main]',
'# Another comment',
'test1 = value 1',
'',
'test2 = value 2',
'',
'[SectionB]',
'test1 = value 1B',
'',
'# Blank line should be above',
'test3 = value 3B',
'',
'[SectionC]',
'# The following option is empty',
'empty_option ='
])
print('*' * 68)
print('original')
print(repr(salt.utils.to_bytes(TEST_FILE_CONTENT)))
print('*' * 68)
maxDiff = None
def setUp(self):
self.tfile = tempfile.NamedTemporaryFile(delete=False, mode='w+')
self.tfile.write(self.TEST_FILE_CONTENT)
self.tfile = tempfile.NamedTemporaryFile(delete=False, mode='w+b')
self.tfile.write(salt.utils.to_bytes(self.TEST_FILE_CONTENT))
self.tfile.close()
def tearDown(self):
@ -128,33 +132,35 @@ empty_option=
ini.set_option(self.tfile.name, {
'SectionB': {'test3': 'new value 3B'},
})
expected = os.linesep.join([
'# Comment on the first line',
'',
'# First main option',
'option1 = main1',
'',
'# Second main option',
'option2 = main2',
'',
'[main]',
'# Another comment',
'test1 = value 1',
'',
'test2 = value 2',
'',
'[SectionB]',
'test1 = value 1B',
'',
'# Blank line should be above',
'test3 = new value 3B',
'',
'[SectionC]',
'# The following option is empty',
'empty_option = ',
''
])
with salt.utils.fopen(self.tfile.name, 'r') as fp:
file_content = fp.read()
self.assertEqual('''\
# Comment on the first line
# First main option
option1 = main1
# Second main option
option2 = main2
[main]
# Another comment
test1 = value 1
test2 = value 2
[SectionB]
test1 = value 1B
# Blank line should be above
test3 = new value 3B
[SectionC]
# The following option is empty
empty_option =
''', file_content)
self.assertEqual(expected, file_content)
def test_empty_lines_preserved_after_multiple_edits(self):
ini.set_option(self.tfile.name, {