annotate Lib/test/test_genericpath.py @ 84007:dbfbdf2b5c19 3.3

Fix #17967: For ftp urls CWD to target instead of hopping to each directory towards target. This fixes a bug where target is accessible, but parent directories are restricted.
author Senthil Kumaran <senthil@uthcode.com>
date Sat, 01 Jun 2013 08:27:06 -0700
parents 6f71e6aa9041
children 3a7cd8efc19f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
1 """
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
2 Tests common to genericpath, macpath, ntpath and posixpath
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
3 """
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
4
73576
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
5 import genericpath
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
6 import os
60671
0f37afb8a9f2 Issue #8422, test_genericpath: skip the creation of a directory with an invalid
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59470
diff changeset
7 import sys
73576
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
8 import unittest
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
9 import warnings
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
10 from test import support
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
11
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
12
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
13 def safe_rmdir(dirname):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
14 try:
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
15 os.rmdir(dirname)
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
16 except OSError:
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
17 pass
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
18
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
19
81342
6f71e6aa9041 #16852: test_genericpath, test_posixpath, test_ntpath, and test_macpath now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81261
diff changeset
20 class GenericTest:
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
21 common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime',
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
22 'getmtime', 'exists', 'isdir', 'isfile']
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
23 attributes = []
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
24
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
25 def test_no_argument(self):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
26 for attr in self.common_attributes + self.attributes:
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
27 with self.assertRaises(TypeError):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
28 getattr(self.pathmodule, attr)()
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
29 raise self.fail("{}.{}() did not raise a TypeError"
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
30 .format(self.pathmodule.__name__, attr))
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
31
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
32 def test_commonprefix(self):
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
33 commonprefix = self.pathmodule.commonprefix
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
34 self.assertEqual(
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
35 commonprefix([]),
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
36 ""
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
37 )
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
38 self.assertEqual(
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
39 commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
40 "/home/swen"
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
41 )
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
42 self.assertEqual(
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
43 commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
44 "/home/swen/"
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
45 )
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
46 self.assertEqual(
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
47 commonprefix(["/home/swen/spam", "/home/swen/spam"]),
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
48 "/home/swen/spam"
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
49 )
59469
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
50 self.assertEqual(
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
51 commonprefix(["home:swenson:spam", "home:swen:spam"]),
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
52 "home:swen"
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
53 )
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
54 self.assertEqual(
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
55 commonprefix([":home:swen:spam", ":home:swen:eggs"]),
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
56 ":home:swen:"
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
57 )
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
58 self.assertEqual(
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
59 commonprefix([":home:swen:spam", ":home:swen:spam"]),
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
60 ":home:swen:spam"
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
61 )
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
62
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
63 self.assertEqual(
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
64 commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]),
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
65 b"/home/swen"
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
66 )
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
67 self.assertEqual(
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
68 commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]),
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
69 b"/home/swen/"
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
70 )
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
71 self.assertEqual(
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
72 commonprefix([b"/home/swen/spam", b"/home/swen/spam"]),
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
73 b"/home/swen/spam"
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
74 )
59469
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
75 self.assertEqual(
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
76 commonprefix([b"home:swenson:spam", b"home:swen:spam"]),
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
77 b"home:swen"
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
78 )
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
79 self.assertEqual(
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
80 commonprefix([b":home:swen:spam", b":home:swen:eggs"]),
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
81 b":home:swen:"
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
82 )
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
83 self.assertEqual(
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
84 commonprefix([b":home:swen:spam", b":home:swen:spam"]),
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
85 b":home:swen:spam"
cdfdefec99ab Merge other tests from test_*path.py into test_genericpath.CommonTest, and do some cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59466
diff changeset
86 )
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
87
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
88 testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
89 'aXc', 'abd', 'ab', 'aX', 'abcX']
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
90 for s1 in testlist:
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
91 for s2 in testlist:
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
92 p = commonprefix([s1, s2])
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
93 self.assertTrue(s1.startswith(p))
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
94 self.assertTrue(s2.startswith(p))
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
95 if s1 != s2:
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
96 n = len(p)
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
97 self.assertNotEqual(s1[n:n+1], s2[n:n+1])
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
98
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
99 def test_getsize(self):
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
100 f = open(support.TESTFN, "wb")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
101 try:
42924
b4b14c4beb82 str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42177
diff changeset
102 f.write(b"foo")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
103 f.close()
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
104 self.assertEqual(self.pathmodule.getsize(support.TESTFN), 3)
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
105 finally:
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
106 if not f.closed:
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
107 f.close()
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
108 support.unlink(support.TESTFN)
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
109
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
110 def test_time(self):
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
111 f = open(support.TESTFN, "wb")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
112 try:
42924
b4b14c4beb82 str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42177
diff changeset
113 f.write(b"foo")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
114 f.close()
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
115 f = open(support.TESTFN, "ab")
42924
b4b14c4beb82 str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42177
diff changeset
116 f.write(b"bar")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
117 f.close()
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
118 f = open(support.TESTFN, "rb")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
119 d = f.read()
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
120 f.close()
42177
8d4b69e31581 Make test_genericpath pass.
Guido van Rossum <guido@python.org>
parents: 39689
diff changeset
121 self.assertEqual(d, b"foobar")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
122
65728
eb50c48d9cb4 Use assertLessEqual to ease diagnosing of failures
Antoine Pitrou <solipsis@pitrou.net>
parents: 55593
diff changeset
123 self.assertLessEqual(
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
124 self.pathmodule.getctime(support.TESTFN),
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
125 self.pathmodule.getmtime(support.TESTFN)
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
126 )
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
127 finally:
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
128 if not f.closed:
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
129 f.close()
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
130 support.unlink(support.TESTFN)
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
131
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
132 def test_exists(self):
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
133 self.assertIs(self.pathmodule.exists(support.TESTFN), False)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
134 f = open(support.TESTFN, "wb")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
135 try:
42924
b4b14c4beb82 str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42177
diff changeset
136 f.write(b"foo")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
137 f.close()
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
138 self.assertIs(self.pathmodule.exists(support.TESTFN), True)
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
139 if not self.pathmodule == genericpath:
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
140 self.assertIs(self.pathmodule.lexists(support.TESTFN),
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
141 True)
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
142 finally:
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
143 if not f.close():
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
144 f.close()
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
145 support.unlink(support.TESTFN)
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
146
77951
62b9bfbc3356 Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
Richard Oudkerk <shibturn@gmail.com>
parents: 73590
diff changeset
147 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
62b9bfbc3356 Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
Richard Oudkerk <shibturn@gmail.com>
parents: 73590
diff changeset
148 def test_exists_fd(self):
62b9bfbc3356 Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
Richard Oudkerk <shibturn@gmail.com>
parents: 73590
diff changeset
149 r, w = os.pipe()
62b9bfbc3356 Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
Richard Oudkerk <shibturn@gmail.com>
parents: 73590
diff changeset
150 try:
62b9bfbc3356 Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
Richard Oudkerk <shibturn@gmail.com>
parents: 73590
diff changeset
151 self.assertTrue(self.pathmodule.exists(r))
62b9bfbc3356 Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
Richard Oudkerk <shibturn@gmail.com>
parents: 73590
diff changeset
152 finally:
62b9bfbc3356 Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
Richard Oudkerk <shibturn@gmail.com>
parents: 73590
diff changeset
153 os.close(r)
62b9bfbc3356 Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
Richard Oudkerk <shibturn@gmail.com>
parents: 73590
diff changeset
154 os.close(w)
62b9bfbc3356 Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
Richard Oudkerk <shibturn@gmail.com>
parents: 73590
diff changeset
155 self.assertFalse(self.pathmodule.exists(r))
62b9bfbc3356 Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
Richard Oudkerk <shibturn@gmail.com>
parents: 73590
diff changeset
156
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
157 def test_isdir(self):
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
158 self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
159 f = open(support.TESTFN, "wb")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
160 try:
42924
b4b14c4beb82 str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42177
diff changeset
161 f.write(b"foo")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
162 f.close()
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
163 self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
164 os.remove(support.TESTFN)
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
165 os.mkdir(support.TESTFN)
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
166 self.assertIs(self.pathmodule.isdir(support.TESTFN), True)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
167 os.rmdir(support.TESTFN)
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
168 finally:
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
169 if not f.close():
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
170 f.close()
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
171 support.unlink(support.TESTFN)
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
172 safe_rmdir(support.TESTFN)
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
173
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
174 def test_isfile(self):
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
175 self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
176 f = open(support.TESTFN, "wb")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
177 try:
42924
b4b14c4beb82 str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42177
diff changeset
178 f.write(b"foo")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
179 f.close()
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
180 self.assertIs(self.pathmodule.isfile(support.TESTFN), True)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
181 os.remove(support.TESTFN)
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
182 os.mkdir(support.TESTFN)
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
183 self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42924
diff changeset
184 os.rmdir(support.TESTFN)
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
185 finally:
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
186 if not f.close():
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
187 f.close()
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
188 support.unlink(support.TESTFN)
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
189 safe_rmdir(support.TESTFN)
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
190
81342
6f71e6aa9041 #16852: test_genericpath, test_posixpath, test_ntpath, and test_macpath now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81261
diff changeset
191 class TestGenericTest(GenericTest, unittest.TestCase):
6f71e6aa9041 #16852: test_genericpath, test_posixpath, test_ntpath, and test_macpath now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81261
diff changeset
192 # Issue 16852: GenericTest can't inherit from unittest.TestCase
6f71e6aa9041 #16852: test_genericpath, test_posixpath, test_ntpath, and test_macpath now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81261
diff changeset
193 # for test discovery purposes; CommonTest inherits from GenericTest
6f71e6aa9041 #16852: test_genericpath, test_posixpath, test_ntpath, and test_macpath now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81261
diff changeset
194 # and is only meant to be inherited by others.
6f71e6aa9041 #16852: test_genericpath, test_posixpath, test_ntpath, and test_macpath now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81261
diff changeset
195 pathmodule = genericpath
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
196
63881
7dc37ae175fc Merged manually from 2.7 branch to 3.x trunk.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 62490
diff changeset
197 # Following TestCase is not supposed to be run from test_genericpath.
7dc37ae175fc Merged manually from 2.7 branch to 3.x trunk.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 62490
diff changeset
198 # It is inherited by other test modules (macpath, ntpath, posixpath).
7dc37ae175fc Merged manually from 2.7 branch to 3.x trunk.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 62490
diff changeset
199
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
200 class CommonTest(GenericTest):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
201 common_attributes = GenericTest.common_attributes + [
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
202 # Properties
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
203 'curdir', 'pardir', 'extsep', 'sep',
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
204 'pathsep', 'defpath', 'altsep', 'devnull',
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
205 # Methods
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
206 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
207 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
208 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
209 ]
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
210
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
211 def test_normcase(self):
62490
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60833
diff changeset
212 normcase = self.pathmodule.normcase
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60833
diff changeset
213 # check that normcase() is idempotent
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60833
diff changeset
214 for p in ["FoO/./BaR", b"FoO/./BaR"]:
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60833
diff changeset
215 p = normcase(p)
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60833
diff changeset
216 self.assertEqual(p, normcase(p))
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
217
62490
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60833
diff changeset
218 self.assertEqual(normcase(''), '')
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60833
diff changeset
219 self.assertEqual(normcase(b''), b'')
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60833
diff changeset
220
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60833
diff changeset
221 # check that normcase raises a TypeError for invalid types
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60833
diff changeset
222 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60833
diff changeset
223 self.assertRaises(TypeError, normcase, path)
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
224
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
225 def test_splitdrive(self):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
226 # splitdrive for non-NT paths
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
227 splitdrive = self.pathmodule.splitdrive
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
228 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
229 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
230 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
231
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
232 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
233 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
234 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
235
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
236 def test_expandvars(self):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
237 if self.pathmodule.__name__ == 'macpath':
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
238 self.skipTest('macpath.expandvars is a stub')
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
239 expandvars = self.pathmodule.expandvars
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
240 with support.EnvironmentVarGuard() as env:
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
241 env.clear()
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
242 env["foo"] = "bar"
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
243 env["{foo"] = "baz1"
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
244 env["{foo}"] = "baz2"
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
245 self.assertEqual(expandvars("foo"), "foo")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
246 self.assertEqual(expandvars("$foo bar"), "bar bar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
247 self.assertEqual(expandvars("${foo}bar"), "barbar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
248 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
249 self.assertEqual(expandvars("$bar bar"), "$bar bar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
250 self.assertEqual(expandvars("$?bar"), "$?bar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
251 self.assertEqual(expandvars("${foo}bar"), "barbar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
252 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
253 self.assertEqual(expandvars("${foo"), "${foo")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
254 self.assertEqual(expandvars("${{foo}}"), "baz1}")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
255 self.assertEqual(expandvars("$foo$foo"), "barbar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
256 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
257
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
258 self.assertEqual(expandvars(b"foo"), b"foo")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
259 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
260 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
261 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
262 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
263 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
264 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
265 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
266 self.assertEqual(expandvars(b"${foo"), b"${foo")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
267 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
268 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
269 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
270
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
271 def test_abspath(self):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
272 self.assertIn("foo", self.pathmodule.abspath("foo"))
73576
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
273 with warnings.catch_warnings():
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
274 warnings.simplefilter("ignore", DeprecationWarning)
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
275 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
276
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
277 # Abspath returns bytes when the arg is bytes
73590
5f239b0ba819 Issue #13374: Deprecate os.getcwdb() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73576
diff changeset
278 with warnings.catch_warnings():
5f239b0ba819 Issue #13374: Deprecate os.getcwdb() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73576
diff changeset
279 warnings.simplefilter("ignore", DeprecationWarning)
5f239b0ba819 Issue #13374: Deprecate os.getcwdb() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73576
diff changeset
280 for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'):
5f239b0ba819 Issue #13374: Deprecate os.getcwdb() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73576
diff changeset
281 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
282
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
283 def test_realpath(self):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
284 self.assertIn("foo", self.pathmodule.realpath("foo"))
73576
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
285 with warnings.catch_warnings():
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
286 warnings.simplefilter("ignore", DeprecationWarning)
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
287 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
288
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
289 def test_normpath_issue5827(self):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
290 # Make sure normpath preserves unicode
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
291 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
292 self.assertIsInstance(self.pathmodule.normpath(path), str)
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
293
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
294 def test_abspath_issue3426(self):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
295 # Check that abspath returns unicode when the arg is unicode
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
296 # with both ASCII and non-ASCII cwds.
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
297 abspath = self.pathmodule.abspath
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
298 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
299 self.assertIsInstance(abspath(path), str)
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
300
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
301 unicwd = '\xe7w\xf0'
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
302 try:
78374
3edc71ed19e7 Issue #15441: Skip test_nonascii_abspath() of test_genericpath on Windows
Victor Stinner <victor.stinner@gmail.com>
parents: 77951
diff changeset
303 os.fsencode(unicwd)
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
304 except (AttributeError, UnicodeEncodeError):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
305 # FS encoding is probably ASCII
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
306 pass
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
307 else:
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
308 with support.temp_cwd(unicwd):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
309 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
310 self.assertIsInstance(abspath(path), str)
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
311
60671
0f37afb8a9f2 Issue #8422, test_genericpath: skip the creation of a directory with an invalid
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59470
diff changeset
312 def test_nonascii_abspath(self):
81260
41658a4fb3cc Issue #16218, #16414, #16444: Backport FS_NONASCII, TESTFN_UNDECODABLE,
Victor Stinner <victor.stinner@gmail.com>
parents: 68257
diff changeset
313 if (support.TESTFN_UNDECODABLE
41658a4fb3cc Issue #16218, #16414, #16444: Backport FS_NONASCII, TESTFN_UNDECODABLE,
Victor Stinner <victor.stinner@gmail.com>
parents: 68257
diff changeset
314 # Mac OS X denies the creation of a directory with an invalid
41658a4fb3cc Issue #16218, #16414, #16444: Backport FS_NONASCII, TESTFN_UNDECODABLE,
Victor Stinner <victor.stinner@gmail.com>
parents: 68257
diff changeset
315 # UTF-8 name. Windows allows to create a directory with an
41658a4fb3cc Issue #16218, #16414, #16444: Backport FS_NONASCII, TESTFN_UNDECODABLE,
Victor Stinner <victor.stinner@gmail.com>
parents: 68257
diff changeset
316 # arbitrary bytes name, but fails to enter this directory
41658a4fb3cc Issue #16218, #16414, #16444: Backport FS_NONASCII, TESTFN_UNDECODABLE,
Victor Stinner <victor.stinner@gmail.com>
parents: 68257
diff changeset
317 # (when the bytes name is used).
41658a4fb3cc Issue #16218, #16414, #16444: Backport FS_NONASCII, TESTFN_UNDECODABLE,
Victor Stinner <victor.stinner@gmail.com>
parents: 68257
diff changeset
318 and sys.platform not in ('win32', 'darwin')):
41658a4fb3cc Issue #16218, #16414, #16444: Backport FS_NONASCII, TESTFN_UNDECODABLE,
Victor Stinner <victor.stinner@gmail.com>
parents: 68257
diff changeset
319 name = support.TESTFN_UNDECODABLE
41658a4fb3cc Issue #16218, #16414, #16444: Backport FS_NONASCII, TESTFN_UNDECODABLE,
Victor Stinner <victor.stinner@gmail.com>
parents: 68257
diff changeset
320 elif support.TESTFN_NONASCII:
41658a4fb3cc Issue #16218, #16414, #16444: Backport FS_NONASCII, TESTFN_UNDECODABLE,
Victor Stinner <victor.stinner@gmail.com>
parents: 68257
diff changeset
321 name = support.TESTFN_NONASCII
41658a4fb3cc Issue #16218, #16414, #16444: Backport FS_NONASCII, TESTFN_UNDECODABLE,
Victor Stinner <victor.stinner@gmail.com>
parents: 68257
diff changeset
322 else:
41658a4fb3cc Issue #16218, #16414, #16444: Backport FS_NONASCII, TESTFN_UNDECODABLE,
Victor Stinner <victor.stinner@gmail.com>
parents: 68257
diff changeset
323 self.skipTest("need support.TESTFN_NONASCII")
41658a4fb3cc Issue #16218, #16414, #16444: Backport FS_NONASCII, TESTFN_UNDECODABLE,
Victor Stinner <victor.stinner@gmail.com>
parents: 68257
diff changeset
324
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
325 # Test non-ASCII, non-UTF8 bytes in the path.
73576
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
326 with warnings.catch_warnings():
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
327 warnings.simplefilter("ignore", DeprecationWarning)
78374
3edc71ed19e7 Issue #15441: Skip test_nonascii_abspath() of test_genericpath on Windows
Victor Stinner <victor.stinner@gmail.com>
parents: 77951
diff changeset
328 with support.temp_cwd(name):
73576
d42811b93357 Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
329 self.test_abspath()
59466
a2f1c15cae6c Merged revisions 78734-78735 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58966
diff changeset
330
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
331
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
diff changeset
332 if __name__=="__main__":
81342
6f71e6aa9041 #16852: test_genericpath, test_posixpath, test_ntpath, and test_macpath now work with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 81261
diff changeset
333 unittest.main()