Serhiy Storchaka | 2e576f5 | 2017-04-24 09:05:00 +0300 | [diff] [blame] | 1 | import collections.abc |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2 | import io |
| 3 | import os |
Przemysław Spodymek | 216b745 | 2018-08-27 23:33:45 +0200 | [diff] [blame] | 4 | import sys |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 5 | import errno |
| 6 | import pathlib |
| 7 | import pickle |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 8 | import socket |
| 9 | import stat |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 10 | import tempfile |
| 11 | import unittest |
Armin Rigo | 22a594a | 2017-04-13 20:08:15 +0200 | [diff] [blame] | 12 | from unittest import mock |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 13 | |
| 14 | from test import support |
Serhiy Storchaka | b21d155 | 2018-03-02 11:53:51 +0200 | [diff] [blame] | 15 | from test.support import TESTFN, FakePath |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 16 | |
| 17 | try: |
| 18 | import grp, pwd |
| 19 | except ImportError: |
| 20 | grp = pwd = None |
| 21 | |
| 22 | |
| 23 | class _BaseFlavourTest(object): |
| 24 | |
| 25 | def _check_parse_parts(self, arg, expected): |
| 26 | f = self.flavour.parse_parts |
| 27 | sep = self.flavour.sep |
| 28 | altsep = self.flavour.altsep |
| 29 | actual = f([x.replace('/', sep) for x in arg]) |
| 30 | self.assertEqual(actual, expected) |
| 31 | if altsep: |
| 32 | actual = f([x.replace('/', altsep) for x in arg]) |
| 33 | self.assertEqual(actual, expected) |
| 34 | |
| 35 | def test_parse_parts_common(self): |
| 36 | check = self._check_parse_parts |
| 37 | sep = self.flavour.sep |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 38 | # Unanchored parts. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 39 | check([], ('', '', [])) |
| 40 | check(['a'], ('', '', ['a'])) |
| 41 | check(['a/'], ('', '', ['a'])) |
| 42 | check(['a', 'b'], ('', '', ['a', 'b'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 43 | # Expansion. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 44 | check(['a/b'], ('', '', ['a', 'b'])) |
| 45 | check(['a/b/'], ('', '', ['a', 'b'])) |
| 46 | check(['a', 'b/c', 'd'], ('', '', ['a', 'b', 'c', 'd'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 47 | # Collapsing and stripping excess slashes. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 48 | check(['a', 'b//c', 'd'], ('', '', ['a', 'b', 'c', 'd'])) |
| 49 | check(['a', 'b/c/', 'd'], ('', '', ['a', 'b', 'c', 'd'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 50 | # Eliminating standalone dots. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 51 | check(['.'], ('', '', [])) |
| 52 | check(['.', '.', 'b'], ('', '', ['b'])) |
| 53 | check(['a', '.', 'b'], ('', '', ['a', 'b'])) |
| 54 | check(['a', '.', '.'], ('', '', ['a'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 55 | # The first part is anchored. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 56 | check(['/a/b'], ('', sep, [sep, 'a', 'b'])) |
| 57 | check(['/a', 'b'], ('', sep, [sep, 'a', 'b'])) |
| 58 | check(['/a/', 'b'], ('', sep, [sep, 'a', 'b'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 59 | # Ignoring parts before an anchored part. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 60 | check(['a', '/b', 'c'], ('', sep, [sep, 'b', 'c'])) |
| 61 | check(['a', '/b', '/c'], ('', sep, [sep, 'c'])) |
| 62 | |
| 63 | |
| 64 | class PosixFlavourTest(_BaseFlavourTest, unittest.TestCase): |
| 65 | flavour = pathlib._posix_flavour |
| 66 | |
| 67 | def test_parse_parts(self): |
| 68 | check = self._check_parse_parts |
| 69 | # Collapsing of excess leading slashes, except for the double-slash |
| 70 | # special case. |
| 71 | check(['//a', 'b'], ('', '//', ['//', 'a', 'b'])) |
| 72 | check(['///a', 'b'], ('', '/', ['/', 'a', 'b'])) |
| 73 | check(['////a', 'b'], ('', '/', ['/', 'a', 'b'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 74 | # Paths which look like NT paths aren't treated specially. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 75 | check(['c:a'], ('', '', ['c:a'])) |
| 76 | check(['c:\\a'], ('', '', ['c:\\a'])) |
| 77 | check(['\\a'], ('', '', ['\\a'])) |
| 78 | |
| 79 | def test_splitroot(self): |
| 80 | f = self.flavour.splitroot |
| 81 | self.assertEqual(f(''), ('', '', '')) |
| 82 | self.assertEqual(f('a'), ('', '', 'a')) |
| 83 | self.assertEqual(f('a/b'), ('', '', 'a/b')) |
| 84 | self.assertEqual(f('a/b/'), ('', '', 'a/b/')) |
| 85 | self.assertEqual(f('/a'), ('', '/', 'a')) |
| 86 | self.assertEqual(f('/a/b'), ('', '/', 'a/b')) |
| 87 | self.assertEqual(f('/a/b/'), ('', '/', 'a/b/')) |
| 88 | # The root is collapsed when there are redundant slashes |
| 89 | # except when there are exactly two leading slashes, which |
| 90 | # is a special case in POSIX. |
| 91 | self.assertEqual(f('//a'), ('', '//', 'a')) |
| 92 | self.assertEqual(f('///a'), ('', '/', 'a')) |
| 93 | self.assertEqual(f('///a/b'), ('', '/', 'a/b')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 94 | # Paths which look like NT paths aren't treated specially. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 95 | self.assertEqual(f('c:/a/b'), ('', '', 'c:/a/b')) |
| 96 | self.assertEqual(f('\\/a/b'), ('', '', '\\/a/b')) |
| 97 | self.assertEqual(f('\\a\\b'), ('', '', '\\a\\b')) |
| 98 | |
| 99 | |
| 100 | class NTFlavourTest(_BaseFlavourTest, unittest.TestCase): |
| 101 | flavour = pathlib._windows_flavour |
| 102 | |
| 103 | def test_parse_parts(self): |
| 104 | check = self._check_parse_parts |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 105 | # First part is anchored. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 106 | check(['c:'], ('c:', '', ['c:'])) |
Antoine Pitrou | 57fffd6 | 2015-02-15 18:03:59 +0100 | [diff] [blame] | 107 | check(['c:/'], ('c:', '\\', ['c:\\'])) |
| 108 | check(['/'], ('', '\\', ['\\'])) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 109 | check(['c:a'], ('c:', '', ['c:', 'a'])) |
Antoine Pitrou | 57fffd6 | 2015-02-15 18:03:59 +0100 | [diff] [blame] | 110 | check(['c:/a'], ('c:', '\\', ['c:\\', 'a'])) |
| 111 | check(['/a'], ('', '\\', ['\\', 'a'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 112 | # UNC paths. |
Antoine Pitrou | 57fffd6 | 2015-02-15 18:03:59 +0100 | [diff] [blame] | 113 | check(['//a/b'], ('\\\\a\\b', '\\', ['\\\\a\\b\\'])) |
| 114 | check(['//a/b/'], ('\\\\a\\b', '\\', ['\\\\a\\b\\'])) |
| 115 | check(['//a/b/c'], ('\\\\a\\b', '\\', ['\\\\a\\b\\', 'c'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 116 | # Second part is anchored, so that the first part is ignored. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 117 | check(['a', 'Z:b', 'c'], ('Z:', '', ['Z:', 'b', 'c'])) |
Antoine Pitrou | 57fffd6 | 2015-02-15 18:03:59 +0100 | [diff] [blame] | 118 | check(['a', 'Z:/b', 'c'], ('Z:', '\\', ['Z:\\', 'b', 'c'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 119 | # UNC paths. |
Antoine Pitrou | 57fffd6 | 2015-02-15 18:03:59 +0100 | [diff] [blame] | 120 | check(['a', '//b/c', 'd'], ('\\\\b\\c', '\\', ['\\\\b\\c\\', 'd'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 121 | # Collapsing and stripping excess slashes. |
Antoine Pitrou | 57fffd6 | 2015-02-15 18:03:59 +0100 | [diff] [blame] | 122 | check(['a', 'Z://b//c/', 'd/'], ('Z:', '\\', ['Z:\\', 'b', 'c', 'd'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 123 | # UNC paths. |
Antoine Pitrou | 57fffd6 | 2015-02-15 18:03:59 +0100 | [diff] [blame] | 124 | check(['a', '//b/c//', 'd'], ('\\\\b\\c', '\\', ['\\\\b\\c\\', 'd'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 125 | # Extended paths. |
Antoine Pitrou | 57fffd6 | 2015-02-15 18:03:59 +0100 | [diff] [blame] | 126 | check(['//?/c:/'], ('\\\\?\\c:', '\\', ['\\\\?\\c:\\'])) |
| 127 | check(['//?/c:/a'], ('\\\\?\\c:', '\\', ['\\\\?\\c:\\', 'a'])) |
| 128 | check(['//?/c:/a', '/b'], ('\\\\?\\c:', '\\', ['\\\\?\\c:\\', 'b'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 129 | # Extended UNC paths (format is "\\?\UNC\server\share"). |
Antoine Pitrou | 57fffd6 | 2015-02-15 18:03:59 +0100 | [diff] [blame] | 130 | check(['//?/UNC/b/c'], ('\\\\?\\UNC\\b\\c', '\\', ['\\\\?\\UNC\\b\\c\\'])) |
| 131 | check(['//?/UNC/b/c/d'], ('\\\\?\\UNC\\b\\c', '\\', ['\\\\?\\UNC\\b\\c\\', 'd'])) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 132 | # Second part has a root but not drive. |
Antoine Pitrou | 57fffd6 | 2015-02-15 18:03:59 +0100 | [diff] [blame] | 133 | check(['a', '/b', 'c'], ('', '\\', ['\\', 'b', 'c'])) |
| 134 | check(['Z:/a', '/b', 'c'], ('Z:', '\\', ['Z:\\', 'b', 'c'])) |
| 135 | check(['//?/Z:/a', '/b', 'c'], ('\\\\?\\Z:', '\\', ['\\\\?\\Z:\\', 'b', 'c'])) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 136 | |
| 137 | def test_splitroot(self): |
| 138 | f = self.flavour.splitroot |
| 139 | self.assertEqual(f(''), ('', '', '')) |
| 140 | self.assertEqual(f('a'), ('', '', 'a')) |
| 141 | self.assertEqual(f('a\\b'), ('', '', 'a\\b')) |
| 142 | self.assertEqual(f('\\a'), ('', '\\', 'a')) |
| 143 | self.assertEqual(f('\\a\\b'), ('', '\\', 'a\\b')) |
| 144 | self.assertEqual(f('c:a\\b'), ('c:', '', 'a\\b')) |
| 145 | self.assertEqual(f('c:\\a\\b'), ('c:', '\\', 'a\\b')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 146 | # Redundant slashes in the root are collapsed. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 147 | self.assertEqual(f('\\\\a'), ('', '\\', 'a')) |
| 148 | self.assertEqual(f('\\\\\\a/b'), ('', '\\', 'a/b')) |
| 149 | self.assertEqual(f('c:\\\\a'), ('c:', '\\', 'a')) |
| 150 | self.assertEqual(f('c:\\\\\\a/b'), ('c:', '\\', 'a/b')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 151 | # Valid UNC paths. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 152 | self.assertEqual(f('\\\\a\\b'), ('\\\\a\\b', '\\', '')) |
| 153 | self.assertEqual(f('\\\\a\\b\\'), ('\\\\a\\b', '\\', '')) |
| 154 | self.assertEqual(f('\\\\a\\b\\c\\d'), ('\\\\a\\b', '\\', 'c\\d')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 155 | # These are non-UNC paths (according to ntpath.py and test_ntpath). |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 156 | # However, command.com says such paths are invalid, so it's |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 157 | # difficult to know what the right semantics are. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 158 | self.assertEqual(f('\\\\\\a\\b'), ('', '\\', 'a\\b')) |
| 159 | self.assertEqual(f('\\\\a'), ('', '\\', 'a')) |
| 160 | |
| 161 | |
| 162 | # |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 163 | # Tests for the pure classes. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 164 | # |
| 165 | |
| 166 | class _BasePurePathTest(object): |
| 167 | |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 168 | # Keys are canonical paths, values are list of tuples of arguments |
| 169 | # supposed to produce equal paths. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 170 | equivalences = { |
| 171 | 'a/b': [ |
| 172 | ('a', 'b'), ('a/', 'b'), ('a', 'b/'), ('a/', 'b/'), |
| 173 | ('a/b/',), ('a//b',), ('a//b//',), |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 174 | # Empty components get removed. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 175 | ('', 'a', 'b'), ('a', '', 'b'), ('a', 'b', ''), |
| 176 | ], |
| 177 | '/b/c/d': [ |
| 178 | ('a', '/b/c', 'd'), ('a', '///b//c', 'd/'), |
| 179 | ('/a', '/b/c', 'd'), |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 180 | # Empty components get removed. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 181 | ('/', 'b', '', 'c/d'), ('/', '', 'b/c/d'), ('', '/b/c/d'), |
| 182 | ], |
| 183 | } |
| 184 | |
| 185 | def setUp(self): |
| 186 | p = self.cls('a') |
| 187 | self.flavour = p._flavour |
| 188 | self.sep = self.flavour.sep |
| 189 | self.altsep = self.flavour.altsep |
| 190 | |
| 191 | def test_constructor_common(self): |
| 192 | P = self.cls |
| 193 | p = P('a') |
| 194 | self.assertIsInstance(p, P) |
| 195 | P('a', 'b', 'c') |
| 196 | P('/a', 'b', 'c') |
| 197 | P('a/b/c') |
| 198 | P('/a/b/c') |
Serhiy Storchaka | b21d155 | 2018-03-02 11:53:51 +0200 | [diff] [blame] | 199 | P(FakePath("a/b/c")) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 200 | self.assertEqual(P(P('a')), P('a')) |
| 201 | self.assertEqual(P(P('a'), 'b'), P('a/b')) |
| 202 | self.assertEqual(P(P('a'), P('b')), P('a/b')) |
Serhiy Storchaka | b21d155 | 2018-03-02 11:53:51 +0200 | [diff] [blame] | 203 | self.assertEqual(P(P('a'), P('b'), P('c')), P(FakePath("a/b/c"))) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 204 | |
Antoine Pitrou | cb5ec77 | 2014-04-23 00:34:15 +0200 | [diff] [blame] | 205 | def _check_str_subclass(self, *args): |
| 206 | # Issue #21127: it should be possible to construct a PurePath object |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 207 | # from a str subclass instance, and it then gets converted to |
Antoine Pitrou | cb5ec77 | 2014-04-23 00:34:15 +0200 | [diff] [blame] | 208 | # a pure str object. |
| 209 | class StrSubclass(str): |
| 210 | pass |
| 211 | P = self.cls |
| 212 | p = P(*(StrSubclass(x) for x in args)) |
| 213 | self.assertEqual(p, P(*args)) |
| 214 | for part in p.parts: |
| 215 | self.assertIs(type(part), str) |
| 216 | |
| 217 | def test_str_subclass_common(self): |
| 218 | self._check_str_subclass('') |
| 219 | self._check_str_subclass('.') |
| 220 | self._check_str_subclass('a') |
| 221 | self._check_str_subclass('a/b.txt') |
| 222 | self._check_str_subclass('/a/b.txt') |
| 223 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 224 | def test_join_common(self): |
| 225 | P = self.cls |
| 226 | p = P('a/b') |
| 227 | pp = p.joinpath('c') |
| 228 | self.assertEqual(pp, P('a/b/c')) |
| 229 | self.assertIs(type(pp), type(p)) |
| 230 | pp = p.joinpath('c', 'd') |
| 231 | self.assertEqual(pp, P('a/b/c/d')) |
| 232 | pp = p.joinpath(P('c')) |
| 233 | self.assertEqual(pp, P('a/b/c')) |
| 234 | pp = p.joinpath('/c') |
| 235 | self.assertEqual(pp, P('/c')) |
| 236 | |
| 237 | def test_div_common(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 238 | # Basically the same as joinpath(). |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 239 | P = self.cls |
| 240 | p = P('a/b') |
| 241 | pp = p / 'c' |
| 242 | self.assertEqual(pp, P('a/b/c')) |
| 243 | self.assertIs(type(pp), type(p)) |
| 244 | pp = p / 'c/d' |
| 245 | self.assertEqual(pp, P('a/b/c/d')) |
| 246 | pp = p / 'c' / 'd' |
| 247 | self.assertEqual(pp, P('a/b/c/d')) |
| 248 | pp = 'c' / p / 'd' |
| 249 | self.assertEqual(pp, P('c/a/b/d')) |
| 250 | pp = p / P('c') |
| 251 | self.assertEqual(pp, P('a/b/c')) |
| 252 | pp = p/ '/c' |
| 253 | self.assertEqual(pp, P('/c')) |
| 254 | |
| 255 | def _check_str(self, expected, args): |
| 256 | p = self.cls(*args) |
| 257 | self.assertEqual(str(p), expected.replace('/', self.sep)) |
| 258 | |
| 259 | def test_str_common(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 260 | # Canonicalized paths roundtrip. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 261 | for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'): |
| 262 | self._check_str(pathstr, (pathstr,)) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 263 | # Special case for the empty path. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 264 | self._check_str('.', ('',)) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 265 | # Other tests for str() are in test_equivalences(). |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 266 | |
| 267 | def test_as_posix_common(self): |
| 268 | P = self.cls |
| 269 | for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'): |
| 270 | self.assertEqual(P(pathstr).as_posix(), pathstr) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 271 | # Other tests for as_posix() are in test_equivalences(). |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 272 | |
| 273 | def test_as_bytes_common(self): |
| 274 | sep = os.fsencode(self.sep) |
| 275 | P = self.cls |
| 276 | self.assertEqual(bytes(P('a/b')), b'a' + sep + b'b') |
| 277 | |
| 278 | def test_as_uri_common(self): |
| 279 | P = self.cls |
| 280 | with self.assertRaises(ValueError): |
| 281 | P('a').as_uri() |
| 282 | with self.assertRaises(ValueError): |
| 283 | P().as_uri() |
| 284 | |
| 285 | def test_repr_common(self): |
| 286 | for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'): |
| 287 | p = self.cls(pathstr) |
| 288 | clsname = p.__class__.__name__ |
| 289 | r = repr(p) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 290 | # The repr() is in the form ClassName("forward-slashes path"). |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 291 | self.assertTrue(r.startswith(clsname + '('), r) |
| 292 | self.assertTrue(r.endswith(')'), r) |
| 293 | inner = r[len(clsname) + 1 : -1] |
| 294 | self.assertEqual(eval(inner), p.as_posix()) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 295 | # The repr() roundtrips. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 296 | q = eval(r, pathlib.__dict__) |
| 297 | self.assertIs(q.__class__, p.__class__) |
| 298 | self.assertEqual(q, p) |
| 299 | self.assertEqual(repr(q), r) |
| 300 | |
| 301 | def test_eq_common(self): |
| 302 | P = self.cls |
| 303 | self.assertEqual(P('a/b'), P('a/b')) |
| 304 | self.assertEqual(P('a/b'), P('a', 'b')) |
| 305 | self.assertNotEqual(P('a/b'), P('a')) |
| 306 | self.assertNotEqual(P('a/b'), P('/a/b')) |
| 307 | self.assertNotEqual(P('a/b'), P()) |
| 308 | self.assertNotEqual(P('/a/b'), P('/')) |
| 309 | self.assertNotEqual(P(), P('/')) |
| 310 | self.assertNotEqual(P(), "") |
| 311 | self.assertNotEqual(P(), {}) |
| 312 | self.assertNotEqual(P(), int) |
| 313 | |
| 314 | def test_match_common(self): |
| 315 | P = self.cls |
| 316 | self.assertRaises(ValueError, P('a').match, '') |
| 317 | self.assertRaises(ValueError, P('a').match, '.') |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 318 | # Simple relative pattern. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 319 | self.assertTrue(P('b.py').match('b.py')) |
| 320 | self.assertTrue(P('a/b.py').match('b.py')) |
| 321 | self.assertTrue(P('/a/b.py').match('b.py')) |
| 322 | self.assertFalse(P('a.py').match('b.py')) |
| 323 | self.assertFalse(P('b/py').match('b.py')) |
| 324 | self.assertFalse(P('/a.py').match('b.py')) |
| 325 | self.assertFalse(P('b.py/c').match('b.py')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 326 | # Wilcard relative pattern. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 327 | self.assertTrue(P('b.py').match('*.py')) |
| 328 | self.assertTrue(P('a/b.py').match('*.py')) |
| 329 | self.assertTrue(P('/a/b.py').match('*.py')) |
| 330 | self.assertFalse(P('b.pyc').match('*.py')) |
| 331 | self.assertFalse(P('b./py').match('*.py')) |
| 332 | self.assertFalse(P('b.py/c').match('*.py')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 333 | # Multi-part relative pattern. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 334 | self.assertTrue(P('ab/c.py').match('a*/*.py')) |
| 335 | self.assertTrue(P('/d/ab/c.py').match('a*/*.py')) |
| 336 | self.assertFalse(P('a.py').match('a*/*.py')) |
| 337 | self.assertFalse(P('/dab/c.py').match('a*/*.py')) |
| 338 | self.assertFalse(P('ab/c.py/d').match('a*/*.py')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 339 | # Absolute pattern. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 340 | self.assertTrue(P('/b.py').match('/*.py')) |
| 341 | self.assertFalse(P('b.py').match('/*.py')) |
| 342 | self.assertFalse(P('a/b.py').match('/*.py')) |
| 343 | self.assertFalse(P('/a/b.py').match('/*.py')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 344 | # Multi-part absolute pattern. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 345 | self.assertTrue(P('/a/b.py').match('/a/*.py')) |
| 346 | self.assertFalse(P('/ab.py').match('/a/*.py')) |
| 347 | self.assertFalse(P('/a/b/c.py').match('/a/*.py')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 348 | # Multi-part glob-style pattern. |
| 349 | self.assertFalse(P('/a/b/c.py').match('/**/*.py')) |
| 350 | self.assertTrue(P('/a/b/c.py').match('/a/**/*.py')) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 351 | |
| 352 | def test_ordering_common(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 353 | # Ordering is tuple-alike. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 354 | def assertLess(a, b): |
| 355 | self.assertLess(a, b) |
| 356 | self.assertGreater(b, a) |
| 357 | P = self.cls |
| 358 | a = P('a') |
| 359 | b = P('a/b') |
| 360 | c = P('abc') |
| 361 | d = P('b') |
| 362 | assertLess(a, b) |
| 363 | assertLess(a, c) |
| 364 | assertLess(a, d) |
| 365 | assertLess(b, c) |
| 366 | assertLess(c, d) |
| 367 | P = self.cls |
| 368 | a = P('/a') |
| 369 | b = P('/a/b') |
| 370 | c = P('/abc') |
| 371 | d = P('/b') |
| 372 | assertLess(a, b) |
| 373 | assertLess(a, c) |
| 374 | assertLess(a, d) |
| 375 | assertLess(b, c) |
| 376 | assertLess(c, d) |
| 377 | with self.assertRaises(TypeError): |
| 378 | P() < {} |
| 379 | |
| 380 | def test_parts_common(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 381 | # `parts` returns a tuple. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 382 | sep = self.sep |
| 383 | P = self.cls |
| 384 | p = P('a/b') |
| 385 | parts = p.parts |
| 386 | self.assertEqual(parts, ('a', 'b')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 387 | # The object gets reused. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 388 | self.assertIs(parts, p.parts) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 389 | # When the path is absolute, the anchor is a separate part. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 390 | p = P('/a/b') |
| 391 | parts = p.parts |
| 392 | self.assertEqual(parts, (sep, 'a', 'b')) |
| 393 | |
Brett Cannon | 568be63 | 2016-06-10 12:20:49 -0700 | [diff] [blame] | 394 | def test_fspath_common(self): |
| 395 | P = self.cls |
| 396 | p = P('a/b') |
| 397 | self._check_str(p.__fspath__(), ('a/b',)) |
| 398 | self._check_str(os.fspath(p), ('a/b',)) |
| 399 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 400 | def test_equivalences(self): |
| 401 | for k, tuples in self.equivalences.items(): |
| 402 | canon = k.replace('/', self.sep) |
| 403 | posix = k.replace(self.sep, '/') |
| 404 | if canon != posix: |
| 405 | tuples = tuples + [ |
| 406 | tuple(part.replace('/', self.sep) for part in t) |
| 407 | for t in tuples |
| 408 | ] |
| 409 | tuples.append((posix, )) |
| 410 | pcanon = self.cls(canon) |
| 411 | for t in tuples: |
| 412 | p = self.cls(*t) |
| 413 | self.assertEqual(p, pcanon, "failed with args {}".format(t)) |
| 414 | self.assertEqual(hash(p), hash(pcanon)) |
| 415 | self.assertEqual(str(p), canon) |
| 416 | self.assertEqual(p.as_posix(), posix) |
| 417 | |
| 418 | def test_parent_common(self): |
| 419 | # Relative |
| 420 | P = self.cls |
| 421 | p = P('a/b/c') |
| 422 | self.assertEqual(p.parent, P('a/b')) |
| 423 | self.assertEqual(p.parent.parent, P('a')) |
| 424 | self.assertEqual(p.parent.parent.parent, P()) |
| 425 | self.assertEqual(p.parent.parent.parent.parent, P()) |
| 426 | # Anchored |
| 427 | p = P('/a/b/c') |
| 428 | self.assertEqual(p.parent, P('/a/b')) |
| 429 | self.assertEqual(p.parent.parent, P('/a')) |
| 430 | self.assertEqual(p.parent.parent.parent, P('/')) |
| 431 | self.assertEqual(p.parent.parent.parent.parent, P('/')) |
| 432 | |
| 433 | def test_parents_common(self): |
| 434 | # Relative |
| 435 | P = self.cls |
| 436 | p = P('a/b/c') |
| 437 | par = p.parents |
| 438 | self.assertEqual(len(par), 3) |
| 439 | self.assertEqual(par[0], P('a/b')) |
| 440 | self.assertEqual(par[1], P('a')) |
| 441 | self.assertEqual(par[2], P('.')) |
| 442 | self.assertEqual(list(par), [P('a/b'), P('a'), P('.')]) |
| 443 | with self.assertRaises(IndexError): |
| 444 | par[-1] |
| 445 | with self.assertRaises(IndexError): |
| 446 | par[3] |
| 447 | with self.assertRaises(TypeError): |
| 448 | par[0] = p |
| 449 | # Anchored |
| 450 | p = P('/a/b/c') |
| 451 | par = p.parents |
| 452 | self.assertEqual(len(par), 3) |
| 453 | self.assertEqual(par[0], P('/a/b')) |
| 454 | self.assertEqual(par[1], P('/a')) |
| 455 | self.assertEqual(par[2], P('/')) |
| 456 | self.assertEqual(list(par), [P('/a/b'), P('/a'), P('/')]) |
| 457 | with self.assertRaises(IndexError): |
| 458 | par[3] |
| 459 | |
| 460 | def test_drive_common(self): |
| 461 | P = self.cls |
| 462 | self.assertEqual(P('a/b').drive, '') |
| 463 | self.assertEqual(P('/a/b').drive, '') |
| 464 | self.assertEqual(P('').drive, '') |
| 465 | |
| 466 | def test_root_common(self): |
| 467 | P = self.cls |
| 468 | sep = self.sep |
| 469 | self.assertEqual(P('').root, '') |
| 470 | self.assertEqual(P('a/b').root, '') |
| 471 | self.assertEqual(P('/').root, sep) |
| 472 | self.assertEqual(P('/a/b').root, sep) |
| 473 | |
| 474 | def test_anchor_common(self): |
| 475 | P = self.cls |
| 476 | sep = self.sep |
| 477 | self.assertEqual(P('').anchor, '') |
| 478 | self.assertEqual(P('a/b').anchor, '') |
| 479 | self.assertEqual(P('/').anchor, sep) |
| 480 | self.assertEqual(P('/a/b').anchor, sep) |
| 481 | |
| 482 | def test_name_common(self): |
| 483 | P = self.cls |
| 484 | self.assertEqual(P('').name, '') |
| 485 | self.assertEqual(P('.').name, '') |
| 486 | self.assertEqual(P('/').name, '') |
| 487 | self.assertEqual(P('a/b').name, 'b') |
| 488 | self.assertEqual(P('/a/b').name, 'b') |
| 489 | self.assertEqual(P('/a/b/.').name, 'b') |
| 490 | self.assertEqual(P('a/b.py').name, 'b.py') |
| 491 | self.assertEqual(P('/a/b.py').name, 'b.py') |
| 492 | |
| 493 | def test_suffix_common(self): |
| 494 | P = self.cls |
| 495 | self.assertEqual(P('').suffix, '') |
| 496 | self.assertEqual(P('.').suffix, '') |
| 497 | self.assertEqual(P('..').suffix, '') |
| 498 | self.assertEqual(P('/').suffix, '') |
| 499 | self.assertEqual(P('a/b').suffix, '') |
| 500 | self.assertEqual(P('/a/b').suffix, '') |
| 501 | self.assertEqual(P('/a/b/.').suffix, '') |
| 502 | self.assertEqual(P('a/b.py').suffix, '.py') |
| 503 | self.assertEqual(P('/a/b.py').suffix, '.py') |
| 504 | self.assertEqual(P('a/.hgrc').suffix, '') |
| 505 | self.assertEqual(P('/a/.hgrc').suffix, '') |
| 506 | self.assertEqual(P('a/.hg.rc').suffix, '.rc') |
| 507 | self.assertEqual(P('/a/.hg.rc').suffix, '.rc') |
| 508 | self.assertEqual(P('a/b.tar.gz').suffix, '.gz') |
| 509 | self.assertEqual(P('/a/b.tar.gz').suffix, '.gz') |
| 510 | self.assertEqual(P('a/Some name. Ending with a dot.').suffix, '') |
| 511 | self.assertEqual(P('/a/Some name. Ending with a dot.').suffix, '') |
| 512 | |
| 513 | def test_suffixes_common(self): |
| 514 | P = self.cls |
| 515 | self.assertEqual(P('').suffixes, []) |
| 516 | self.assertEqual(P('.').suffixes, []) |
| 517 | self.assertEqual(P('/').suffixes, []) |
| 518 | self.assertEqual(P('a/b').suffixes, []) |
| 519 | self.assertEqual(P('/a/b').suffixes, []) |
| 520 | self.assertEqual(P('/a/b/.').suffixes, []) |
| 521 | self.assertEqual(P('a/b.py').suffixes, ['.py']) |
| 522 | self.assertEqual(P('/a/b.py').suffixes, ['.py']) |
| 523 | self.assertEqual(P('a/.hgrc').suffixes, []) |
| 524 | self.assertEqual(P('/a/.hgrc').suffixes, []) |
| 525 | self.assertEqual(P('a/.hg.rc').suffixes, ['.rc']) |
| 526 | self.assertEqual(P('/a/.hg.rc').suffixes, ['.rc']) |
| 527 | self.assertEqual(P('a/b.tar.gz').suffixes, ['.tar', '.gz']) |
| 528 | self.assertEqual(P('/a/b.tar.gz').suffixes, ['.tar', '.gz']) |
| 529 | self.assertEqual(P('a/Some name. Ending with a dot.').suffixes, []) |
| 530 | self.assertEqual(P('/a/Some name. Ending with a dot.').suffixes, []) |
| 531 | |
| 532 | def test_stem_common(self): |
| 533 | P = self.cls |
| 534 | self.assertEqual(P('').stem, '') |
| 535 | self.assertEqual(P('.').stem, '') |
| 536 | self.assertEqual(P('..').stem, '..') |
| 537 | self.assertEqual(P('/').stem, '') |
| 538 | self.assertEqual(P('a/b').stem, 'b') |
| 539 | self.assertEqual(P('a/b.py').stem, 'b') |
| 540 | self.assertEqual(P('a/.hgrc').stem, '.hgrc') |
| 541 | self.assertEqual(P('a/.hg.rc').stem, '.hg') |
| 542 | self.assertEqual(P('a/b.tar.gz').stem, 'b.tar') |
| 543 | self.assertEqual(P('a/Some name. Ending with a dot.').stem, |
| 544 | 'Some name. Ending with a dot.') |
| 545 | |
| 546 | def test_with_name_common(self): |
| 547 | P = self.cls |
| 548 | self.assertEqual(P('a/b').with_name('d.xml'), P('a/d.xml')) |
| 549 | self.assertEqual(P('/a/b').with_name('d.xml'), P('/a/d.xml')) |
| 550 | self.assertEqual(P('a/b.py').with_name('d.xml'), P('a/d.xml')) |
| 551 | self.assertEqual(P('/a/b.py').with_name('d.xml'), P('/a/d.xml')) |
| 552 | self.assertEqual(P('a/Dot ending.').with_name('d.xml'), P('a/d.xml')) |
| 553 | self.assertEqual(P('/a/Dot ending.').with_name('d.xml'), P('/a/d.xml')) |
| 554 | self.assertRaises(ValueError, P('').with_name, 'd.xml') |
| 555 | self.assertRaises(ValueError, P('.').with_name, 'd.xml') |
| 556 | self.assertRaises(ValueError, P('/').with_name, 'd.xml') |
Antoine Pitrou | 7084e73 | 2014-07-06 21:31:12 -0400 | [diff] [blame] | 557 | self.assertRaises(ValueError, P('a/b').with_name, '') |
| 558 | self.assertRaises(ValueError, P('a/b').with_name, '/c') |
| 559 | self.assertRaises(ValueError, P('a/b').with_name, 'c/') |
| 560 | self.assertRaises(ValueError, P('a/b').with_name, 'c/d') |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 561 | |
| 562 | def test_with_suffix_common(self): |
| 563 | P = self.cls |
| 564 | self.assertEqual(P('a/b').with_suffix('.gz'), P('a/b.gz')) |
| 565 | self.assertEqual(P('/a/b').with_suffix('.gz'), P('/a/b.gz')) |
| 566 | self.assertEqual(P('a/b.py').with_suffix('.gz'), P('a/b.gz')) |
| 567 | self.assertEqual(P('/a/b.py').with_suffix('.gz'), P('/a/b.gz')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 568 | # Stripping suffix. |
Antoine Pitrou | e50dafc | 2014-07-06 21:37:15 -0400 | [diff] [blame] | 569 | self.assertEqual(P('a/b.py').with_suffix(''), P('a/b')) |
| 570 | self.assertEqual(P('/a/b').with_suffix(''), P('/a/b')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 571 | # Path doesn't have a "filename" component. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 572 | self.assertRaises(ValueError, P('').with_suffix, '.gz') |
| 573 | self.assertRaises(ValueError, P('.').with_suffix, '.gz') |
| 574 | self.assertRaises(ValueError, P('/').with_suffix, '.gz') |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 575 | # Invalid suffix. |
Antoine Pitrou | 1b02da9 | 2014-01-03 00:07:17 +0100 | [diff] [blame] | 576 | self.assertRaises(ValueError, P('a/b').with_suffix, 'gz') |
| 577 | self.assertRaises(ValueError, P('a/b').with_suffix, '/') |
Antoine Pitrou | e50dafc | 2014-07-06 21:37:15 -0400 | [diff] [blame] | 578 | self.assertRaises(ValueError, P('a/b').with_suffix, '.') |
Antoine Pitrou | 1b02da9 | 2014-01-03 00:07:17 +0100 | [diff] [blame] | 579 | self.assertRaises(ValueError, P('a/b').with_suffix, '/.gz') |
| 580 | self.assertRaises(ValueError, P('a/b').with_suffix, 'c/d') |
| 581 | self.assertRaises(ValueError, P('a/b').with_suffix, '.c/.d') |
Antoine Pitrou | e50dafc | 2014-07-06 21:37:15 -0400 | [diff] [blame] | 582 | self.assertRaises(ValueError, P('a/b').with_suffix, './.d') |
| 583 | self.assertRaises(ValueError, P('a/b').with_suffix, '.d/.') |
Berker Peksag | 423d05f | 2018-08-11 08:45:06 +0300 | [diff] [blame] | 584 | self.assertRaises(ValueError, P('a/b').with_suffix, |
| 585 | (self.flavour.sep, 'd')) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 586 | |
| 587 | def test_relative_to_common(self): |
| 588 | P = self.cls |
| 589 | p = P('a/b') |
| 590 | self.assertRaises(TypeError, p.relative_to) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 591 | self.assertRaises(TypeError, p.relative_to, b'a') |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 592 | self.assertEqual(p.relative_to(P()), P('a/b')) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 593 | self.assertEqual(p.relative_to(''), P('a/b')) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 594 | self.assertEqual(p.relative_to(P('a')), P('b')) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 595 | self.assertEqual(p.relative_to('a'), P('b')) |
| 596 | self.assertEqual(p.relative_to('a/'), P('b')) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 597 | self.assertEqual(p.relative_to(P('a/b')), P()) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 598 | self.assertEqual(p.relative_to('a/b'), P()) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 599 | # With several args. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 600 | self.assertEqual(p.relative_to('a', 'b'), P()) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 601 | # Unrelated paths. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 602 | self.assertRaises(ValueError, p.relative_to, P('c')) |
| 603 | self.assertRaises(ValueError, p.relative_to, P('a/b/c')) |
| 604 | self.assertRaises(ValueError, p.relative_to, P('a/c')) |
| 605 | self.assertRaises(ValueError, p.relative_to, P('/a')) |
| 606 | p = P('/a/b') |
| 607 | self.assertEqual(p.relative_to(P('/')), P('a/b')) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 608 | self.assertEqual(p.relative_to('/'), P('a/b')) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 609 | self.assertEqual(p.relative_to(P('/a')), P('b')) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 610 | self.assertEqual(p.relative_to('/a'), P('b')) |
| 611 | self.assertEqual(p.relative_to('/a/'), P('b')) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 612 | self.assertEqual(p.relative_to(P('/a/b')), P()) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 613 | self.assertEqual(p.relative_to('/a/b'), P()) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 614 | # Unrelated paths. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 615 | self.assertRaises(ValueError, p.relative_to, P('/c')) |
| 616 | self.assertRaises(ValueError, p.relative_to, P('/a/b/c')) |
| 617 | self.assertRaises(ValueError, p.relative_to, P('/a/c')) |
| 618 | self.assertRaises(ValueError, p.relative_to, P()) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 619 | self.assertRaises(ValueError, p.relative_to, '') |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 620 | self.assertRaises(ValueError, p.relative_to, P('a')) |
| 621 | |
| 622 | def test_pickling_common(self): |
| 623 | P = self.cls |
| 624 | p = P('/a/b') |
| 625 | for proto in range(0, pickle.HIGHEST_PROTOCOL + 1): |
| 626 | dumped = pickle.dumps(p, proto) |
| 627 | pp = pickle.loads(dumped) |
| 628 | self.assertIs(pp.__class__, p.__class__) |
| 629 | self.assertEqual(pp, p) |
| 630 | self.assertEqual(hash(pp), hash(p)) |
| 631 | self.assertEqual(str(pp), str(p)) |
| 632 | |
| 633 | |
| 634 | class PurePosixPathTest(_BasePurePathTest, unittest.TestCase): |
| 635 | cls = pathlib.PurePosixPath |
| 636 | |
| 637 | def test_root(self): |
| 638 | P = self.cls |
| 639 | self.assertEqual(P('/a/b').root, '/') |
| 640 | self.assertEqual(P('///a/b').root, '/') |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 641 | # POSIX special case for two leading slashes. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 642 | self.assertEqual(P('//a/b').root, '//') |
| 643 | |
| 644 | def test_eq(self): |
| 645 | P = self.cls |
| 646 | self.assertNotEqual(P('a/b'), P('A/b')) |
| 647 | self.assertEqual(P('/a'), P('///a')) |
| 648 | self.assertNotEqual(P('/a'), P('//a')) |
| 649 | |
| 650 | def test_as_uri(self): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 651 | P = self.cls |
| 652 | self.assertEqual(P('/').as_uri(), 'file:///') |
| 653 | self.assertEqual(P('/a/b.c').as_uri(), 'file:///a/b.c') |
| 654 | self.assertEqual(P('/a/b%#c').as_uri(), 'file:///a/b%25%23c') |
Antoine Pitrou | 29eac42 | 2013-11-22 17:57:03 +0100 | [diff] [blame] | 655 | |
| 656 | def test_as_uri_non_ascii(self): |
| 657 | from urllib.parse import quote_from_bytes |
| 658 | P = self.cls |
| 659 | try: |
| 660 | os.fsencode('\xe9') |
| 661 | except UnicodeEncodeError: |
| 662 | self.skipTest("\\xe9 cannot be encoded to the filesystem encoding") |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 663 | self.assertEqual(P('/a/b\xe9').as_uri(), |
| 664 | 'file:///a/b' + quote_from_bytes(os.fsencode('\xe9'))) |
| 665 | |
| 666 | def test_match(self): |
| 667 | P = self.cls |
| 668 | self.assertFalse(P('A.py').match('a.PY')) |
| 669 | |
| 670 | def test_is_absolute(self): |
| 671 | P = self.cls |
| 672 | self.assertFalse(P().is_absolute()) |
| 673 | self.assertFalse(P('a').is_absolute()) |
| 674 | self.assertFalse(P('a/b/').is_absolute()) |
| 675 | self.assertTrue(P('/').is_absolute()) |
| 676 | self.assertTrue(P('/a').is_absolute()) |
| 677 | self.assertTrue(P('/a/b/').is_absolute()) |
| 678 | self.assertTrue(P('//a').is_absolute()) |
| 679 | self.assertTrue(P('//a/b').is_absolute()) |
| 680 | |
| 681 | def test_is_reserved(self): |
| 682 | P = self.cls |
| 683 | self.assertIs(False, P('').is_reserved()) |
| 684 | self.assertIs(False, P('/').is_reserved()) |
| 685 | self.assertIs(False, P('/foo/bar').is_reserved()) |
| 686 | self.assertIs(False, P('/dev/con/PRN/NUL').is_reserved()) |
| 687 | |
| 688 | def test_join(self): |
| 689 | P = self.cls |
| 690 | p = P('//a') |
| 691 | pp = p.joinpath('b') |
| 692 | self.assertEqual(pp, P('//a/b')) |
| 693 | pp = P('/a').joinpath('//c') |
| 694 | self.assertEqual(pp, P('//c')) |
| 695 | pp = P('//a').joinpath('/c') |
| 696 | self.assertEqual(pp, P('/c')) |
| 697 | |
| 698 | def test_div(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 699 | # Basically the same as joinpath(). |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 700 | P = self.cls |
| 701 | p = P('//a') |
| 702 | pp = p / 'b' |
| 703 | self.assertEqual(pp, P('//a/b')) |
| 704 | pp = P('/a') / '//c' |
| 705 | self.assertEqual(pp, P('//c')) |
| 706 | pp = P('//a') / '/c' |
| 707 | self.assertEqual(pp, P('/c')) |
| 708 | |
| 709 | |
| 710 | class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase): |
| 711 | cls = pathlib.PureWindowsPath |
| 712 | |
| 713 | equivalences = _BasePurePathTest.equivalences.copy() |
| 714 | equivalences.update({ |
| 715 | 'c:a': [ ('c:', 'a'), ('c:', 'a/'), ('/', 'c:', 'a') ], |
| 716 | 'c:/a': [ |
| 717 | ('c:/', 'a'), ('c:', '/', 'a'), ('c:', '/a'), |
| 718 | ('/z', 'c:/', 'a'), ('//x/y', 'c:/', 'a'), |
| 719 | ], |
| 720 | '//a/b/': [ ('//a/b',) ], |
| 721 | '//a/b/c': [ |
| 722 | ('//a/b', 'c'), ('//a/b/', 'c'), |
| 723 | ], |
| 724 | }) |
| 725 | |
| 726 | def test_str(self): |
| 727 | p = self.cls('a/b/c') |
| 728 | self.assertEqual(str(p), 'a\\b\\c') |
| 729 | p = self.cls('c:/a/b/c') |
| 730 | self.assertEqual(str(p), 'c:\\a\\b\\c') |
| 731 | p = self.cls('//a/b') |
| 732 | self.assertEqual(str(p), '\\\\a\\b\\') |
| 733 | p = self.cls('//a/b/c') |
| 734 | self.assertEqual(str(p), '\\\\a\\b\\c') |
| 735 | p = self.cls('//a/b/c/d') |
| 736 | self.assertEqual(str(p), '\\\\a\\b\\c\\d') |
| 737 | |
Antoine Pitrou | cb5ec77 | 2014-04-23 00:34:15 +0200 | [diff] [blame] | 738 | def test_str_subclass(self): |
| 739 | self._check_str_subclass('c:') |
| 740 | self._check_str_subclass('c:a') |
| 741 | self._check_str_subclass('c:a\\b.txt') |
| 742 | self._check_str_subclass('c:\\') |
| 743 | self._check_str_subclass('c:\\a') |
| 744 | self._check_str_subclass('c:\\a\\b.txt') |
| 745 | self._check_str_subclass('\\\\some\\share') |
| 746 | self._check_str_subclass('\\\\some\\share\\a') |
| 747 | self._check_str_subclass('\\\\some\\share\\a\\b.txt') |
| 748 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 749 | def test_eq(self): |
| 750 | P = self.cls |
| 751 | self.assertEqual(P('c:a/b'), P('c:a/b')) |
| 752 | self.assertEqual(P('c:a/b'), P('c:', 'a', 'b')) |
| 753 | self.assertNotEqual(P('c:a/b'), P('d:a/b')) |
| 754 | self.assertNotEqual(P('c:a/b'), P('c:/a/b')) |
| 755 | self.assertNotEqual(P('/a/b'), P('c:/a/b')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 756 | # Case-insensitivity. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 757 | self.assertEqual(P('a/B'), P('A/b')) |
| 758 | self.assertEqual(P('C:a/B'), P('c:A/b')) |
| 759 | self.assertEqual(P('//Some/SHARE/a/B'), P('//somE/share/A/b')) |
| 760 | |
| 761 | def test_as_uri(self): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 762 | P = self.cls |
| 763 | with self.assertRaises(ValueError): |
| 764 | P('/a/b').as_uri() |
| 765 | with self.assertRaises(ValueError): |
| 766 | P('c:a/b').as_uri() |
| 767 | self.assertEqual(P('c:/').as_uri(), 'file:///c:/') |
| 768 | self.assertEqual(P('c:/a/b.c').as_uri(), 'file:///c:/a/b.c') |
| 769 | self.assertEqual(P('c:/a/b%#c').as_uri(), 'file:///c:/a/b%25%23c') |
| 770 | self.assertEqual(P('c:/a/b\xe9').as_uri(), 'file:///c:/a/b%C3%A9') |
| 771 | self.assertEqual(P('//some/share/').as_uri(), 'file://some/share/') |
| 772 | self.assertEqual(P('//some/share/a/b.c').as_uri(), |
| 773 | 'file://some/share/a/b.c') |
| 774 | self.assertEqual(P('//some/share/a/b%#c\xe9').as_uri(), |
| 775 | 'file://some/share/a/b%25%23c%C3%A9') |
| 776 | |
| 777 | def test_match_common(self): |
| 778 | P = self.cls |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 779 | # Absolute patterns. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 780 | self.assertTrue(P('c:/b.py').match('/*.py')) |
| 781 | self.assertTrue(P('c:/b.py').match('c:*.py')) |
| 782 | self.assertTrue(P('c:/b.py').match('c:/*.py')) |
| 783 | self.assertFalse(P('d:/b.py').match('c:/*.py')) # wrong drive |
| 784 | self.assertFalse(P('b.py').match('/*.py')) |
| 785 | self.assertFalse(P('b.py').match('c:*.py')) |
| 786 | self.assertFalse(P('b.py').match('c:/*.py')) |
| 787 | self.assertFalse(P('c:b.py').match('/*.py')) |
| 788 | self.assertFalse(P('c:b.py').match('c:/*.py')) |
| 789 | self.assertFalse(P('/b.py').match('c:*.py')) |
| 790 | self.assertFalse(P('/b.py').match('c:/*.py')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 791 | # UNC patterns. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 792 | self.assertTrue(P('//some/share/a.py').match('/*.py')) |
| 793 | self.assertTrue(P('//some/share/a.py').match('//some/share/*.py')) |
| 794 | self.assertFalse(P('//other/share/a.py').match('//some/share/*.py')) |
| 795 | self.assertFalse(P('//some/share/a/b.py').match('//some/share/*.py')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 796 | # Case-insensitivity. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 797 | self.assertTrue(P('B.py').match('b.PY')) |
| 798 | self.assertTrue(P('c:/a/B.Py').match('C:/A/*.pY')) |
| 799 | self.assertTrue(P('//Some/Share/B.Py').match('//somE/sharE/*.pY')) |
| 800 | |
| 801 | def test_ordering_common(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 802 | # Case-insensitivity. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 803 | def assertOrderedEqual(a, b): |
| 804 | self.assertLessEqual(a, b) |
| 805 | self.assertGreaterEqual(b, a) |
| 806 | P = self.cls |
| 807 | p = P('c:A/b') |
| 808 | q = P('C:a/B') |
| 809 | assertOrderedEqual(p, q) |
| 810 | self.assertFalse(p < q) |
| 811 | self.assertFalse(p > q) |
| 812 | p = P('//some/Share/A/b') |
| 813 | q = P('//Some/SHARE/a/B') |
| 814 | assertOrderedEqual(p, q) |
| 815 | self.assertFalse(p < q) |
| 816 | self.assertFalse(p > q) |
| 817 | |
| 818 | def test_parts(self): |
| 819 | P = self.cls |
| 820 | p = P('c:a/b') |
| 821 | parts = p.parts |
| 822 | self.assertEqual(parts, ('c:', 'a', 'b')) |
| 823 | p = P('c:/a/b') |
| 824 | parts = p.parts |
| 825 | self.assertEqual(parts, ('c:\\', 'a', 'b')) |
| 826 | p = P('//a/b/c/d') |
| 827 | parts = p.parts |
| 828 | self.assertEqual(parts, ('\\\\a\\b\\', 'c', 'd')) |
| 829 | |
| 830 | def test_parent(self): |
| 831 | # Anchored |
| 832 | P = self.cls |
| 833 | p = P('z:a/b/c') |
| 834 | self.assertEqual(p.parent, P('z:a/b')) |
| 835 | self.assertEqual(p.parent.parent, P('z:a')) |
| 836 | self.assertEqual(p.parent.parent.parent, P('z:')) |
| 837 | self.assertEqual(p.parent.parent.parent.parent, P('z:')) |
| 838 | p = P('z:/a/b/c') |
| 839 | self.assertEqual(p.parent, P('z:/a/b')) |
| 840 | self.assertEqual(p.parent.parent, P('z:/a')) |
| 841 | self.assertEqual(p.parent.parent.parent, P('z:/')) |
| 842 | self.assertEqual(p.parent.parent.parent.parent, P('z:/')) |
| 843 | p = P('//a/b/c/d') |
| 844 | self.assertEqual(p.parent, P('//a/b/c')) |
| 845 | self.assertEqual(p.parent.parent, P('//a/b')) |
| 846 | self.assertEqual(p.parent.parent.parent, P('//a/b')) |
| 847 | |
| 848 | def test_parents(self): |
| 849 | # Anchored |
| 850 | P = self.cls |
| 851 | p = P('z:a/b/') |
| 852 | par = p.parents |
| 853 | self.assertEqual(len(par), 2) |
| 854 | self.assertEqual(par[0], P('z:a')) |
| 855 | self.assertEqual(par[1], P('z:')) |
| 856 | self.assertEqual(list(par), [P('z:a'), P('z:')]) |
| 857 | with self.assertRaises(IndexError): |
| 858 | par[2] |
| 859 | p = P('z:/a/b/') |
| 860 | par = p.parents |
| 861 | self.assertEqual(len(par), 2) |
| 862 | self.assertEqual(par[0], P('z:/a')) |
| 863 | self.assertEqual(par[1], P('z:/')) |
| 864 | self.assertEqual(list(par), [P('z:/a'), P('z:/')]) |
| 865 | with self.assertRaises(IndexError): |
| 866 | par[2] |
| 867 | p = P('//a/b/c/d') |
| 868 | par = p.parents |
| 869 | self.assertEqual(len(par), 2) |
| 870 | self.assertEqual(par[0], P('//a/b/c')) |
| 871 | self.assertEqual(par[1], P('//a/b')) |
| 872 | self.assertEqual(list(par), [P('//a/b/c'), P('//a/b')]) |
| 873 | with self.assertRaises(IndexError): |
| 874 | par[2] |
| 875 | |
| 876 | def test_drive(self): |
| 877 | P = self.cls |
| 878 | self.assertEqual(P('c:').drive, 'c:') |
| 879 | self.assertEqual(P('c:a/b').drive, 'c:') |
| 880 | self.assertEqual(P('c:/').drive, 'c:') |
| 881 | self.assertEqual(P('c:/a/b/').drive, 'c:') |
| 882 | self.assertEqual(P('//a/b').drive, '\\\\a\\b') |
| 883 | self.assertEqual(P('//a/b/').drive, '\\\\a\\b') |
| 884 | self.assertEqual(P('//a/b/c/d').drive, '\\\\a\\b') |
| 885 | |
| 886 | def test_root(self): |
| 887 | P = self.cls |
| 888 | self.assertEqual(P('c:').root, '') |
| 889 | self.assertEqual(P('c:a/b').root, '') |
| 890 | self.assertEqual(P('c:/').root, '\\') |
| 891 | self.assertEqual(P('c:/a/b/').root, '\\') |
| 892 | self.assertEqual(P('//a/b').root, '\\') |
| 893 | self.assertEqual(P('//a/b/').root, '\\') |
| 894 | self.assertEqual(P('//a/b/c/d').root, '\\') |
| 895 | |
| 896 | def test_anchor(self): |
| 897 | P = self.cls |
| 898 | self.assertEqual(P('c:').anchor, 'c:') |
| 899 | self.assertEqual(P('c:a/b').anchor, 'c:') |
| 900 | self.assertEqual(P('c:/').anchor, 'c:\\') |
| 901 | self.assertEqual(P('c:/a/b/').anchor, 'c:\\') |
| 902 | self.assertEqual(P('//a/b').anchor, '\\\\a\\b\\') |
| 903 | self.assertEqual(P('//a/b/').anchor, '\\\\a\\b\\') |
| 904 | self.assertEqual(P('//a/b/c/d').anchor, '\\\\a\\b\\') |
| 905 | |
| 906 | def test_name(self): |
| 907 | P = self.cls |
| 908 | self.assertEqual(P('c:').name, '') |
| 909 | self.assertEqual(P('c:/').name, '') |
| 910 | self.assertEqual(P('c:a/b').name, 'b') |
| 911 | self.assertEqual(P('c:/a/b').name, 'b') |
| 912 | self.assertEqual(P('c:a/b.py').name, 'b.py') |
| 913 | self.assertEqual(P('c:/a/b.py').name, 'b.py') |
| 914 | self.assertEqual(P('//My.py/Share.php').name, '') |
| 915 | self.assertEqual(P('//My.py/Share.php/a/b').name, 'b') |
| 916 | |
| 917 | def test_suffix(self): |
| 918 | P = self.cls |
| 919 | self.assertEqual(P('c:').suffix, '') |
| 920 | self.assertEqual(P('c:/').suffix, '') |
| 921 | self.assertEqual(P('c:a/b').suffix, '') |
| 922 | self.assertEqual(P('c:/a/b').suffix, '') |
| 923 | self.assertEqual(P('c:a/b.py').suffix, '.py') |
| 924 | self.assertEqual(P('c:/a/b.py').suffix, '.py') |
| 925 | self.assertEqual(P('c:a/.hgrc').suffix, '') |
| 926 | self.assertEqual(P('c:/a/.hgrc').suffix, '') |
| 927 | self.assertEqual(P('c:a/.hg.rc').suffix, '.rc') |
| 928 | self.assertEqual(P('c:/a/.hg.rc').suffix, '.rc') |
| 929 | self.assertEqual(P('c:a/b.tar.gz').suffix, '.gz') |
| 930 | self.assertEqual(P('c:/a/b.tar.gz').suffix, '.gz') |
| 931 | self.assertEqual(P('c:a/Some name. Ending with a dot.').suffix, '') |
| 932 | self.assertEqual(P('c:/a/Some name. Ending with a dot.').suffix, '') |
| 933 | self.assertEqual(P('//My.py/Share.php').suffix, '') |
| 934 | self.assertEqual(P('//My.py/Share.php/a/b').suffix, '') |
| 935 | |
| 936 | def test_suffixes(self): |
| 937 | P = self.cls |
| 938 | self.assertEqual(P('c:').suffixes, []) |
| 939 | self.assertEqual(P('c:/').suffixes, []) |
| 940 | self.assertEqual(P('c:a/b').suffixes, []) |
| 941 | self.assertEqual(P('c:/a/b').suffixes, []) |
| 942 | self.assertEqual(P('c:a/b.py').suffixes, ['.py']) |
| 943 | self.assertEqual(P('c:/a/b.py').suffixes, ['.py']) |
| 944 | self.assertEqual(P('c:a/.hgrc').suffixes, []) |
| 945 | self.assertEqual(P('c:/a/.hgrc').suffixes, []) |
| 946 | self.assertEqual(P('c:a/.hg.rc').suffixes, ['.rc']) |
| 947 | self.assertEqual(P('c:/a/.hg.rc').suffixes, ['.rc']) |
| 948 | self.assertEqual(P('c:a/b.tar.gz').suffixes, ['.tar', '.gz']) |
| 949 | self.assertEqual(P('c:/a/b.tar.gz').suffixes, ['.tar', '.gz']) |
| 950 | self.assertEqual(P('//My.py/Share.php').suffixes, []) |
| 951 | self.assertEqual(P('//My.py/Share.php/a/b').suffixes, []) |
| 952 | self.assertEqual(P('c:a/Some name. Ending with a dot.').suffixes, []) |
| 953 | self.assertEqual(P('c:/a/Some name. Ending with a dot.').suffixes, []) |
| 954 | |
| 955 | def test_stem(self): |
| 956 | P = self.cls |
| 957 | self.assertEqual(P('c:').stem, '') |
| 958 | self.assertEqual(P('c:.').stem, '') |
| 959 | self.assertEqual(P('c:..').stem, '..') |
| 960 | self.assertEqual(P('c:/').stem, '') |
| 961 | self.assertEqual(P('c:a/b').stem, 'b') |
| 962 | self.assertEqual(P('c:a/b.py').stem, 'b') |
| 963 | self.assertEqual(P('c:a/.hgrc').stem, '.hgrc') |
| 964 | self.assertEqual(P('c:a/.hg.rc').stem, '.hg') |
| 965 | self.assertEqual(P('c:a/b.tar.gz').stem, 'b.tar') |
| 966 | self.assertEqual(P('c:a/Some name. Ending with a dot.').stem, |
| 967 | 'Some name. Ending with a dot.') |
| 968 | |
| 969 | def test_with_name(self): |
| 970 | P = self.cls |
| 971 | self.assertEqual(P('c:a/b').with_name('d.xml'), P('c:a/d.xml')) |
| 972 | self.assertEqual(P('c:/a/b').with_name('d.xml'), P('c:/a/d.xml')) |
| 973 | self.assertEqual(P('c:a/Dot ending.').with_name('d.xml'), P('c:a/d.xml')) |
| 974 | self.assertEqual(P('c:/a/Dot ending.').with_name('d.xml'), P('c:/a/d.xml')) |
| 975 | self.assertRaises(ValueError, P('c:').with_name, 'd.xml') |
| 976 | self.assertRaises(ValueError, P('c:/').with_name, 'd.xml') |
| 977 | self.assertRaises(ValueError, P('//My/Share').with_name, 'd.xml') |
Antoine Pitrou | 7084e73 | 2014-07-06 21:31:12 -0400 | [diff] [blame] | 978 | self.assertRaises(ValueError, P('c:a/b').with_name, 'd:') |
| 979 | self.assertRaises(ValueError, P('c:a/b').with_name, 'd:e') |
| 980 | self.assertRaises(ValueError, P('c:a/b').with_name, 'd:/e') |
| 981 | self.assertRaises(ValueError, P('c:a/b').with_name, '//My/Share') |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 982 | |
| 983 | def test_with_suffix(self): |
| 984 | P = self.cls |
| 985 | self.assertEqual(P('c:a/b').with_suffix('.gz'), P('c:a/b.gz')) |
| 986 | self.assertEqual(P('c:/a/b').with_suffix('.gz'), P('c:/a/b.gz')) |
| 987 | self.assertEqual(P('c:a/b.py').with_suffix('.gz'), P('c:a/b.gz')) |
| 988 | self.assertEqual(P('c:/a/b.py').with_suffix('.gz'), P('c:/a/b.gz')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 989 | # Path doesn't have a "filename" component. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 990 | self.assertRaises(ValueError, P('').with_suffix, '.gz') |
| 991 | self.assertRaises(ValueError, P('.').with_suffix, '.gz') |
| 992 | self.assertRaises(ValueError, P('/').with_suffix, '.gz') |
| 993 | self.assertRaises(ValueError, P('//My/Share').with_suffix, '.gz') |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 994 | # Invalid suffix. |
Antoine Pitrou | 1b02da9 | 2014-01-03 00:07:17 +0100 | [diff] [blame] | 995 | self.assertRaises(ValueError, P('c:a/b').with_suffix, 'gz') |
| 996 | self.assertRaises(ValueError, P('c:a/b').with_suffix, '/') |
| 997 | self.assertRaises(ValueError, P('c:a/b').with_suffix, '\\') |
| 998 | self.assertRaises(ValueError, P('c:a/b').with_suffix, 'c:') |
| 999 | self.assertRaises(ValueError, P('c:a/b').with_suffix, '/.gz') |
| 1000 | self.assertRaises(ValueError, P('c:a/b').with_suffix, '\\.gz') |
| 1001 | self.assertRaises(ValueError, P('c:a/b').with_suffix, 'c:.gz') |
| 1002 | self.assertRaises(ValueError, P('c:a/b').with_suffix, 'c/d') |
| 1003 | self.assertRaises(ValueError, P('c:a/b').with_suffix, 'c\\d') |
| 1004 | self.assertRaises(ValueError, P('c:a/b').with_suffix, '.c/d') |
| 1005 | self.assertRaises(ValueError, P('c:a/b').with_suffix, '.c\\d') |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1006 | |
| 1007 | def test_relative_to(self): |
| 1008 | P = self.cls |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 1009 | p = P('C:Foo/Bar') |
| 1010 | self.assertEqual(p.relative_to(P('c:')), P('Foo/Bar')) |
| 1011 | self.assertEqual(p.relative_to('c:'), P('Foo/Bar')) |
| 1012 | self.assertEqual(p.relative_to(P('c:foO')), P('Bar')) |
| 1013 | self.assertEqual(p.relative_to('c:foO'), P('Bar')) |
| 1014 | self.assertEqual(p.relative_to('c:foO/'), P('Bar')) |
| 1015 | self.assertEqual(p.relative_to(P('c:foO/baR')), P()) |
| 1016 | self.assertEqual(p.relative_to('c:foO/baR'), P()) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1017 | # Unrelated paths. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1018 | self.assertRaises(ValueError, p.relative_to, P()) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 1019 | self.assertRaises(ValueError, p.relative_to, '') |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1020 | self.assertRaises(ValueError, p.relative_to, P('d:')) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 1021 | self.assertRaises(ValueError, p.relative_to, P('/')) |
| 1022 | self.assertRaises(ValueError, p.relative_to, P('Foo')) |
| 1023 | self.assertRaises(ValueError, p.relative_to, P('/Foo')) |
| 1024 | self.assertRaises(ValueError, p.relative_to, P('C:/Foo')) |
| 1025 | self.assertRaises(ValueError, p.relative_to, P('C:Foo/Bar/Baz')) |
| 1026 | self.assertRaises(ValueError, p.relative_to, P('C:Foo/Baz')) |
| 1027 | p = P('C:/Foo/Bar') |
| 1028 | self.assertEqual(p.relative_to(P('c:')), P('/Foo/Bar')) |
| 1029 | self.assertEqual(p.relative_to('c:'), P('/Foo/Bar')) |
| 1030 | self.assertEqual(str(p.relative_to(P('c:'))), '\\Foo\\Bar') |
| 1031 | self.assertEqual(str(p.relative_to('c:')), '\\Foo\\Bar') |
| 1032 | self.assertEqual(p.relative_to(P('c:/')), P('Foo/Bar')) |
| 1033 | self.assertEqual(p.relative_to('c:/'), P('Foo/Bar')) |
| 1034 | self.assertEqual(p.relative_to(P('c:/foO')), P('Bar')) |
| 1035 | self.assertEqual(p.relative_to('c:/foO'), P('Bar')) |
| 1036 | self.assertEqual(p.relative_to('c:/foO/'), P('Bar')) |
| 1037 | self.assertEqual(p.relative_to(P('c:/foO/baR')), P()) |
| 1038 | self.assertEqual(p.relative_to('c:/foO/baR'), P()) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1039 | # Unrelated paths. |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 1040 | self.assertRaises(ValueError, p.relative_to, P('C:/Baz')) |
| 1041 | self.assertRaises(ValueError, p.relative_to, P('C:/Foo/Bar/Baz')) |
| 1042 | self.assertRaises(ValueError, p.relative_to, P('C:/Foo/Baz')) |
| 1043 | self.assertRaises(ValueError, p.relative_to, P('C:Foo')) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1044 | self.assertRaises(ValueError, p.relative_to, P('d:')) |
| 1045 | self.assertRaises(ValueError, p.relative_to, P('d:/')) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 1046 | self.assertRaises(ValueError, p.relative_to, P('/')) |
| 1047 | self.assertRaises(ValueError, p.relative_to, P('/Foo')) |
| 1048 | self.assertRaises(ValueError, p.relative_to, P('//C/Foo')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1049 | # UNC paths. |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 1050 | p = P('//Server/Share/Foo/Bar') |
| 1051 | self.assertEqual(p.relative_to(P('//sErver/sHare')), P('Foo/Bar')) |
| 1052 | self.assertEqual(p.relative_to('//sErver/sHare'), P('Foo/Bar')) |
| 1053 | self.assertEqual(p.relative_to('//sErver/sHare/'), P('Foo/Bar')) |
| 1054 | self.assertEqual(p.relative_to(P('//sErver/sHare/Foo')), P('Bar')) |
| 1055 | self.assertEqual(p.relative_to('//sErver/sHare/Foo'), P('Bar')) |
| 1056 | self.assertEqual(p.relative_to('//sErver/sHare/Foo/'), P('Bar')) |
| 1057 | self.assertEqual(p.relative_to(P('//sErver/sHare/Foo/Bar')), P()) |
| 1058 | self.assertEqual(p.relative_to('//sErver/sHare/Foo/Bar'), P()) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1059 | # Unrelated paths. |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 1060 | self.assertRaises(ValueError, p.relative_to, P('/Server/Share/Foo')) |
| 1061 | self.assertRaises(ValueError, p.relative_to, P('c:/Server/Share/Foo')) |
| 1062 | self.assertRaises(ValueError, p.relative_to, P('//z/Share/Foo')) |
| 1063 | self.assertRaises(ValueError, p.relative_to, P('//Server/z/Foo')) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1064 | |
| 1065 | def test_is_absolute(self): |
| 1066 | P = self.cls |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1067 | # Under NT, only paths with both a drive and a root are absolute. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1068 | self.assertFalse(P().is_absolute()) |
| 1069 | self.assertFalse(P('a').is_absolute()) |
| 1070 | self.assertFalse(P('a/b/').is_absolute()) |
| 1071 | self.assertFalse(P('/').is_absolute()) |
| 1072 | self.assertFalse(P('/a').is_absolute()) |
| 1073 | self.assertFalse(P('/a/b/').is_absolute()) |
| 1074 | self.assertFalse(P('c:').is_absolute()) |
| 1075 | self.assertFalse(P('c:a').is_absolute()) |
| 1076 | self.assertFalse(P('c:a/b/').is_absolute()) |
| 1077 | self.assertTrue(P('c:/').is_absolute()) |
| 1078 | self.assertTrue(P('c:/a').is_absolute()) |
| 1079 | self.assertTrue(P('c:/a/b/').is_absolute()) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1080 | # UNC paths are absolute by definition. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1081 | self.assertTrue(P('//a/b').is_absolute()) |
| 1082 | self.assertTrue(P('//a/b/').is_absolute()) |
| 1083 | self.assertTrue(P('//a/b/c').is_absolute()) |
| 1084 | self.assertTrue(P('//a/b/c/d').is_absolute()) |
| 1085 | |
Serhiy Storchaka | a993902 | 2013-12-06 17:14:12 +0200 | [diff] [blame] | 1086 | def test_join(self): |
| 1087 | P = self.cls |
| 1088 | p = P('C:/a/b') |
| 1089 | pp = p.joinpath('x/y') |
| 1090 | self.assertEqual(pp, P('C:/a/b/x/y')) |
| 1091 | pp = p.joinpath('/x/y') |
| 1092 | self.assertEqual(pp, P('C:/x/y')) |
| 1093 | # Joining with a different drive => the first path is ignored, even |
| 1094 | # if the second path is relative. |
| 1095 | pp = p.joinpath('D:x/y') |
| 1096 | self.assertEqual(pp, P('D:x/y')) |
| 1097 | pp = p.joinpath('D:/x/y') |
| 1098 | self.assertEqual(pp, P('D:/x/y')) |
| 1099 | pp = p.joinpath('//host/share/x/y') |
| 1100 | self.assertEqual(pp, P('//host/share/x/y')) |
| 1101 | # Joining with the same drive => the first path is appended to if |
| 1102 | # the second path is relative. |
| 1103 | pp = p.joinpath('c:x/y') |
| 1104 | self.assertEqual(pp, P('C:/a/b/x/y')) |
| 1105 | pp = p.joinpath('c:/x/y') |
| 1106 | self.assertEqual(pp, P('C:/x/y')) |
| 1107 | |
| 1108 | def test_div(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1109 | # Basically the same as joinpath(). |
Serhiy Storchaka | a993902 | 2013-12-06 17:14:12 +0200 | [diff] [blame] | 1110 | P = self.cls |
| 1111 | p = P('C:/a/b') |
| 1112 | self.assertEqual(p / 'x/y', P('C:/a/b/x/y')) |
| 1113 | self.assertEqual(p / 'x' / 'y', P('C:/a/b/x/y')) |
| 1114 | self.assertEqual(p / '/x/y', P('C:/x/y')) |
| 1115 | self.assertEqual(p / '/x' / 'y', P('C:/x/y')) |
| 1116 | # Joining with a different drive => the first path is ignored, even |
| 1117 | # if the second path is relative. |
| 1118 | self.assertEqual(p / 'D:x/y', P('D:x/y')) |
| 1119 | self.assertEqual(p / 'D:' / 'x/y', P('D:x/y')) |
| 1120 | self.assertEqual(p / 'D:/x/y', P('D:/x/y')) |
| 1121 | self.assertEqual(p / 'D:' / '/x/y', P('D:/x/y')) |
| 1122 | self.assertEqual(p / '//host/share/x/y', P('//host/share/x/y')) |
| 1123 | # Joining with the same drive => the first path is appended to if |
| 1124 | # the second path is relative. |
Serhiy Storchaka | 010ff58 | 2013-12-06 17:25:51 +0200 | [diff] [blame] | 1125 | self.assertEqual(p / 'c:x/y', P('C:/a/b/x/y')) |
| 1126 | self.assertEqual(p / 'c:/x/y', P('C:/x/y')) |
Serhiy Storchaka | a993902 | 2013-12-06 17:14:12 +0200 | [diff] [blame] | 1127 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1128 | def test_is_reserved(self): |
| 1129 | P = self.cls |
| 1130 | self.assertIs(False, P('').is_reserved()) |
| 1131 | self.assertIs(False, P('/').is_reserved()) |
| 1132 | self.assertIs(False, P('/foo/bar').is_reserved()) |
| 1133 | self.assertIs(True, P('con').is_reserved()) |
| 1134 | self.assertIs(True, P('NUL').is_reserved()) |
| 1135 | self.assertIs(True, P('NUL.txt').is_reserved()) |
| 1136 | self.assertIs(True, P('com1').is_reserved()) |
| 1137 | self.assertIs(True, P('com9.bar').is_reserved()) |
| 1138 | self.assertIs(False, P('bar.com9').is_reserved()) |
| 1139 | self.assertIs(True, P('lpt1').is_reserved()) |
| 1140 | self.assertIs(True, P('lpt9.bar').is_reserved()) |
| 1141 | self.assertIs(False, P('bar.lpt9').is_reserved()) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1142 | # Only the last component matters. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1143 | self.assertIs(False, P('c:/NUL/con/baz').is_reserved()) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1144 | # UNC paths are never reserved. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1145 | self.assertIs(False, P('//my/share/nul/con/aux').is_reserved()) |
| 1146 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1147 | class PurePathTest(_BasePurePathTest, unittest.TestCase): |
| 1148 | cls = pathlib.PurePath |
| 1149 | |
| 1150 | def test_concrete_class(self): |
| 1151 | p = self.cls('a') |
| 1152 | self.assertIs(type(p), |
| 1153 | pathlib.PureWindowsPath if os.name == 'nt' else pathlib.PurePosixPath) |
| 1154 | |
| 1155 | def test_different_flavours_unequal(self): |
| 1156 | p = pathlib.PurePosixPath('a') |
| 1157 | q = pathlib.PureWindowsPath('a') |
| 1158 | self.assertNotEqual(p, q) |
| 1159 | |
| 1160 | def test_different_flavours_unordered(self): |
| 1161 | p = pathlib.PurePosixPath('a') |
| 1162 | q = pathlib.PureWindowsPath('a') |
| 1163 | with self.assertRaises(TypeError): |
| 1164 | p < q |
| 1165 | with self.assertRaises(TypeError): |
| 1166 | p <= q |
| 1167 | with self.assertRaises(TypeError): |
| 1168 | p > q |
| 1169 | with self.assertRaises(TypeError): |
| 1170 | p >= q |
| 1171 | |
| 1172 | |
| 1173 | # |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1174 | # Tests for the concrete classes. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1175 | # |
| 1176 | |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1177 | # Make sure any symbolic links in the base test path are resolved. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1178 | BASE = os.path.realpath(TESTFN) |
| 1179 | join = lambda *x: os.path.join(BASE, *x) |
| 1180 | rel_join = lambda *x: os.path.join(TESTFN, *x) |
| 1181 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1182 | only_nt = unittest.skipIf(os.name != 'nt', |
| 1183 | 'test requires a Windows-compatible system') |
| 1184 | only_posix = unittest.skipIf(os.name == 'nt', |
| 1185 | 'test requires a POSIX-compatible system') |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1186 | |
| 1187 | @only_posix |
| 1188 | class PosixPathAsPureTest(PurePosixPathTest): |
| 1189 | cls = pathlib.PosixPath |
| 1190 | |
| 1191 | @only_nt |
| 1192 | class WindowsPathAsPureTest(PureWindowsPathTest): |
| 1193 | cls = pathlib.WindowsPath |
| 1194 | |
Victor Stinner | d756963 | 2016-03-11 22:53:00 +0100 | [diff] [blame] | 1195 | def test_owner(self): |
| 1196 | P = self.cls |
| 1197 | with self.assertRaises(NotImplementedError): |
| 1198 | P('c:/').owner() |
| 1199 | |
| 1200 | def test_group(self): |
| 1201 | P = self.cls |
| 1202 | with self.assertRaises(NotImplementedError): |
| 1203 | P('c:/').group() |
| 1204 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1205 | |
| 1206 | class _BasePathTest(object): |
| 1207 | """Tests for the FS-accessing functionalities of the Path classes.""" |
| 1208 | |
| 1209 | # (BASE) |
| 1210 | # | |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 1211 | # |-- brokenLink -> non-existing |
| 1212 | # |-- dirA |
| 1213 | # | `-- linkC -> ../dirB |
| 1214 | # |-- dirB |
| 1215 | # | |-- fileB |
| 1216 | # | `-- linkD -> ../dirB |
| 1217 | # |-- dirC |
| 1218 | # | |-- dirD |
| 1219 | # | | `-- fileD |
| 1220 | # | `-- fileC |
Guido van Rossum | bc9fdda | 2016-01-07 10:56:36 -0800 | [diff] [blame] | 1221 | # |-- dirE # No permissions |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1222 | # |-- fileA |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 1223 | # |-- linkA -> fileA |
| 1224 | # `-- linkB -> dirB |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1225 | # |
| 1226 | |
| 1227 | def setUp(self): |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 1228 | def cleanup(): |
| 1229 | os.chmod(join('dirE'), 0o777) |
| 1230 | support.rmtree(BASE) |
| 1231 | self.addCleanup(cleanup) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1232 | os.mkdir(BASE) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1233 | os.mkdir(join('dirA')) |
| 1234 | os.mkdir(join('dirB')) |
| 1235 | os.mkdir(join('dirC')) |
| 1236 | os.mkdir(join('dirC', 'dirD')) |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 1237 | os.mkdir(join('dirE')) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1238 | with open(join('fileA'), 'wb') as f: |
| 1239 | f.write(b"this is file A\n") |
| 1240 | with open(join('dirB', 'fileB'), 'wb') as f: |
| 1241 | f.write(b"this is file B\n") |
| 1242 | with open(join('dirC', 'fileC'), 'wb') as f: |
| 1243 | f.write(b"this is file C\n") |
| 1244 | with open(join('dirC', 'dirD', 'fileD'), 'wb') as f: |
| 1245 | f.write(b"this is file D\n") |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 1246 | os.chmod(join('dirE'), 0) |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1247 | if support.can_symlink(): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1248 | # Relative symlinks. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1249 | os.symlink('fileA', join('linkA')) |
| 1250 | os.symlink('non-existing', join('brokenLink')) |
Antoine Pitrou | 51af82c | 2013-12-03 11:01:08 +0100 | [diff] [blame] | 1251 | self.dirlink('dirB', join('linkB')) |
| 1252 | self.dirlink(os.path.join('..', 'dirB'), join('dirA', 'linkC')) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1253 | # This one goes upwards, creating a loop. |
Antoine Pitrou | 51af82c | 2013-12-03 11:01:08 +0100 | [diff] [blame] | 1254 | self.dirlink(os.path.join('..', 'dirB'), join('dirB', 'linkD')) |
| 1255 | |
| 1256 | if os.name == 'nt': |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1257 | # Workaround for http://bugs.python.org/issue13772. |
Antoine Pitrou | 51af82c | 2013-12-03 11:01:08 +0100 | [diff] [blame] | 1258 | def dirlink(self, src, dest): |
| 1259 | os.symlink(src, dest, target_is_directory=True) |
| 1260 | else: |
| 1261 | def dirlink(self, src, dest): |
| 1262 | os.symlink(src, dest) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1263 | |
| 1264 | def assertSame(self, path_a, path_b): |
| 1265 | self.assertTrue(os.path.samefile(str(path_a), str(path_b)), |
| 1266 | "%r and %r don't point to the same file" % |
| 1267 | (path_a, path_b)) |
| 1268 | |
| 1269 | def assertFileNotFound(self, func, *args, **kwargs): |
| 1270 | with self.assertRaises(FileNotFoundError) as cm: |
| 1271 | func(*args, **kwargs) |
| 1272 | self.assertEqual(cm.exception.errno, errno.ENOENT) |
| 1273 | |
| 1274 | def _test_cwd(self, p): |
| 1275 | q = self.cls(os.getcwd()) |
| 1276 | self.assertEqual(p, q) |
| 1277 | self.assertEqual(str(p), str(q)) |
| 1278 | self.assertIs(type(p), type(q)) |
| 1279 | self.assertTrue(p.is_absolute()) |
| 1280 | |
| 1281 | def test_cwd(self): |
| 1282 | p = self.cls.cwd() |
| 1283 | self._test_cwd(p) |
| 1284 | |
Antoine Pitrou | 17cba7d | 2015-01-12 21:03:41 +0100 | [diff] [blame] | 1285 | def _test_home(self, p): |
| 1286 | q = self.cls(os.path.expanduser('~')) |
| 1287 | self.assertEqual(p, q) |
| 1288 | self.assertEqual(str(p), str(q)) |
| 1289 | self.assertIs(type(p), type(q)) |
| 1290 | self.assertTrue(p.is_absolute()) |
| 1291 | |
| 1292 | def test_home(self): |
| 1293 | p = self.cls.home() |
| 1294 | self._test_home(p) |
| 1295 | |
Antoine Pitrou | 43e3d94 | 2014-05-13 10:50:15 +0200 | [diff] [blame] | 1296 | def test_samefile(self): |
| 1297 | fileA_path = os.path.join(BASE, 'fileA') |
| 1298 | fileB_path = os.path.join(BASE, 'dirB', 'fileB') |
| 1299 | p = self.cls(fileA_path) |
| 1300 | pp = self.cls(fileA_path) |
| 1301 | q = self.cls(fileB_path) |
| 1302 | self.assertTrue(p.samefile(fileA_path)) |
| 1303 | self.assertTrue(p.samefile(pp)) |
| 1304 | self.assertFalse(p.samefile(fileB_path)) |
| 1305 | self.assertFalse(p.samefile(q)) |
| 1306 | # Test the non-existent file case |
| 1307 | non_existent = os.path.join(BASE, 'foo') |
| 1308 | r = self.cls(non_existent) |
| 1309 | self.assertRaises(FileNotFoundError, p.samefile, r) |
| 1310 | self.assertRaises(FileNotFoundError, p.samefile, non_existent) |
| 1311 | self.assertRaises(FileNotFoundError, r.samefile, p) |
| 1312 | self.assertRaises(FileNotFoundError, r.samefile, non_existent) |
| 1313 | self.assertRaises(FileNotFoundError, r.samefile, r) |
| 1314 | self.assertRaises(FileNotFoundError, r.samefile, non_existent) |
| 1315 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1316 | def test_empty_path(self): |
| 1317 | # The empty path points to '.' |
| 1318 | p = self.cls('') |
| 1319 | self.assertEqual(p.stat(), os.stat('.')) |
| 1320 | |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 1321 | def test_expanduser_common(self): |
| 1322 | P = self.cls |
| 1323 | p = P('~') |
| 1324 | self.assertEqual(p.expanduser(), P(os.path.expanduser('~'))) |
| 1325 | p = P('foo') |
| 1326 | self.assertEqual(p.expanduser(), p) |
| 1327 | p = P('/~') |
| 1328 | self.assertEqual(p.expanduser(), p) |
| 1329 | p = P('../~') |
| 1330 | self.assertEqual(p.expanduser(), p) |
| 1331 | p = P(P('').absolute().anchor) / '~' |
| 1332 | self.assertEqual(p.expanduser(), p) |
| 1333 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1334 | def test_exists(self): |
| 1335 | P = self.cls |
| 1336 | p = P(BASE) |
| 1337 | self.assertIs(True, p.exists()) |
| 1338 | self.assertIs(True, (p / 'dirA').exists()) |
| 1339 | self.assertIs(True, (p / 'fileA').exists()) |
Antoine Pitrou | 2b2852b | 2014-10-30 23:14:03 +0100 | [diff] [blame] | 1340 | self.assertIs(False, (p / 'fileA' / 'bah').exists()) |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1341 | if support.can_symlink(): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1342 | self.assertIs(True, (p / 'linkA').exists()) |
| 1343 | self.assertIs(True, (p / 'linkB').exists()) |
Antoine Pitrou | 2b2852b | 2014-10-30 23:14:03 +0100 | [diff] [blame] | 1344 | self.assertIs(True, (p / 'linkB' / 'fileB').exists()) |
| 1345 | self.assertIs(False, (p / 'linkA' / 'bah').exists()) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1346 | self.assertIs(False, (p / 'foo').exists()) |
| 1347 | self.assertIs(False, P('/xyzzy').exists()) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1348 | self.assertIs(False, P(BASE + '\udfff').exists()) |
| 1349 | self.assertIs(False, P(BASE + '\x00').exists()) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1350 | |
| 1351 | def test_open_common(self): |
| 1352 | p = self.cls(BASE) |
| 1353 | with (p / 'fileA').open('r') as f: |
| 1354 | self.assertIsInstance(f, io.TextIOBase) |
| 1355 | self.assertEqual(f.read(), "this is file A\n") |
| 1356 | with (p / 'fileA').open('rb') as f: |
| 1357 | self.assertIsInstance(f, io.BufferedIOBase) |
| 1358 | self.assertEqual(f.read().strip(), b"this is file A") |
| 1359 | with (p / 'fileA').open('rb', buffering=0) as f: |
| 1360 | self.assertIsInstance(f, io.RawIOBase) |
| 1361 | self.assertEqual(f.read().strip(), b"this is file A") |
| 1362 | |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 1363 | def test_read_write_bytes(self): |
| 1364 | p = self.cls(BASE) |
| 1365 | (p / 'fileA').write_bytes(b'abcdefg') |
| 1366 | self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg') |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1367 | # Check that trying to write str does not truncate the file. |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 1368 | self.assertRaises(TypeError, (p / 'fileA').write_bytes, 'somestr') |
| 1369 | self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg') |
| 1370 | |
| 1371 | def test_read_write_text(self): |
| 1372 | p = self.cls(BASE) |
| 1373 | (p / 'fileA').write_text('äbcdefg', encoding='latin-1') |
| 1374 | self.assertEqual((p / 'fileA').read_text( |
| 1375 | encoding='utf-8', errors='ignore'), 'bcdefg') |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1376 | # Check that trying to write bytes does not truncate the file. |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 1377 | self.assertRaises(TypeError, (p / 'fileA').write_text, b'somebytes') |
| 1378 | self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'äbcdefg') |
| 1379 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1380 | def test_iterdir(self): |
| 1381 | P = self.cls |
| 1382 | p = P(BASE) |
| 1383 | it = p.iterdir() |
| 1384 | paths = set(it) |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 1385 | expected = ['dirA', 'dirB', 'dirC', 'dirE', 'fileA'] |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1386 | if support.can_symlink(): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1387 | expected += ['linkA', 'linkB', 'brokenLink'] |
| 1388 | self.assertEqual(paths, { P(BASE, q) for q in expected }) |
| 1389 | |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1390 | @support.skip_unless_symlink |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1391 | def test_iterdir_symlink(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1392 | # __iter__ on a symlink to a directory. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1393 | P = self.cls |
| 1394 | p = P(BASE, 'linkB') |
| 1395 | paths = set(p.iterdir()) |
| 1396 | expected = { P(BASE, 'linkB', q) for q in ['fileB', 'linkD'] } |
| 1397 | self.assertEqual(paths, expected) |
| 1398 | |
| 1399 | def test_iterdir_nodir(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1400 | # __iter__ on something that is not a directory. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1401 | p = self.cls(BASE, 'fileA') |
| 1402 | with self.assertRaises(OSError) as cm: |
| 1403 | next(p.iterdir()) |
| 1404 | # ENOENT or EINVAL under Windows, ENOTDIR otherwise |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1405 | # (see issue #12802). |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1406 | self.assertIn(cm.exception.errno, (errno.ENOTDIR, |
| 1407 | errno.ENOENT, errno.EINVAL)) |
| 1408 | |
| 1409 | def test_glob_common(self): |
| 1410 | def _check(glob, expected): |
| 1411 | self.assertEqual(set(glob), { P(BASE, q) for q in expected }) |
| 1412 | P = self.cls |
| 1413 | p = P(BASE) |
| 1414 | it = p.glob("fileA") |
Serhiy Storchaka | 2e576f5 | 2017-04-24 09:05:00 +0300 | [diff] [blame] | 1415 | self.assertIsInstance(it, collections.abc.Iterator) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1416 | _check(it, ["fileA"]) |
| 1417 | _check(p.glob("fileB"), []) |
| 1418 | _check(p.glob("dir*/file*"), ["dirB/fileB", "dirC/fileC"]) |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1419 | if not support.can_symlink(): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1420 | _check(p.glob("*A"), ['dirA', 'fileA']) |
| 1421 | else: |
| 1422 | _check(p.glob("*A"), ['dirA', 'fileA', 'linkA']) |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1423 | if not support.can_symlink(): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1424 | _check(p.glob("*B/*"), ['dirB/fileB']) |
| 1425 | else: |
| 1426 | _check(p.glob("*B/*"), ['dirB/fileB', 'dirB/linkD', |
| 1427 | 'linkB/fileB', 'linkB/linkD']) |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1428 | if not support.can_symlink(): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1429 | _check(p.glob("*/fileB"), ['dirB/fileB']) |
| 1430 | else: |
| 1431 | _check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB']) |
| 1432 | |
| 1433 | def test_rglob_common(self): |
| 1434 | def _check(glob, expected): |
| 1435 | self.assertEqual(set(glob), { P(BASE, q) for q in expected }) |
| 1436 | P = self.cls |
| 1437 | p = P(BASE) |
| 1438 | it = p.rglob("fileA") |
Serhiy Storchaka | 2e576f5 | 2017-04-24 09:05:00 +0300 | [diff] [blame] | 1439 | self.assertIsInstance(it, collections.abc.Iterator) |
Guido van Rossum | bc9fdda | 2016-01-07 10:56:36 -0800 | [diff] [blame] | 1440 | _check(it, ["fileA"]) |
| 1441 | _check(p.rglob("fileB"), ["dirB/fileB"]) |
| 1442 | _check(p.rglob("*/fileA"), []) |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1443 | if not support.can_symlink(): |
Guido van Rossum | 9c39b67 | 2016-01-07 13:12:34 -0800 | [diff] [blame] | 1444 | _check(p.rglob("*/fileB"), ["dirB/fileB"]) |
| 1445 | else: |
| 1446 | _check(p.rglob("*/fileB"), ["dirB/fileB", "dirB/linkD/fileB", |
| 1447 | "linkB/fileB", "dirA/linkC/fileB"]) |
Guido van Rossum | bc9fdda | 2016-01-07 10:56:36 -0800 | [diff] [blame] | 1448 | _check(p.rglob("file*"), ["fileA", "dirB/fileB", |
| 1449 | "dirC/fileC", "dirC/dirD/fileD"]) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1450 | p = P(BASE, "dirC") |
| 1451 | _check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"]) |
| 1452 | _check(p.rglob("*/*"), ["dirC/dirD/fileD"]) |
| 1453 | |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1454 | @support.skip_unless_symlink |
Guido van Rossum | 69bfb15 | 2016-01-06 10:31:33 -0800 | [diff] [blame] | 1455 | def test_rglob_symlink_loop(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1456 | # Don't get fooled by symlink loops (Issue #26012). |
Guido van Rossum | 69bfb15 | 2016-01-06 10:31:33 -0800 | [diff] [blame] | 1457 | P = self.cls |
| 1458 | p = P(BASE) |
| 1459 | given = set(p.rglob('*')) |
| 1460 | expect = {'brokenLink', |
| 1461 | 'dirA', 'dirA/linkC', |
| 1462 | 'dirB', 'dirB/fileB', 'dirB/linkD', |
| 1463 | 'dirC', 'dirC/dirD', 'dirC/dirD/fileD', 'dirC/fileC', |
| 1464 | 'dirE', |
| 1465 | 'fileA', |
| 1466 | 'linkA', |
| 1467 | 'linkB', |
| 1468 | } |
| 1469 | self.assertEqual(given, {p / x for x in expect}) |
| 1470 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1471 | def test_glob_dotdot(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1472 | # ".." is not special in globs. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1473 | P = self.cls |
| 1474 | p = P(BASE) |
| 1475 | self.assertEqual(set(p.glob("..")), { P(BASE, "..") }) |
| 1476 | self.assertEqual(set(p.glob("dirA/../file*")), { P(BASE, "dirA/../fileA") }) |
| 1477 | self.assertEqual(set(p.glob("../xyzzy")), set()) |
| 1478 | |
Ezio Melotti | ac1e7f6 | 2015-12-28 23:50:19 +0200 | [diff] [blame] | 1479 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1480 | def _check_resolve(self, p, expected, strict=True): |
| 1481 | q = p.resolve(strict) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1482 | self.assertEqual(q, expected) |
| 1483 | |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1484 | # This can be used to check both relative and absolute resolutions. |
Ezio Melotti | ac1e7f6 | 2015-12-28 23:50:19 +0200 | [diff] [blame] | 1485 | _check_resolve_relative = _check_resolve_absolute = _check_resolve |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1486 | |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1487 | @support.skip_unless_symlink |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1488 | def test_resolve_common(self): |
| 1489 | P = self.cls |
| 1490 | p = P(BASE, 'foo') |
| 1491 | with self.assertRaises(OSError) as cm: |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1492 | p.resolve(strict=True) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1493 | self.assertEqual(cm.exception.errno, errno.ENOENT) |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1494 | # Non-strict |
| 1495 | self.assertEqual(str(p.resolve(strict=False)), |
| 1496 | os.path.join(BASE, 'foo')) |
| 1497 | p = P(BASE, 'foo', 'in', 'spam') |
| 1498 | self.assertEqual(str(p.resolve(strict=False)), |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 1499 | os.path.join(BASE, 'foo', 'in', 'spam')) |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1500 | p = P(BASE, '..', 'foo', 'in', 'spam') |
| 1501 | self.assertEqual(str(p.resolve(strict=False)), |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 1502 | os.path.abspath(os.path.join('foo', 'in', 'spam'))) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1503 | # These are all relative symlinks. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1504 | p = P(BASE, 'dirB', 'fileB') |
| 1505 | self._check_resolve_relative(p, p) |
| 1506 | p = P(BASE, 'linkA') |
| 1507 | self._check_resolve_relative(p, P(BASE, 'fileA')) |
| 1508 | p = P(BASE, 'dirA', 'linkC', 'fileB') |
| 1509 | self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB')) |
| 1510 | p = P(BASE, 'dirB', 'linkD', 'fileB') |
| 1511 | self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB')) |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1512 | # Non-strict |
| 1513 | p = P(BASE, 'dirA', 'linkC', 'fileB', 'foo', 'in', 'spam') |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 1514 | self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB', 'foo', 'in', |
| 1515 | 'spam'), False) |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1516 | p = P(BASE, 'dirA', 'linkC', '..', 'foo', 'in', 'spam') |
| 1517 | if os.name == 'nt': |
| 1518 | # In Windows, if linkY points to dirB, 'dirA\linkY\..' |
| 1519 | # resolves to 'dirA' without resolving linkY first. |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 1520 | self._check_resolve_relative(p, P(BASE, 'dirA', 'foo', 'in', |
| 1521 | 'spam'), False) |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1522 | else: |
| 1523 | # In Posix, if linkY points to dirB, 'dirA/linkY/..' |
| 1524 | # resolves to 'dirB/..' first before resolving to parent of dirB. |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 1525 | self._check_resolve_relative(p, P(BASE, 'foo', 'in', 'spam'), False) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1526 | # Now create absolute symlinks. |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 1527 | d = support._longpath(tempfile.mkdtemp(suffix='-dirD', dir=os.getcwd())) |
Victor Stinner | ec86469 | 2014-07-21 19:19:05 +0200 | [diff] [blame] | 1528 | self.addCleanup(support.rmtree, d) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1529 | os.symlink(os.path.join(d), join('dirA', 'linkX')) |
| 1530 | os.symlink(join('dirB'), os.path.join(d, 'linkY')) |
| 1531 | p = P(BASE, 'dirA', 'linkX', 'linkY', 'fileB') |
| 1532 | self._check_resolve_absolute(p, P(BASE, 'dirB', 'fileB')) |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1533 | # Non-strict |
| 1534 | p = P(BASE, 'dirA', 'linkX', 'linkY', 'foo', 'in', 'spam') |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 1535 | self._check_resolve_relative(p, P(BASE, 'dirB', 'foo', 'in', 'spam'), |
| 1536 | False) |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1537 | p = P(BASE, 'dirA', 'linkX', 'linkY', '..', 'foo', 'in', 'spam') |
| 1538 | if os.name == 'nt': |
| 1539 | # In Windows, if linkY points to dirB, 'dirA\linkY\..' |
| 1540 | # resolves to 'dirA' without resolving linkY first. |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 1541 | self._check_resolve_relative(p, P(d, 'foo', 'in', 'spam'), False) |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1542 | else: |
| 1543 | # In Posix, if linkY points to dirB, 'dirA/linkY/..' |
| 1544 | # resolves to 'dirB/..' first before resolving to parent of dirB. |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 1545 | self._check_resolve_relative(p, P(BASE, 'foo', 'in', 'spam'), False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1546 | |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1547 | @support.skip_unless_symlink |
Antoine Pitrou | 51af82c | 2013-12-03 11:01:08 +0100 | [diff] [blame] | 1548 | def test_resolve_dot(self): |
| 1549 | # See https://bitbucket.org/pitrou/pathlib/issue/9/pathresolve-fails-on-complex-symlinks |
| 1550 | p = self.cls(BASE) |
| 1551 | self.dirlink('.', join('0')) |
Antoine Pitrou | cc15751 | 2013-12-03 17:13:13 +0100 | [diff] [blame] | 1552 | self.dirlink(os.path.join('0', '0'), join('1')) |
| 1553 | self.dirlink(os.path.join('1', '1'), join('2')) |
Antoine Pitrou | 51af82c | 2013-12-03 11:01:08 +0100 | [diff] [blame] | 1554 | q = p / '2' |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1555 | self.assertEqual(q.resolve(strict=True), p) |
| 1556 | r = q / '3' / '4' |
| 1557 | self.assertRaises(FileNotFoundError, r.resolve, strict=True) |
| 1558 | # Non-strict |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 1559 | self.assertEqual(r.resolve(strict=False), p / '3' / '4') |
Antoine Pitrou | 51af82c | 2013-12-03 11:01:08 +0100 | [diff] [blame] | 1560 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1561 | def test_with(self): |
| 1562 | p = self.cls(BASE) |
| 1563 | it = p.iterdir() |
| 1564 | it2 = p.iterdir() |
| 1565 | next(it2) |
| 1566 | with p: |
| 1567 | pass |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1568 | # I/O operation on closed path. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1569 | self.assertRaises(ValueError, next, it) |
| 1570 | self.assertRaises(ValueError, next, it2) |
| 1571 | self.assertRaises(ValueError, p.open) |
| 1572 | self.assertRaises(ValueError, p.resolve) |
| 1573 | self.assertRaises(ValueError, p.absolute) |
| 1574 | self.assertRaises(ValueError, p.__enter__) |
| 1575 | |
| 1576 | def test_chmod(self): |
| 1577 | p = self.cls(BASE) / 'fileA' |
| 1578 | mode = p.stat().st_mode |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1579 | # Clear writable bit. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1580 | new_mode = mode & ~0o222 |
| 1581 | p.chmod(new_mode) |
| 1582 | self.assertEqual(p.stat().st_mode, new_mode) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1583 | # Set writable bit. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1584 | new_mode = mode | 0o222 |
| 1585 | p.chmod(new_mode) |
| 1586 | self.assertEqual(p.stat().st_mode, new_mode) |
| 1587 | |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1588 | # XXX also need a test for lchmod. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1589 | |
| 1590 | def test_stat(self): |
| 1591 | p = self.cls(BASE) / 'fileA' |
| 1592 | st = p.stat() |
| 1593 | self.assertEqual(p.stat(), st) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1594 | # Change file mode by flipping write bit. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1595 | p.chmod(st.st_mode ^ 0o222) |
| 1596 | self.addCleanup(p.chmod, st.st_mode) |
| 1597 | self.assertNotEqual(p.stat(), st) |
| 1598 | |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1599 | @support.skip_unless_symlink |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1600 | def test_lstat(self): |
| 1601 | p = self.cls(BASE)/ 'linkA' |
| 1602 | st = p.stat() |
| 1603 | self.assertNotEqual(st, p.lstat()) |
| 1604 | |
| 1605 | def test_lstat_nosymlink(self): |
| 1606 | p = self.cls(BASE) / 'fileA' |
| 1607 | st = p.stat() |
| 1608 | self.assertEqual(st, p.lstat()) |
| 1609 | |
| 1610 | @unittest.skipUnless(pwd, "the pwd module is needed for this test") |
| 1611 | def test_owner(self): |
| 1612 | p = self.cls(BASE) / 'fileA' |
| 1613 | uid = p.stat().st_uid |
Antoine Pitrou | 2cf4b0f | 2013-11-25 19:51:53 +0100 | [diff] [blame] | 1614 | try: |
| 1615 | name = pwd.getpwuid(uid).pw_name |
| 1616 | except KeyError: |
| 1617 | self.skipTest( |
| 1618 | "user %d doesn't have an entry in the system database" % uid) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1619 | self.assertEqual(name, p.owner()) |
| 1620 | |
| 1621 | @unittest.skipUnless(grp, "the grp module is needed for this test") |
| 1622 | def test_group(self): |
| 1623 | p = self.cls(BASE) / 'fileA' |
| 1624 | gid = p.stat().st_gid |
Antoine Pitrou | 2cf4b0f | 2013-11-25 19:51:53 +0100 | [diff] [blame] | 1625 | try: |
| 1626 | name = grp.getgrgid(gid).gr_name |
| 1627 | except KeyError: |
| 1628 | self.skipTest( |
| 1629 | "group %d doesn't have an entry in the system database" % gid) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1630 | self.assertEqual(name, p.group()) |
| 1631 | |
| 1632 | def test_unlink(self): |
| 1633 | p = self.cls(BASE) / 'fileA' |
| 1634 | p.unlink() |
| 1635 | self.assertFileNotFound(p.stat) |
| 1636 | self.assertFileNotFound(p.unlink) |
| 1637 | |
zlohhcuB treboR | d9e006b | 2019-05-16 00:02:11 +0200 | [diff] [blame^] | 1638 | def test_unlink_missing_ok(self): |
| 1639 | p = self.cls(BASE) / 'fileAAA' |
| 1640 | self.assertFileNotFound(p.unlink) |
| 1641 | p.unlink(missing_ok=True) |
| 1642 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1643 | def test_rmdir(self): |
| 1644 | p = self.cls(BASE) / 'dirA' |
| 1645 | for q in p.iterdir(): |
| 1646 | q.unlink() |
| 1647 | p.rmdir() |
| 1648 | self.assertFileNotFound(p.stat) |
| 1649 | self.assertFileNotFound(p.unlink) |
| 1650 | |
Joannah Nanjekye | 6b5b013 | 2019-05-04 11:27:10 -0400 | [diff] [blame] | 1651 | def test_link_to(self): |
| 1652 | P = self.cls(BASE) |
| 1653 | p = P / 'fileA' |
| 1654 | size = p.stat().st_size |
| 1655 | # linking to another path. |
| 1656 | q = P / 'dirA' / 'fileAA' |
| 1657 | try: |
| 1658 | p.link_to(q) |
| 1659 | except PermissionError as e: |
| 1660 | self.skipTest('os.link(): %s' % e) |
| 1661 | self.assertEqual(q.stat().st_size, size) |
| 1662 | self.assertEqual(os.path.samefile(p, q), True) |
| 1663 | self.assertTrue(p.stat) |
| 1664 | # Linking to a str of a relative path. |
| 1665 | r = rel_join('fileAAA') |
| 1666 | q.link_to(r) |
| 1667 | self.assertEqual(os.stat(r).st_size, size) |
| 1668 | self.assertTrue(q.stat) |
| 1669 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1670 | def test_rename(self): |
| 1671 | P = self.cls(BASE) |
| 1672 | p = P / 'fileA' |
| 1673 | size = p.stat().st_size |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1674 | # Renaming to another path. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1675 | q = P / 'dirA' / 'fileAA' |
| 1676 | p.rename(q) |
| 1677 | self.assertEqual(q.stat().st_size, size) |
| 1678 | self.assertFileNotFound(p.stat) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1679 | # Renaming to a str of a relative path. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1680 | r = rel_join('fileAAA') |
| 1681 | q.rename(r) |
| 1682 | self.assertEqual(os.stat(r).st_size, size) |
| 1683 | self.assertFileNotFound(q.stat) |
| 1684 | |
| 1685 | def test_replace(self): |
| 1686 | P = self.cls(BASE) |
| 1687 | p = P / 'fileA' |
| 1688 | size = p.stat().st_size |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1689 | # Replacing a non-existing path. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1690 | q = P / 'dirA' / 'fileAA' |
| 1691 | p.replace(q) |
| 1692 | self.assertEqual(q.stat().st_size, size) |
| 1693 | self.assertFileNotFound(p.stat) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1694 | # Replacing another (existing) path. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1695 | r = rel_join('dirB', 'fileB') |
| 1696 | q.replace(r) |
| 1697 | self.assertEqual(os.stat(r).st_size, size) |
| 1698 | self.assertFileNotFound(q.stat) |
| 1699 | |
| 1700 | def test_touch_common(self): |
| 1701 | P = self.cls(BASE) |
| 1702 | p = P / 'newfileA' |
| 1703 | self.assertFalse(p.exists()) |
| 1704 | p.touch() |
| 1705 | self.assertTrue(p.exists()) |
Antoine Pitrou | 0f57564 | 2013-11-22 23:20:08 +0100 | [diff] [blame] | 1706 | st = p.stat() |
| 1707 | old_mtime = st.st_mtime |
| 1708 | old_mtime_ns = st.st_mtime_ns |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1709 | # Rewind the mtime sufficiently far in the past to work around |
| 1710 | # filesystem-specific timestamp granularity. |
| 1711 | os.utime(str(p), (old_mtime - 10, old_mtime - 10)) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1712 | # The file mtime should be refreshed by calling touch() again. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1713 | p.touch() |
Antoine Pitrou | 0f57564 | 2013-11-22 23:20:08 +0100 | [diff] [blame] | 1714 | st = p.stat() |
Antoine Pitrou | 2cf3917 | 2013-11-23 15:25:59 +0100 | [diff] [blame] | 1715 | self.assertGreaterEqual(st.st_mtime_ns, old_mtime_ns) |
| 1716 | self.assertGreaterEqual(st.st_mtime, old_mtime) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1717 | # Now with exist_ok=False. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1718 | p = P / 'newfileB' |
| 1719 | self.assertFalse(p.exists()) |
| 1720 | p.touch(mode=0o700, exist_ok=False) |
| 1721 | self.assertTrue(p.exists()) |
| 1722 | self.assertRaises(OSError, p.touch, exist_ok=False) |
| 1723 | |
Antoine Pitrou | 8b78493 | 2013-11-23 14:52:39 +0100 | [diff] [blame] | 1724 | def test_touch_nochange(self): |
| 1725 | P = self.cls(BASE) |
| 1726 | p = P / 'fileA' |
| 1727 | p.touch() |
| 1728 | with p.open('rb') as f: |
| 1729 | self.assertEqual(f.read().strip(), b"this is file A") |
| 1730 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1731 | def test_mkdir(self): |
| 1732 | P = self.cls(BASE) |
| 1733 | p = P / 'newdirA' |
| 1734 | self.assertFalse(p.exists()) |
| 1735 | p.mkdir() |
| 1736 | self.assertTrue(p.exists()) |
| 1737 | self.assertTrue(p.is_dir()) |
| 1738 | with self.assertRaises(OSError) as cm: |
| 1739 | p.mkdir() |
| 1740 | self.assertEqual(cm.exception.errno, errno.EEXIST) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1741 | |
| 1742 | def test_mkdir_parents(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1743 | # Creating a chain of directories. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1744 | p = self.cls(BASE, 'newdirB', 'newdirC') |
| 1745 | self.assertFalse(p.exists()) |
| 1746 | with self.assertRaises(OSError) as cm: |
| 1747 | p.mkdir() |
| 1748 | self.assertEqual(cm.exception.errno, errno.ENOENT) |
| 1749 | p.mkdir(parents=True) |
| 1750 | self.assertTrue(p.exists()) |
| 1751 | self.assertTrue(p.is_dir()) |
| 1752 | with self.assertRaises(OSError) as cm: |
| 1753 | p.mkdir(parents=True) |
| 1754 | self.assertEqual(cm.exception.errno, errno.EEXIST) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1755 | # Test `mode` arg. |
| 1756 | mode = stat.S_IMODE(p.stat().st_mode) # Default mode. |
Antoine Pitrou | 0048c98 | 2013-12-16 20:22:37 +0100 | [diff] [blame] | 1757 | p = self.cls(BASE, 'newdirD', 'newdirE') |
| 1758 | p.mkdir(0o555, parents=True) |
| 1759 | self.assertTrue(p.exists()) |
| 1760 | self.assertTrue(p.is_dir()) |
| 1761 | if os.name != 'nt': |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1762 | # The directory's permissions follow the mode argument. |
Gregory P. Smith | b599c61 | 2014-01-20 01:10:33 -0800 | [diff] [blame] | 1763 | self.assertEqual(stat.S_IMODE(p.stat().st_mode), 0o7555 & mode) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1764 | # The parent's permissions follow the default process settings. |
Antoine Pitrou | 0048c98 | 2013-12-16 20:22:37 +0100 | [diff] [blame] | 1765 | self.assertEqual(stat.S_IMODE(p.parent.stat().st_mode), mode) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1766 | |
Barry Warsaw | 7c549c4 | 2014-08-05 11:28:12 -0400 | [diff] [blame] | 1767 | def test_mkdir_exist_ok(self): |
| 1768 | p = self.cls(BASE, 'dirB') |
| 1769 | st_ctime_first = p.stat().st_ctime |
| 1770 | self.assertTrue(p.exists()) |
| 1771 | self.assertTrue(p.is_dir()) |
| 1772 | with self.assertRaises(FileExistsError) as cm: |
| 1773 | p.mkdir() |
| 1774 | self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1775 | p.mkdir(exist_ok=True) |
| 1776 | self.assertTrue(p.exists()) |
| 1777 | self.assertEqual(p.stat().st_ctime, st_ctime_first) |
| 1778 | |
| 1779 | def test_mkdir_exist_ok_with_parent(self): |
| 1780 | p = self.cls(BASE, 'dirC') |
| 1781 | self.assertTrue(p.exists()) |
| 1782 | with self.assertRaises(FileExistsError) as cm: |
| 1783 | p.mkdir() |
| 1784 | self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1785 | p = p / 'newdirC' |
| 1786 | p.mkdir(parents=True) |
| 1787 | st_ctime_first = p.stat().st_ctime |
| 1788 | self.assertTrue(p.exists()) |
| 1789 | with self.assertRaises(FileExistsError) as cm: |
| 1790 | p.mkdir(parents=True) |
| 1791 | self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1792 | p.mkdir(parents=True, exist_ok=True) |
| 1793 | self.assertTrue(p.exists()) |
| 1794 | self.assertEqual(p.stat().st_ctime, st_ctime_first) |
| 1795 | |
Serhiy Storchaka | af7b9ec | 2017-03-24 20:51:53 +0200 | [diff] [blame] | 1796 | def test_mkdir_exist_ok_root(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1797 | # Issue #25803: A drive root could raise PermissionError on Windows. |
Serhiy Storchaka | af7b9ec | 2017-03-24 20:51:53 +0200 | [diff] [blame] | 1798 | self.cls('/').resolve().mkdir(exist_ok=True) |
| 1799 | self.cls('/').resolve().mkdir(parents=True, exist_ok=True) |
| 1800 | |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1801 | @only_nt # XXX: not sure how to test this on POSIX. |
Steve Dower | d3c4853 | 2017-02-04 14:54:56 -0800 | [diff] [blame] | 1802 | def test_mkdir_with_unknown_drive(self): |
| 1803 | for d in 'ZYXWVUTSRQPONMLKJIHGFEDCBA': |
| 1804 | p = self.cls(d + ':\\') |
| 1805 | if not p.is_dir(): |
| 1806 | break |
| 1807 | else: |
| 1808 | self.skipTest("cannot find a drive that doesn't exist") |
| 1809 | with self.assertRaises(OSError): |
| 1810 | (p / 'child' / 'path').mkdir(parents=True) |
| 1811 | |
Barry Warsaw | 7c549c4 | 2014-08-05 11:28:12 -0400 | [diff] [blame] | 1812 | def test_mkdir_with_child_file(self): |
| 1813 | p = self.cls(BASE, 'dirB', 'fileB') |
| 1814 | self.assertTrue(p.exists()) |
| 1815 | # An exception is raised when the last path component is an existing |
| 1816 | # regular file, regardless of whether exist_ok is true or not. |
| 1817 | with self.assertRaises(FileExistsError) as cm: |
| 1818 | p.mkdir(parents=True) |
| 1819 | self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1820 | with self.assertRaises(FileExistsError) as cm: |
| 1821 | p.mkdir(parents=True, exist_ok=True) |
| 1822 | self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1823 | |
| 1824 | def test_mkdir_no_parents_file(self): |
| 1825 | p = self.cls(BASE, 'fileA') |
| 1826 | self.assertTrue(p.exists()) |
| 1827 | # An exception is raised when the last path component is an existing |
| 1828 | # regular file, regardless of whether exist_ok is true or not. |
| 1829 | with self.assertRaises(FileExistsError) as cm: |
| 1830 | p.mkdir() |
| 1831 | self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1832 | with self.assertRaises(FileExistsError) as cm: |
| 1833 | p.mkdir(exist_ok=True) |
| 1834 | self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1835 | |
Armin Rigo | 22a594a | 2017-04-13 20:08:15 +0200 | [diff] [blame] | 1836 | def test_mkdir_concurrent_parent_creation(self): |
| 1837 | for pattern_num in range(32): |
| 1838 | p = self.cls(BASE, 'dirCPC%d' % pattern_num) |
| 1839 | self.assertFalse(p.exists()) |
| 1840 | |
| 1841 | def my_mkdir(path, mode=0o777): |
| 1842 | path = str(path) |
| 1843 | # Emulate another process that would create the directory |
| 1844 | # just before we try to create it ourselves. We do it |
| 1845 | # in all possible pattern combinations, assuming that this |
| 1846 | # function is called at most 5 times (dirCPC/dir1/dir2, |
| 1847 | # dirCPC/dir1, dirCPC, dirCPC/dir1, dirCPC/dir1/dir2). |
| 1848 | if pattern.pop(): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1849 | os.mkdir(path, mode) # From another process. |
Armin Rigo | 22a594a | 2017-04-13 20:08:15 +0200 | [diff] [blame] | 1850 | concurrently_created.add(path) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1851 | os.mkdir(path, mode) # Our real call. |
Armin Rigo | 22a594a | 2017-04-13 20:08:15 +0200 | [diff] [blame] | 1852 | |
| 1853 | pattern = [bool(pattern_num & (1 << n)) for n in range(5)] |
| 1854 | concurrently_created = set() |
| 1855 | p12 = p / 'dir1' / 'dir2' |
| 1856 | try: |
| 1857 | with mock.patch("pathlib._normal_accessor.mkdir", my_mkdir): |
| 1858 | p12.mkdir(parents=True, exist_ok=False) |
| 1859 | except FileExistsError: |
| 1860 | self.assertIn(str(p12), concurrently_created) |
| 1861 | else: |
| 1862 | self.assertNotIn(str(p12), concurrently_created) |
| 1863 | self.assertTrue(p.exists()) |
| 1864 | |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1865 | @support.skip_unless_symlink |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1866 | def test_symlink_to(self): |
| 1867 | P = self.cls(BASE) |
| 1868 | target = P / 'fileA' |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1869 | # Symlinking a path target. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1870 | link = P / 'dirA' / 'linkAA' |
| 1871 | link.symlink_to(target) |
| 1872 | self.assertEqual(link.stat(), target.stat()) |
| 1873 | self.assertNotEqual(link.lstat(), target.stat()) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1874 | # Symlinking a str target. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1875 | link = P / 'dirA' / 'linkAAA' |
| 1876 | link.symlink_to(str(target)) |
| 1877 | self.assertEqual(link.stat(), target.stat()) |
| 1878 | self.assertNotEqual(link.lstat(), target.stat()) |
| 1879 | self.assertFalse(link.is_dir()) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1880 | # Symlinking to a directory. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1881 | target = P / 'dirB' |
| 1882 | link = P / 'dirA' / 'linkAAAA' |
| 1883 | link.symlink_to(target, target_is_directory=True) |
| 1884 | self.assertEqual(link.stat(), target.stat()) |
| 1885 | self.assertNotEqual(link.lstat(), target.stat()) |
| 1886 | self.assertTrue(link.is_dir()) |
| 1887 | self.assertTrue(list(link.iterdir())) |
| 1888 | |
| 1889 | def test_is_dir(self): |
| 1890 | P = self.cls(BASE) |
| 1891 | self.assertTrue((P / 'dirA').is_dir()) |
| 1892 | self.assertFalse((P / 'fileA').is_dir()) |
| 1893 | self.assertFalse((P / 'non-existing').is_dir()) |
Antoine Pitrou | 2b2852b | 2014-10-30 23:14:03 +0100 | [diff] [blame] | 1894 | self.assertFalse((P / 'fileA' / 'bah').is_dir()) |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1895 | if support.can_symlink(): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1896 | self.assertFalse((P / 'linkA').is_dir()) |
| 1897 | self.assertTrue((P / 'linkB').is_dir()) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1898 | self.assertFalse((P/ 'brokenLink').is_dir(), False) |
| 1899 | self.assertIs((P / 'dirA\udfff').is_dir(), False) |
| 1900 | self.assertIs((P / 'dirA\x00').is_dir(), False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1901 | |
| 1902 | def test_is_file(self): |
| 1903 | P = self.cls(BASE) |
| 1904 | self.assertTrue((P / 'fileA').is_file()) |
| 1905 | self.assertFalse((P / 'dirA').is_file()) |
| 1906 | self.assertFalse((P / 'non-existing').is_file()) |
Antoine Pitrou | 2b2852b | 2014-10-30 23:14:03 +0100 | [diff] [blame] | 1907 | self.assertFalse((P / 'fileA' / 'bah').is_file()) |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1908 | if support.can_symlink(): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1909 | self.assertTrue((P / 'linkA').is_file()) |
| 1910 | self.assertFalse((P / 'linkB').is_file()) |
| 1911 | self.assertFalse((P/ 'brokenLink').is_file()) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1912 | self.assertIs((P / 'fileA\udfff').is_file(), False) |
| 1913 | self.assertIs((P / 'fileA\x00').is_file(), False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1914 | |
Cooper Lees | 173ff4a | 2017-08-01 15:35:45 -0700 | [diff] [blame] | 1915 | @only_posix |
| 1916 | def test_is_mount(self): |
| 1917 | P = self.cls(BASE) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 1918 | R = self.cls('/') # TODO: Work out Windows. |
Cooper Lees | 173ff4a | 2017-08-01 15:35:45 -0700 | [diff] [blame] | 1919 | self.assertFalse((P / 'fileA').is_mount()) |
| 1920 | self.assertFalse((P / 'dirA').is_mount()) |
| 1921 | self.assertFalse((P / 'non-existing').is_mount()) |
| 1922 | self.assertFalse((P / 'fileA' / 'bah').is_mount()) |
| 1923 | self.assertTrue(R.is_mount()) |
| 1924 | if support.can_symlink(): |
| 1925 | self.assertFalse((P / 'linkA').is_mount()) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1926 | self.assertIs(self.cls('/\udfff').is_mount(), False) |
| 1927 | self.assertIs(self.cls('/\x00').is_mount(), False) |
Cooper Lees | 173ff4a | 2017-08-01 15:35:45 -0700 | [diff] [blame] | 1928 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1929 | def test_is_symlink(self): |
| 1930 | P = self.cls(BASE) |
| 1931 | self.assertFalse((P / 'fileA').is_symlink()) |
| 1932 | self.assertFalse((P / 'dirA').is_symlink()) |
| 1933 | self.assertFalse((P / 'non-existing').is_symlink()) |
Antoine Pitrou | 2b2852b | 2014-10-30 23:14:03 +0100 | [diff] [blame] | 1934 | self.assertFalse((P / 'fileA' / 'bah').is_symlink()) |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 1935 | if support.can_symlink(): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1936 | self.assertTrue((P / 'linkA').is_symlink()) |
| 1937 | self.assertTrue((P / 'linkB').is_symlink()) |
| 1938 | self.assertTrue((P/ 'brokenLink').is_symlink()) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1939 | self.assertIs((P / 'fileA\udfff').is_file(), False) |
| 1940 | self.assertIs((P / 'fileA\x00').is_file(), False) |
| 1941 | if support.can_symlink(): |
| 1942 | self.assertIs((P / 'linkA\udfff').is_file(), False) |
| 1943 | self.assertIs((P / 'linkA\x00').is_file(), False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1944 | |
| 1945 | def test_is_fifo_false(self): |
| 1946 | P = self.cls(BASE) |
| 1947 | self.assertFalse((P / 'fileA').is_fifo()) |
| 1948 | self.assertFalse((P / 'dirA').is_fifo()) |
| 1949 | self.assertFalse((P / 'non-existing').is_fifo()) |
Antoine Pitrou | 2b2852b | 2014-10-30 23:14:03 +0100 | [diff] [blame] | 1950 | self.assertFalse((P / 'fileA' / 'bah').is_fifo()) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1951 | self.assertIs((P / 'fileA\udfff').is_fifo(), False) |
| 1952 | self.assertIs((P / 'fileA\x00').is_fifo(), False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1953 | |
| 1954 | @unittest.skipUnless(hasattr(os, "mkfifo"), "os.mkfifo() required") |
| 1955 | def test_is_fifo_true(self): |
| 1956 | P = self.cls(BASE, 'myfifo') |
xdegaye | 92c2ca7 | 2017-11-12 17:31:07 +0100 | [diff] [blame] | 1957 | try: |
| 1958 | os.mkfifo(str(P)) |
| 1959 | except PermissionError as e: |
| 1960 | self.skipTest('os.mkfifo(): %s' % e) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1961 | self.assertTrue(P.is_fifo()) |
| 1962 | self.assertFalse(P.is_socket()) |
| 1963 | self.assertFalse(P.is_file()) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1964 | self.assertIs(self.cls(BASE, 'myfifo\udfff').is_fifo(), False) |
| 1965 | self.assertIs(self.cls(BASE, 'myfifo\x00').is_fifo(), False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1966 | |
| 1967 | def test_is_socket_false(self): |
| 1968 | P = self.cls(BASE) |
| 1969 | self.assertFalse((P / 'fileA').is_socket()) |
| 1970 | self.assertFalse((P / 'dirA').is_socket()) |
| 1971 | self.assertFalse((P / 'non-existing').is_socket()) |
Antoine Pitrou | 2b2852b | 2014-10-30 23:14:03 +0100 | [diff] [blame] | 1972 | self.assertFalse((P / 'fileA' / 'bah').is_socket()) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1973 | self.assertIs((P / 'fileA\udfff').is_socket(), False) |
| 1974 | self.assertIs((P / 'fileA\x00').is_socket(), False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1975 | |
| 1976 | @unittest.skipUnless(hasattr(socket, "AF_UNIX"), "Unix sockets required") |
| 1977 | def test_is_socket_true(self): |
| 1978 | P = self.cls(BASE, 'mysock') |
Antoine Pitrou | 330ce59 | 2013-11-22 18:05:06 +0100 | [diff] [blame] | 1979 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1980 | self.addCleanup(sock.close) |
Antoine Pitrou | 330ce59 | 2013-11-22 18:05:06 +0100 | [diff] [blame] | 1981 | try: |
| 1982 | sock.bind(str(P)) |
| 1983 | except OSError as e: |
Xavier de Gaye | e88ed05 | 2016-12-14 11:52:28 +0100 | [diff] [blame] | 1984 | if (isinstance(e, PermissionError) or |
| 1985 | "AF_UNIX path too long" in str(e)): |
Antoine Pitrou | 330ce59 | 2013-11-22 18:05:06 +0100 | [diff] [blame] | 1986 | self.skipTest("cannot bind Unix socket: " + str(e)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1987 | self.assertTrue(P.is_socket()) |
| 1988 | self.assertFalse(P.is_fifo()) |
| 1989 | self.assertFalse(P.is_file()) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1990 | self.assertIs(self.cls(BASE, 'mysock\udfff').is_socket(), False) |
| 1991 | self.assertIs(self.cls(BASE, 'mysock\x00').is_socket(), False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1992 | |
| 1993 | def test_is_block_device_false(self): |
| 1994 | P = self.cls(BASE) |
| 1995 | self.assertFalse((P / 'fileA').is_block_device()) |
| 1996 | self.assertFalse((P / 'dirA').is_block_device()) |
| 1997 | self.assertFalse((P / 'non-existing').is_block_device()) |
Antoine Pitrou | 2b2852b | 2014-10-30 23:14:03 +0100 | [diff] [blame] | 1998 | self.assertFalse((P / 'fileA' / 'bah').is_block_device()) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1999 | self.assertIs((P / 'fileA\udfff').is_block_device(), False) |
| 2000 | self.assertIs((P / 'fileA\x00').is_block_device(), False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2001 | |
| 2002 | def test_is_char_device_false(self): |
| 2003 | P = self.cls(BASE) |
| 2004 | self.assertFalse((P / 'fileA').is_char_device()) |
| 2005 | self.assertFalse((P / 'dirA').is_char_device()) |
| 2006 | self.assertFalse((P / 'non-existing').is_char_device()) |
Antoine Pitrou | 2b2852b | 2014-10-30 23:14:03 +0100 | [diff] [blame] | 2007 | self.assertFalse((P / 'fileA' / 'bah').is_char_device()) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 2008 | self.assertIs((P / 'fileA\udfff').is_char_device(), False) |
| 2009 | self.assertIs((P / 'fileA\x00').is_char_device(), False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2010 | |
| 2011 | def test_is_char_device_true(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 2012 | # Under Unix, /dev/null should generally be a char device. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2013 | P = self.cls('/dev/null') |
| 2014 | if not P.exists(): |
| 2015 | self.skipTest("/dev/null required") |
| 2016 | self.assertTrue(P.is_char_device()) |
| 2017 | self.assertFalse(P.is_block_device()) |
| 2018 | self.assertFalse(P.is_file()) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 2019 | self.assertIs(self.cls('/dev/null\udfff').is_char_device(), False) |
| 2020 | self.assertIs(self.cls('/dev/null\x00').is_char_device(), False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2021 | |
| 2022 | def test_pickling_common(self): |
| 2023 | p = self.cls(BASE, 'fileA') |
| 2024 | for proto in range(0, pickle.HIGHEST_PROTOCOL + 1): |
| 2025 | dumped = pickle.dumps(p, proto) |
| 2026 | pp = pickle.loads(dumped) |
| 2027 | self.assertEqual(pp.stat(), p.stat()) |
| 2028 | |
| 2029 | def test_parts_interning(self): |
| 2030 | P = self.cls |
| 2031 | p = P('/usr/bin/foo') |
| 2032 | q = P('/usr/local/bin') |
| 2033 | # 'usr' |
| 2034 | self.assertIs(p.parts[1], q.parts[1]) |
| 2035 | # 'bin' |
| 2036 | self.assertIs(p.parts[2], q.parts[3]) |
| 2037 | |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 2038 | def _check_complex_symlinks(self, link0_target): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 2039 | # Test solving a non-looping chain of symlinks (issue #19887). |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 2040 | P = self.cls(BASE) |
| 2041 | self.dirlink(os.path.join('link0', 'link0'), join('link1')) |
| 2042 | self.dirlink(os.path.join('link1', 'link1'), join('link2')) |
| 2043 | self.dirlink(os.path.join('link2', 'link2'), join('link3')) |
| 2044 | self.dirlink(link0_target, join('link0')) |
| 2045 | |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 2046 | # Resolve absolute paths. |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 2047 | p = (P / 'link0').resolve() |
| 2048 | self.assertEqual(p, P) |
| 2049 | self.assertEqual(str(p), BASE) |
| 2050 | p = (P / 'link1').resolve() |
| 2051 | self.assertEqual(p, P) |
| 2052 | self.assertEqual(str(p), BASE) |
| 2053 | p = (P / 'link2').resolve() |
| 2054 | self.assertEqual(p, P) |
| 2055 | self.assertEqual(str(p), BASE) |
| 2056 | p = (P / 'link3').resolve() |
| 2057 | self.assertEqual(p, P) |
| 2058 | self.assertEqual(str(p), BASE) |
| 2059 | |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 2060 | # Resolve relative paths. |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 2061 | old_path = os.getcwd() |
| 2062 | os.chdir(BASE) |
| 2063 | try: |
| 2064 | p = self.cls('link0').resolve() |
| 2065 | self.assertEqual(p, P) |
| 2066 | self.assertEqual(str(p), BASE) |
| 2067 | p = self.cls('link1').resolve() |
| 2068 | self.assertEqual(p, P) |
| 2069 | self.assertEqual(str(p), BASE) |
| 2070 | p = self.cls('link2').resolve() |
| 2071 | self.assertEqual(p, P) |
| 2072 | self.assertEqual(str(p), BASE) |
| 2073 | p = self.cls('link3').resolve() |
| 2074 | self.assertEqual(p, P) |
| 2075 | self.assertEqual(str(p), BASE) |
| 2076 | finally: |
| 2077 | os.chdir(old_path) |
| 2078 | |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 2079 | @support.skip_unless_symlink |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 2080 | def test_complex_symlinks_absolute(self): |
| 2081 | self._check_complex_symlinks(BASE) |
| 2082 | |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 2083 | @support.skip_unless_symlink |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 2084 | def test_complex_symlinks_relative(self): |
| 2085 | self._check_complex_symlinks('.') |
| 2086 | |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 2087 | @support.skip_unless_symlink |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 2088 | def test_complex_symlinks_relative_dot_dot(self): |
| 2089 | self._check_complex_symlinks(os.path.join('dirA', '..')) |
| 2090 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2091 | |
| 2092 | class PathTest(_BasePathTest, unittest.TestCase): |
| 2093 | cls = pathlib.Path |
| 2094 | |
| 2095 | def test_concrete_class(self): |
| 2096 | p = self.cls('a') |
| 2097 | self.assertIs(type(p), |
| 2098 | pathlib.WindowsPath if os.name == 'nt' else pathlib.PosixPath) |
| 2099 | |
| 2100 | def test_unsupported_flavour(self): |
| 2101 | if os.name == 'nt': |
| 2102 | self.assertRaises(NotImplementedError, pathlib.PosixPath) |
| 2103 | else: |
| 2104 | self.assertRaises(NotImplementedError, pathlib.WindowsPath) |
| 2105 | |
Berker Peksag | 4a208e4 | 2016-01-30 17:50:48 +0200 | [diff] [blame] | 2106 | def test_glob_empty_pattern(self): |
| 2107 | p = self.cls() |
| 2108 | with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'): |
| 2109 | list(p.glob('')) |
| 2110 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2111 | |
| 2112 | @only_posix |
| 2113 | class PosixPathTest(_BasePathTest, unittest.TestCase): |
| 2114 | cls = pathlib.PosixPath |
| 2115 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 2116 | def _check_symlink_loop(self, *args, strict=True): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2117 | path = self.cls(*args) |
| 2118 | with self.assertRaises(RuntimeError): |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 2119 | print(path.resolve(strict)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2120 | |
| 2121 | def test_open_mode(self): |
| 2122 | old_mask = os.umask(0) |
| 2123 | self.addCleanup(os.umask, old_mask) |
| 2124 | p = self.cls(BASE) |
| 2125 | with (p / 'new_file').open('wb'): |
| 2126 | pass |
| 2127 | st = os.stat(join('new_file')) |
| 2128 | self.assertEqual(stat.S_IMODE(st.st_mode), 0o666) |
| 2129 | os.umask(0o022) |
| 2130 | with (p / 'other_new_file').open('wb'): |
| 2131 | pass |
| 2132 | st = os.stat(join('other_new_file')) |
| 2133 | self.assertEqual(stat.S_IMODE(st.st_mode), 0o644) |
| 2134 | |
| 2135 | def test_touch_mode(self): |
| 2136 | old_mask = os.umask(0) |
| 2137 | self.addCleanup(os.umask, old_mask) |
| 2138 | p = self.cls(BASE) |
| 2139 | (p / 'new_file').touch() |
| 2140 | st = os.stat(join('new_file')) |
| 2141 | self.assertEqual(stat.S_IMODE(st.st_mode), 0o666) |
| 2142 | os.umask(0o022) |
| 2143 | (p / 'other_new_file').touch() |
| 2144 | st = os.stat(join('other_new_file')) |
| 2145 | self.assertEqual(stat.S_IMODE(st.st_mode), 0o644) |
| 2146 | (p / 'masked_new_file').touch(mode=0o750) |
| 2147 | st = os.stat(join('masked_new_file')) |
| 2148 | self.assertEqual(stat.S_IMODE(st.st_mode), 0o750) |
| 2149 | |
Vajrasky Kok | ec1f5df | 2017-03-29 02:32:35 +0700 | [diff] [blame] | 2150 | @support.skip_unless_symlink |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2151 | def test_resolve_loop(self): |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 2152 | # Loops with relative symlinks. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2153 | os.symlink('linkX/inside', join('linkX')) |
| 2154 | self._check_symlink_loop(BASE, 'linkX') |
| 2155 | os.symlink('linkY', join('linkY')) |
| 2156 | self._check_symlink_loop(BASE, 'linkY') |
| 2157 | os.symlink('linkZ/../linkZ', join('linkZ')) |
| 2158 | self._check_symlink_loop(BASE, 'linkZ') |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 2159 | # Non-strict |
| 2160 | self._check_symlink_loop(BASE, 'linkZ', 'foo', strict=False) |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 2161 | # Loops with absolute symlinks. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2162 | os.symlink(join('linkU/inside'), join('linkU')) |
| 2163 | self._check_symlink_loop(BASE, 'linkU') |
| 2164 | os.symlink(join('linkV'), join('linkV')) |
| 2165 | self._check_symlink_loop(BASE, 'linkV') |
| 2166 | os.symlink(join('linkW/../linkW'), join('linkW')) |
| 2167 | self._check_symlink_loop(BASE, 'linkW') |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 2168 | # Non-strict |
| 2169 | self._check_symlink_loop(BASE, 'linkW', 'foo', strict=False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2170 | |
| 2171 | def test_glob(self): |
| 2172 | P = self.cls |
| 2173 | p = P(BASE) |
Brett Cannon | fe77f4e | 2013-11-22 16:14:10 -0500 | [diff] [blame] | 2174 | given = set(p.glob("FILEa")) |
| 2175 | expect = set() if not support.fs_is_case_insensitive(BASE) else given |
| 2176 | self.assertEqual(given, expect) |
Antoine Pitrou | 2dd38fb | 2013-11-22 22:26:01 +0100 | [diff] [blame] | 2177 | self.assertEqual(set(p.glob("FILEa*")), set()) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2178 | |
| 2179 | def test_rglob(self): |
| 2180 | P = self.cls |
| 2181 | p = P(BASE, "dirC") |
Brett Cannon | fe77f4e | 2013-11-22 16:14:10 -0500 | [diff] [blame] | 2182 | given = set(p.rglob("FILEd")) |
| 2183 | expect = set() if not support.fs_is_case_insensitive(BASE) else given |
| 2184 | self.assertEqual(given, expect) |
Antoine Pitrou | 2dd38fb | 2013-11-22 22:26:01 +0100 | [diff] [blame] | 2185 | self.assertEqual(set(p.rglob("FILEd*")), set()) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2186 | |
Xavier de Gaye | fb24eea | 2016-12-13 09:11:38 +0100 | [diff] [blame] | 2187 | @unittest.skipUnless(hasattr(pwd, 'getpwall'), |
| 2188 | 'pwd module does not expose getpwall()') |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 2189 | def test_expanduser(self): |
| 2190 | P = self.cls |
| 2191 | support.import_module('pwd') |
| 2192 | import pwd |
| 2193 | pwdent = pwd.getpwuid(os.getuid()) |
| 2194 | username = pwdent.pw_name |
Serhiy Storchaka | a3fd0b2 | 2016-05-03 21:17:03 +0300 | [diff] [blame] | 2195 | userhome = pwdent.pw_dir.rstrip('/') or '/' |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 2196 | # Find arbitrary different user (if exists). |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 2197 | for pwdent in pwd.getpwall(): |
| 2198 | othername = pwdent.pw_name |
| 2199 | otherhome = pwdent.pw_dir.rstrip('/') |
Victor Stinner | e969439 | 2015-01-10 09:00:20 +0100 | [diff] [blame] | 2200 | if othername != username and otherhome: |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 2201 | break |
Anders Kaseorg | 5c0d462 | 2018-05-14 10:00:37 -0400 | [diff] [blame] | 2202 | else: |
| 2203 | othername = username |
| 2204 | otherhome = userhome |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 2205 | |
| 2206 | p1 = P('~/Documents') |
| 2207 | p2 = P('~' + username + '/Documents') |
| 2208 | p3 = P('~' + othername + '/Documents') |
| 2209 | p4 = P('../~' + username + '/Documents') |
| 2210 | p5 = P('/~' + username + '/Documents') |
| 2211 | p6 = P('') |
| 2212 | p7 = P('~fakeuser/Documents') |
| 2213 | |
| 2214 | with support.EnvironmentVarGuard() as env: |
| 2215 | env.pop('HOME', None) |
| 2216 | |
| 2217 | self.assertEqual(p1.expanduser(), P(userhome) / 'Documents') |
| 2218 | self.assertEqual(p2.expanduser(), P(userhome) / 'Documents') |
| 2219 | self.assertEqual(p3.expanduser(), P(otherhome) / 'Documents') |
| 2220 | self.assertEqual(p4.expanduser(), p4) |
| 2221 | self.assertEqual(p5.expanduser(), p5) |
| 2222 | self.assertEqual(p6.expanduser(), p6) |
| 2223 | self.assertRaises(RuntimeError, p7.expanduser) |
| 2224 | |
| 2225 | env['HOME'] = '/tmp' |
| 2226 | self.assertEqual(p1.expanduser(), P('/tmp/Documents')) |
| 2227 | self.assertEqual(p2.expanduser(), P(userhome) / 'Documents') |
| 2228 | self.assertEqual(p3.expanduser(), P(otherhome) / 'Documents') |
| 2229 | self.assertEqual(p4.expanduser(), p4) |
| 2230 | self.assertEqual(p5.expanduser(), p5) |
| 2231 | self.assertEqual(p6.expanduser(), p6) |
| 2232 | self.assertRaises(RuntimeError, p7.expanduser) |
| 2233 | |
Przemysław Spodymek | 216b745 | 2018-08-27 23:33:45 +0200 | [diff] [blame] | 2234 | @unittest.skipIf(sys.platform != "darwin", |
| 2235 | "Bad file descriptor in /dev/fd affects only macOS") |
| 2236 | def test_handling_bad_descriptor(self): |
| 2237 | try: |
| 2238 | file_descriptors = list(pathlib.Path('/dev/fd').rglob("*"))[3:] |
| 2239 | if not file_descriptors: |
| 2240 | self.skipTest("no file descriptors - issue was not reproduced") |
| 2241 | # Checking all file descriptors because there is no guarantee |
| 2242 | # which one will fail. |
| 2243 | for f in file_descriptors: |
| 2244 | f.exists() |
| 2245 | f.is_dir() |
| 2246 | f.is_file() |
| 2247 | f.is_symlink() |
| 2248 | f.is_block_device() |
| 2249 | f.is_char_device() |
| 2250 | f.is_fifo() |
| 2251 | f.is_socket() |
| 2252 | except OSError as e: |
| 2253 | if e.errno == errno.EBADF: |
| 2254 | self.fail("Bad file descriptor not handled.") |
| 2255 | raise |
| 2256 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2257 | |
| 2258 | @only_nt |
| 2259 | class WindowsPathTest(_BasePathTest, unittest.TestCase): |
| 2260 | cls = pathlib.WindowsPath |
| 2261 | |
| 2262 | def test_glob(self): |
| 2263 | P = self.cls |
| 2264 | p = P(BASE) |
| 2265 | self.assertEqual(set(p.glob("FILEa")), { P(BASE, "fileA") }) |
| 2266 | |
| 2267 | def test_rglob(self): |
| 2268 | P = self.cls |
| 2269 | p = P(BASE, "dirC") |
| 2270 | self.assertEqual(set(p.rglob("FILEd")), { P(BASE, "dirC/dirD/fileD") }) |
| 2271 | |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 2272 | def test_expanduser(self): |
| 2273 | P = self.cls |
| 2274 | with support.EnvironmentVarGuard() as env: |
| 2275 | env.pop('HOME', None) |
| 2276 | env.pop('USERPROFILE', None) |
| 2277 | env.pop('HOMEPATH', None) |
| 2278 | env.pop('HOMEDRIVE', None) |
| 2279 | env['USERNAME'] = 'alice' |
| 2280 | |
| 2281 | # test that the path returns unchanged |
| 2282 | p1 = P('~/My Documents') |
| 2283 | p2 = P('~alice/My Documents') |
| 2284 | p3 = P('~bob/My Documents') |
| 2285 | p4 = P('/~/My Documents') |
| 2286 | p5 = P('d:~/My Documents') |
| 2287 | p6 = P('') |
| 2288 | self.assertRaises(RuntimeError, p1.expanduser) |
| 2289 | self.assertRaises(RuntimeError, p2.expanduser) |
| 2290 | self.assertRaises(RuntimeError, p3.expanduser) |
| 2291 | self.assertEqual(p4.expanduser(), p4) |
| 2292 | self.assertEqual(p5.expanduser(), p5) |
| 2293 | self.assertEqual(p6.expanduser(), p6) |
| 2294 | |
| 2295 | def check(): |
| 2296 | env.pop('USERNAME', None) |
| 2297 | self.assertEqual(p1.expanduser(), |
| 2298 | P('C:/Users/alice/My Documents')) |
| 2299 | self.assertRaises(KeyError, p2.expanduser) |
| 2300 | env['USERNAME'] = 'alice' |
| 2301 | self.assertEqual(p2.expanduser(), |
| 2302 | P('C:/Users/alice/My Documents')) |
| 2303 | self.assertEqual(p3.expanduser(), |
| 2304 | P('C:/Users/bob/My Documents')) |
| 2305 | self.assertEqual(p4.expanduser(), p4) |
| 2306 | self.assertEqual(p5.expanduser(), p5) |
| 2307 | self.assertEqual(p6.expanduser(), p6) |
| 2308 | |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 2309 | # Test the first lookup key in the env vars. |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 2310 | env['HOME'] = 'C:\\Users\\alice' |
| 2311 | check() |
| 2312 | |
Anthony Shaw | 83da926 | 2019-01-07 07:31:29 +1100 | [diff] [blame] | 2313 | # Test that HOMEPATH is available instead. |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 2314 | env.pop('HOME', None) |
| 2315 | env['HOMEPATH'] = 'C:\\Users\\alice' |
| 2316 | check() |
| 2317 | |
| 2318 | env['HOMEDRIVE'] = 'C:\\' |
| 2319 | env['HOMEPATH'] = 'Users\\alice' |
| 2320 | check() |
| 2321 | |
| 2322 | env.pop('HOMEDRIVE', None) |
| 2323 | env.pop('HOMEPATH', None) |
| 2324 | env['USERPROFILE'] = 'C:\\Users\\alice' |
| 2325 | check() |
| 2326 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 2327 | |
| 2328 | if __name__ == "__main__": |
| 2329 | unittest.main() |