def test_editfile_nosuch(tmpdir, fx_edit): """ fl.editfile('nosuchfile', 'foo', 'bar', None) => should throw an exception """ pytest.debug_func() with pytest.raises(IOError) as err: fl.editfile(fx_edit.fp.strpath, 's', 'foo', 'bar', None) exp = "No such file or directory: '{}'".format(fx_edit.fp.strpath) assert exp in str(err)
def test_editfile_rgx(tmpdir, fx_edit): """ fl.editfile('legit', 's', 'foo', 'bar', None) """ pytest.debug_func() xdata = [re.sub('^foo', 'bar', z) for z in fx_edit.tdata] fx_edit.fp.write("\n".join(fx_edit.tdata)) fl.editfile(fx_edit.fp.strpath, 's', '^foo', 'bar', None) assert fx_edit.fp.exists() assert fx_edit.forig.exists() assert "\n".join(xdata) == fx_edit.fp.read() assert "\n".join(fx_edit.tdata) == fx_edit.forig.read()
def test_editfile_empty(tmpdir, fx_edit): """ fl.editfile('emptyfile', 's', 'foo', 'bar', None) => rename emptyfile emptyfile.original, both empty """ pytest.debug_func() fx_edit.fp.ensure() fl.editfile(fx_edit.fp.strpath, 's', 'foo', 'bar', None) assert fx_edit.fp.exists() assert fx_edit.forig.exists() assert "" == fx_edit.fp.read() assert "" == fx_edit.forig.read()
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_suffix(tmpdir, fx_edit): """ fl.editfile('legit', 's', 'foo', 'bar', 'old') """ pytest.debug_func() fold = tmpdir.join(fx_edit.fp.basename + ".old") xdata = [z.replace('foo', 'bar') for z in fx_edit.tdata] fx_edit.fp.write("\n".join(fx_edit.tdata)) fl.editfile(fx_edit.fp.strpath, 's', 'foo', 'bar', 'old') assert fx_edit.fp.exists() assert fold.exists() assert not fx_edit.forig.exists() assert "\n".join(xdata) == fx_edit.fp.read() assert "\n".join(fx_edit.tdata) == fold.read()
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))