1"""
2Tests common to genericpath, ntpath and posixpath
3"""
4
5import genericpath
6import os
7import sys
8import unittest
9import warnings
10from test.support import os_helper
11from test.support import warnings_helper
12from test.support.script_helper import assert_python_ok
13from test.support.os_helper import FakePath
14
15
16def create_file(filename, data=b'foo'):
17    with open(filename, 'xb', 0) as fp:
18        fp.write(data)
19
20
21class GenericTest:
22    common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime',
23                         'getmtime', 'exists', 'isdir', 'isfile']
24    attributes = []
25
26    def test_no_argument(self):
27        for attr in self.common_attributes + self.attributes:
28            with self.assertRaises(TypeError):
29                getattr(self.pathmodule, attr)()
30                raise self.fail("{}.{}() did not raise a TypeError"
31                                .format(self.pathmodule.__name__, attr))
32
33    def test_commonprefix(self):
34        commonprefix = self.pathmodule.commonprefix
35        self.assertEqual(
36            commonprefix([]),
37            ""
38        )
39        self.assertEqual(
40            commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
41            "/home/swen"
42        )
43        self.assertEqual(
44            commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
45            "/home/swen/"
46        )
47        self.assertEqual(
48            commonprefix(["/home/swen/spam", "/home/swen/spam"]),
49            "/home/swen/spam"
50        )
51        self.assertEqual(
52            commonprefix(["home:swenson:spam", "home:swen:spam"]),
53            "home:swen"
54        )
55        self.assertEqual(
56            commonprefix([":home:swen:spam", ":home:swen:eggs"]),
57            ":home:swen:"
58        )
59        self.assertEqual(
60            commonprefix([":home:swen:spam", ":home:swen:spam"]),
61            ":home:swen:spam"
62        )
63
64        self.assertEqual(
65            commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]),
66            b"/home/swen"
67        )
68        self.assertEqual(
69            commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]),
70            b"/home/swen/"
71        )
72        self.assertEqual(
73            commonprefix([b"/home/swen/spam", b"/home/swen/spam"]),
74            b"/home/swen/spam"
75        )
76        self.assertEqual(
77            commonprefix([b"home:swenson:spam", b"home:swen:spam"]),
78            b"home:swen"
79        )
80        self.assertEqual(
81            commonprefix([b":home:swen:spam", b":home:swen:eggs"]),
82            b":home:swen:"
83        )
84        self.assertEqual(
85            commonprefix([b":home:swen:spam", b":home:swen:spam"]),
86            b":home:swen:spam"
87        )
88
89        testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
90                    'aXc', 'abd', 'ab', 'aX', 'abcX']
91        for s1 in testlist:
92            for s2 in testlist:
93                p = commonprefix([s1, s2])
94                self.assertTrue(s1.startswith(p))
95                self.assertTrue(s2.startswith(p))
96                if s1 != s2:
97                    n = len(p)
98                    self.assertNotEqual(s1[n:n+1], s2[n:n+1])
99
100    def test_getsize(self):
101        filename = os_helper.TESTFN
102        self.addCleanup(os_helper.unlink, filename)
103
104        create_file(filename, b'Hello')
105        self.assertEqual(self.pathmodule.getsize(filename), 5)
106        os.remove(filename)
107
108        create_file(filename, b'Hello World!')
109        self.assertEqual(self.pathmodule.getsize(filename), 12)
110
111    def test_filetime(self):
112        filename = os_helper.TESTFN
113        self.addCleanup(os_helper.unlink, filename)
114
115        create_file(filename, b'foo')
116
117        with open(filename, "ab", 0) as f:
118            f.write(b"bar")
119
120        with open(filename, "rb", 0) as f:
121            data = f.read()
122        self.assertEqual(data, b"foobar")
123
124        self.assertLessEqual(
125            self.pathmodule.getctime(filename),
126            self.pathmodule.getmtime(filename)
127        )
128
129    def test_exists(self):
130        filename = os_helper.TESTFN
131        bfilename = os.fsencode(filename)
132        self.addCleanup(os_helper.unlink, filename)
133
134        self.assertIs(self.pathmodule.exists(filename), False)
135        self.assertIs(self.pathmodule.exists(bfilename), False)
136
137        create_file(filename)
138
139        self.assertIs(self.pathmodule.exists(filename), True)
140        self.assertIs(self.pathmodule.exists(bfilename), True)
141
142        self.assertIs(self.pathmodule.exists(filename + '\udfff'), False)
143        self.assertIs(self.pathmodule.exists(bfilename + b'\xff'), False)
144        self.assertIs(self.pathmodule.exists(filename + '\x00'), False)
145        self.assertIs(self.pathmodule.exists(bfilename + b'\x00'), False)
146
147        if self.pathmodule is not genericpath:
148            self.assertIs(self.pathmodule.lexists(filename), True)
149            self.assertIs(self.pathmodule.lexists(bfilename), True)
150
151            self.assertIs(self.pathmodule.lexists(filename + '\udfff'), False)
152            self.assertIs(self.pathmodule.lexists(bfilename + b'\xff'), False)
153            self.assertIs(self.pathmodule.lexists(filename + '\x00'), False)
154            self.assertIs(self.pathmodule.lexists(bfilename + b'\x00'), False)
155
156    @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
157    def test_exists_fd(self):
158        r, w = os.pipe()
159        try:
160            self.assertTrue(self.pathmodule.exists(r))
161        finally:
162            os.close(r)
163            os.close(w)
164        self.assertFalse(self.pathmodule.exists(r))
165
166    def test_isdir(self):
167        filename = os_helper.TESTFN
168        bfilename = os.fsencode(filename)
169        self.assertIs(self.pathmodule.isdir(filename), False)
170        self.assertIs(self.pathmodule.isdir(bfilename), False)
171
172        self.assertIs(self.pathmodule.isdir(filename + '\udfff'), False)
173        self.assertIs(self.pathmodule.isdir(bfilename + b'\xff'), False)
174        self.assertIs(self.pathmodule.isdir(filename + '\x00'), False)
175        self.assertIs(self.pathmodule.isdir(bfilename + b'\x00'), False)
176
177        try:
178            create_file(filename)
179            self.assertIs(self.pathmodule.isdir(filename), False)
180            self.assertIs(self.pathmodule.isdir(bfilename), False)
181        finally:
182            os_helper.unlink(filename)
183
184        try:
185            os.mkdir(filename)
186            self.assertIs(self.pathmodule.isdir(filename), True)
187            self.assertIs(self.pathmodule.isdir(bfilename), True)
188        finally:
189            os_helper.rmdir(filename)
190
191    def test_isfile(self):
192        filename = os_helper.TESTFN
193        bfilename = os.fsencode(filename)
194        self.assertIs(self.pathmodule.isfile(filename), False)
195        self.assertIs(self.pathmodule.isfile(bfilename), False)
196
197        self.assertIs(self.pathmodule.isfile(filename + '\udfff'), False)
198        self.assertIs(self.pathmodule.isfile(bfilename + b'\xff'), False)
199        self.assertIs(self.pathmodule.isfile(filename + '\x00'), False)
200        self.assertIs(self.pathmodule.isfile(bfilename + b'\x00'), False)
201
202        try:
203            create_file(filename)
204            self.assertIs(self.pathmodule.isfile(filename), True)
205            self.assertIs(self.pathmodule.isfile(bfilename), True)
206        finally:
207            os_helper.unlink(filename)
208
209        try:
210            os.mkdir(filename)
211            self.assertIs(self.pathmodule.isfile(filename), False)
212            self.assertIs(self.pathmodule.isfile(bfilename), False)
213        finally:
214            os_helper.rmdir(filename)
215
216    def test_samefile(self):
217        file1 = os_helper.TESTFN
218        file2 = os_helper.TESTFN + "2"
219        self.addCleanup(os_helper.unlink, file1)
220        self.addCleanup(os_helper.unlink, file2)
221
222        create_file(file1)
223        self.assertTrue(self.pathmodule.samefile(file1, file1))
224
225        create_file(file2)
226        self.assertFalse(self.pathmodule.samefile(file1, file2))
227
228        self.assertRaises(TypeError, self.pathmodule.samefile)
229
230    def _test_samefile_on_link_func(self, func):
231        test_fn1 = os_helper.TESTFN
232        test_fn2 = os_helper.TESTFN + "2"
233        self.addCleanup(os_helper.unlink, test_fn1)
234        self.addCleanup(os_helper.unlink, test_fn2)
235
236        create_file(test_fn1)
237
238        func(test_fn1, test_fn2)
239        self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2))
240        os.remove(test_fn2)
241
242        create_file(test_fn2)
243        self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2))
244
245    @os_helper.skip_unless_symlink
246    def test_samefile_on_symlink(self):
247        self._test_samefile_on_link_func(os.symlink)
248
249    def test_samefile_on_link(self):
250        try:
251            self._test_samefile_on_link_func(os.link)
252        except PermissionError as e:
253            self.skipTest('os.link(): %s' % e)
254
255    def test_samestat(self):
256        test_fn1 = os_helper.TESTFN
257        test_fn2 = os_helper.TESTFN + "2"
258        self.addCleanup(os_helper.unlink, test_fn1)
259        self.addCleanup(os_helper.unlink, test_fn2)
260
261        create_file(test_fn1)
262        stat1 = os.stat(test_fn1)
263        self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1)))
264
265        create_file(test_fn2)
266        stat2 = os.stat(test_fn2)
267        self.assertFalse(self.pathmodule.samestat(stat1, stat2))
268
269        self.assertRaises(TypeError, self.pathmodule.samestat)
270
271    def _test_samestat_on_link_func(self, func):
272        test_fn1 = os_helper.TESTFN + "1"
273        test_fn2 = os_helper.TESTFN + "2"
274        self.addCleanup(os_helper.unlink, test_fn1)
275        self.addCleanup(os_helper.unlink, test_fn2)
276
277        create_file(test_fn1)
278        func(test_fn1, test_fn2)
279        self.assertTrue(self.pathmodule.samestat(os.stat(test_fn1),
280                                                 os.stat(test_fn2)))
281        os.remove(test_fn2)
282
283        create_file(test_fn2)
284        self.assertFalse(self.pathmodule.samestat(os.stat(test_fn1),
285                                                  os.stat(test_fn2)))
286
287    @os_helper.skip_unless_symlink
288    def test_samestat_on_symlink(self):
289        self._test_samestat_on_link_func(os.symlink)
290
291    def test_samestat_on_link(self):
292        try:
293            self._test_samestat_on_link_func(os.link)
294        except PermissionError as e:
295            self.skipTest('os.link(): %s' % e)
296
297    def test_sameopenfile(self):
298        filename = os_helper.TESTFN
299        self.addCleanup(os_helper.unlink, filename)
300        create_file(filename)
301
302        with open(filename, "rb", 0) as fp1:
303            fd1 = fp1.fileno()
304            with open(filename, "rb", 0) as fp2:
305                fd2 = fp2.fileno()
306                self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2))
307
308
309class TestGenericTest(GenericTest, unittest.TestCase):
310    # Issue 16852: GenericTest can't inherit from unittest.TestCase
311    # for test discovery purposes; CommonTest inherits from GenericTest
312    # and is only meant to be inherited by others.
313    pathmodule = genericpath
314
315    def test_invalid_paths(self):
316        for attr in GenericTest.common_attributes:
317            # os.path.commonprefix doesn't raise ValueError
318            if attr == 'commonprefix':
319                continue
320            func = getattr(self.pathmodule, attr)
321            with self.subTest(attr=attr):
322                if attr in ('exists', 'isdir', 'isfile'):
323                    func('/tmp\udfffabcds')
324                    func(b'/tmp\xffabcds')
325                    func('/tmp\x00abcds')
326                    func(b'/tmp\x00abcds')
327                else:
328                    with self.assertRaises((OSError, UnicodeEncodeError)):
329                        func('/tmp\udfffabcds')
330                    with self.assertRaises((OSError, UnicodeDecodeError)):
331                        func(b'/tmp\xffabcds')
332                    with self.assertRaisesRegex(ValueError, 'embedded null'):
333                        func('/tmp\x00abcds')
334                    with self.assertRaisesRegex(ValueError, 'embedded null'):
335                        func(b'/tmp\x00abcds')
336
337# Following TestCase is not supposed to be run from test_genericpath.
338# It is inherited by other test modules (ntpath, posixpath).
339
340class CommonTest(GenericTest):
341    common_attributes = GenericTest.common_attributes + [
342        # Properties
343        'curdir', 'pardir', 'extsep', 'sep',
344        'pathsep', 'defpath', 'altsep', 'devnull',
345        # Methods
346        'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
347        'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
348        'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
349    ]
350
351    def test_normcase(self):
352        normcase = self.pathmodule.normcase
353        # check that normcase() is idempotent
354        for p in ["FoO/./BaR", b"FoO/./BaR"]:
355            p = normcase(p)
356            self.assertEqual(p, normcase(p))
357
358        self.assertEqual(normcase(''), '')
359        self.assertEqual(normcase(b''), b'')
360
361        # check that normcase raises a TypeError for invalid types
362        for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
363            self.assertRaises(TypeError, normcase, path)
364
365    def test_splitdrive(self):
366        # splitdrive for non-NT paths
367        splitdrive = self.pathmodule.splitdrive
368        self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
369        self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
370        self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
371
372        self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
373        self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
374        self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
375
376    def test_expandvars(self):
377        expandvars = self.pathmodule.expandvars
378        with os_helper.EnvironmentVarGuard() as env:
379            env.clear()
380            env["foo"] = "bar"
381            env["{foo"] = "baz1"
382            env["{foo}"] = "baz2"
383            self.assertEqual(expandvars("foo"), "foo")
384            self.assertEqual(expandvars("$foo bar"), "bar bar")
385            self.assertEqual(expandvars("${foo}bar"), "barbar")
386            self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
387            self.assertEqual(expandvars("$bar bar"), "$bar bar")
388            self.assertEqual(expandvars("$?bar"), "$?bar")
389            self.assertEqual(expandvars("$foo}bar"), "bar}bar")
390            self.assertEqual(expandvars("${foo"), "${foo")
391            self.assertEqual(expandvars("${{foo}}"), "baz1}")
392            self.assertEqual(expandvars("$foo$foo"), "barbar")
393            self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
394
395            self.assertEqual(expandvars(b"foo"), b"foo")
396            self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
397            self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
398            self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
399            self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
400            self.assertEqual(expandvars(b"$?bar"), b"$?bar")
401            self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
402            self.assertEqual(expandvars(b"${foo"), b"${foo")
403            self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
404            self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
405            self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
406
407    @unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
408    def test_expandvars_nonascii(self):
409        expandvars = self.pathmodule.expandvars
410        def check(value, expected):
411            self.assertEqual(expandvars(value), expected)
412        with os_helper.EnvironmentVarGuard() as env:
413            env.clear()
414            nonascii = os_helper.FS_NONASCII
415            env['spam'] = nonascii
416            env[nonascii] = 'ham' + nonascii
417            check(nonascii, nonascii)
418            check('$spam bar', '%s bar' % nonascii)
419            check('${spam}bar', '%sbar' % nonascii)
420            check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
421            check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii)
422            check('$spam}bar', '%s}bar' % nonascii)
423
424            check(os.fsencode(nonascii), os.fsencode(nonascii))
425            check(b'$spam bar', os.fsencode('%s bar' % nonascii))
426            check(b'${spam}bar', os.fsencode('%sbar' % nonascii))
427            check(os.fsencode('${%s}bar' % nonascii),
428                  os.fsencode('ham%sbar' % nonascii))
429            check(os.fsencode('$bar%s bar' % nonascii),
430                  os.fsencode('$bar%s bar' % nonascii))
431            check(b'$spam}bar', os.fsencode('%s}bar' % nonascii))
432
433    def test_abspath(self):
434        self.assertIn("foo", self.pathmodule.abspath("foo"))
435        with warnings.catch_warnings():
436            warnings.simplefilter("ignore", DeprecationWarning)
437            self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
438
439        # avoid UnicodeDecodeError on Windows
440        undecodable_path = b'' if sys.platform == 'win32' else b'f\xf2\xf2'
441
442        # Abspath returns bytes when the arg is bytes
443        with warnings.catch_warnings():
444            warnings.simplefilter("ignore", DeprecationWarning)
445            for path in (b'', b'foo', undecodable_path, b'/foo', b'C:\\'):
446                self.assertIsInstance(self.pathmodule.abspath(path), bytes)
447
448    def test_realpath(self):
449        self.assertIn("foo", self.pathmodule.realpath("foo"))
450        with warnings.catch_warnings():
451            warnings.simplefilter("ignore", DeprecationWarning)
452            self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
453
454    def test_normpath_issue5827(self):
455        # Make sure normpath preserves unicode
456        for path in ('', '.', '/', '\\', '///foo/.//bar//'):
457            self.assertIsInstance(self.pathmodule.normpath(path), str)
458
459    def test_abspath_issue3426(self):
460        # Check that abspath returns unicode when the arg is unicode
461        # with both ASCII and non-ASCII cwds.
462        abspath = self.pathmodule.abspath
463        for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
464            self.assertIsInstance(abspath(path), str)
465
466        unicwd = '\xe7w\xf0'
467        try:
468            os.fsencode(unicwd)
469        except (AttributeError, UnicodeEncodeError):
470            # FS encoding is probably ASCII
471            pass
472        else:
473            with os_helper.temp_cwd(unicwd):
474                for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
475                    self.assertIsInstance(abspath(path), str)
476
477    def test_nonascii_abspath(self):
478        if (os_helper.TESTFN_UNDECODABLE
479        # Mac OS X denies the creation of a directory with an invalid
480        # UTF-8 name. Windows allows creating a directory with an
481        # arbitrary bytes name, but fails to enter this directory
482        # (when the bytes name is used).
483        and sys.platform not in ('win32', 'darwin')):
484            name = os_helper.TESTFN_UNDECODABLE
485        elif os_helper.TESTFN_NONASCII:
486            name = os_helper.TESTFN_NONASCII
487        else:
488            self.skipTest("need os_helper.TESTFN_NONASCII")
489
490        with warnings.catch_warnings():
491            warnings.simplefilter("ignore", DeprecationWarning)
492            with os_helper.temp_cwd(name):
493                self.test_abspath()
494
495    def test_join_errors(self):
496        # Check join() raises friendly TypeErrors.
497        with warnings_helper.check_warnings(('', BytesWarning), quiet=True):
498            errmsg = "Can't mix strings and bytes in path components"
499            with self.assertRaisesRegex(TypeError, errmsg):
500                self.pathmodule.join(b'bytes', 'str')
501            with self.assertRaisesRegex(TypeError, errmsg):
502                self.pathmodule.join('str', b'bytes')
503            # regression, see #15377
504            with self.assertRaisesRegex(TypeError, 'int'):
505                self.pathmodule.join(42, 'str')
506            with self.assertRaisesRegex(TypeError, 'int'):
507                self.pathmodule.join('str', 42)
508            with self.assertRaisesRegex(TypeError, 'int'):
509                self.pathmodule.join(42)
510            with self.assertRaisesRegex(TypeError, 'list'):
511                self.pathmodule.join([])
512            with self.assertRaisesRegex(TypeError, 'bytearray'):
513                self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar'))
514
515    def test_relpath_errors(self):
516        # Check relpath() raises friendly TypeErrors.
517        with warnings_helper.check_warnings(
518                ('', (BytesWarning, DeprecationWarning)), quiet=True):
519            errmsg = "Can't mix strings and bytes in path components"
520            with self.assertRaisesRegex(TypeError, errmsg):
521                self.pathmodule.relpath(b'bytes', 'str')
522            with self.assertRaisesRegex(TypeError, errmsg):
523                self.pathmodule.relpath('str', b'bytes')
524            with self.assertRaisesRegex(TypeError, 'int'):
525                self.pathmodule.relpath(42, 'str')
526            with self.assertRaisesRegex(TypeError, 'int'):
527                self.pathmodule.relpath('str', 42)
528            with self.assertRaisesRegex(TypeError, 'bytearray'):
529                self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar'))
530
531    def test_import(self):
532        assert_python_ok('-S', '-c', 'import ' + self.pathmodule.__name__)
533
534
535class PathLikeTests(unittest.TestCase):
536
537    def setUp(self):
538        self.file_name = os_helper.TESTFN
539        self.file_path = FakePath(os_helper.TESTFN)
540        self.addCleanup(os_helper.unlink, self.file_name)
541        create_file(self.file_name, b"test_genericpath.PathLikeTests")
542
543    def assertPathEqual(self, func):
544        self.assertEqual(func(self.file_path), func(self.file_name))
545
546    def test_path_exists(self):
547        self.assertPathEqual(os.path.exists)
548
549    def test_path_isfile(self):
550        self.assertPathEqual(os.path.isfile)
551
552    def test_path_isdir(self):
553        self.assertPathEqual(os.path.isdir)
554
555    def test_path_commonprefix(self):
556        self.assertEqual(os.path.commonprefix([self.file_path, self.file_name]),
557                         self.file_name)
558
559    def test_path_getsize(self):
560        self.assertPathEqual(os.path.getsize)
561
562    def test_path_getmtime(self):
563        self.assertPathEqual(os.path.getatime)
564
565    def test_path_getctime(self):
566        self.assertPathEqual(os.path.getctime)
567
568    def test_path_samefile(self):
569        self.assertTrue(os.path.samefile(self.file_path, self.file_name))
570
571
572if __name__ == "__main__":
573    unittest.main()
574