예제 #1
0
    def test_line_ensure_location_end(self):
        '''
        Check that file.line uses ``location=end`` if a
        match is not found and replaces content if it is.
        '''
        # File DOESN'T contain the match
        with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tfile:
            tfile.write(salt.utils.to_bytes('first=foo'))
            tfile.flush()
        filemod.line(tfile.name,
                     content='second=bar',
                     match='second=',
                     mode='ensure',
                     location='end')
        expected = os.linesep.join(['first=foo', 'second=bar'])
        with salt.utils.files.fopen(tfile.name) as tfile2:
            self.assertEqual(tfile2.read(), expected)

        # File DOES contain the match
        with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tfile:
            tfile.write(
                salt.utils.to_bytes(
                    os.linesep.join(['second=foo', 'first=foo']) + os.linesep))
            tfile.flush()
        filemod.line(tfile.name,
                     content='second=bar',
                     match='second=',
                     mode='ensure',
                     location='end')
        expected = os.linesep.join(['second=bar', 'first=foo']) + os.linesep
        with salt.utils.files.fopen(tfile.name) as tfile2:
            self.assertEqual(tfile2.read(), expected)
예제 #2
0
    def test_delete_line_in_empty_file(self):
        '''
        Tests that when calling file.line with ``mode=delete``,
        the function doesn't stack trace if the file is empty.
        Should return ``False``.

        See Issue #38438.
        '''
        # 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='delete'))

        # Close and remove the file
        empty_file.close()
        os.remove(empty_file.name)
예제 #3
0
파일: file_test.py 프로젝트: bryson/salt
    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)