Commit e321274b authored by Victor Stinner's avatar Victor Stinner

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
parent ba8b0a7d
...@@ -10,11 +10,9 @@ import warnings ...@@ -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: ...@@ -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") with open(filename, "rb", 0) as f:
d = f.read() data = f.read()
f.close() self.assertEqual(data, b"foobar")
self.assertEqual(d, b"foobar")
self.assertLessEqual(
self.assertLessEqual( self.pathmodule.getctime(filename),
self.pathmodule.getctime(support.TESTFN), self.pathmodule.getmtime(filename)
self.pathmodule.getmtime(support.TESTFN) )
)
finally:
if not f.closed:
f.close()
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: ...@@ -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() create_file(filename)
self.assertIs(self.pathmodule.isdir(support.TESTFN), False) self.assertIs(self.pathmodule.isdir(filename), False)
os.remove(support.TESTFN)
os.mkdir(support.TESTFN) def test_isdir_dir(self):
self.assertIs(self.pathmodule.isdir(support.TESTFN), True) filename = support.TESTFN
os.rmdir(support.TESTFN) self.addCleanup(support.rmdir, filename)
finally: self.assertIs(self.pathmodule.isdir(filename), False)
if not f.close():
f.close() os.mkdir(filename)
support.unlink(support.TESTFN) self.assertIs(self.pathmodule.isdir(filename), True)
safe_rmdir(support.TESTFN)
def test_isfile_file(self):
def test_isfile(self): filename = support.TESTFN
self.assertIs(self.pathmodule.isfile(support.TESTFN), False) self.addCleanup(support.unlink, filename)
f = open(support.TESTFN, "wb") self.assertIs(self.pathmodule.isfile(filename), False)
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 create_file(filename)
def _create_file(filename): self.assertIs(self.pathmodule.isfile(filename), True)
with open(filename, 'wb') as f:
f.write(b'foo') 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: ...@@ -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): def test_samestat(self):
try: test_fn1 = support.TESTFN
test_fn1 = support.TESTFN + "1" test_fn2 = support.TESTFN + "2"
test_fn2 = support.TESTFN + "2" self.addCleanup(support.unlink, test_fn1)
self._create_file(test_fn1) self.addCleanup(support.unlink, test_fn2)
func(test_fn1, test_fn2) create_file(test_fn1)
self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2)) stat1 = os.stat(test_fn1)
os.remove(test_fn2) self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1)))
self._create_file(test_fn2) create_file(test_fn2)
self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2)) stat2 = os.stat(test_fn2)
finally: self.assertFalse(self.pathmodule.samestat(stat1, stat2))
os.remove(test_fn1)
os.remove(test_fn2)
def test_samestat(self): self.assertRaises(TypeError, self.pathmodule.samestat)
try:
test_fn = support.TESTFN + "1" def _test_samestat_on_link_func(self, func):
self._create_file(test_fn) test_fn1 = support.TESTFN + "1"
test_fns = [test_fn]*2 test_fn2 = support.TESTFN + "2"
stats = map(os.stat, test_fns) self.addCleanup(support.unlink, test_fn1)
self.assertTrue(self.pathmodule.samestat(*stats)) self.addCleanup(support.unlink, test_fn2)
finally:
os.remove(test_fn) 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: ...@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment