ยปCore Development>Code coverage>Lib/test/test_posixpath.py

Python code coverage for Lib/test/test_posixpath.py

#countcontent
1n/aimport os
2n/aimport posixpath
3n/aimport unittest
4n/aimport warnings
5n/afrom posixpath import realpath, abspath, dirname, basename
6n/afrom test import support, test_genericpath
7n/a
8n/atry:
9n/a import posix
10n/aexcept ImportError:
11n/a posix = None
12n/a
13n/a# An absolute path to a temporary filename for testing. We can't rely on TESTFN
14n/a# being an absolute path, so we need this.
15n/a
16n/aABSTFN = abspath(support.TESTFN)
17n/a
18n/adef skip_if_ABSTFN_contains_backslash(test):
19n/a """
20n/a On Windows, posixpath.abspath still returns paths with backslashes
21n/a instead of posix forward slashes. If this is the case, several tests
22n/a fail, so skip them.
23n/a """
24n/a found_backslash = '\\' in ABSTFN
25n/a msg = "ABSTFN is not a posix path - tests fail"
26n/a return [test, unittest.skip(msg)(test)][found_backslash]
27n/a
28n/adef safe_rmdir(dirname):
29n/a try:
30n/a os.rmdir(dirname)
31n/a except OSError:
32n/a pass
33n/a
34n/aclass PosixPathTest(unittest.TestCase):
35n/a
36n/a def setUp(self):
37n/a self.tearDown()
38n/a
39n/a def tearDown(self):
40n/a for suffix in ["", "1", "2"]:
41n/a support.unlink(support.TESTFN + suffix)
42n/a safe_rmdir(support.TESTFN + suffix)
43n/a
44n/a def test_join(self):
45n/a self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"),
46n/a "/bar/baz")
47n/a self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz")
48n/a self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"),
49n/a "/foo/bar/baz/")
50n/a
51n/a self.assertEqual(posixpath.join(b"/foo", b"bar", b"/bar", b"baz"),
52n/a b"/bar/baz")
53n/a self.assertEqual(posixpath.join(b"/foo", b"bar", b"baz"),
54n/a b"/foo/bar/baz")
55n/a self.assertEqual(posixpath.join(b"/foo/", b"bar/", b"baz/"),
56n/a b"/foo/bar/baz/")
57n/a
58n/a def test_split(self):
59n/a self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))
60n/a self.assertEqual(posixpath.split("/"), ("/", ""))
61n/a self.assertEqual(posixpath.split("foo"), ("", "foo"))
62n/a self.assertEqual(posixpath.split("////foo"), ("////", "foo"))
63n/a self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar"))
64n/a
65n/a self.assertEqual(posixpath.split(b"/foo/bar"), (b"/foo", b"bar"))
66n/a self.assertEqual(posixpath.split(b"/"), (b"/", b""))
67n/a self.assertEqual(posixpath.split(b"foo"), (b"", b"foo"))
68n/a self.assertEqual(posixpath.split(b"////foo"), (b"////", b"foo"))
69n/a self.assertEqual(posixpath.split(b"//foo//bar"), (b"//foo", b"bar"))
70n/a
71n/a def splitextTest(self, path, filename, ext):
72n/a self.assertEqual(posixpath.splitext(path), (filename, ext))
73n/a self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext))
74n/a self.assertEqual(posixpath.splitext("abc/" + path),
75n/a ("abc/" + filename, ext))
76n/a self.assertEqual(posixpath.splitext("abc.def/" + path),
77n/a ("abc.def/" + filename, ext))
78n/a self.assertEqual(posixpath.splitext("/abc.def/" + path),
79n/a ("/abc.def/" + filename, ext))
80n/a self.assertEqual(posixpath.splitext(path + "/"),
81n/a (filename + ext + "/", ""))
82n/a
83n/a path = bytes(path, "ASCII")
84n/a filename = bytes(filename, "ASCII")
85n/a ext = bytes(ext, "ASCII")
86n/a
87n/a self.assertEqual(posixpath.splitext(path), (filename, ext))
88n/a self.assertEqual(posixpath.splitext(b"/" + path),
89n/a (b"/" + filename, ext))
90n/a self.assertEqual(posixpath.splitext(b"abc/" + path),
91n/a (b"abc/" + filename, ext))
92n/a self.assertEqual(posixpath.splitext(b"abc.def/" + path),
93n/a (b"abc.def/" + filename, ext))
94n/a self.assertEqual(posixpath.splitext(b"/abc.def/" + path),
95n/a (b"/abc.def/" + filename, ext))
96n/a self.assertEqual(posixpath.splitext(path + b"/"),
97n/a (filename + ext + b"/", b""))
98n/a
99n/a def test_splitext(self):
100n/a self.splitextTest("foo.bar", "foo", ".bar")
101n/a self.splitextTest("foo.boo.bar", "foo.boo", ".bar")
102n/a self.splitextTest("foo.boo.biff.bar", "foo.boo.biff", ".bar")
103n/a self.splitextTest(".csh.rc", ".csh", ".rc")
104n/a self.splitextTest("nodots", "nodots", "")
105n/a self.splitextTest(".cshrc", ".cshrc", "")
106n/a self.splitextTest("...manydots", "...manydots", "")
107n/a self.splitextTest("...manydots.ext", "...manydots", ".ext")
108n/a self.splitextTest(".", ".", "")
109n/a self.splitextTest("..", "..", "")
110n/a self.splitextTest("........", "........", "")
111n/a self.splitextTest("", "", "")
112n/a
113n/a def test_isabs(self):
114n/a self.assertIs(posixpath.isabs(""), False)
115n/a self.assertIs(posixpath.isabs("/"), True)
116n/a self.assertIs(posixpath.isabs("/foo"), True)
117n/a self.assertIs(posixpath.isabs("/foo/bar"), True)
118n/a self.assertIs(posixpath.isabs("foo/bar"), False)
119n/a
120n/a self.assertIs(posixpath.isabs(b""), False)
121n/a self.assertIs(posixpath.isabs(b"/"), True)
122n/a self.assertIs(posixpath.isabs(b"/foo"), True)
123n/a self.assertIs(posixpath.isabs(b"/foo/bar"), True)
124n/a self.assertIs(posixpath.isabs(b"foo/bar"), False)
125n/a
126n/a def test_basename(self):
127n/a self.assertEqual(posixpath.basename("/foo/bar"), "bar")
128n/a self.assertEqual(posixpath.basename("/"), "")
129n/a self.assertEqual(posixpath.basename("foo"), "foo")
130n/a self.assertEqual(posixpath.basename("////foo"), "foo")
131n/a self.assertEqual(posixpath.basename("//foo//bar"), "bar")
132n/a
133n/a self.assertEqual(posixpath.basename(b"/foo/bar"), b"bar")
134n/a self.assertEqual(posixpath.basename(b"/"), b"")
135n/a self.assertEqual(posixpath.basename(b"foo"), b"foo")
136n/a self.assertEqual(posixpath.basename(b"////foo"), b"foo")
137n/a self.assertEqual(posixpath.basename(b"//foo//bar"), b"bar")
138n/a
139n/a def test_dirname(self):
140n/a self.assertEqual(posixpath.dirname("/foo/bar"), "/foo")
141n/a self.assertEqual(posixpath.dirname("/"), "/")
142n/a self.assertEqual(posixpath.dirname("foo"), "")
143n/a self.assertEqual(posixpath.dirname("////foo"), "////")
144n/a self.assertEqual(posixpath.dirname("//foo//bar"), "//foo")
145n/a
146n/a self.assertEqual(posixpath.dirname(b"/foo/bar"), b"/foo")
147n/a self.assertEqual(posixpath.dirname(b"/"), b"/")
148n/a self.assertEqual(posixpath.dirname(b"foo"), b"")
149n/a self.assertEqual(posixpath.dirname(b"////foo"), b"////")
150n/a self.assertEqual(posixpath.dirname(b"//foo//bar"), b"//foo")
151n/a
152n/a def test_islink(self):
153n/a self.assertIs(posixpath.islink(support.TESTFN + "1"), False)
154n/a self.assertIs(posixpath.lexists(support.TESTFN + "2"), False)
155n/a f = open(support.TESTFN + "1", "wb")
156n/a try:
157n/a f.write(b"foo")
158n/a f.close()
159n/a self.assertIs(posixpath.islink(support.TESTFN + "1"), False)
160n/a if support.can_symlink():
161n/a os.symlink(support.TESTFN + "1", support.TESTFN + "2")
162n/a self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
163n/a os.remove(support.TESTFN + "1")
164n/a self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
165n/a self.assertIs(posixpath.exists(support.TESTFN + "2"), False)
166n/a self.assertIs(posixpath.lexists(support.TESTFN + "2"), True)
167n/a finally:
168n/a if not f.close():
169n/a f.close()
170n/a
171n/a def test_ismount(self):
172n/a self.assertIs(posixpath.ismount("/"), True)
173n/a with warnings.catch_warnings():
174n/a warnings.simplefilter("ignore", DeprecationWarning)
175n/a self.assertIs(posixpath.ismount(b"/"), True)
176n/a
177n/a def test_ismount_non_existent(self):
178n/a # Non-existent mountpoint.
179n/a self.assertIs(posixpath.ismount(ABSTFN), False)
180n/a try:
181n/a os.mkdir(ABSTFN)
182n/a self.assertIs(posixpath.ismount(ABSTFN), False)
183n/a finally:
184n/a safe_rmdir(ABSTFN)
185n/a
186n/a @unittest.skipUnless(support.can_symlink(),
187n/a "Test requires symlink support")
188n/a def test_ismount_symlinks(self):
189n/a # Symlinks are never mountpoints.
190n/a try:
191n/a os.symlink("/", ABSTFN)
192n/a self.assertIs(posixpath.ismount(ABSTFN), False)
193n/a finally:
194n/a os.unlink(ABSTFN)
195n/a
196n/a @unittest.skipIf(posix is None, "Test requires posix module")
197n/a def test_ismount_different_device(self):
198n/a # Simulate the path being on a different device from its parent by
199n/a # mocking out st_dev.
200n/a save_lstat = os.lstat
201n/a def fake_lstat(path):
202n/a st_ino = 0
203n/a st_dev = 0
204n/a if path == ABSTFN:
205n/a st_dev = 1
206n/a st_ino = 1
207n/a return posix.stat_result((0, st_ino, st_dev, 0, 0, 0, 0, 0, 0, 0))
208n/a try:
209n/a os.lstat = fake_lstat
210n/a self.assertIs(posixpath.ismount(ABSTFN), True)
211n/a finally:
212n/a os.lstat = save_lstat
213n/a
214n/a @unittest.skipIf(posix is None, "Test requires posix module")
215n/a def test_ismount_directory_not_readable(self):
216n/a # issue #2466: Simulate ismount run on a directory that is not
217n/a # readable, which used to return False.
218n/a save_lstat = os.lstat
219n/a def fake_lstat(path):
220n/a st_ino = 0
221n/a st_dev = 0
222n/a if path.startswith(ABSTFN) and path != ABSTFN:
223n/a # ismount tries to read something inside the ABSTFN directory;
224n/a # simulate this being forbidden (no read permission).
225n/a raise OSError("Fake [Errno 13] Permission denied")
226n/a if path == ABSTFN:
227n/a st_dev = 1
228n/a st_ino = 1
229n/a return posix.stat_result((0, st_ino, st_dev, 0, 0, 0, 0, 0, 0, 0))
230n/a try:
231n/a os.lstat = fake_lstat
232n/a self.assertIs(posixpath.ismount(ABSTFN), True)
233n/a finally:
234n/a os.lstat = save_lstat
235n/a
236n/a def test_expanduser(self):
237n/a self.assertEqual(posixpath.expanduser("foo"), "foo")
238n/a self.assertEqual(posixpath.expanduser(b"foo"), b"foo")
239n/a with support.EnvironmentVarGuard() as env:
240n/a for home in '/', '', '//', '///':
241n/a with self.subTest(home=home):
242n/a env['HOME'] = home
243n/a self.assertEqual(posixpath.expanduser("~"), "/")
244n/a self.assertEqual(posixpath.expanduser("~/"), "/")
245n/a self.assertEqual(posixpath.expanduser("~/foo"), "/foo")
246n/a try:
247n/a import pwd
248n/a except ImportError:
249n/a pass
250n/a else:
251n/a self.assertIsInstance(posixpath.expanduser("~/"), str)
252n/a self.assertIsInstance(posixpath.expanduser(b"~/"), bytes)
253n/a # if home directory == root directory, this test makes no sense
254n/a if posixpath.expanduser("~") != '/':
255n/a self.assertEqual(
256n/a posixpath.expanduser("~") + "/",
257n/a posixpath.expanduser("~/")
258n/a )
259n/a self.assertEqual(
260n/a posixpath.expanduser(b"~") + b"/",
261n/a posixpath.expanduser(b"~/")
262n/a )
263n/a self.assertIsInstance(posixpath.expanduser("~root/"), str)
264n/a self.assertIsInstance(posixpath.expanduser("~foo/"), str)
265n/a self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes)
266n/a self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes)
267n/a
268n/a with support.EnvironmentVarGuard() as env:
269n/a # expanduser should fall back to using the password database
270n/a del env['HOME']
271n/a home = pwd.getpwuid(os.getuid()).pw_dir
272n/a # $HOME can end with a trailing /, so strip it (see #17809)
273n/a home = home.rstrip("/") or '/'
274n/a self.assertEqual(posixpath.expanduser("~"), home)
275n/a
276n/a def test_normpath(self):
277n/a self.assertEqual(posixpath.normpath(""), ".")
278n/a self.assertEqual(posixpath.normpath("/"), "/")
279n/a self.assertEqual(posixpath.normpath("//"), "//")
280n/a self.assertEqual(posixpath.normpath("///"), "/")
281n/a self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar")
282n/a self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"),
283n/a "/foo/baz")
284n/a self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
285n/a
286n/a self.assertEqual(posixpath.normpath(b""), b".")
287n/a self.assertEqual(posixpath.normpath(b"/"), b"/")
288n/a self.assertEqual(posixpath.normpath(b"//"), b"//")
289n/a self.assertEqual(posixpath.normpath(b"///"), b"/")
290n/a self.assertEqual(posixpath.normpath(b"///foo/.//bar//"), b"/foo/bar")
291n/a self.assertEqual(posixpath.normpath(b"///foo/.//bar//.//..//.//baz"),
292n/a b"/foo/baz")
293n/a self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"),
294n/a b"/foo/bar")
295n/a
296n/a @skip_if_ABSTFN_contains_backslash
297n/a def test_realpath_curdir(self):
298n/a self.assertEqual(realpath('.'), os.getcwd())
299n/a self.assertEqual(realpath('./.'), os.getcwd())
300n/a self.assertEqual(realpath('/'.join(['.'] * 100)), os.getcwd())
301n/a
302n/a self.assertEqual(realpath(b'.'), os.getcwdb())
303n/a self.assertEqual(realpath(b'./.'), os.getcwdb())
304n/a self.assertEqual(realpath(b'/'.join([b'.'] * 100)), os.getcwdb())
305n/a
306n/a @skip_if_ABSTFN_contains_backslash
307n/a def test_realpath_pardir(self):
308n/a self.assertEqual(realpath('..'), dirname(os.getcwd()))
309n/a self.assertEqual(realpath('../..'), dirname(dirname(os.getcwd())))
310n/a self.assertEqual(realpath('/'.join(['..'] * 100)), '/')
311n/a
312n/a self.assertEqual(realpath(b'..'), dirname(os.getcwdb()))
313n/a self.assertEqual(realpath(b'../..'), dirname(dirname(os.getcwdb())))
314n/a self.assertEqual(realpath(b'/'.join([b'..'] * 100)), b'/')
315n/a
316n/a @unittest.skipUnless(hasattr(os, "symlink"),
317n/a "Missing symlink implementation")
318n/a @skip_if_ABSTFN_contains_backslash
319n/a def test_realpath_basic(self):
320n/a # Basic operation.
321n/a try:
322n/a os.symlink(ABSTFN+"1", ABSTFN)
323n/a self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
324n/a finally:
325n/a support.unlink(ABSTFN)
326n/a
327n/a @unittest.skipUnless(hasattr(os, "symlink"),
328n/a "Missing symlink implementation")
329n/a @skip_if_ABSTFN_contains_backslash
330n/a def test_realpath_relative(self):
331n/a try:
332n/a os.symlink(posixpath.relpath(ABSTFN+"1"), ABSTFN)
333n/a self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
334n/a finally:
335n/a support.unlink(ABSTFN)
336n/a
337n/a @unittest.skipUnless(hasattr(os, "symlink"),
338n/a "Missing symlink implementation")
339n/a @skip_if_ABSTFN_contains_backslash
340n/a def test_realpath_symlink_loops(self):
341n/a # Bug #930024, return the path unchanged if we get into an infinite
342n/a # symlink loop.
343n/a try:
344n/a os.symlink(ABSTFN, ABSTFN)
345n/a self.assertEqual(realpath(ABSTFN), ABSTFN)
346n/a
347n/a os.symlink(ABSTFN+"1", ABSTFN+"2")
348n/a os.symlink(ABSTFN+"2", ABSTFN+"1")
349n/a self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1")
350n/a self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2")
351n/a
352n/a self.assertEqual(realpath(ABSTFN+"1/x"), ABSTFN+"1/x")
353n/a self.assertEqual(realpath(ABSTFN+"1/.."), dirname(ABSTFN))
354n/a self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x")
355n/a os.symlink(ABSTFN+"x", ABSTFN+"y")
356n/a self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"),
357n/a ABSTFN + "y")
358n/a self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"),
359n/a ABSTFN + "1")
360n/a
361n/a os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a")
362n/a self.assertEqual(realpath(ABSTFN+"a"), ABSTFN+"a/b")
363n/a
364n/a os.symlink("../" + basename(dirname(ABSTFN)) + "/" +
365n/a basename(ABSTFN) + "c", ABSTFN+"c")
366n/a self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c")
367n/a
368n/a # Test using relative path as well.
369n/a with support.change_cwd(dirname(ABSTFN)):
370n/a self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
371n/a finally:
372n/a support.unlink(ABSTFN)
373n/a support.unlink(ABSTFN+"1")
374n/a support.unlink(ABSTFN+"2")
375n/a support.unlink(ABSTFN+"y")
376n/a support.unlink(ABSTFN+"c")
377n/a support.unlink(ABSTFN+"a")
378n/a
379n/a @unittest.skipUnless(hasattr(os, "symlink"),
380n/a "Missing symlink implementation")
381n/a @skip_if_ABSTFN_contains_backslash
382n/a def test_realpath_repeated_indirect_symlinks(self):
383n/a # Issue #6975.
384n/a try:
385n/a os.mkdir(ABSTFN)
386n/a os.symlink('../' + basename(ABSTFN), ABSTFN + '/self')
387n/a os.symlink('self/self/self', ABSTFN + '/link')
388n/a self.assertEqual(realpath(ABSTFN + '/link'), ABSTFN)
389n/a finally:
390n/a support.unlink(ABSTFN + '/self')
391n/a support.unlink(ABSTFN + '/link')
392n/a safe_rmdir(ABSTFN)
393n/a
394n/a @unittest.skipUnless(hasattr(os, "symlink"),
395n/a "Missing symlink implementation")
396n/a @skip_if_ABSTFN_contains_backslash
397n/a def test_realpath_deep_recursion(self):
398n/a depth = 10
399n/a try:
400n/a os.mkdir(ABSTFN)
401n/a for i in range(depth):
402n/a os.symlink('/'.join(['%d' % i] * 10), ABSTFN + '/%d' % (i + 1))
403n/a os.symlink('.', ABSTFN + '/0')
404n/a self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN)
405n/a
406n/a # Test using relative path as well.
407n/a with support.change_cwd(ABSTFN):
408n/a self.assertEqual(realpath('%d' % depth), ABSTFN)
409n/a finally:
410n/a for i in range(depth + 1):
411n/a support.unlink(ABSTFN + '/%d' % i)
412n/a safe_rmdir(ABSTFN)
413n/a
414n/a @unittest.skipUnless(hasattr(os, "symlink"),
415n/a "Missing symlink implementation")
416n/a @skip_if_ABSTFN_contains_backslash
417n/a def test_realpath_resolve_parents(self):
418n/a # We also need to resolve any symlinks in the parents of a relative
419n/a # path passed to realpath. E.g.: current working directory is
420n/a # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
421n/a # realpath("a"). This should return /usr/share/doc/a/.
422n/a try:
423n/a os.mkdir(ABSTFN)
424n/a os.mkdir(ABSTFN + "/y")
425n/a os.symlink(ABSTFN + "/y", ABSTFN + "/k")
426n/a
427n/a with support.change_cwd(ABSTFN + "/k"):
428n/a self.assertEqual(realpath("a"), ABSTFN + "/y/a")
429n/a finally:
430n/a support.unlink(ABSTFN + "/k")
431n/a safe_rmdir(ABSTFN + "/y")
432n/a safe_rmdir(ABSTFN)
433n/a
434n/a @unittest.skipUnless(hasattr(os, "symlink"),
435n/a "Missing symlink implementation")
436n/a @skip_if_ABSTFN_contains_backslash
437n/a def test_realpath_resolve_before_normalizing(self):
438n/a # Bug #990669: Symbolic links should be resolved before we
439n/a # normalize the path. E.g.: if we have directories 'a', 'k' and 'y'
440n/a # in the following hierarchy:
441n/a # a/k/y
442n/a #
443n/a # and a symbolic link 'link-y' pointing to 'y' in directory 'a',
444n/a # then realpath("link-y/..") should return 'k', not 'a'.
445n/a try:
446n/a os.mkdir(ABSTFN)
447n/a os.mkdir(ABSTFN + "/k")
448n/a os.mkdir(ABSTFN + "/k/y")
449n/a os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y")
450n/a
451n/a # Absolute path.
452n/a self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k")
453n/a # Relative path.
454n/a with support.change_cwd(dirname(ABSTFN)):
455n/a self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."),
456n/a ABSTFN + "/k")
457n/a finally:
458n/a support.unlink(ABSTFN + "/link-y")
459n/a safe_rmdir(ABSTFN + "/k/y")
460n/a safe_rmdir(ABSTFN + "/k")
461n/a safe_rmdir(ABSTFN)
462n/a
463n/a @unittest.skipUnless(hasattr(os, "symlink"),
464n/a "Missing symlink implementation")
465n/a @skip_if_ABSTFN_contains_backslash
466n/a def test_realpath_resolve_first(self):
467n/a # Bug #1213894: The first component of the path, if not absolute,
468n/a # must be resolved too.
469n/a
470n/a try:
471n/a os.mkdir(ABSTFN)
472n/a os.mkdir(ABSTFN + "/k")
473n/a os.symlink(ABSTFN, ABSTFN + "link")
474n/a with support.change_cwd(dirname(ABSTFN)):
475n/a base = basename(ABSTFN)
476n/a self.assertEqual(realpath(base + "link"), ABSTFN)
477n/a self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
478n/a finally:
479n/a support.unlink(ABSTFN + "link")
480n/a safe_rmdir(ABSTFN + "/k")
481n/a safe_rmdir(ABSTFN)
482n/a
483n/a def test_relpath(self):
484n/a (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
485n/a try:
486n/a curdir = os.path.split(os.getcwd())[-1]
487n/a self.assertRaises(ValueError, posixpath.relpath, "")
488n/a self.assertEqual(posixpath.relpath("a"), "a")
489n/a self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a")
490n/a self.assertEqual(posixpath.relpath("a/b"), "a/b")
491n/a self.assertEqual(posixpath.relpath("../a/b"), "../a/b")
492n/a self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a")
493n/a self.assertEqual(posixpath.relpath("a/b", "../c"),
494n/a "../"+curdir+"/a/b")
495n/a self.assertEqual(posixpath.relpath("a", "b/c"), "../../a")
496n/a self.assertEqual(posixpath.relpath("a", "a"), ".")
497n/a self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat')
498n/a self.assertEqual(posixpath.relpath("/foo/bar/bat", "/foo/bar"), 'bat')
499n/a self.assertEqual(posixpath.relpath("/foo/bar/bat", "/"), 'foo/bar/bat')
500n/a self.assertEqual(posixpath.relpath("/", "/foo/bar/bat"), '../../..')
501n/a self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x"), '../foo/bar/bat')
502n/a self.assertEqual(posixpath.relpath("/x", "/foo/bar/bat"), '../../../x')
503n/a self.assertEqual(posixpath.relpath("/", "/"), '.')
504n/a self.assertEqual(posixpath.relpath("/a", "/a"), '.')
505n/a self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.')
506n/a finally:
507n/a os.getcwd = real_getcwd
508n/a
509n/a def test_relpath_bytes(self):
510n/a (real_getcwdb, os.getcwdb) = (os.getcwdb, lambda: br"/home/user/bar")
511n/a try:
512n/a curdir = os.path.split(os.getcwdb())[-1]
513n/a self.assertRaises(ValueError, posixpath.relpath, b"")
514n/a self.assertEqual(posixpath.relpath(b"a"), b"a")
515n/a self.assertEqual(posixpath.relpath(posixpath.abspath(b"a")), b"a")
516n/a self.assertEqual(posixpath.relpath(b"a/b"), b"a/b")
517n/a self.assertEqual(posixpath.relpath(b"../a/b"), b"../a/b")
518n/a self.assertEqual(posixpath.relpath(b"a", b"../b"),
519n/a b"../"+curdir+b"/a")
520n/a self.assertEqual(posixpath.relpath(b"a/b", b"../c"),
521n/a b"../"+curdir+b"/a/b")
522n/a self.assertEqual(posixpath.relpath(b"a", b"b/c"), b"../../a")
523n/a self.assertEqual(posixpath.relpath(b"a", b"a"), b".")
524n/a self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x/y/z"), b'../../../foo/bar/bat')
525n/a self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/foo/bar"), b'bat')
526n/a self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/"), b'foo/bar/bat')
527n/a self.assertEqual(posixpath.relpath(b"/", b"/foo/bar/bat"), b'../../..')
528n/a self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x"), b'../foo/bar/bat')
529n/a self.assertEqual(posixpath.relpath(b"/x", b"/foo/bar/bat"), b'../../../x')
530n/a self.assertEqual(posixpath.relpath(b"/", b"/"), b'.')
531n/a self.assertEqual(posixpath.relpath(b"/a", b"/a"), b'.')
532n/a self.assertEqual(posixpath.relpath(b"/a/b", b"/a/b"), b'.')
533n/a
534n/a self.assertRaises(TypeError, posixpath.relpath, b"bytes", "str")
535n/a self.assertRaises(TypeError, posixpath.relpath, "str", b"bytes")
536n/a finally:
537n/a os.getcwdb = real_getcwdb
538n/a
539n/a def test_commonpath(self):
540n/a def check(paths, expected):
541n/a self.assertEqual(posixpath.commonpath(paths), expected)
542n/a self.assertEqual(posixpath.commonpath([os.fsencode(p) for p in paths]),
543n/a os.fsencode(expected))
544n/a def check_error(exc, paths):
545n/a self.assertRaises(exc, posixpath.commonpath, paths)
546n/a self.assertRaises(exc, posixpath.commonpath,
547n/a [os.fsencode(p) for p in paths])
548n/a
549n/a self.assertRaises(ValueError, posixpath.commonpath, [])
550n/a check_error(ValueError, ['/usr', 'usr'])
551n/a check_error(ValueError, ['usr', '/usr'])
552n/a
553n/a check(['/usr/local'], '/usr/local')
554n/a check(['/usr/local', '/usr/local'], '/usr/local')
555n/a check(['/usr/local/', '/usr/local'], '/usr/local')
556n/a check(['/usr/local/', '/usr/local/'], '/usr/local')
557n/a check(['/usr//local', '//usr/local'], '/usr/local')
558n/a check(['/usr/./local', '/./usr/local'], '/usr/local')
559n/a check(['/', '/dev'], '/')
560n/a check(['/usr', '/dev'], '/')
561n/a check(['/usr/lib/', '/usr/lib/python3'], '/usr/lib')
562n/a check(['/usr/lib/', '/usr/lib64/'], '/usr')
563n/a
564n/a check(['/usr/lib', '/usr/lib64'], '/usr')
565n/a check(['/usr/lib/', '/usr/lib64'], '/usr')
566n/a
567n/a check(['spam'], 'spam')
568n/a check(['spam', 'spam'], 'spam')
569n/a check(['spam', 'alot'], '')
570n/a check(['and/jam', 'and/spam'], 'and')
571n/a check(['and//jam', 'and/spam//'], 'and')
572n/a check(['and/./jam', './and/spam'], 'and')
573n/a check(['and/jam', 'and/spam', 'alot'], '')
574n/a check(['and/jam', 'and/spam', 'and'], 'and')
575n/a
576n/a check([''], '')
577n/a check(['', 'spam/alot'], '')
578n/a check_error(ValueError, ['', '/spam/alot'])
579n/a
580n/a self.assertRaises(TypeError, posixpath.commonpath,
581n/a [b'/usr/lib/', '/usr/lib/python3'])
582n/a self.assertRaises(TypeError, posixpath.commonpath,
583n/a [b'/usr/lib/', 'usr/lib/python3'])
584n/a self.assertRaises(TypeError, posixpath.commonpath,
585n/a [b'usr/lib/', '/usr/lib/python3'])
586n/a self.assertRaises(TypeError, posixpath.commonpath,
587n/a ['/usr/lib/', b'/usr/lib/python3'])
588n/a self.assertRaises(TypeError, posixpath.commonpath,
589n/a ['/usr/lib/', b'usr/lib/python3'])
590n/a self.assertRaises(TypeError, posixpath.commonpath,
591n/a ['usr/lib/', b'/usr/lib/python3'])
592n/a
593n/a
594n/aclass PosixCommonTest(test_genericpath.CommonTest, unittest.TestCase):
595n/a pathmodule = posixpath
596n/a attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat']
597n/a
598n/a
599n/aclass PathLikeTests(unittest.TestCase):
600n/a
601n/a path = posixpath
602n/a
603n/a class PathLike:
604n/a def __init__(self, path=''):
605n/a self.path = path
606n/a def __fspath__(self):
607n/a if isinstance(self.path, BaseException):
608n/a raise self.path
609n/a else:
610n/a return self.path
611n/a
612n/a def setUp(self):
613n/a self.file_name = support.TESTFN.lower()
614n/a self.file_path = self.PathLike(support.TESTFN)
615n/a self.addCleanup(support.unlink, self.file_name)
616n/a with open(self.file_name, 'xb', 0) as file:
617n/a file.write(b"test_posixpath.PathLikeTests")
618n/a
619n/a def assertPathEqual(self, func):
620n/a self.assertEqual(func(self.file_path), func(self.file_name))
621n/a
622n/a def test_path_normcase(self):
623n/a self.assertPathEqual(self.path.normcase)
624n/a
625n/a def test_path_isabs(self):
626n/a self.assertPathEqual(self.path.isabs)
627n/a
628n/a def test_path_join(self):
629n/a self.assertEqual(self.path.join('a', self.PathLike('b'), 'c'),
630n/a self.path.join('a', 'b', 'c'))
631n/a
632n/a def test_path_split(self):
633n/a self.assertPathEqual(self.path.split)
634n/a
635n/a def test_path_splitext(self):
636n/a self.assertPathEqual(self.path.splitext)
637n/a
638n/a def test_path_splitdrive(self):
639n/a self.assertPathEqual(self.path.splitdrive)
640n/a
641n/a def test_path_basename(self):
642n/a self.assertPathEqual(self.path.basename)
643n/a
644n/a def test_path_dirname(self):
645n/a self.assertPathEqual(self.path.dirname)
646n/a
647n/a def test_path_islink(self):
648n/a self.assertPathEqual(self.path.islink)
649n/a
650n/a def test_path_lexists(self):
651n/a self.assertPathEqual(self.path.lexists)
652n/a
653n/a def test_path_ismount(self):
654n/a self.assertPathEqual(self.path.ismount)
655n/a
656n/a def test_path_expanduser(self):
657n/a self.assertPathEqual(self.path.expanduser)
658n/a
659n/a def test_path_expandvars(self):
660n/a self.assertPathEqual(self.path.expandvars)
661n/a
662n/a def test_path_normpath(self):
663n/a self.assertPathEqual(self.path.normpath)
664n/a
665n/a def test_path_abspath(self):
666n/a self.assertPathEqual(self.path.abspath)
667n/a
668n/a def test_path_realpath(self):
669n/a self.assertPathEqual(self.path.realpath)
670n/a
671n/a def test_path_relpath(self):
672n/a self.assertPathEqual(self.path.relpath)
673n/a
674n/a def test_path_commonpath(self):
675n/a common_path = self.path.commonpath([self.file_path, self.file_name])
676n/a self.assertEqual(common_path, self.file_name)
677n/a
678n/a
679n/aif __name__=="__main__":
680n/a unittest.main()