Skip to content

Commit 4801727

Browse files
committed
Add in ActiveTests so its easier to test in future
1 parent 2592f32 commit 4801727

File tree

3 files changed

+57
-63
lines changed

3 files changed

+57
-63
lines changed

Lib/tarfile.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -928,10 +928,10 @@ def __exit__(self, type, value, traceback):
928928
else:
929929
# An exception occurred. We must not call close() because
930930
# it would try to write end-of-archive blocks and padding.
931-
#INFO: This was used in Python3.6, but we aren't based off io.BufferedReader in Python2
932-
# if not self._extfileobj:
933-
# self.fileobj.close()
934-
self.close()
931+
if not self._extfileobj:
932+
self.fileobj.close()
933+
934+
935935
#class ExFileObject
936936

937937
#------------------
@@ -1962,14 +1962,16 @@ def gettarinfo(self, name=None, arcname=None, fileobj=None):
19621962
tarinfo.devminor = os.minor(statres.st_rdev)
19631963
return tarinfo
19641964

1965-
def list(self, verbose=True):
1965+
def list(self, verbose=True, members=None):
19661966
"""Print a table of contents to sys.stdout. If `verbose' is False, only
19671967
the names of the members are printed. If it is True, an `ls -l'-like
19681968
output is produced.
19691969
"""
19701970
self._check()
19711971

1972-
for tarinfo in self:
1972+
if members is None:
1973+
members = self.getmembers()
1974+
for tarinfo in members:
19731975
if verbose:
19741976
print filemode(tarinfo.mode),
19751977
print "%s/%s" % (tarinfo.uname or tarinfo.uid,

Lib/test/test_tarfile.py

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def test_fileobj_readlines(self):
8080
"I will gladly admit that Python is not the fastest running scripting language.\n",
8181
"fileobj.readlines() failed")
8282

83-
#FIX: This now breaks with the new tarfile
8483
def test_fileobj_iter(self):
8584
self.tar.extract("ustar/regtype", TEMPDIR)
8685
tarinfo = self.tar.getmember("ustar/regtype")
@@ -238,18 +237,17 @@ def test_list_verbose(self):
238237
self.assertIn('pax' + ('/123' * 125) + '/longlink link to pax' +
239238
('/123' * 125) + '/longname', out)
240239

241-
#INFO: Python2 list doesn't have the "members" functionality, so can't test
242-
# def test_list_members(self):
243-
# tio = io.TextIOWrapper(io.BytesIO(), 'ascii', newline='\n')
244-
# def members(tar):
245-
# for tarinfo in tar.getmembers():
246-
# if 'reg' in tarinfo.name:
247-
# yield tarinfo
248-
# with support.swap_attr(sys, 'stdout', tio):
249-
# self.tar.list(verbose=False, members=members(self.tar))
250-
# out = tio.detach().getvalue()
251-
# self.assertIn(b'ustar/regtype', out)
252-
# self.assertNotIn(b'ustar/conttype', out)
240+
def test_list_members(self):
241+
tio = io.BufferedRandom(io.BytesIO())
242+
def members(tar):
243+
for tarinfo in tar.getmembers():
244+
if 'reg' in tarinfo.name:
245+
yield tarinfo
246+
with support.swap_attr(sys, 'stdout', tio):
247+
self.tar.list(verbose=False, members=members(self.tar))
248+
out = tio.detach().getvalue()
249+
self.assertIn(b'ustar/regtype', out)
250+
self.assertNotIn(b'ustar/conttype', out)
253251

254252
class GzipListTest(ListTest):
255253
tarname = gzipname
@@ -577,9 +575,6 @@ def test_parallel_iteration(self):
577575
self.assertEqual(m1.offset, m2.offset)
578576
self.assertEqual(m1.name, m2.name)
579577

580-
# FIX: Brought in from Python3.6
581-
# class MiscReadTest(MiscReadTestBase, unittest.TestCase):
582-
# test_fail_comp = None
583578

584579
class StreamReadTest(CommonReadTest):
585580

@@ -622,13 +617,6 @@ def test_compare_members(self):
622617
finally:
623618
tar1.close()
624619

625-
# FIX: Brought in from Python3.6
626-
# class GzipStreamReadTest(GzipTest, StreamReadTest):
627-
# pass
628-
#
629-
#INFO: Python2 doesn't have LZMA
630-
# class LzmaStreamReadTest(LzmaTest, StreamReadTest):
631-
# pass
632620

633621
class TarTest:
634622
tarname = tarname
@@ -640,6 +628,7 @@ class TarTest:
640628
def mode(self):
641629
return self.prefix + self.suffix
642630

631+
643632
class DetectReadTest(TarTest, unittest.TestCase):
644633
def _testfunc_file(self, name, mode):
645634
try:
@@ -710,10 +699,6 @@ def test_detect_stream_bz2(self):
710699
self._testfunc_file(tmpname, "r|*")
711700

712701

713-
#INFO: Python2 doesn't have LZMA
714-
# class LzmaDetectReadTest(LzmaTest, DetectReadTest):
715-
# pass
716-
717702
class MemberReadTest(ReadTest):
718703

719704
def _test_member(self, tarinfo, chksum=None, **kwargs):
@@ -916,6 +901,7 @@ def test_eof_marker(self):
916901
t.size = tarfile.RECORDSIZE - tarfile.BLOCKSIZE
917902
tar.addfile(t, io.BytesIO(b"a" * t.size))
918903

904+
919905
class WriteTest(WriteTestBase):
920906

921907
mode = "w:"
@@ -984,6 +970,7 @@ def test_directory_size(self):
984970
finally:
985971
os.rmdir(path)
986972

973+
987974
#INFO: We don't have pathlib on Python2, not sure if we can really test this
988975
# def test_gettarinfo_pathlike_name(self):
989976
# with tarfile.open(tmpname, self.mode) as tar:
@@ -996,8 +983,7 @@ def test_directory_size(self):
996983
# self.assertEqual(tarinfo.name, tarinfo2.name)
997984
# self.assertEqual(tarinfo.size, 3)
998985

999-
@unittest.skipUnless(hasattr(os, "link"),
1000-
"Missing hardlink implementation")
986+
@unittest.skipUnless(hasattr(os, "link"),"Missing hardlink implementation")
1001987
def test_link_size(self):
1002988
if hasattr(os, "link"):
1003989
link = os.path.join(TEMPDIR, "link")
@@ -1546,21 +1532,6 @@ def test_create_taropen_pathlike_name(self):
15461532
self.assertIn('spameggs42', names[0])
15471533

15481534

1549-
#FIX: Brought in by Python3.6
1550-
# class GzipCreateTest(GzipTest, CreateTest):
1551-
# pass
1552-
1553-
1554-
#FIX: Brought in by Python3.6
1555-
# class Bz2CreateTest(Bz2Test, CreateTest):
1556-
# pass
1557-
1558-
1559-
#INFO: Python2 doesn't have LZMA
1560-
# class LzmaCreateTest(LzmaTest, CreateTest):
1561-
# pass
1562-
1563-
15641535
class CreateWithXModeTest(CreateTest):
15651536

15661537
prefix = "x"
@@ -1813,12 +1784,29 @@ def test_error_handler_utf8(self):
18131784
self.assertEqual(tar.getnames()[0], "\xe4\xf6\xfc/" + u"\u20ac".encode("utf8"))
18141785

18151786
# FIX: Came in on Python3.6
1816-
# # Test the same as above for the 100 bytes link field.
1817-
# def test_unicode_link1(self):
1818-
# self._test_ustar_link("0123456789" * 10)
1819-
# self._test_ustar_link("0123456789" * 10 + "0", ValueError)
1820-
# self._test_ustar_link("0123456789" * 9 + "01234567\xff")
1821-
# self._test_ustar_link("0123456789" * 9 + "012345678\xff", ValueError)
1787+
# Test the same as above for the 100 bytes link field.
1788+
def test_unicode_link1(self):
1789+
self._test_ustar_link("0123456789" * 10)
1790+
self._test_ustar_link("0123456789" * 10 + "0", ValueError)
1791+
# Use a two byte UTF-8 character
1792+
self._test_ustar_link("0123456789" * 9 + "01234567\303\251")
1793+
self._test_ustar_link("0123456789" * 9 + "012345678\303\251", ValueError)
1794+
1795+
def _test_ustar_link(self, name, exc=None):
1796+
with tarfile.open(tmpname, "w", format=0, encoding="utf-8") as tar:
1797+
t = tarfile.TarInfo("foo")
1798+
t.linkname = name
1799+
if exc is None:
1800+
tar.addfile(t)
1801+
else:
1802+
self.assertRaises(exc, tar.addfile, t)
1803+
1804+
if exc is None:
1805+
with tarfile.open(tmpname, "r", encoding="utf-8") as tar:
1806+
for t in tar:
1807+
self.assertEqual(name, t.linkname)
1808+
break
1809+
18221810

18231811
class AppendTest(unittest.TestCase):
18241812
# Test append mode (cp. patch #1652681).
@@ -1979,7 +1967,7 @@ def test_char_fields(self):
19791967
"foo")
19801968
self.assertEqual(tarfile.nts(b"foo\0bar\0"),
19811969
"foo")
1982-
1970+
19831971
def test_read_number_fields(self):
19841972
# Issue 13158: Test if GNU tar specific base-256 number fields
19851973
# are decoded correctly.
@@ -1989,12 +1977,6 @@ def test_read_number_fields(self):
19891977
0o10000000)
19901978
self.assertEqual(tarfile.nti(b"\x80\x00\x00\x00\xff\xff\xff\xff"),
19911979
0xffffffff)
1992-
# self.assertEqual(tarfile.nti(b"\xff\xff\xff\xff\xff\xff\xff\xff"),
1993-
# -1)
1994-
# self.assertEqual(tarfile.nti(b"\xff\xff\xff\xff\xff\xff\xff\x9c"),
1995-
# -100)
1996-
# self.assertEqual(tarfile.nti(b"\xff\x00\x00\x00\x00\x00\x00\x00"),
1997-
# -0x100000000000000)
19981980

19991981
# Issue 24514: Test if empty number fields are converted to zero.
20001982
self.assertEqual(tarfile.nti("\0"), 0)

Makefile.pre.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,16 @@ testall: @DEF_MAKE_RULE@ platform
873873
-$(TESTPYTHON) $(TESTPROG) -uall $(TESTOPTS)
874874
$(TESTPYTHON) $(TESTPROG) -uall $(TESTOPTS)
875875

876+
877+
TESTOPTSA= $(TESTOPTS) -vvv
878+
TESTPROGA= $(srcdir)/Lib/test/test_shutil.py
879+
TESTPYTHONA= $(RUNSHARED) ./$(BUILDPYTHON) -Wd -3 -E -tt $(TESTPYTHONOPTS)
880+
testA:
881+
-find $(srcdir)/Lib -name '*.py[co]' -print | xargs rm -f
882+
-$(TESTPYTHONA) $(TESTPROGA) $(TESTOPTSA)
883+
$(TESTPYTHONA) $(TESTPROGA) $(TESTOPTSA)
884+
885+
876886
# Run the unitests for both architectures in a Universal build on OSX
877887
# Must be run on an Intel box.
878888
testuniversal: @DEF_MAKE_RULE@ platform

0 commit comments

Comments
 (0)