How to use safe_rmdir method in autotest

Best Python code snippet using autotest_python

test_posixpath.py

Source:test_posixpath.py Github

copy

Full Screen

...13 """14 found_backslash = '\\' in ABSTFN15 msg = "ABSTFN is not a posix path - tests fail"16 return [test, unittest.skip(msg)(test)][found_backslash]17def safe_rmdir(dirname):18 try:19 os.rmdir(dirname)20 except OSError:21 pass22class PosixPathTest(unittest.TestCase):23 def setUp(self):24 self.tearDown()25 def tearDown(self):26 for suffix in ["", "1", "2"]:27 test_support.unlink(test_support.TESTFN + suffix)28 safe_rmdir(test_support.TESTFN + suffix)29 def test_join(self):30 self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), "/bar/baz")31 self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz")32 self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), "/foo/bar/baz/")33 def test_split(self):34 self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))35 self.assertEqual(posixpath.split("/"), ("/", ""))36 self.assertEqual(posixpath.split("foo"), ("", "foo"))37 self.assertEqual(posixpath.split("////foo"), ("////", "foo"))38 self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar"))39 def splitextTest(self, path, filename, ext):40 self.assertEqual(posixpath.splitext(path), (filename, ext))41 self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext))42 self.assertEqual(posixpath.splitext("abc/" + path), ("abc/" + filename, ext))43 self.assertEqual(posixpath.splitext("abc.def/" + path), ("abc.def/" + filename, ext))44 self.assertEqual(posixpath.splitext("/abc.def/" + path), ("/abc.def/" + filename, ext))45 self.assertEqual(posixpath.splitext(path + "/"), (filename + ext + "/", ""))46 def test_splitext(self):47 self.splitextTest("foo.bar", "foo", ".bar")48 self.splitextTest("foo.boo.bar", "foo.boo", ".bar")49 self.splitextTest("foo.boo.biff.bar", "foo.boo.biff", ".bar")50 self.splitextTest(".csh.rc", ".csh", ".rc")51 self.splitextTest("nodots", "nodots", "")52 self.splitextTest(".cshrc", ".cshrc", "")53 self.splitextTest("...manydots", "...manydots", "")54 self.splitextTest("...manydots.ext", "...manydots", ".ext")55 self.splitextTest(".", ".", "")56 self.splitextTest("..", "..", "")57 self.splitextTest("........", "........", "")58 self.splitextTest("", "", "")59 def test_isabs(self):60 self.assertIs(posixpath.isabs(""), False)61 self.assertIs(posixpath.isabs("/"), True)62 self.assertIs(posixpath.isabs("/foo"), True)63 self.assertIs(posixpath.isabs("/foo/bar"), True)64 self.assertIs(posixpath.isabs("foo/bar"), False)65 def test_basename(self):66 self.assertEqual(posixpath.basename("/foo/bar"), "bar")67 self.assertEqual(posixpath.basename("/"), "")68 self.assertEqual(posixpath.basename("foo"), "foo")69 self.assertEqual(posixpath.basename("////foo"), "foo")70 self.assertEqual(posixpath.basename("//foo//bar"), "bar")71 def test_dirname(self):72 self.assertEqual(posixpath.dirname("/foo/bar"), "/foo")73 self.assertEqual(posixpath.dirname("/"), "/")74 self.assertEqual(posixpath.dirname("foo"), "")75 self.assertEqual(posixpath.dirname("////foo"), "////")76 self.assertEqual(posixpath.dirname("//foo//bar"), "//foo")77 def test_islink(self):78 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)79 f = open(test_support.TESTFN + "1", "wb")80 try:81 f.write("foo")82 f.close()83 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)84 if hasattr(os, "symlink"):85 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")86 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)87 os.remove(test_support.TESTFN + "1")88 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)89 self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False)90 self.assertIs(posixpath.lexists(test_support.TESTFN + "2"), True)91 finally:92 if not f.close():93 f.close()94 def test_samefile(self):95 f = open(test_support.TESTFN + "1", "wb")96 try:97 f.write("foo")98 f.close()99 self.assertIs(100 posixpath.samefile(101 test_support.TESTFN + "1",102 test_support.TESTFN + "1"103 ),104 True105 )106 # If we don't have links, assume that os.stat doesn't return107 # reasonable inode information and thus, that samefile() doesn't108 # work.109 if hasattr(os, "symlink"):110 os.symlink(111 test_support.TESTFN + "1",112 test_support.TESTFN + "2"113 )114 self.assertIs(115 posixpath.samefile(116 test_support.TESTFN + "1",117 test_support.TESTFN + "2"118 ),119 True120 )121 os.remove(test_support.TESTFN + "2")122 f = open(test_support.TESTFN + "2", "wb")123 f.write("bar")124 f.close()125 self.assertIs(126 posixpath.samefile(127 test_support.TESTFN + "1",128 test_support.TESTFN + "2"129 ),130 False131 )132 finally:133 if not f.close():134 f.close()135 def test_samestat(self):136 f = open(test_support.TESTFN + "1", "wb")137 try:138 f.write("foo")139 f.close()140 self.assertIs(141 posixpath.samestat(142 os.stat(test_support.TESTFN + "1"),143 os.stat(test_support.TESTFN + "1")144 ),145 True146 )147 # If we don't have links, assume that os.stat() doesn't return148 # reasonable inode information and thus, that samestat() doesn't149 # work.150 if hasattr(os, "symlink"):151 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")152 self.assertIs(153 posixpath.samestat(154 os.stat(test_support.TESTFN + "1"),155 os.stat(test_support.TESTFN + "2")156 ),157 True158 )159 os.remove(test_support.TESTFN + "2")160 f = open(test_support.TESTFN + "2", "wb")161 f.write("bar")162 f.close()163 self.assertIs(164 posixpath.samestat(165 os.stat(test_support.TESTFN + "1"),166 os.stat(test_support.TESTFN + "2")167 ),168 False169 )170 finally:171 if not f.close():172 f.close()173 def test_ismount(self):174 self.assertIs(posixpath.ismount("/"), True)175 def test_expanduser(self):176 self.assertEqual(posixpath.expanduser("foo"), "foo")177 try:178 import pwd179 except ImportError:180 pass181 else:182 self.assertIsInstance(posixpath.expanduser("~/"), basestring)183 # if home directory == root directory, this test makes no sense184 if posixpath.expanduser("~") != '/':185 self.assertEqual(186 posixpath.expanduser("~") + "/",187 posixpath.expanduser("~/")188 )189 self.assertIsInstance(posixpath.expanduser("~root/"), basestring)190 self.assertIsInstance(posixpath.expanduser("~foo/"), basestring)191 with test_support.EnvironmentVarGuard() as env:192 env['HOME'] = '/'193 self.assertEqual(posixpath.expanduser("~"), "/")194 self.assertEqual(posixpath.expanduser("~/foo"), "/foo")195 def test_normpath(self):196 self.assertEqual(posixpath.normpath(""), ".")197 self.assertEqual(posixpath.normpath("/"), "/")198 self.assertEqual(posixpath.normpath("//"), "//")199 self.assertEqual(posixpath.normpath("///"), "/")200 self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar")201 self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")202 self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")203 @skip_if_ABSTFN_contains_backslash204 def test_realpath_curdir(self):205 self.assertEqual(realpath('.'), os.getcwd())206 self.assertEqual(realpath('./.'), os.getcwd())207 self.assertEqual(realpath('/'.join(['.'] * 100)), os.getcwd())208 @skip_if_ABSTFN_contains_backslash209 def test_realpath_pardir(self):210 self.assertEqual(realpath('..'), dirname(os.getcwd()))211 self.assertEqual(realpath('../..'), dirname(dirname(os.getcwd())))212 self.assertEqual(realpath('/'.join(['..'] * 100)), '/')213 if hasattr(os, "symlink"):214 def test_realpath_basic(self):215 # Basic operation.216 try:217 os.symlink(ABSTFN+"1", ABSTFN)218 self.assertEqual(realpath(ABSTFN), ABSTFN+"1")219 finally:220 test_support.unlink(ABSTFN)221 def test_realpath_symlink_loops(self):222 # Bug #930024, return the path unchanged if we get into an infinite223 # symlink loop.224 try:225 old_path = abspath('.')226 os.symlink(ABSTFN, ABSTFN)227 self.assertEqual(realpath(ABSTFN), ABSTFN)228 os.symlink(ABSTFN+"1", ABSTFN+"2")229 os.symlink(ABSTFN+"2", ABSTFN+"1")230 self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1")231 self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2")232 self.assertEqual(realpath(ABSTFN+"1/x"), ABSTFN+"1/x")233 self.assertEqual(realpath(ABSTFN+"1/.."), dirname(ABSTFN))234 self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x")235 os.symlink(ABSTFN+"x", ABSTFN+"y")236 self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"),237 ABSTFN + "y")238 self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"),239 ABSTFN + "1")240 os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a")241 self.assertEqual(realpath(ABSTFN+"a"), ABSTFN+"a/b")242 os.symlink("../" + basename(dirname(ABSTFN)) + "/" +243 basename(ABSTFN) + "c", ABSTFN+"c")244 self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c")245 # Test using relative path as well.246 os.chdir(dirname(ABSTFN))247 self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)248 finally:249 os.chdir(old_path)250 test_support.unlink(ABSTFN)251 test_support.unlink(ABSTFN+"1")252 test_support.unlink(ABSTFN+"2")253 test_support.unlink(ABSTFN+"y")254 test_support.unlink(ABSTFN+"c")255 test_support.unlink(ABSTFN+"a")256 def test_realpath_repeated_indirect_symlinks(self):257 # Issue #6975.258 try:259 os.mkdir(ABSTFN)260 os.symlink('../' + basename(ABSTFN), ABSTFN + '/self')261 os.symlink('self/self/self', ABSTFN + '/link')262 self.assertEqual(realpath(ABSTFN + '/link'), ABSTFN)263 finally:264 test_support.unlink(ABSTFN + '/self')265 test_support.unlink(ABSTFN + '/link')266 safe_rmdir(ABSTFN)267 def test_realpath_deep_recursion(self):268 depth = 10269 old_path = abspath('.')270 try:271 os.mkdir(ABSTFN)272 for i in range(depth):273 os.symlink('/'.join(['%d' % i] * 10), ABSTFN + '/%d' % (i + 1))274 os.symlink('.', ABSTFN + '/0')275 self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN)276 # Test using relative path as well.277 os.chdir(ABSTFN)278 self.assertEqual(realpath('%d' % depth), ABSTFN)279 finally:280 os.chdir(old_path)281 for i in range(depth + 1):282 test_support.unlink(ABSTFN + '/%d' % i)283 safe_rmdir(ABSTFN)284 def test_realpath_resolve_parents(self):285 # We also need to resolve any symlinks in the parents of a relative286 # path passed to realpath. E.g.: current working directory is287 # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call288 # realpath("a"). This should return /usr/share/doc/a/.289 try:290 old_path = abspath('.')291 os.mkdir(ABSTFN)292 os.mkdir(ABSTFN + "/y")293 os.symlink(ABSTFN + "/y", ABSTFN + "/k")294 os.chdir(ABSTFN + "/k")295 self.assertEqual(realpath("a"), ABSTFN + "/y/a")296 finally:297 os.chdir(old_path)298 test_support.unlink(ABSTFN + "/k")299 safe_rmdir(ABSTFN + "/y")300 safe_rmdir(ABSTFN)301 def test_realpath_resolve_before_normalizing(self):302 # Bug #990669: Symbolic links should be resolved before we303 # normalize the path. E.g.: if we have directories 'a', 'k' and 'y'304 # in the following hierarchy:305 # a/k/y306 #307 # and a symbolic link 'link-y' pointing to 'y' in directory 'a',308 # then realpath("link-y/..") should return 'k', not 'a'.309 try:310 old_path = abspath('.')311 os.mkdir(ABSTFN)312 os.mkdir(ABSTFN + "/k")313 os.mkdir(ABSTFN + "/k/y")314 os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y")315 # Absolute path.316 self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k")317 # Relative path.318 os.chdir(dirname(ABSTFN))319 self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."),320 ABSTFN + "/k")321 finally:322 os.chdir(old_path)323 test_support.unlink(ABSTFN + "/link-y")324 safe_rmdir(ABSTFN + "/k/y")325 safe_rmdir(ABSTFN + "/k")326 safe_rmdir(ABSTFN)327 def test_realpath_resolve_first(self):328 # Bug #1213894: The first component of the path, if not absolute,329 # must be resolved too.330 try:331 old_path = abspath('.')332 os.mkdir(ABSTFN)333 os.mkdir(ABSTFN + "/k")334 os.symlink(ABSTFN, ABSTFN + "link")335 os.chdir(dirname(ABSTFN))336 base = basename(ABSTFN)337 self.assertEqual(realpath(base + "link"), ABSTFN)338 self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")339 finally:340 os.chdir(old_path)341 test_support.unlink(ABSTFN + "link")342 safe_rmdir(ABSTFN + "/k")343 safe_rmdir(ABSTFN)344 def test_relpath(self):345 (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")346 try:347 curdir = os.path.split(os.getcwd())[-1]348 self.assertRaises(ValueError, posixpath.relpath, "")349 self.assertEqual(posixpath.relpath("a"), "a")350 self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a")351 self.assertEqual(posixpath.relpath("a/b"), "a/b")352 self.assertEqual(posixpath.relpath("../a/b"), "../a/b")353 self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a")354 self.assertEqual(posixpath.relpath("a/b", "../c"), "../"+curdir+"/a/b")355 self.assertEqual(posixpath.relpath("a", "b/c"), "../../a")356 self.assertEqual(posixpath.relpath("a", "a"), ".")357 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat')...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful