Ejemplo n.º 1
0
    def test_replace_prepend(self):
        new_content = "Well, I didn't vote for you."

        self.assertRaises(CommandExecutionError,
                          filemod.blockreplace,
                          self.tfile.name,
                          '#-- START BLOCK 2',
                          '#-- END BLOCK 2',
                          new_content,
                          prepend_if_not_found=False,
                          backup=False)
        with open(self.tfile.name, 'rb') as fp:
            self.assertNotIn(
                '#-- START BLOCK 2' + "\n" + new_content + "\n" +
                '#-- END BLOCK 2', fp.read())

        filemod.blockreplace(self.tfile.name,
                             '#-- START BLOCK 2',
                             '#-- END BLOCK 2',
                             new_content,
                             backup=False,
                             prepend_if_not_found=True)

        with open(self.tfile.name, 'rb') as fp:
            self.assertTrue(
                fp.read().startswith('#-- START BLOCK 2' + "\n" + new_content +
                                     "\n" + '#-- END BLOCK 2'))
Ejemplo n.º 2
0
    def test_replace_append(self):
        new_content = "Well, I didn't vote for you."

        self.assertRaises(CommandExecutionError,
                          filemod.blockreplace,
                          self.tfile.name,
                          '#-- START BLOCK 2',
                          '#-- END BLOCK 2',
                          new_content,
                          append_if_not_found=False,
                          backup=False)
        with salt.utils.files.fopen(self.tfile.name, 'r') as fp:
            self.assertNotIn(
                '#-- START BLOCK 2' + "\n" + new_content + '#-- END BLOCK 2',
                fp.read())

        filemod.blockreplace(self.tfile.name,
                             '#-- START BLOCK 2',
                             '#-- END BLOCK 2',
                             new_content,
                             backup=False,
                             append_if_not_found=True)

        with salt.utils.files.fopen(self.tfile.name, 'r') as fp:
            self.assertIn(
                '#-- START BLOCK 2' + "\n" + new_content + '#-- END BLOCK 2',
                fp.read())
Ejemplo n.º 3
0
    def test_replace_prepend(self):
        new_content = "Well, I didn't vote for you."

        self.assertRaises(
            CommandExecutionError,
            filemod.blockreplace,
            self.tfile.name,
            '#-- START BLOCK 2',
            '#-- END BLOCK 2',
            new_content,
            prepend_if_not_found=False,
            backup=False
        )
        with salt.utils.files.fopen(self.tfile.name, 'rb') as fp:
            self.assertNotIn(salt.utils.stringutils.to_bytes(
                os.linesep.join([
                    '#-- START BLOCK 2',
                    '{0}#-- END BLOCK 2'.format(new_content)])),
                fp.read())

        filemod.blockreplace(self.tfile.name,
                             '#-- START BLOCK 2', '#-- END BLOCK 2',
                             new_content,
                             backup=False,
                             prepend_if_not_found=True)

        with salt.utils.files.fopen(self.tfile.name, 'rb') as fp:
            self.assertTrue(
                fp.read().startswith(salt.utils.stringutils.to_bytes(
                    os.linesep.join([
                        '#-- START BLOCK 2',
                        '{0}#-- END BLOCK 2'.format(new_content)]))))
Ejemplo n.º 4
0
    def test_no_modifications(self):
        filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 4', backup=False)
        before_ctime = os.stat(self.tfile.name).st_mtime
        filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 4', backup=False)
        after_ctime = os.stat(self.tfile.name).st_mtime

        self.assertEqual(before_ctime, after_ctime)
Ejemplo n.º 5
0
    def test_replace_prepend(self):
        new_content = "Well, I didn't vote for you."

        self.assertRaises(
            CommandExecutionError,
            filemod.blockreplace,
            self.tfile.name,
            '#-- START BLOCK 2',
            '#-- END BLOCK 2',
            new_content,
            prepend_if_not_found=False,
            backup=False
        )
        with salt.utils.fopen(self.tfile.name, 'r') as fp:
            self.assertNotIn(
                '#-- START BLOCK 2' + "\n"
                + new_content + "\n" + '#-- END BLOCK 2',
                fp.read())

        filemod.blockreplace(self.tfile.name,
                             '#-- START BLOCK 2', '#-- END BLOCK 2',
                             new_content,
                             backup=False,
                             prepend_if_not_found=True)

        with salt.utils.fopen(self.tfile.name, 'r') as fp:
            self.assertTrue(
                fp.read().startswith(
                    '#-- START BLOCK 2'
                    + "\n" + new_content
                    + "\n" + '#-- END BLOCK 2'))
Ejemplo n.º 6
0
def test_no_modifications(multiline_file):
    if salt.utils.platform.is_windows():
        check_perms_patch = win_file.check_perms
    else:
        check_perms_patch = filemod.check_perms
    with patch.object(filemod, "check_perms", check_perms_patch):
        filemod.blockreplace(
            multiline_file,
            marker_start="#-- START BLOCK 1",
            marker_end="#-- END BLOCK 1",
            content="new content 4",
            backup=False,
            append_newline=None,
        )
    before_ctime = os.stat(multiline_file).st_mtime
    if salt.utils.platform.is_windows():
        check_perms_patch = win_file.check_perms
    else:
        check_perms_patch = filemod.check_perms
    with patch.object(filemod, "check_perms", check_perms_patch):
        filemod.blockreplace(
            multiline_file,
            marker_start="#-- START BLOCK 1",
            marker_end="#-- END BLOCK 1",
            content="new content 4",
            backup=False,
            append_newline=None,
        )
    after_ctime = os.stat(multiline_file).st_mtime

    assert before_ctime == after_ctime
Ejemplo n.º 7
0
def test_show_changes(multiline_file):
    if salt.utils.platform.is_windows():
        check_perms_patch = win_file.check_perms
    else:
        check_perms_patch = filemod.check_perms
    with patch.object(filemod, "check_perms", check_perms_patch):
        ret = filemod.blockreplace(
            multiline_file,
            marker_start="// START BLOCK",
            marker_end="// END BLOCK",
            content="new content 6",
            backup=False,
            show_changes=True,
        )

        assert ret.startswith("---")  # looks like a diff

        ret = filemod.blockreplace(
            multiline_file,
            marker_start="// START BLOCK",
            marker_end="// END BLOCK",
            content="new content 7",
            backup=False,
            show_changes=False,
        )

        assert isinstance(ret, bool)
Ejemplo n.º 8
0
    def test_show_changes(self):
        ret = filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 6', backup=False, show_changes=True)

        self.assertTrue(ret.startswith('---')) # looks like a diff

        ret = filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 7', backup=False, show_changes=False)

        self.assertIsInstance(ret, bool)
Ejemplo n.º 9
0
    def test_replace_multiline(self):
        new_multiline_content = "Who's that then?\nWell, how'd you become king, then?\nWe found them. I'm not a witch.\nWe shall say 'Ni' again to you, if you do not appease us."
        filemod.blockreplace(self.tfile.name, '#-- START BLOCK 1', '#-- END BLOCK 1', new_multiline_content, backup=False)

        with open(self.tfile.name, 'rb') as fp:
            filecontent=fp.read()
        self.assertIn('#-- START BLOCK 1'+"\n"+new_multiline_content+"\n"+'#-- END BLOCK 1', filecontent)
        self.assertNotIn('old content part 1', filecontent)
        self.assertNotIn('old content part 2', filecontent)
Ejemplo n.º 10
0
def test_unfinished_block_exception(multiline_file):
    with pytest.raises(CommandExecutionError):
        filemod.blockreplace(
            multiline_file,
            marker_start="#-- START BLOCK UNFINISHED",
            marker_end="#-- END BLOCK UNFINISHED",
            content="foobar",
            backup=False,
        )
Ejemplo n.º 11
0
    def test_dry_run(self):
        before_ctime = os.stat(self.tfile.name).st_mtime
        filemod.blockreplace(self.tfile.name,
                             '// START BLOCK',
                             '// END BLOCK',
                             'new content 5',
                             dry_run=True)
        after_ctime = os.stat(self.tfile.name).st_mtime

        self.assertEqual(before_ctime, after_ctime)
Ejemplo n.º 12
0
    def test_dry_run(self):
        before_ctime = os.stat(self.tfile.name).st_mtime
        filemod.blockreplace(self.tfile.name,
                             '// START BLOCK',
                             '// END BLOCK',
                             'new content 5',
                             dry_run=True)
        after_ctime = os.stat(self.tfile.name).st_mtime

        self.assertEqual(before_ctime, after_ctime)
Ejemplo n.º 13
0
    def test_replace_partial_marked_lines(self):
        filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 1', backup=False)

        with open(self.tfile.name, 'rb') as fp:
            filecontent=fp.read()
        self.assertIn('new content 1', filecontent)
        self.assertNotIn('to be removed', filecontent)
        self.assertIn('first part of start line', filecontent)
        self.assertIn('first part of end line', filecontent)
        self.assertIn('part of start line not removed', filecontent)
        self.assertIn('part of end line not removed', filecontent)
Ejemplo n.º 14
0
def test_dry_run(multiline_file):
    before_ctime = os.stat(multiline_file).st_mtime
    filemod.blockreplace(
        multiline_file,
        marker_start="// START BLOCK",
        marker_end="// END BLOCK",
        content="new content 5",
        dry_run=True,
    )
    after_ctime = os.stat(multiline_file).st_mtime

    assert before_ctime == after_ctime
Ejemplo n.º 15
0
    def test_no_modifications(self):
        filemod.blockreplace(self.tfile.name,
                             '// START BLOCK', '// END BLOCK',
                             'new content 4',
                             backup=False)
        before_ctime = os.stat(self.tfile.name).st_mtime
        filemod.blockreplace(self.tfile.name,
                             '// START BLOCK',
                             '// END BLOCK',
                             'new content 4',
                             backup=False)
        after_ctime = os.stat(self.tfile.name).st_mtime

        self.assertEqual(before_ctime, after_ctime)
Ejemplo n.º 16
0
    def test_replace_partial_marked_lines(self):
        filemod.blockreplace(self.tfile.name,
                             '// START BLOCK',
                             '// END BLOCK',
                             'new content 1',
                             backup=False)

        with salt.utils.fopen(self.tfile.name, 'r') as fp:
            filecontent = fp.read()
        self.assertIn('new content 1', filecontent)
        self.assertNotIn('to be removed', filecontent)
        self.assertIn('first part of start line', filecontent)
        self.assertIn('first part of end line', filecontent)
        self.assertIn('part of start line not removed', filecontent)
        self.assertIn('part of end line not removed', filecontent)
Ejemplo n.º 17
0
    def test_replace_multiline(self):
        new_multiline_content = "Who's that then?\nWell, how'd you become king, then?\nWe found them. I'm not a witch.\nWe shall say 'Ni' again to you, if you do not appease us."
        filemod.blockreplace(self.tfile.name,
                             '#-- START BLOCK 1',
                             '#-- END BLOCK 1',
                             new_multiline_content,
                             backup=False)

        with open(self.tfile.name, 'rb') as fp:
            filecontent = fp.read()
        self.assertIn(
            '#-- START BLOCK 1' + "\n" + new_multiline_content + "\n" +
            '#-- END BLOCK 1', filecontent)
        self.assertNotIn('old content part 1', filecontent)
        self.assertNotIn('old content part 2', filecontent)
Ejemplo n.º 18
0
    def test_backup(self):
        fext = '.bak'
        bak_file = '{0}{1}'.format(self.tfile.name, fext)

        filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 2', backup=fext)

        self.assertTrue(os.path.exists(bak_file))
        os.unlink(bak_file)
        self.assertFalse(os.path.exists(bak_file))

        fext = '.bak'
        bak_file = '{0}{1}'.format(self.tfile.name, fext)

        filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 3', backup=False)

        self.assertFalse(os.path.exists(bak_file))
Ejemplo n.º 19
0
def test_replace_prepend(multiline_file):
    new_content = "Well, I didn't vote for you."

    with pytest.raises(CommandExecutionError):
        filemod.blockreplace(
            multiline_file,
            marker_start="#-- START BLOCK 2",
            marker_end="#-- END BLOCK 2",
            content=new_content,
            prepend_if_not_found=False,
            backup=False,
        )
    with salt.utils.files.fopen(multiline_file, "rb") as fp:
        assert (
            salt.utils.stringutils.to_bytes(
                os.linesep.join(
                    ["#-- START BLOCK 2", "{}#-- END BLOCK 2".format(new_content)]
                )
            )
            not in fp.read()
        )

    if salt.utils.platform.is_windows():
        check_perms_patch = win_file.check_perms
    else:
        check_perms_patch = filemod.check_perms
    with patch.object(filemod, "check_perms", check_perms_patch):
        filemod.blockreplace(
            multiline_file,
            marker_start="#-- START BLOCK 2",
            marker_end="#-- END BLOCK 2",
            content=new_content,
            backup=False,
            prepend_if_not_found=True,
        )

    with salt.utils.files.fopen(multiline_file, "rb") as fp:
        assert fp.read().startswith(
            salt.utils.stringutils.to_bytes(
                os.linesep.join(
                    [
                        "#-- START BLOCK 2",
                        "{}#-- END BLOCK 2".format(new_content),
                    ]
                )
            )
        )
Ejemplo n.º 20
0
    def test_no_modifications(self):
        filemod.blockreplace(self.tfile.name,
                             marker_start='#-- START BLOCK 1',
                             marker_end='#-- END BLOCK 1',
                             content='new content 4',
                             backup=False,
                             append_newline=None)
        before_ctime = os.stat(self.tfile.name).st_mtime
        filemod.blockreplace(self.tfile.name,
                             marker_start='#-- START BLOCK 1',
                             marker_end='#-- END BLOCK 1',
                             content='new content 4',
                             backup=False,
                             append_newline=None)
        after_ctime = os.stat(self.tfile.name).st_mtime

        self.assertEqual(before_ctime, after_ctime)
Ejemplo n.º 21
0
    def test_show_changes(self):
        ret = filemod.blockreplace(self.tfile.name,
                                   '// START BLOCK',
                                   '// END BLOCK',
                                   'new content 6',
                                   backup=False,
                                   show_changes=True)

        self.assertTrue(ret.startswith('---'))  # looks like a diff

        ret = filemod.blockreplace(self.tfile.name,
                                   '// START BLOCK',
                                   '// END BLOCK',
                                   'new content 7',
                                   backup=False,
                                   show_changes=False)

        self.assertIsInstance(ret, bool)
Ejemplo n.º 22
0
def test_replace_insert_before(multiline_file):
    new_content = "Well, I didn't vote for you."

    with pytest.raises(CommandExecutionError):
        filemod.blockreplace(
            multiline_file,
            marker_start="#-- START BLOCK 2",
            marker_end="#-- END BLOCK 2",
            content=new_content,
            insert_before_match="not in the text",
            backup=False,
        )
    with salt.utils.files.fopen(multiline_file, "r") as fp:
        assert (
            "#-- START BLOCK 2" + "\n" + new_content + "#-- END BLOCK 2"
            not in salt.utils.stringutils.to_unicode(fp.read())
        )

    if salt.utils.platform.is_windows():
        check_perms_patch = win_file.check_perms
    else:
        check_perms_patch = filemod.check_perms
    with patch.object(filemod, "check_perms", check_perms_patch):
        filemod.blockreplace(
            multiline_file,
            marker_start="#-- START BLOCK 2",
            marker_end="#-- END BLOCK 2",
            content=new_content,
            backup=False,
            insert_before_match="malesuada",
        )

    with salt.utils.files.fopen(multiline_file, "rb") as fp:
        assert (
            salt.utils.stringutils.to_bytes(
                os.linesep.join(
                    ["#-- START BLOCK 2", "{}#-- END BLOCK 2".format(new_content)]
                )
            )
            in fp.read()
        )
Ejemplo n.º 23
0
    def test_backup(self):
        fext = '.bak'
        bak_file = '{0}{1}'.format(self.tfile.name, fext)

        filemod.blockreplace(
            self.tfile.name,
            '// START BLOCK', '// END BLOCK', 'new content 2',
            backup=fext)

        self.assertTrue(os.path.exists(bak_file))
        os.unlink(bak_file)
        self.assertFalse(os.path.exists(bak_file))

        fext = '.bak'
        bak_file = '{0}{1}'.format(self.tfile.name, fext)

        filemod.blockreplace(self.tfile.name,
                             '// START BLOCK', '// END BLOCK', 'new content 3',
                             backup=False)

        self.assertFalse(os.path.exists(bak_file))
Ejemplo n.º 24
0
    def test_replace_multiline(self):
        new_multiline_content = os.linesep.join([
            "Who's that then?",
            "Well, how'd you become king, then?",
            "We found them. I'm not a witch.",
            "We shall say 'Ni' again to you, if you do not appease us."
        ])
        filemod.blockreplace(self.tfile.name,
                             '#-- START BLOCK 1',
                             '#-- END BLOCK 1',
                             new_multiline_content,
                             backup=False)

        with salt.utils.files.fopen(self.tfile.name, 'rb') as fp:
            filecontent = fp.read()
        self.assertIn(salt.utils.stringutils.to_bytes(
            os.linesep.join([
                '#-- START BLOCK 1', new_multiline_content, '#-- END BLOCK 1'])),
            filecontent)
        self.assertNotIn(b'old content part 1', filecontent)
        self.assertNotIn(b'old content part 2', filecontent)
Ejemplo n.º 25
0
 def test_replace_append_newline_at_eof(self):
     '''
     Check that file.blockreplace works consistently on files with and
     without newlines at end of file.
     '''
     base = 'bar'
     args = {
             'marker_start': '#start',
             'marker_end': '#stop',
             'content': 'baz',
             'append_if_not_found': True,
     }
     block = '{marker_start}\n{content}{marker_end}\n'.format(**args)
     expected = base + '\n' + block
     # File ending with a newline
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         tfile.write(base + '\n')
         tfile.flush()
         filemod.blockreplace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), expected)
     # File not ending with a newline
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         tfile.write(base)
         tfile.flush()
         filemod.blockreplace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), expected)
     # A newline should not be added in empty files
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         filemod.blockreplace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), block)
Ejemplo n.º 26
0
 def test_replace_append_newline_at_eof(self):
     '''
     Check that file.blockreplace works consistently on files with and
     without newlines at end of file.
     '''
     base = 'bar'
     args = {
             'marker_start': '#start',
             'marker_end': '#stop',
             'content': 'baz',
             'append_if_not_found': True,
     }
     block = '{marker_start}\n{content}\n{marker_end}\n'.format(**args)
     expected = base + '\n' + block
     # File ending with a newline
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         tfile.write(base + '\n')
         tfile.flush()
         filemod.blockreplace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), expected)
     # File not ending with a newline
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         tfile.write(base)
         tfile.flush()
         filemod.blockreplace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), expected)
     # A newline should not be added in empty files
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         filemod.blockreplace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), block)
Ejemplo n.º 27
0
def test_replace_partial_marked_lines(multiline_file):
    if salt.utils.platform.is_windows():
        check_perms_patch = win_file.check_perms
    else:
        check_perms_patch = filemod.check_perms
    with patch.object(filemod, "check_perms", check_perms_patch):
        filemod.blockreplace(
            multiline_file,
            marker_start="// START BLOCK",
            marker_end="// END BLOCK",
            content="new content 1",
            backup=False,
        )

    with salt.utils.files.fopen(multiline_file, "r") as fp:
        filecontent = salt.utils.stringutils.to_unicode(fp.read())
    assert "new content 1" in filecontent
    assert "to be removed" not in filecontent
    assert "first part of start line" in filecontent
    assert "first part of end line" not in filecontent
    assert "part of start line not removed" in filecontent
    assert "part of end line not removed" in filecontent
Ejemplo n.º 28
0
def test_backup(multiline_file):
    fext = ".bak"
    bak_file = "{}{}".format(multiline_file, fext)

    if salt.utils.platform.is_windows():
        check_perms_patch = win_file.check_perms
    else:
        check_perms_patch = filemod.check_perms
    with patch.object(filemod, "check_perms", check_perms_patch):
        filemod.blockreplace(
            multiline_file,
            marker_start="// START BLOCK",
            marker_end="// END BLOCK",
            content="new content 2",
            backup=fext,
        )

    assert os.path.exists(bak_file)
    os.unlink(bak_file)
    assert not os.path.exists(bak_file)

    fext = ".bak"
    bak_file = "{}{}".format(multiline_file, fext)

    if salt.utils.platform.is_windows():
        check_perms_patch = win_file.check_perms
    else:
        check_perms_patch = filemod.check_perms
    with patch.object(filemod, "check_perms", check_perms_patch):
        filemod.blockreplace(
            multiline_file,
            marker_start="// START BLOCK",
            marker_end="// END BLOCK",
            content="new content 3",
            backup=False,
        )

    assert not os.path.exists(bak_file)
Ejemplo n.º 29
0
def test_replace_multiline(multiline_file):
    new_multiline_content = os.linesep.join(
        [
            "Who's that then?",
            "Well, how'd you become king, then?",
            "We found them. I'm not a witch.",
            "We shall say 'Ni' again to you, if you do not appease us.",
        ]
    )
    if salt.utils.platform.is_windows():
        check_perms_patch = win_file.check_perms
    else:
        check_perms_patch = filemod.check_perms
    with patch.object(filemod, "check_perms", check_perms_patch):
        filemod.blockreplace(
            multiline_file,
            marker_start="#-- START BLOCK 1",
            marker_end="#-- END BLOCK 1",
            content=new_multiline_content,
            backup=False,
            append_newline=None,
        )

    with salt.utils.files.fopen(multiline_file, "rb") as fp:
        filecontent = fp.read()

    assert (
        salt.utils.stringutils.to_bytes(
            os.linesep.join(
                ["#-- START BLOCK 1", new_multiline_content, "#-- END BLOCK 1"]
            )
        )
        in filecontent
    )
    assert b"old content part 1" not in filecontent
    assert b"old content part 2" not in filecontent
Ejemplo n.º 30
0
def test_replace_append_newline_at_eof(subdir):
    """
    Check that file.blockreplace works consistently on files with and
    without newlines at end of file.
    """
    base = "bar"
    args = {
        "marker_start": "#start",
        "marker_end": "#stop",
        "content": "baz",
        "append_if_not_found": True,
    }
    block = os.linesep.join(["#start", "baz#stop"]) + os.linesep
    # File ending with a newline
    with salt.utils.files.fopen(str(subdir / "tfile"), "w+b") as tfile:
        tfile.write(salt.utils.stringutils.to_bytes(base + os.linesep))
        tfile.flush()
    if salt.utils.platform.is_windows():
        check_perms_patch = win_file.check_perms
    else:
        check_perms_patch = filemod.check_perms
    with patch.object(filemod, "check_perms", check_perms_patch):
        filemod.blockreplace(tfile.name, **args)
    expected = os.linesep.join([base, block])
    with salt.utils.files.fopen(tfile.name) as tfile2:
        assert salt.utils.stringutils.to_unicode(tfile2.read()) == expected
    os.remove(tfile.name)

    # File not ending with a newline
    with salt.utils.files.fopen(str(subdir / "tfile"), "w+b") as tfile:
        tfile.write(salt.utils.stringutils.to_bytes(base))
        tfile.flush()
    if salt.utils.platform.is_windows():
        check_perms_patch = win_file.check_perms
    else:
        check_perms_patch = filemod.check_perms
    with patch.object(filemod, "check_perms", check_perms_patch):
        filemod.blockreplace(tfile.name, **args)
    with salt.utils.files.fopen(tfile.name) as tfile2:
        assert salt.utils.stringutils.to_unicode(tfile2.read()) == expected
    os.remove(tfile.name)

    # A newline should not be added in empty files
    with salt.utils.files.fopen(str(subdir / "tfile"), "w+b") as tfile:
        pass
    if salt.utils.platform.is_windows():
        check_perms_patch = win_file.check_perms
    else:
        check_perms_patch = filemod.check_perms
    with patch.object(filemod, "check_perms", check_perms_patch):
        filemod.blockreplace(tfile.name, **args)
    with salt.utils.files.fopen(tfile.name) as tfile2:
        assert salt.utils.stringutils.to_unicode(tfile2.read()) == block
    os.remove(tfile.name)
Ejemplo n.º 31
0
    def test_replace_append_newline_at_eof(self):
        '''
        Check that file.blockreplace works consistently on files with and
        without newlines at end of file.
        '''
        base = 'bar'
        args = {
            'marker_start': '#start',
            'marker_end': '#stop',
            'content': 'baz',
            'append_if_not_found': True,
        }
        block = os.linesep.join(['#start', 'baz#stop']) + os.linesep
        # File ending with a newline
        with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tfile:
            tfile.write(salt.utils.to_bytes(base + os.linesep))
            tfile.flush()
        filemod.blockreplace(tfile.name, **args)
        expected = os.linesep.join([base, block])
        with salt.utils.files.fopen(tfile.name) as tfile2:
            self.assertEqual(tfile2.read(), expected)
        os.remove(tfile.name)

        # File not ending with a newline
        with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tfile:
            tfile.write(salt.utils.to_bytes(base))
            tfile.flush()
        filemod.blockreplace(tfile.name, **args)
        with salt.utils.files.fopen(tfile.name) as tfile2:
            self.assertEqual(tfile2.read(), expected)
        os.remove(tfile.name)

        # A newline should not be added in empty files
        with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tfile:
            pass
        filemod.blockreplace(tfile.name, **args)
        with salt.utils.files.fopen(tfile.name) as tfile2:
            self.assertEqual(tfile2.read(), block)
        os.remove(tfile.name)