mirror of
https://github.com/python/cpython.git
synced 2025-10-17 20:28:43 +00:00
Enhance and modernize test_genericpath
* Replace "try/finally: os.remove()" with self.addCleanup(support.unlink) or self.addCleanup(support.rmdir): the support function handles the case when the file doesn't exist * Replace "try/finally: f.close()" with "with open(...) as f:" * test_getsize: add a second test with a different size * Create file using "x" mode to ensure that the file didn't exist before, to detect bugs in tests * Open files in unbuffered mode (buferring=0) to write immediatly data on disk * Replace map() with simpler code * Split isdir() unit test into two units tests to make them less dependant, same change for isfile() test * test_samefile(): test also two different files
This commit is contained in:
parent
ba8b0a7db4
commit
e321274b2e
1 changed files with 134 additions and 136 deletions
|
@ -10,11 +10,9 @@ import warnings
|
||||||
from test import support
|
from test import support
|
||||||
|
|
||||||
|
|
||||||
def safe_rmdir(dirname):
|
def create_file(filename, data=b'foo'):
|
||||||
try:
|
with open(filename, 'xb', 0) as fp:
|
||||||
os.rmdir(dirname)
|
fp.write(data)
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class GenericTest:
|
class GenericTest:
|
||||||
|
@ -97,52 +95,47 @@ class GenericTest:
|
||||||
self.assertNotEqual(s1[n:n+1], s2[n:n+1])
|
self.assertNotEqual(s1[n:n+1], s2[n:n+1])
|
||||||
|
|
||||||
def test_getsize(self):
|
def test_getsize(self):
|
||||||
f = open(support.TESTFN, "wb")
|
filename = support.TESTFN
|
||||||
try:
|
self.addCleanup(support.unlink, filename)
|
||||||
f.write(b"foo")
|
|
||||||
f.close()
|
|
||||||
self.assertEqual(self.pathmodule.getsize(support.TESTFN), 3)
|
|
||||||
finally:
|
|
||||||
if not f.closed:
|
|
||||||
f.close()
|
|
||||||
support.unlink(support.TESTFN)
|
|
||||||
|
|
||||||
def test_time(self):
|
create_file(filename, b'Hello')
|
||||||
f = open(support.TESTFN, "wb")
|
self.assertEqual(self.pathmodule.getsize(filename), 5)
|
||||||
try:
|
os.remove(filename)
|
||||||
f.write(b"foo")
|
|
||||||
f.close()
|
create_file(filename, b'Hello World!')
|
||||||
f = open(support.TESTFN, "ab")
|
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")
|
f.write(b"bar")
|
||||||
f.close()
|
|
||||||
f = open(support.TESTFN, "rb")
|
|
||||||
d = f.read()
|
|
||||||
f.close()
|
|
||||||
self.assertEqual(d, b"foobar")
|
|
||||||
|
|
||||||
self.assertLessEqual(
|
with open(filename, "rb", 0) as f:
|
||||||
self.pathmodule.getctime(support.TESTFN),
|
data = f.read()
|
||||||
self.pathmodule.getmtime(support.TESTFN)
|
self.assertEqual(data, b"foobar")
|
||||||
)
|
|
||||||
finally:
|
self.assertLessEqual(
|
||||||
if not f.closed:
|
self.pathmodule.getctime(filename),
|
||||||
f.close()
|
self.pathmodule.getmtime(filename)
|
||||||
support.unlink(support.TESTFN)
|
)
|
||||||
|
|
||||||
def test_exists(self):
|
def test_exists(self):
|
||||||
self.assertIs(self.pathmodule.exists(support.TESTFN), False)
|
filename = support.TESTFN
|
||||||
f = open(support.TESTFN, "wb")
|
self.addCleanup(support.unlink, filename)
|
||||||
try:
|
|
||||||
|
self.assertIs(self.pathmodule.exists(filename), False)
|
||||||
|
|
||||||
|
with open(filename, "xb") as f:
|
||||||
f.write(b"foo")
|
f.write(b"foo")
|
||||||
f.close()
|
|
||||||
self.assertIs(self.pathmodule.exists(support.TESTFN), True)
|
self.assertIs(self.pathmodule.exists(filename), True)
|
||||||
if not self.pathmodule == genericpath:
|
|
||||||
self.assertIs(self.pathmodule.lexists(support.TESTFN),
|
if not self.pathmodule == genericpath:
|
||||||
True)
|
self.assertIs(self.pathmodule.lexists(filename), True)
|
||||||
finally:
|
|
||||||
if not f.close():
|
|
||||||
f.close()
|
|
||||||
support.unlink(support.TESTFN)
|
|
||||||
|
|
||||||
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
|
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
|
||||||
def test_exists_fd(self):
|
def test_exists_fd(self):
|
||||||
|
@ -154,53 +147,66 @@ class GenericTest:
|
||||||
os.close(w)
|
os.close(w)
|
||||||
self.assertFalse(self.pathmodule.exists(r))
|
self.assertFalse(self.pathmodule.exists(r))
|
||||||
|
|
||||||
def test_isdir(self):
|
def test_isdir_file(self):
|
||||||
self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
|
filename = support.TESTFN
|
||||||
f = open(support.TESTFN, "wb")
|
self.addCleanup(support.unlink, filename)
|
||||||
try:
|
self.assertIs(self.pathmodule.isdir(filename), False)
|
||||||
f.write(b"foo")
|
|
||||||
f.close()
|
|
||||||
self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
|
|
||||||
os.remove(support.TESTFN)
|
|
||||||
os.mkdir(support.TESTFN)
|
|
||||||
self.assertIs(self.pathmodule.isdir(support.TESTFN), True)
|
|
||||||
os.rmdir(support.TESTFN)
|
|
||||||
finally:
|
|
||||||
if not f.close():
|
|
||||||
f.close()
|
|
||||||
support.unlink(support.TESTFN)
|
|
||||||
safe_rmdir(support.TESTFN)
|
|
||||||
|
|
||||||
def test_isfile(self):
|
create_file(filename)
|
||||||
self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
|
self.assertIs(self.pathmodule.isdir(filename), False)
|
||||||
f = open(support.TESTFN, "wb")
|
|
||||||
try:
|
|
||||||
f.write(b"foo")
|
|
||||||
f.close()
|
|
||||||
self.assertIs(self.pathmodule.isfile(support.TESTFN), True)
|
|
||||||
os.remove(support.TESTFN)
|
|
||||||
os.mkdir(support.TESTFN)
|
|
||||||
self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
|
|
||||||
os.rmdir(support.TESTFN)
|
|
||||||
finally:
|
|
||||||
if not f.close():
|
|
||||||
f.close()
|
|
||||||
support.unlink(support.TESTFN)
|
|
||||||
safe_rmdir(support.TESTFN)
|
|
||||||
|
|
||||||
@staticmethod
|
def test_isdir_dir(self):
|
||||||
def _create_file(filename):
|
filename = support.TESTFN
|
||||||
with open(filename, 'wb') as f:
|
self.addCleanup(support.rmdir, filename)
|
||||||
f.write(b'foo')
|
self.assertIs(self.pathmodule.isdir(filename), False)
|
||||||
|
|
||||||
|
os.mkdir(filename)
|
||||||
|
self.assertIs(self.pathmodule.isdir(filename), True)
|
||||||
|
|
||||||
|
def test_isfile_file(self):
|
||||||
|
filename = support.TESTFN
|
||||||
|
self.addCleanup(support.unlink, filename)
|
||||||
|
self.assertIs(self.pathmodule.isfile(filename), False)
|
||||||
|
|
||||||
|
create_file(filename)
|
||||||
|
self.assertIs(self.pathmodule.isfile(filename), True)
|
||||||
|
|
||||||
|
def test_isfile_dir(self):
|
||||||
|
filename = support.TESTFN
|
||||||
|
self.addCleanup(support.rmdir, filename)
|
||||||
|
self.assertIs(self.pathmodule.isfile(filename), False)
|
||||||
|
|
||||||
|
os.mkdir(filename)
|
||||||
|
self.assertIs(self.pathmodule.isfile(filename), False)
|
||||||
|
|
||||||
def test_samefile(self):
|
def test_samefile(self):
|
||||||
try:
|
file1 = support.TESTFN
|
||||||
test_fn = support.TESTFN + "1"
|
file2 = support.TESTFN + "2"
|
||||||
self._create_file(test_fn)
|
self.addCleanup(support.unlink, file1)
|
||||||
self.assertTrue(self.pathmodule.samefile(test_fn, test_fn))
|
self.addCleanup(support.unlink, file2)
|
||||||
self.assertRaises(TypeError, self.pathmodule.samefile)
|
|
||||||
finally:
|
create_file(file1)
|
||||||
os.remove(test_fn)
|
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
|
@support.skip_unless_symlink
|
||||||
def test_samefile_on_symlink(self):
|
def test_samefile_on_symlink(self):
|
||||||
|
@ -209,31 +215,37 @@ class GenericTest:
|
||||||
def test_samefile_on_link(self):
|
def test_samefile_on_link(self):
|
||||||
self._test_samefile_on_link_func(os.link)
|
self._test_samefile_on_link_func(os.link)
|
||||||
|
|
||||||
def _test_samefile_on_link_func(self, func):
|
|
||||||
try:
|
|
||||||
test_fn1 = support.TESTFN + "1"
|
|
||||||
test_fn2 = support.TESTFN + "2"
|
|
||||||
self._create_file(test_fn1)
|
|
||||||
|
|
||||||
func(test_fn1, test_fn2)
|
|
||||||
self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2))
|
|
||||||
os.remove(test_fn2)
|
|
||||||
|
|
||||||
self._create_file(test_fn2)
|
|
||||||
self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2))
|
|
||||||
finally:
|
|
||||||
os.remove(test_fn1)
|
|
||||||
os.remove(test_fn2)
|
|
||||||
|
|
||||||
def test_samestat(self):
|
def test_samestat(self):
|
||||||
try:
|
test_fn1 = support.TESTFN
|
||||||
test_fn = support.TESTFN + "1"
|
test_fn2 = support.TESTFN + "2"
|
||||||
self._create_file(test_fn)
|
self.addCleanup(support.unlink, test_fn1)
|
||||||
test_fns = [test_fn]*2
|
self.addCleanup(support.unlink, test_fn2)
|
||||||
stats = map(os.stat, test_fns)
|
|
||||||
self.assertTrue(self.pathmodule.samestat(*stats))
|
create_file(test_fn1)
|
||||||
finally:
|
stat1 = os.stat(test_fn1)
|
||||||
os.remove(test_fn)
|
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
|
@support.skip_unless_symlink
|
||||||
def test_samestat_on_symlink(self):
|
def test_samestat_on_symlink(self):
|
||||||
|
@ -242,31 +254,17 @@ class GenericTest:
|
||||||
def test_samestat_on_link(self):
|
def test_samestat_on_link(self):
|
||||||
self._test_samestat_on_link_func(os.link)
|
self._test_samestat_on_link_func(os.link)
|
||||||
|
|
||||||
def _test_samestat_on_link_func(self, func):
|
|
||||||
try:
|
|
||||||
test_fn1 = support.TESTFN + "1"
|
|
||||||
test_fn2 = support.TESTFN + "2"
|
|
||||||
self._create_file(test_fn1)
|
|
||||||
test_fns = (test_fn1, test_fn2)
|
|
||||||
func(*test_fns)
|
|
||||||
stats = map(os.stat, test_fns)
|
|
||||||
self.assertTrue(self.pathmodule.samestat(*stats))
|
|
||||||
os.remove(test_fn2)
|
|
||||||
|
|
||||||
self._create_file(test_fn2)
|
|
||||||
stats = map(os.stat, test_fns)
|
|
||||||
self.assertFalse(self.pathmodule.samestat(*stats))
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, self.pathmodule.samestat)
|
|
||||||
finally:
|
|
||||||
os.remove(test_fn1)
|
|
||||||
os.remove(test_fn2)
|
|
||||||
|
|
||||||
def test_sameopenfile(self):
|
def test_sameopenfile(self):
|
||||||
fname = support.TESTFN + "1"
|
filename = support.TESTFN
|
||||||
with open(fname, "wb") as a, open(fname, "wb") as b:
|
self.addCleanup(support.unlink, filename)
|
||||||
self.assertTrue(self.pathmodule.sameopenfile(
|
create_file(filename)
|
||||||
a.fileno(), b.fileno()))
|
|
||||||
|
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):
|
class TestGenericTest(GenericTest, unittest.TestCase):
|
||||||
# Issue 16852: GenericTest can't inherit from unittest.TestCase
|
# Issue 16852: GenericTest can't inherit from unittest.TestCase
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue