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'))
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())
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)]))))
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)
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'))
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
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)
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)
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)
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, )
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)
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)
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
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)
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)
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))
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), ] ) ) )
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)
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() )
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))
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)
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)
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)
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
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)
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
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)
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)