Beispiel #1
0
    def test_nobackup(self):
        fext = '.bak'
        bak_file = '{0}{1}'.format(self.tfile.name, fext)

        filemod.replace(self.tfile.name, r'Etiam', 'Salticus', backup=False)

        self.assertFalse(os.path.exists(bak_file))
def test_nobackup(multiline_file):
    fext = ".bak"
    bak_file = "{}{}".format(multiline_file, fext)

    filemod.replace(multiline_file, r"Etiam", "Salticus", backup=False)

    assert not os.path.exists(bak_file)
Beispiel #3
0
    def test_nobackup(self):
        fext = '.bak'
        bak_file = '{0}{1}'.format(self.tfile.name, fext)

        filemod.replace(self.tfile.name, r'Etiam', 'Salticus', backup=False)

        self.assertFalse(os.path.exists(bak_file))
Beispiel #4
0
    def test_replace_append_if_not_found(self):
        '''
        Check that file.replace append_if_not_found works
        '''
        args = {
            'pattern': '#*baz=(?P<value>.*)',
            'repl': 'baz=\\g<value>',
            'append_if_not_found': True,
        }
        base = os.linesep.join(['foo=1', 'bar=2'])

        # File ending with a newline, no match
        with tempfile.NamedTemporaryFile('w+b', delete=False) as tfile:
            tfile.write(salt.utils.to_bytes(base + os.linesep))
            tfile.flush()
        filemod.replace(tfile.name, **args)
        expected = os.linesep.join([base, 'baz=\\g<value>']) + os.linesep
        with salt.utils.files.fopen(tfile.name) as tfile2:
            self.assertEqual(tfile2.read(), expected)
        os.remove(tfile.name)

        # File not ending with a newline, no match
        with tempfile.NamedTemporaryFile('w+b', delete=False) as tfile:
            tfile.write(salt.utils.to_bytes(base))
            tfile.flush()
        filemod.replace(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('w+b', delete=False) as tfile:
            pass
        filemod.replace(tfile.name, **args)
        expected = args['repl'] + os.linesep
        with salt.utils.files.fopen(tfile.name) as tfile2:
            self.assertEqual(tfile2.read(), expected)
        os.remove(tfile.name)

        # Using not_found_content, rather than repl
        with tempfile.NamedTemporaryFile('w+b', delete=False) as tfile:
            tfile.write(salt.utils.to_bytes(base))
            tfile.flush()
        args['not_found_content'] = 'baz=3'
        expected = os.linesep.join([base, 'baz=3']) + os.linesep
        filemod.replace(tfile.name, **args)
        with salt.utils.files.fopen(tfile.name) as tfile2:
            self.assertEqual(tfile2.read(), expected)
        os.remove(tfile.name)

        # not appending if matches
        with tempfile.NamedTemporaryFile('w+b', delete=False) as tfile:
            base = os.linesep.join(['foo=1', 'baz=42', 'bar=2'])
            tfile.write(salt.utils.to_bytes(base))
            tfile.flush()
        expected = base
        filemod.replace(tfile.name, **args)
        with salt.utils.files.fopen(tfile.name) as tfile2:
            self.assertEqual(tfile2.read(), expected)
def test_replace_append_if_not_found(subdir):
    """
    Check that file.replace append_if_not_found works
    """
    args = {
        "pattern": "#*baz=(?P<value>.*)",
        "repl": "baz=\\g<value>",
        "append_if_not_found": True,
    }
    base = os.linesep.join(["foo=1", "bar=2"])

    # File ending with a newline, no match
    with salt.utils.files.fopen(str(subdir / "tfile"), "w+b") as tfile:
        tfile.write(salt.utils.stringutils.to_bytes(base + os.linesep))
        tfile.flush()
    filemod.replace(tfile.name, **args)
    expected = os.linesep.join([base, "baz=\\g<value>"]) + os.linesep
    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, no match
    with salt.utils.files.fopen(str(subdir / "tfile"), "w+b") as tfile:
        tfile.write(salt.utils.stringutils.to_bytes(base))
        tfile.flush()
    filemod.replace(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
    filemod.replace(tfile.name, **args)
    expected = args["repl"] + os.linesep
    with salt.utils.files.fopen(tfile.name) as tfile2:
        assert salt.utils.stringutils.to_unicode(tfile2.read()) == expected
    os.remove(tfile.name)

    # Using not_found_content, rather than repl
    with salt.utils.files.fopen(str(subdir / "tfile"), "w+b") as tfile:
        tfile.write(salt.utils.stringutils.to_bytes(base))
        tfile.flush()
    args["not_found_content"] = "baz=3"
    expected = os.linesep.join([base, "baz=3"]) + os.linesep
    filemod.replace(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)

    # not appending if matches
    with salt.utils.files.fopen(str(subdir / "tfile"), "w+b") as tfile:
        base = os.linesep.join(["foo=1", "baz=42", "bar=2"])
        tfile.write(salt.utils.stringutils.to_bytes(base))
        tfile.flush()
    expected = base
    filemod.replace(tfile.name, **args)
    with salt.utils.files.fopen(tfile.name) as tfile2:
        assert salt.utils.stringutils.to_unicode(tfile2.read()) == expected
Beispiel #6
0
    def test_backup(self):
        fext = '.bak'
        bak_file = '{0}{1}'.format(self.tfile.name, fext)

        filemod.replace(self.tfile.name, r'Etiam', 'Salticus', backup=fext)

        self.assertTrue(os.path.exists(bak_file))
        os.unlink(bak_file)
Beispiel #7
0
    def test_backup(self):
        fext = '.bak'
        bak_file = '{0}{1}'.format(self.tfile.name, fext)

        filemod.replace(self.tfile.name, r'Etiam', 'Salticus', backup=fext)

        self.assertTrue(os.path.exists(bak_file))
        os.unlink(bak_file)
Beispiel #8
0
 def test_numeric_repl(self):
     '''
     This test covers cases where the replacement string is numeric, and the
     CLI parser yamlifies it into a numeric type. If not converted back to a
     string type in file.replace, a TypeError occurs when the replacemen is
     attempted. See https://github.com/saltstack/salt/issues/9097 for more
     information.
     '''
     filemod.replace(self.tfile.name, r'Etiam', 123)
Beispiel #9
0
 def test_numeric_repl(self):
     '''
     This test covers cases where the replacement string is numeric, and the
     CLI parser yamlifies it into a numeric type. If not converted back to a
     string type in file.replace, a TypeError occurs when the replacemen is
     attempted. See https://github.com/saltstack/salt/issues/9097 for more
     information.
     '''
     filemod.replace(self.tfile.name, r'Etiam', 123)
def test_numeric_repl(multiline_file):
    """
    This test covers cases where the replacement string is numeric, and the
    CLI parser yamlifies it into a numeric type. If not converted back to a
    string type in file.replace, a TypeError occurs when the replacemen is
    attempted. See https://github.com/saltstack/salt/issues/9097 for more
    information.
    """
    filemod.replace(multiline_file, r"Etiam", 123)
Beispiel #11
0
 def test_replace_append_if_not_found(self):
     '''
     Check that file.replace append_if_not_found works
     '''
     args = {
         'pattern': '#*baz=(?P<value>.*)',
         'repl': 'baz=\\g<value>',
         'append_if_not_found': True,
     }
     base = 'foo=1\nbar=2'
     expected = '{base}\n{repl}\n'.format(base=base, **args)
     # File ending with a newline, no match
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         tfile.write(base + '\n')
         tfile.flush()
         filemod.replace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), expected)
     # File not ending with a newline, no match
     with tempfile.NamedTemporaryFile('w+') as tfile:
         tfile.write(base)
         tfile.flush()
         filemod.replace(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('w+') as tfile:
         filemod.replace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), args['repl'] + '\n')
     # Using not_found_content, rather than repl
     with tempfile.NamedTemporaryFile('w+') as tfile:
         args['not_found_content'] = 'baz=3'
         expected = '{base}\n{not_found_content}\n'.format(base=base,
                                                           **args)
         tfile.write(base)
         tfile.flush()
         filemod.replace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), expected)
     # not appending if matches
     with tempfile.NamedTemporaryFile('w+') as tfile:
         base = 'foo=1\n#baz=42\nbar=2\n'
         expected = 'foo=1\nbaz=42\nbar=2\n'
         tfile.write(base)
         tfile.flush()
         filemod.replace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), expected)
Beispiel #12
0
 def test_replace_append_if_not_found(self):
     '''
     Check that file.replace append_if_not_found works
     '''
     args = {
             'pattern': '#*baz=(?P<value>.*)',
             'repl': 'baz=\\g<value>',
             'append_if_not_found': True,
     }
     base = 'foo=1\nbar=2'
     expected = '{base}\n{repl}\n'.format(base=base, **args)
     # File ending with a newline, no match
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         tfile.write(base + '\n')
         tfile.flush()
         filemod.replace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), expected)
     # File not ending with a newline, no match
     with tempfile.NamedTemporaryFile('w+') as tfile:
         tfile.write(base)
         tfile.flush()
         filemod.replace(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('w+') as tfile:
         filemod.replace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), args['repl'] + '\n')
     # Using not_found_content, rather than repl
     with tempfile.NamedTemporaryFile('w+') as tfile:
         args['not_found_content'] = 'baz=3'
         expected = '{base}\n{not_found_content}\n'.format(base=base, **args)
         tfile.write(base)
         tfile.flush()
         filemod.replace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), expected)
     # not appending if matches
     with tempfile.NamedTemporaryFile('w+') as tfile:
         base = 'foo=1\n#baz=42\nbar=2\n'
         expected = 'foo=1\nbaz=42\nbar=2\n'
         tfile.write(base)
         tfile.flush()
         filemod.replace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), expected)
Beispiel #13
0
    def test_noshow_changes(self):
        ret = filemod.replace(self.tfile.name,
                              r'Etiam',
                              'Salticus',
                              show_changes=False)

        self.assertIsInstance(ret, bool)
Beispiel #14
0
    def test_show_changes(self):
        ret = filemod.replace(self.tfile.name,
                              r'Etiam',
                              'Salticus',
                              show_changes=True)

        self.assertTrue(ret.startswith('---'))  # looks like a diff
def test_show_changes(multiline_file):
    ret = filemod.replace(multiline_file,
                          r"Etiam",
                          "Salticus",
                          show_changes=True)

    assert ret.startswith("---")  # looks like a diff
def test_noshow_changes(multiline_file):
    ret = filemod.replace(multiline_file,
                          r"Etiam",
                          "Salticus",
                          show_changes=False)

    assert isinstance(ret, bool)
def test_search_only_return_false(multiline_file):
    ret = filemod.replace(multiline_file,
                          r"Etian",
                          "Salticus",
                          search_only=True)

    assert isinstance(ret, bool)
    assert ret is False
Beispiel #18
0
    def test_dry_run(self):
        before_ctime = os.stat(self.tfile.name).st_mtime
        filemod.replace(self.tfile.name, r'Etiam', 'Salticus', dry_run=True)
        after_ctime = os.stat(self.tfile.name).st_mtime

        self.assertEqual(before_ctime, after_ctime)
def test_re_str_flags(multiline_file):
    # upper- & lower-case
    filemod.replace(multiline_file,
                    r"Etiam",
                    "Salticus",
                    flags=["MULTILINE", "ignorecase"])
def test_re_int_flags(multiline_file):
    filemod.replace(multiline_file, r"Etiam", "Salticus", flags=10)
def test_empty_flags_list(multiline_file):
    filemod.replace(multiline_file, r"Etiam", "Salticus", flags=[])
Beispiel #22
0
    def test_replace(self):
        filemod.replace(self.tfile.name, r'Etiam', 'Salticus', backup=False)

        with salt.utils.fopen(self.tfile.name, 'r') as fp:
            self.assertIn('Salticus', fp.read())
Beispiel #23
0
 def test_re_int_flags(self):
     filemod.replace(self.tfile.name, r'Etiam', 'Salticus', flags=10)
Beispiel #24
0
 def test_re_str_flags(self):
     # upper- & lower-case
     filemod.replace(self.tfile.name,
                     r'Etiam', 'Salticus',
                     flags=['MULTILINE', 'ignorecase'])
Beispiel #25
0
    def test_noshow_changes(self):
        ret = filemod.replace(self.tfile.name,
                              r'Etiam', 'Salticus',
                              show_changes=False)

        self.assertIsInstance(ret, bool)
Beispiel #26
0
    def test_show_changes(self):
        ret = filemod.replace(self.tfile.name,
                              r'Etiam', 'Salticus',
                              show_changes=True)

        self.assertTrue(ret.startswith('---'))  # looks like a diff
def test_replace(multiline_file):
    filemod.replace(multiline_file, r"Etiam", "Salticus", backup=False)

    with salt.utils.files.fopen(multiline_file, "r") as fp:
        assert "Salticus" in salt.utils.stringutils.to_unicode(fp.read())
Beispiel #28
0
 def test_re_str_flags(self):
     # upper- & lower-case
     filemod.replace(self.tfile.name,
                     r'Etiam', 'Salticus',
                     flags=['MULTILINE', 'ignorecase'])
Beispiel #29
0
 def test_re_int_flags(self):
     filemod.replace(self.tfile.name, r'Etiam', 'Salticus', flags=10)
def test_dry_run(multiline_file):
    before_ctime = os.stat(multiline_file).st_mtime
    filemod.replace(multiline_file, r"Etiam", "Salticus", dry_run=True)
    after_ctime = os.stat(multiline_file).st_mtime

    assert before_ctime == after_ctime
Beispiel #31
0
    def test_replace(self):
        filemod.replace(self.tfile.name, r'Etiam', 'Salticus', backup=False)

        with salt.utils.fopen(self.tfile.name, 'r') as fp:
            self.assertIn('Salticus', fp.read())
Beispiel #32
0
    def test_dry_run(self):
        before_ctime = os.stat(self.tfile.name).st_mtime
        filemod.replace(self.tfile.name, r'Etiam', 'Salticus', dry_run=True)
        after_ctime = os.stat(self.tfile.name).st_mtime

        self.assertEqual(before_ctime, after_ctime)