mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Write a unit test demonstrating stack trace in #31135
Here is the stack trace that happens when running file.line with mode=replace on a file that exists, but is empty, as described in the bug report: unit.modules.file_test.FileModuleTestCase.test_replace_line_in_empty_file ................................................. Traceback (most recent call last): File "/root/SaltStack/salt/tests/unit/modules/file_test.py", line 593, in test_replace_line_in_empty_file mode='replace')) File "/root/SaltStack/salt/salt/modules/file.py", line 1523, in line for line in body.split(os.linesep)]) TypeError: expected a character buffer object
This commit is contained in:
parent
80a99c4cc5
commit
94a00c66eb
1 changed files with 26 additions and 0 deletions
|
@ -571,6 +571,32 @@ class FileModuleTestCase(TestCase):
|
|||
saltenv='base')
|
||||
self.assertEqual(ret, 'This is a templated file.')
|
||||
|
||||
def test_replace_line_in_empty_file(self):
|
||||
'''
|
||||
Tests that when calling file.line with ``mode=replace``,
|
||||
the function doesn't stack trace if the file is empty.
|
||||
Should return ``False``.
|
||||
|
||||
See Issue #31135.
|
||||
'''
|
||||
# Create an empty temporary named file
|
||||
empty_file = tempfile.NamedTemporaryFile(delete=False,
|
||||
mode='w+')
|
||||
|
||||
# Assert that the file was created and is empty
|
||||
self.assertEqual(os.stat(empty_file.name).st_size, 0)
|
||||
|
||||
# Now call the function on the empty file and assert
|
||||
# the return is False instead of stack-tracing
|
||||
self.assertFalse(filemod.line(empty_file.name,
|
||||
content='foo',
|
||||
match='bar',
|
||||
mode='replace'))
|
||||
|
||||
# Close and remove the file
|
||||
empty_file.close()
|
||||
os.remove(empty_file.name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from integration import run_tests
|
||||
|
|
Loading…
Add table
Reference in a new issue