@@ -217,6 +217,84 @@ class LzmaUstarReadTest(LzmaTest, UstarReadTest):
217217 pass
218218
219219
220+ class ListTest (ReadTest , unittest .TestCase ):
221+
222+ # Override setUp to use default encoding (UTF-8)
223+ def setUp (self ):
224+ self .tar = tarfile .open (self .tarname , mode = self .mode )
225+
226+ def test_list (self ):
227+ tio = io .TextIOWrapper (io .BytesIO (), 'ascii' , newline = '\n ' )
228+ with support .swap_attr (sys , 'stdout' , tio ):
229+ self .tar .list (verbose = False )
230+ out = tio .detach ().getvalue ()
231+ self .assertIn (b'ustar/conttype' , out )
232+ self .assertIn (b'ustar/regtype' , out )
233+ self .assertIn (b'ustar/lnktype' , out )
234+ self .assertIn (b'ustar' + (b'/12345' * 40 ) + b'67/longname' , out )
235+ self .assertIn (b'./ustar/linktest2/symtype' , out )
236+ self .assertIn (b'./ustar/linktest2/lnktype' , out )
237+ # Make sure it puts trailing slash for directory
238+ self .assertIn (b'ustar/dirtype/' , out )
239+ self .assertIn (b'ustar/dirtype-with-size/' , out )
240+ # Make sure it is able to print unencodable characters
241+ self .assertIn (br'ustar/umlauts-'
242+ br'\udcc4\udcd6\udcdc\udce4\udcf6\udcfc\udcdf' , out )
243+ self .assertIn (br'misc/regtype-hpux-signed-chksum-'
244+ br'\udcc4\udcd6\udcdc\udce4\udcf6\udcfc\udcdf' , out )
245+ self .assertIn (br'misc/regtype-old-v7-signed-chksum-'
246+ br'\udcc4\udcd6\udcdc\udce4\udcf6\udcfc\udcdf' , out )
247+ self .assertIn (br'pax/bad-pax-\udce4\udcf6\udcfc' , out )
248+ self .assertIn (br'pax/hdrcharset-\udce4\udcf6\udcfc' , out )
249+ # Make sure it prints files separated by one newline without any
250+ # 'ls -l'-like accessories if verbose flag is not being used
251+ # ...
252+ # ustar/conttype
253+ # ustar/regtype
254+ # ...
255+ self .assertRegex (out , br'ustar/conttype ?\r?\n'
256+ br'ustar/regtype ?\r?\n' )
257+ # Make sure it does not print the source of link without verbose flag
258+ self .assertNotIn (b'link to' , out )
259+ self .assertNotIn (b'->' , out )
260+
261+ def test_list_verbose (self ):
262+ tio = io .TextIOWrapper (io .BytesIO (), 'ascii' , newline = '\n ' )
263+ with support .swap_attr (sys , 'stdout' , tio ):
264+ self .tar .list (verbose = True )
265+ out = tio .detach ().getvalue ()
266+ # Make sure it prints files separated by one newline with 'ls -l'-like
267+ # accessories if verbose flag is being used
268+ # ...
269+ # ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/conttype
270+ # ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/regtype
271+ # ...
272+ self .assertRegex (out , (br'-rw-r--r-- tarfile/tarfile\s+7011 '
273+ br'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d '
274+ br'ustar/\w+type ?\r?\n' ) * 2 )
275+ # Make sure it prints the source of link with verbose flag
276+ self .assertIn (b'ustar/symtype -> regtype' , out )
277+ self .assertIn (b'./ustar/linktest2/symtype -> ../linktest1/regtype' , out )
278+ self .assertIn (b'./ustar/linktest2/lnktype link to '
279+ b'./ustar/linktest1/regtype' , out )
280+ self .assertIn (b'gnu' + (b'/123' * 125 ) + b'/longlink link to gnu' +
281+ (b'/123' * 125 ) + b'/longname' , out )
282+ self .assertIn (b'pax' + (b'/123' * 125 ) + b'/longlink link to pax' +
283+ (b'/123' * 125 ) + b'/longname' , out )
284+
285+
286+ class GzipListTest (GzipTest , ListTest ):
287+ pass
288+
289+
290+ class Bz2ListTest (Bz2Test , ListTest ):
291+ pass
292+
293+
294+ class LzmaListTest (LzmaTest , ListTest ):
295+ pass
296+
297+
220298class CommonReadTest (ReadTest ):
221299
222300 def test_empty_tarfile (self ):
0 commit comments