Ejemplo n.º 1
0
def test_append_newline_at_eof(subdir):
    """
    Check that file.append works consistently on files with and without
    newlines at end of file.
    """
    # File ending with a newline
    with salt.utils.files.fopen(str(subdir / "tfile"), "wb") as tfile:
        tfile.write(salt.utils.stringutils.to_bytes("foo" + os.linesep))
        tfile.flush()
    filemod.append(tfile.name, "bar")
    expected = os.linesep.join(["foo", "bar", ""])
    with salt.utils.files.fopen(tfile.name) as tfile2:
        new_file = salt.utils.stringutils.to_unicode(tfile2.read())
    assert new_file == expected
    os.remove(tfile.name)

    # File not ending with a newline
    with salt.utils.files.fopen(str(subdir / "tfile"), "wb") as tfile:
        tfile.write(salt.utils.stringutils.to_bytes("foo"))
        tfile.flush()
    filemod.append(tfile.name, "bar")
    with salt.utils.files.fopen(tfile.name) as tfile2:
        assert salt.utils.stringutils.to_unicode(tfile2.read()) == expected

    # A newline should be added in empty files
    with salt.utils.files.fopen(str(subdir / "tfile"), "wb") as tfile:
        filemod.append(tfile.name, salt.utils.stringutils.to_str("bar"))
    with salt.utils.files.fopen(tfile.name) as tfile2:
        assert salt.utils.stringutils.to_unicode(tfile2.read()) == "bar" + os.linesep
    os.remove(tfile.name)
Ejemplo n.º 2
0
    def test_append_newline_at_eof(self):
        '''
        Check that file.append works consistently on files with and without
        newlines at end of file.
        '''
        # File ending with a newline
        with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tfile:
            tfile.write(salt.utils.to_bytes('foo' + os.linesep))
            tfile.flush()
        filemod.append(tfile.name, 'bar')
        expected = os.linesep.join(['foo', 'bar']) + os.linesep
        with salt.utils.files.fopen(tfile.name) as tfile2:
            self.assertEqual(tfile2.read(), expected)

        # File not ending with a newline
        with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tfile:
            tfile.write(salt.utils.to_bytes('foo'))
            tfile.flush()
        filemod.append(tfile.name, 'bar')
        with salt.utils.fopen(tfile.name) as tfile2:
            self.assertEqual(tfile2.read(), expected)

        # A newline should be added in empty files
        with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tfile:
            filemod.append(tfile.name, 'bar')
        with salt.utils.files.fopen(tfile.name) as tfile2:
            self.assertEqual(tfile2.read(), 'bar' + os.linesep)
Ejemplo n.º 3
0
 def test_append_newline_at_eof(self):
     '''
     Check that file.append works consistently on files with and without
     newlines at end of file.
     '''
     # File ending with a newline
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         tfile.write('foo\n')
         tfile.flush()
         filemod.append(tfile.name, 'bar')
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), 'foo\nbar\n')
     # File not ending with a newline
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         tfile.write('foo')
         tfile.flush()
         filemod.append(tfile.name, 'bar')
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), 'foo\nbar\n')
     # A newline should not be added in empty files
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         filemod.append(tfile.name, 'bar')
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), 'bar\n')
Ejemplo n.º 4
0
 def test_append_newline_at_eof(self):
     '''
     Check that file.append works consistently on files with and without
     newlines at end of file.
     '''
     # File ending with a newline
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         tfile.write('foo\n')
         tfile.flush()
         filemod.append(tfile.name, 'bar')
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), 'foo\nbar\n')
     # File not ending with a newline
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         tfile.write('foo')
         tfile.flush()
         filemod.append(tfile.name, 'bar')
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), 'foo\nbar\n')
     # A newline should not be added in empty files
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         filemod.append(tfile.name, 'bar')
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), 'bar\n')