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