Merge pull request #43363 from twangboy/scratch_ini_tests

Fix `unit.modules.test_ini_manage` for Windows
This commit is contained in:
Mike Place 2017-09-11 11:10:30 -06:00 committed by GitHub
commit 9b89e49846
2 changed files with 62 additions and 59 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,38 @@ 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='
])
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):
@ -121,40 +121,42 @@ empty_option=
})
with salt.utils.fopen(self.tfile.name, 'r') as fp:
file_content = fp.read()
self.assertIn('\nempty_option = \n', file_content,
'empty_option was not preserved')
expected = '{0}{1}{0}'.format(os.linesep, 'empty_option = ')
self.assertIn(expected, file_content, 'empty_option was not preserved')
def test_empty_lines_preserved_after_edit(self):
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, {