@@ -1362,25 +1362,25 @@ class ZstdWriterTests(AbstractWriterTests, unittest.TestCase):
13621362
13631363class AbstractRemoveTests :
13641364
1365- def _test_removing_indexes (self , test_files , indexes ):
1365+ def _test_removing_members (self , test_files , indexes ):
13661366 """Test underlying _remove_members() for removing members at given
13671367 indexes."""
13681368 # calculate the expected results
13691369 expected_files = []
1370- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1370+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
13711371 for i , (file , data ) in enumerate (test_files ):
13721372 if i not in indexes :
13731373 zh .writestr (file , data )
13741374 expected_files .append (file )
13751375 expected_size = os .path .getsize (TESTFN )
13761376
13771377 # prepare the test zip
1378- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1378+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
13791379 for file , data in test_files :
13801380 zh .writestr (file , data )
13811381
13821382 # do the removal and check the result
1383- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1383+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
13841384 members = {zh .infolist ()[i ] for i in indexes }
13851385 zh ._remove_members (members )
13861386
@@ -1401,19 +1401,14 @@ def _test_removing_combinations(self, test_files, n=None):
14011401 """Test underlying _remove_members() for removing random combinations
14021402 of members."""
14031403 ln = len (test_files )
1404- if n is None :
1405- # iterate n from 1 to all
1406- for n in range (1 , ln + 1 ):
1407- for indexes in itertools .combinations (range (ln ), n ):
1408- with self .subTest (remove = indexes ):
1409- self ._test_removing_indexes (test_files , indexes )
1410- else :
1404+ for n in (range (1 , ln + 1 ) if n is None else (n ,)):
14111405 for indexes in itertools .combinations (range (ln ), n ):
14121406 with self .subTest (remove = indexes ):
1413- self ._test_removing_indexes (test_files , indexes )
1407+ self ._test_removing_members (test_files , indexes )
14141408
14151409 def test_basic (self ):
1416- # Test underlying _remove_members() for removing random combinations of members.
1410+ """Test underlying _remove_members() for removing random combinations
1411+ of members."""
14171412 test_files = [
14181413 ('file0.txt' , b'Lorem ipsum dolor sit amet, consectetur adipiscing elit' ),
14191414 ('file1.txt' , b'Duis aute irure dolor in reprehenderit in voluptate velit esse' ),
@@ -1423,7 +1418,8 @@ def test_basic(self):
14231418 self ._test_removing_combinations (test_files )
14241419
14251420 def test_duplicated_arcname (self ):
1426- # Test underlying _remove_members() for removing any one of random duplicated members.
1421+ """Test underlying _remove_members() for removing any one of random
1422+ duplicated members."""
14271423 dupl_file = 'file.txt'
14281424 test_files = [
14291425 ('file0.txt' , b'Lorem ipsum dolor sit amet, consectetur adipiscing elit' ),
@@ -1441,11 +1437,11 @@ def test_duplicated_arcname(self):
14411437
14421438 for index in dups :
14431439 indexes = [index ]
1444- with self .subTest (dups = dups , indexes = indexes ):
1445- self ._test_removing_indexes (files , indexes )
1440+ with self .subTest (dups = dups , remove = indexes ):
1441+ self ._test_removing_members (files , indexes )
14461442
14471443 def test_non_physical (self ):
1448- # Test underlying _remove_members() for non-physical removing.
1444+ """ Test underlying _remove_members() for non-physical removing."""
14491445 test_files = [
14501446 ('file0.txt' , b'Lorem ipsum dolor sit amet, consectetur adipiscing elit' ),
14511447 ('file1.txt' , b'Duis aute irure dolor in reprehenderit in voluptate velit esse' ),
@@ -1458,14 +1454,14 @@ def test_non_physical(self):
14581454 with self .subTest (remove = indexes ):
14591455 # prepare the test zip
14601456 expected = {}
1461- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1457+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
14621458 for i , (file , data ) in enumerate (test_files ):
14631459 zh .writestr (file , data )
14641460 if i not in indexes :
14651461 expected [file ] = zh .getinfo (file ).header_offset
14661462
14671463 # do the removal and check the result
1468- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1464+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
14691465 members = {zh .infolist ()[i ] for i in indexes }
14701466 zh ._remove_members (members , remove_physical = False )
14711467 self .assertEqual (zh .namelist (), list (expected ))
@@ -1474,105 +1470,105 @@ def test_non_physical(self):
14741470 self .assertIsNone (zh .testzip ())
14751471
14761472 def test_verify (self ):
1477- # Test if params are passed to underlying _remove_members() correctly,
1478- # or never passed if conditions not met.
1473+ """ Test if params are passed to underlying _remove_members() correctly,
1474+ or never passed if conditions not met."""
14791475 file0 = 'file0.txt'
14801476 file = 'datafile.txt'
14811477 data = b'Sed ut perspiciatis unde omnis iste natus error sit voluptatem'
14821478
14831479 # closed: error and do nothing
1484- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1480+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
14851481 zh .writestr (file , data )
1486- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1482+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
14871483 zh .close ()
14881484 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
14891485 with self .assertRaises (ValueError ):
14901486 zh .remove (file )
14911487 mock_fn .assert_not_called ()
14921488
14931489 # writing: error and do nothing
1494- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1490+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
14951491 zh .writestr (file , data )
1496- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1492+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
14971493 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
14981494 with zh .open (file0 , 'w' ) as fh :
14991495 with self .assertRaises (ValueError ):
15001496 zh .remove (file )
15011497 mock_fn .assert_not_called ()
15021498
15031499 # mode 'r': error and do nothing
1504- with zipfile .ZipFile (TESTFN , 'r' ) as zh :
1500+ with zipfile .ZipFile (TESTFN , 'r' , self . compression ) as zh :
15051501 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
15061502 with self .assertRaises (ValueError ):
15071503 zh .remove (file )
15081504 mock_fn .assert_not_called ()
15091505
15101506 # mode 'a': the most general use case
1511- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1507+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
15121508 zh .writestr (file , data )
15131509 # -- remove with arcname
1514- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1510+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
15151511 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
15161512 zh .remove (file )
15171513 mock_fn .assert_called_once_with ({zh .getinfo (file )})
15181514 # -- remove with zinfo
1519- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1515+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
15201516 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
15211517 zinfo = zh .getinfo (file )
15221518 zh .remove (zinfo )
15231519 mock_fn .assert_called_once_with ({zinfo })
15241520 # -- remove with nonexist arcname
1525- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1521+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
15261522 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
15271523 with self .assertRaises (KeyError ):
15281524 zh .remove ('nonexist.file' )
15291525 mock_fn .assert_not_called ()
15301526 # -- remove with nonexist zinfo (even if same name)
1531- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1527+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
15321528 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
15331529 zinfo = zipfile .ZipInfo (file )
15341530 with self .assertRaises (KeyError ):
15351531 zh .remove (zinfo )
15361532 mock_fn .assert_not_called ()
15371533
15381534 # mode 'w': like 'a'; allows removing a just written member
1539- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1535+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
15401536 zh .writestr (file , data )
15411537 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
15421538 zh .remove (file )
15431539 mock_fn .assert_called_once_with ({zh .getinfo (file )})
15441540
15451541 # mode 'x': like 'w'
15461542 os .remove (TESTFN )
1547- with zipfile .ZipFile (TESTFN , 'x' ) as zh :
1543+ with zipfile .ZipFile (TESTFN , 'x' , self . compression ) as zh :
15481544 zh .writestr (file , data )
15491545 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
15501546 zh .remove (file )
15511547 mock_fn .assert_called_once_with ({zh .getinfo (file )})
15521548
15531549 def test_zip64 (self ):
1554- # Test if members use zip64.
1550+ """ Test if members use zip64."""
15551551 file = 'datafile.txt'
15561552 file1 = 'pre.txt'
15571553 file2 = 'post.txt'
15581554 data = b'Sed ut perspiciatis unde omnis iste natus error sit voluptatem'
15591555 data1 = b'Lorem ipsum dolor sit amet, consectetur adipiscing elit'
15601556 data2 = b'Duis aute irure dolor in reprehenderit in voluptate velit esse'
1561- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1557+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
15621558 with zh .open (file1 , 'w' , force_zip64 = True ) as fh :
15631559 fh .write (data1 )
15641560 with zh .open (file2 , 'w' , force_zip64 = True ) as fh :
15651561 fh .write (data2 )
15661562 expected_size = os .path .getsize (TESTFN )
15671563
1568- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1564+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
15691565 with zh .open (file1 , 'w' , force_zip64 = True ) as fh :
15701566 fh .write (data1 )
15711567 with zh .open (file , 'w' , force_zip64 = True ) as fh :
15721568 fh .write (data )
15731569 with zh .open (file2 , 'w' , force_zip64 = True ) as fh :
15741570 fh .write (data2 )
1575- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1571+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
15761572 zh .remove (file )
15771573 self .assertIsNone (zh .testzip ())
15781574 self .assertEqual (os .path .getsize (TESTFN ), expected_size )
0 commit comments