Repository URL to install this package:
"""
Tests common to genericpath, ntpath and posixpath
"""
import genericpath
import os
import sys
import unittest
import warnings
from test import support
from test.support.script_helper import assert_python_ok
from test.support import FakePath
def create_file(filename, data=b'foo'):
with open(filename, 'xb', 0) as fp:
fp.write(data)
class GenericTest:
common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime',
'getmtime', 'exists', 'isdir', 'isfile']
attributes = []
def test_no_argument(self):
for attr in self.common_attributes + self.attributes:
with self.assertRaises(TypeError):
getattr(self.pathmodule, attr)()
raise self.fail("{}.{}() did not raise a TypeError"
.format(self.pathmodule.__name__, attr))
def test_commonprefix(self):
commonprefix = self.pathmodule.commonprefix
self.assertEqual(
commonprefix([]),
""
)
self.assertEqual(
commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
"/home/swen"
)
self.assertEqual(
commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
"/home/swen/"
)
self.assertEqual(
commonprefix(["/home/swen/spam", "/home/swen/spam"]),
"/home/swen/spam"
)
self.assertEqual(
commonprefix(["home:swenson:spam", "home:swen:spam"]),
"home:swen"
)
self.assertEqual(
commonprefix([":home:swen:spam", ":home:swen:eggs"]),
":home:swen:"
)
self.assertEqual(
commonprefix([":home:swen:spam", ":home:swen:spam"]),
":home:swen:spam"
)
self.assertEqual(
commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]),
b"/home/swen"
)
self.assertEqual(
commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]),
b"/home/swen/"
)
self.assertEqual(
commonprefix([b"/home/swen/spam", b"/home/swen/spam"]),
b"/home/swen/spam"
)
self.assertEqual(
commonprefix([b"home:swenson:spam", b"home:swen:spam"]),
b"home:swen"
)
self.assertEqual(
commonprefix([b":home:swen:spam", b":home:swen:eggs"]),
b":home:swen:"
)
self.assertEqual(
commonprefix([b":home:swen:spam", b":home:swen:spam"]),
b":home:swen:spam"
)
testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
'aXc', 'abd', 'ab', 'aX', 'abcX']
for s1 in testlist:
for s2 in testlist:
p = commonprefix([s1, s2])
self.assertTrue(s1.startswith(p))
self.assertTrue(s2.startswith(p))
if s1 != s2:
n = len(p)
self.assertNotEqual(s1[n:n+1], s2[n:n+1])
def test_getsize(self):
filename = support.TESTFN
self.addCleanup(support.unlink, filename)
create_file(filename, b'Hello')
self.assertEqual(self.pathmodule.getsize(filename), 5)
os.remove(filename)
create_file(filename, b'Hello World!')
self.assertEqual(self.pathmodule.getsize(filename), 12)
def test_filetime(self):
filename = support.TESTFN
self.addCleanup(support.unlink, filename)
create_file(filename, b'foo')
with open(filename, "ab", 0) as f:
f.write(b"bar")
with open(filename, "rb", 0) as f:
data = f.read()
self.assertEqual(data, b"foobar")
self.assertLessEqual(
self.pathmodule.getctime(filename),
self.pathmodule.getmtime(filename)
)
def test_exists(self):
filename = support.TESTFN
bfilename = os.fsencode(filename)
self.addCleanup(support.unlink, filename)
self.assertIs(self.pathmodule.exists(filename), False)
self.assertIs(self.pathmodule.exists(bfilename), False)
create_file(filename)
self.assertIs(self.pathmodule.exists(filename), True)
self.assertIs(self.pathmodule.exists(bfilename), True)
self.assertIs(self.pathmodule.exists(filename + '\udfff'), False)
self.assertIs(self.pathmodule.exists(bfilename + b'\xff'), False)
self.assertIs(self.pathmodule.exists(filename + '\x00'), False)
self.assertIs(self.pathmodule.exists(bfilename + b'\x00'), False)
if self.pathmodule is not genericpath:
self.assertIs(self.pathmodule.lexists(filename), True)
self.assertIs(self.pathmodule.lexists(bfilename), True)
self.assertIs(self.pathmodule.lexists(filename + '\udfff'), False)
self.assertIs(self.pathmodule.lexists(bfilename + b'\xff'), False)
self.assertIs(self.pathmodule.lexists(filename + '\x00'), False)
self.assertIs(self.pathmodule.lexists(bfilename + b'\x00'), False)
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
def test_exists_fd(self):
r, w = os.pipe()
try:
self.assertTrue(self.pathmodule.exists(r))
finally:
os.close(r)
os.close(w)
self.assertFalse(self.pathmodule.exists(r))
def test_isdir(self):
filename = support.TESTFN
bfilename = os.fsencode(filename)
self.assertIs(self.pathmodule.isdir(filename), False)
self.assertIs(self.pathmodule.isdir(bfilename), False)
self.assertIs(self.pathmodule.isdir(filename + '\udfff'), False)
self.assertIs(self.pathmodule.isdir(bfilename + b'\xff'), False)
self.assertIs(self.pathmodule.isdir(filename + '\x00'), False)
self.assertIs(self.pathmodule.isdir(bfilename + b'\x00'), False)
try:
create_file(filename)
self.assertIs(self.pathmodule.isdir(filename), False)
self.assertIs(self.pathmodule.isdir(bfilename), False)
finally:
support.unlink(filename)
try:
os.mkdir(filename)
self.assertIs(self.pathmodule.isdir(filename), True)
self.assertIs(self.pathmodule.isdir(bfilename), True)
finally:
support.rmdir(filename)
def test_isfile(self):
filename = support.TESTFN
bfilename = os.fsencode(filename)
self.assertIs(self.pathmodule.isfile(filename), False)
self.assertIs(self.pathmodule.isfile(bfilename), False)
self.assertIs(self.pathmodule.isfile(filename + '\udfff'), False)
self.assertIs(self.pathmodule.isfile(bfilename + b'\xff'), False)
self.assertIs(self.pathmodule.isfile(filename + '\x00'), False)
self.assertIs(self.pathmodule.isfile(bfilename + b'\x00'), False)
try:
create_file(filename)
self.assertIs(self.pathmodule.isfile(filename), True)
self.assertIs(self.pathmodule.isfile(bfilename), True)
finally:
support.unlink(filename)
try:
os.mkdir(filename)
self.assertIs(self.pathmodule.isfile(filename), False)
self.assertIs(self.pathmodule.isfile(bfilename), False)
finally:
support.rmdir(filename)
def test_samefile(self):
file1 = support.TESTFN
file2 = support.TESTFN + "2"
self.addCleanup(support.unlink, file1)
self.addCleanup(support.unlink, file2)
create_file(file1)
self.assertTrue(self.pathmodule.samefile(file1, file1))
create_file(file2)
self.assertFalse(self.pathmodule.samefile(file1, file2))
self.assertRaises(TypeError, self.pathmodule.samefile)
def _test_samefile_on_link_func(self, func):
test_fn1 = support.TESTFN
test_fn2 = support.TESTFN + "2"
self.addCleanup(support.unlink, test_fn1)
self.addCleanup(support.unlink, test_fn2)
create_file(test_fn1)
func(test_fn1, test_fn2)
self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2))
os.remove(test_fn2)
create_file(test_fn2)
self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2))
@support.skip_unless_symlink
def test_samefile_on_symlink(self):
self._test_samefile_on_link_func(os.symlink)
def test_samefile_on_link(self):
try:
self._test_samefile_on_link_func(os.link)
except PermissionError as e:
self.skipTest('os.link(): %s' % e)
def test_samestat(self):
test_fn1 = support.TESTFN
test_fn2 = support.TESTFN + "2"
self.addCleanup(support.unlink, test_fn1)
self.addCleanup(support.unlink, test_fn2)
create_file(test_fn1)
stat1 = os.stat(test_fn1)
self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1)))
create_file(test_fn2)
stat2 = os.stat(test_fn2)
self.assertFalse(self.pathmodule.samestat(stat1, stat2))
self.assertRaises(TypeError, self.pathmodule.samestat)
def _test_samestat_on_link_func(self, func):
test_fn1 = support.TESTFN + "1"
test_fn2 = support.TESTFN + "2"
self.addCleanup(support.unlink, test_fn1)
self.addCleanup(support.unlink, test_fn2)
create_file(test_fn1)
func(test_fn1, test_fn2)
self.assertTrue(self.pathmodule.samestat(os.stat(test_fn1),
os.stat(test_fn2)))
os.remove(test_fn2)
create_file(test_fn2)
self.assertFalse(self.pathmodule.samestat(os.stat(test_fn1),
os.stat(test_fn2)))
@support.skip_unless_symlink
def test_samestat_on_symlink(self):
self._test_samestat_on_link_func(os.symlink)
def test_samestat_on_link(self):
try:
self._test_samestat_on_link_func(os.link)
except PermissionError as e:
self.skipTest('os.link(): %s' % e)
def test_sameopenfile(self):
filename = support.TESTFN
self.addCleanup(support.unlink, filename)
create_file(filename)
with open(filename, "rb", 0) as fp1:
fd1 = fp1.fileno()
with open(filename, "rb", 0) as fp2:
fd2 = fp2.fileno()
self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2))
class TestGenericTest(GenericTest, unittest.TestCase):
# Issue 16852: GenericTest can't inherit from unittest.TestCase
# for test discovery purposes; CommonTest inherits from GenericTest
# and is only meant to be inherited by others.
pathmodule = genericpath
def test_invalid_paths(self):
for attr in GenericTest.common_attributes:
# os.path.commonprefix doesn't raise ValueError
if attr == 'commonprefix':
continue
func = getattr(self.pathmodule, attr)
with self.subTest(attr=attr):
if attr in ('exists', 'isdir', 'isfile'):
func('/tmp\udfffabcds')
func(b'/tmp\xffabcds')
func('/tmp\x00abcds')
func(b'/tmp\x00abcds')
else:
with self.assertRaises((OSError, UnicodeEncodeError)):
func('/tmp\udfffabcds')
with self.assertRaises((OSError, UnicodeDecodeError)):
func(b'/tmp\xffabcds')
with self.assertRaisesRegex(ValueError, 'embedded null'):
func('/tmp\x00abcds')
with self.assertRaisesRegex(ValueError, 'embedded null'):
func(b'/tmp\x00abcds')
# Following TestCase is not supposed to be run from test_genericpath.
# It is inherited by other test modules (ntpath, posixpath).
class CommonTest(GenericTest):
common_attributes = GenericTest.common_attributes + [
# Properties
'curdir', 'pardir', 'extsep', 'sep',
'pathsep', 'defpath', 'altsep', 'devnull',
# Methods
'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
Loading ...