@@ -1149,25 +1149,25 @@ class LzmaWriterTests(AbstractWriterTests, unittest.TestCase):
11491149
11501150class AbstractRemoveTests :
11511151
1152- def _test_removing_indexes (self , test_files , indexes ):
1152+ def _test_removing_members (self , test_files , indexes ):
11531153 """Test underlying _remove_members() for removing members at given
11541154 indexes."""
11551155 # calculate the expected results
11561156 expected_files = []
1157- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1157+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
11581158 for i , (file , data ) in enumerate (test_files ):
11591159 if i not in indexes :
11601160 zh .writestr (file , data )
11611161 expected_files .append (file )
11621162 expected_size = os .path .getsize (TESTFN )
11631163
11641164 # prepare the test zip
1165- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1165+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
11661166 for file , data in test_files :
11671167 zh .writestr (file , data )
11681168
11691169 # do the removal and check the result
1170- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1170+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
11711171 members = {zh .infolist ()[i ] for i in indexes }
11721172 zh ._remove_members (members )
11731173
@@ -1188,19 +1188,14 @@ def _test_removing_combinations(self, test_files, n=None):
11881188 """Test underlying _remove_members() for removing random combinations
11891189 of members."""
11901190 ln = len (test_files )
1191- if n is None :
1192- # iterate n from 1 to all
1193- for n in range (1 , ln + 1 ):
1194- for indexes in itertools .combinations (range (ln ), n ):
1195- with self .subTest (remove = indexes ):
1196- self ._test_removing_indexes (test_files , indexes )
1197- else :
1191+ for n in (range (1 , ln + 1 ) if n is None else (n ,)):
11981192 for indexes in itertools .combinations (range (ln ), n ):
11991193 with self .subTest (remove = indexes ):
1200- self ._test_removing_indexes (test_files , indexes )
1194+ self ._test_removing_members (test_files , indexes )
12011195
12021196 def test_basic (self ):
1203- # Test underlying _remove_members() for removing random combinations of members.
1197+ """Test underlying _remove_members() for removing random combinations
1198+ of members."""
12041199 test_files = [
12051200 ('file0.txt' , b'Lorem ipsum dolor sit amet, consectetur adipiscing elit' ),
12061201 ('file1.txt' , b'Duis aute irure dolor in reprehenderit in voluptate velit esse' ),
@@ -1210,7 +1205,8 @@ def test_basic(self):
12101205 self ._test_removing_combinations (test_files )
12111206
12121207 def test_duplicated_arcname (self ):
1213- # Test underlying _remove_members() for removing any one of random duplicated members.
1208+ """Test underlying _remove_members() for removing any one of random
1209+ duplicated members."""
12141210 dupl_file = 'file.txt'
12151211 test_files = [
12161212 ('file0.txt' , b'Lorem ipsum dolor sit amet, consectetur adipiscing elit' ),
@@ -1228,11 +1224,11 @@ def test_duplicated_arcname(self):
12281224
12291225 for index in dups :
12301226 indexes = [index ]
1231- with self .subTest (dups = dups , indexes = indexes ):
1232- self ._test_removing_indexes (files , indexes )
1227+ with self .subTest (dups = dups , remove = indexes ):
1228+ self ._test_removing_members (files , indexes )
12331229
12341230 def test_non_physical (self ):
1235- # Test underlying _remove_members() for non-physical removing.
1231+ """ Test underlying _remove_members() for non-physical removing."""
12361232 test_files = [
12371233 ('file0.txt' , b'Lorem ipsum dolor sit amet, consectetur adipiscing elit' ),
12381234 ('file1.txt' , b'Duis aute irure dolor in reprehenderit in voluptate velit esse' ),
@@ -1245,14 +1241,14 @@ def test_non_physical(self):
12451241 with self .subTest (remove = indexes ):
12461242 # prepare the test zip
12471243 expected = {}
1248- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1244+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
12491245 for i , (file , data ) in enumerate (test_files ):
12501246 zh .writestr (file , data )
12511247 if i not in indexes :
12521248 expected [file ] = zh .getinfo (file ).header_offset
12531249
12541250 # do the removal and check the result
1255- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1251+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
12561252 members = {zh .infolist ()[i ] for i in indexes }
12571253 zh ._remove_members (members , remove_physical = False )
12581254 self .assertEqual (zh .namelist (), list (expected ))
@@ -1261,105 +1257,105 @@ def test_non_physical(self):
12611257 self .assertIsNone (zh .testzip ())
12621258
12631259 def test_verify (self ):
1264- # Test if params are passed to underlying _remove_members() correctly,
1265- # or never passed if conditions not met.
1260+ """ Test if params are passed to underlying _remove_members() correctly,
1261+ or never passed if conditions not met."""
12661262 file0 = 'file0.txt'
12671263 file = 'datafile.txt'
12681264 data = b'Sed ut perspiciatis unde omnis iste natus error sit voluptatem'
12691265
12701266 # closed: error and do nothing
1271- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1267+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
12721268 zh .writestr (file , data )
1273- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1269+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
12741270 zh .close ()
12751271 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
12761272 with self .assertRaises (ValueError ):
12771273 zh .remove (file )
12781274 mock_fn .assert_not_called ()
12791275
12801276 # writing: error and do nothing
1281- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1277+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
12821278 zh .writestr (file , data )
1283- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1279+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
12841280 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
12851281 with zh .open (file0 , 'w' ) as fh :
12861282 with self .assertRaises (ValueError ):
12871283 zh .remove (file )
12881284 mock_fn .assert_not_called ()
12891285
12901286 # mode 'r': error and do nothing
1291- with zipfile .ZipFile (TESTFN , 'r' ) as zh :
1287+ with zipfile .ZipFile (TESTFN , 'r' , self . compression ) as zh :
12921288 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
12931289 with self .assertRaises (ValueError ):
12941290 zh .remove (file )
12951291 mock_fn .assert_not_called ()
12961292
12971293 # mode 'a': the most general use case
1298- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1294+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
12991295 zh .writestr (file , data )
13001296 # -- remove with arcname
1301- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1297+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
13021298 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
13031299 zh .remove (file )
13041300 mock_fn .assert_called_once_with ({zh .getinfo (file )})
13051301 # -- remove with zinfo
1306- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1302+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
13071303 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
13081304 zinfo = zh .getinfo (file )
13091305 zh .remove (zinfo )
13101306 mock_fn .assert_called_once_with ({zinfo })
13111307 # -- remove with nonexist arcname
1312- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1308+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
13131309 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
13141310 with self .assertRaises (KeyError ):
13151311 zh .remove ('nonexist.file' )
13161312 mock_fn .assert_not_called ()
13171313 # -- remove with nonexist zinfo (even if same name)
1318- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1314+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
13191315 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
13201316 zinfo = zipfile .ZipInfo (file )
13211317 with self .assertRaises (KeyError ):
13221318 zh .remove (zinfo )
13231319 mock_fn .assert_not_called ()
13241320
13251321 # mode 'w': like 'a'; allows removing a just written member
1326- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1322+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
13271323 zh .writestr (file , data )
13281324 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
13291325 zh .remove (file )
13301326 mock_fn .assert_called_once_with ({zh .getinfo (file )})
13311327
13321328 # mode 'x': like 'w'
13331329 os .remove (TESTFN )
1334- with zipfile .ZipFile (TESTFN , 'x' ) as zh :
1330+ with zipfile .ZipFile (TESTFN , 'x' , self . compression ) as zh :
13351331 zh .writestr (file , data )
13361332 with mock .patch ('zipfile.ZipFile._remove_members' ) as mock_fn :
13371333 zh .remove (file )
13381334 mock_fn .assert_called_once_with ({zh .getinfo (file )})
13391335
13401336 def test_zip64 (self ):
1341- # Test if members use zip64.
1337+ """ Test if members use zip64."""
13421338 file = 'datafile.txt'
13431339 file1 = 'pre.txt'
13441340 file2 = 'post.txt'
13451341 data = b'Sed ut perspiciatis unde omnis iste natus error sit voluptatem'
13461342 data1 = b'Lorem ipsum dolor sit amet, consectetur adipiscing elit'
13471343 data2 = b'Duis aute irure dolor in reprehenderit in voluptate velit esse'
1348- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1344+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
13491345 with zh .open (file1 , 'w' , force_zip64 = True ) as fh :
13501346 fh .write (data1 )
13511347 with zh .open (file2 , 'w' , force_zip64 = True ) as fh :
13521348 fh .write (data2 )
13531349 expected_size = os .path .getsize (TESTFN )
13541350
1355- with zipfile .ZipFile (TESTFN , 'w' ) as zh :
1351+ with zipfile .ZipFile (TESTFN , 'w' , self . compression ) as zh :
13561352 with zh .open (file1 , 'w' , force_zip64 = True ) as fh :
13571353 fh .write (data1 )
13581354 with zh .open (file , 'w' , force_zip64 = True ) as fh :
13591355 fh .write (data )
13601356 with zh .open (file2 , 'w' , force_zip64 = True ) as fh :
13611357 fh .write (data2 )
1362- with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1358+ with zipfile .ZipFile (TESTFN , 'a' , self . compression ) as zh :
13631359 zh .remove (file )
13641360 self .assertIsNone (zh .testzip ())
13651361 self .assertEqual (os .path .getsize (TESTFN ), expected_size )
0 commit comments