def test_editfile_empty(self): """ fl.editfile('emptyfile', 's', 'foo', 'bar', None) => rename emptyfile emptyfile.original, both empty """ fpath = U.pj(self.testdir, 'emptyfile') forig = fpath + ".original" U.touch(fpath) fl.editfile(fpath, 's', 'foo', 'bar', None) self.assertEq(True, U.exists(fpath)) self.assertEq(True, U.exists(forig)) self.assertEq([], U.contents(fpath)) self.assertEq([], U.contents(forig))
def test_editfile_delete(self): """ fl.editfile('legit', 's', 'foo', '', None) """ fp = U.pj(self.testdir, U.function_name()) forig = fp + ".original" tdata = ["foo bar", "bar foo", "barfoo", "foobar foo", "loofafool"] xdata = [re.sub('^foo', '', z) for z in tdata] U.writefile(fp, tdata, newlines=True) fl.editfile(fp, 's', '^foo', '', None) self.assertEq(True, U.exists(fp)) self.assertEq(True, U.exists(forig)) self.assertEq(xdata, U.contents(fp)) self.assertEq(tdata, U.contents(forig))
def test_editfile_suffix(self): """ fl.editfile('legit', 's', 'foo', 'bar', 'old') """ fp = U.pj(self.testdir, U.function_name()) forig = fp + ".old" tdata = ["foo bar", "bar foo", "barfoo", "foobar foo", "loofafool"] xdata = [z.replace('foo', 'bar') for z in tdata] U.writefile(fp, tdata, newlines=True) fl.editfile(fp, 's', 'foo', 'bar', 'old') self.assertEq(True, U.exists(fp)) self.assertEq(True, U.exists(forig)) self.assertEq(xdata, U.contents(fp)) self.assertEq(tdata, U.contents(forig))
def test_editfile_legit(self): """ fl.editfile('legit', 's', 'foo', 'bar', None) """ fp = U.pj(self.testdir, 'legit') forig = fp + ".original" tdata = ["foo bar", "bar foo", "barfoo", "foobar foo", "loofafool"] xdata = [z.replace('foo', 'bar') for z in tdata] U.writefile(fp, tdata, newlines=True) fl.editfile(fp, 's', 'foo', 'bar', None) self.assertEq(True, U.exists(fp)) self.assertEq(True, U.exists(forig)) self.assertEq(xdata, U.contents(fp)) self.assertEq(tdata, U.contents(forig))
def fl_edit_ok(self, eopt='', iopt='', files=0, inp=[], exp=[]): """ Common code for all the test_fl_edit_* routines """ fl = [] for idx in range(files): (fd, fp) = tempfile.mkstemp(dir=self.testdir) os.close(fd) fl.append(fp) util.writefile(fp, inp) suffix = 'original' with util.Chdir(self.testdir): cmd = "fl edit " if eopt != '': cmd += '-e "%s" ' % eopt if iopt != '': cmd += '-i %s ' % iopt suffix = iopt if 0 < len(fl): cmd += " ".join(fl) result = pexpect.run(cmd) self.assertTrue(result == "", "Expected '%s' to be empty" % result) for fp in fl: forig = fp + "." + suffix self.assertTrue(util.exists(forig), "Expected %s to exist" % forig) self.assertTrue("foo four five foo" in util.contents(forig), "Contents of %s have changed" % forig) self.assertTrue(util.exists(fp), "Expected %s to exist" % fp) self.assertEq(exp, util.contents(fp)) for fp in fl: util.safe_unlink(fp)