def test_remove_noperm(tmpdir): testpath = "%s/test_remove_noperm" % tmpdir os.makedirs(testpath + "/foo/bar") os.chmod(testpath + "/foo/bar", 0) with pytest.raises(PermissionError): sh.remove(testpath + "/foo", recursive=True) sh.remove(testpath + "/foo", recursive=True, rename_on_fail=True) assert not os.path.exists(testpath + "/foo") assert len(glob.glob(testpath + "/foo.DELETEME.*")) == 1
def test_ignore_readonly2(tmpdir): """Test the case where there was no permission issue to begin with, so a double call to shutil.rmtree would raise FileNotFoundError """ os.makedirs(f"{tmpdir}/foo/bar") sh.remove(f"{tmpdir}/foo", force=False, recursive=True, ignore_readonly=True) assert not os.path.exists(f"{tmpdir}/foo")
def test_remove(str_or_path, tmpdir): os.environ["UNITTEST_BASH"] = str(tmpdir) testpath = str_or_path(f"{tmpdir}/test_remove") testpath_env = str_or_path("$UNITTEST_BASH/test_remove") # remove file with open(testpath, "w"): pass assert os.path.exists(testpath) sh.remove(testpath_env) assert not os.path.exists(testpath) # remove dir os.mkdir(testpath) assert os.path.exists(testpath) sh.remove(testpath_env) assert not os.path.exists(testpath) # recursive os.mkdir(testpath) os.mkdir(f"{testpath}/dir2") sh.remove(testpath_env, recursive=True) assert not os.path.exists(testpath) # recursive must also work on a file with open(testpath, "w"): pass assert os.path.exists(testpath) sh.remove(testpath_env, recursive=True) assert not os.path.exists(testpath)
def test_ignore_readonly1(tmpdir): """Test the ignore_readonly=True flag""" os.makedirs(f"{tmpdir}/foo/bar/baz") os.chmod(f"{tmpdir}/foo/bar/baz", 0o500) os.chmod(f"{tmpdir}/foo/bar", 0o500) os.chmod(f"{tmpdir}/foo", 0o500) with pytest.raises(PermissionError): sh.remove(f"{tmpdir}/foo", recursive=True) assert os.path.exists(f"{tmpdir}/foo/bar/baz") sh.remove(f"{tmpdir}/foo", force=False, recursive=True, ignore_readonly=True) assert not os.path.exists(f"{tmpdir}/foo")
def test_remove_symlinks(str_or_path, tmpdir): os.environ["UNITTEST_BASH"] = str(tmpdir) testpath = f"{tmpdir}/test_remove" testpath_env = str_or_path("$UNITTEST_BASH/test_remove") # remove dir and symlink to dir os.mkdir(testpath) os.symlink(testpath, testpath + ".lnk") assert os.path.exists(testpath) assert os.path.exists(testpath + ".lnk") sh.remove(f"{testpath_env}.lnk") sh.remove(testpath_env) assert not os.path.exists(testpath) assert not os.path.exists(testpath + ".lnk") # recursive on a symlink to dir must delete the symlink os.mkdir(testpath) with open(f"{testpath}/donttouch", "w"): pass os.symlink(testpath, testpath + ".lnk") sh.remove(f"{testpath_env}.lnk", recursive=True) assert not os.path.exists(testpath + ".lnk") assert os.path.exists(f"{testpath}/donttouch") os.remove(f"{testpath}/donttouch") os.rmdir(testpath)
def test_remove_force2(): sh.remove("NOTEXIST.txt", force=True)
def test_remove_force1(): with pytest.raises(FileNotFoundError): sh.remove("NOTEXIST.txt", force=False)