3
3
import argparse
4
4
import lzma
5
5
import zipfile
6
+ import zlib
6
7
from pathlib import Path
7
8
8
9
import pytest
19
20
("manylinux2010" , "linux" , "linux" ),
20
21
],
21
22
)
22
- def test_environment_action (
23
+ def test_plat_environment_action (
23
24
monkeypatch : pytest .MonkeyPatch ,
24
25
environ : str | None ,
25
26
passed : str | None ,
@@ -44,7 +45,50 @@ def test_environment_action(
44
45
assert expected == args .PLAT
45
46
46
47
47
- def test_environment_action_invalid_env (monkeypatch : pytest .MonkeyPatch ) -> None :
48
+ _all_zip_level : list [int ] = list (
49
+ range (zlib .Z_NO_COMPRESSION , zlib .Z_BEST_COMPRESSION + 1 )
50
+ )
51
+
52
+
53
+ @pytest .mark .parametrize (
54
+ ("environ" , "passed" , "expected" ),
55
+ [
56
+ (None , None , - 1 ),
57
+ (0 , None , 0 ),
58
+ (0 , 1 , 1 ),
59
+ (6 , 1 , 1 ),
60
+ ],
61
+ )
62
+ def test_zip_environment_action (
63
+ monkeypatch : pytest .MonkeyPatch ,
64
+ environ : int | None ,
65
+ passed : int | None ,
66
+ expected : int ,
67
+ ) -> None :
68
+ choices = _all_zip_level
69
+ argv = []
70
+ if passed is not None :
71
+ argv = ["--zip-compression-level" , str (passed )]
72
+ if environ is not None :
73
+ monkeypatch .setenv ("AUDITWHEEL_ZIP_COMPRESSION_LEVEL" , str (environ ))
74
+ p = argparse .ArgumentParser ()
75
+ p .add_argument (
76
+ "-z" ,
77
+ "--zip-compression-level" ,
78
+ action = EnvironmentDefault ,
79
+ metavar = "zip" ,
80
+ env = "AUDITWHEEL_ZIP_COMPRESSION_LEVEL" ,
81
+ dest = "zip" ,
82
+ type = int ,
83
+ help = "Compress level to be used to create zip file." ,
84
+ choices = choices ,
85
+ default = zlib .Z_DEFAULT_COMPRESSION ,
86
+ )
87
+ args = p .parse_args (argv )
88
+ assert expected == args .zip
89
+
90
+
91
+ def test_environment_action_invalid_plat_env (monkeypatch : pytest .MonkeyPatch ) -> None :
48
92
choices = ["linux" , "manylinux1" , "manylinux2010" ]
49
93
monkeypatch .setenv ("AUDITWHEEL_PLAT" , "foo" )
50
94
p = argparse .ArgumentParser ()
@@ -59,6 +103,39 @@ def test_environment_action_invalid_env(monkeypatch: pytest.MonkeyPatch) -> None
59
103
)
60
104
61
105
106
+ def test_environment_action_invalid_zip_env (monkeypatch : pytest .MonkeyPatch ) -> None :
107
+ choices = _all_zip_level
108
+ monkeypatch .setenv ("AUDITWHEEL_ZIP_COMPRESSION_LEVEL" , "foo" )
109
+ p = argparse .ArgumentParser ()
110
+ with pytest .raises (argparse .ArgumentError ):
111
+ p .add_argument (
112
+ "-z" ,
113
+ "--zip-compression-level" ,
114
+ action = EnvironmentDefault ,
115
+ metavar = "zip" ,
116
+ env = "AUDITWHEEL_ZIP_COMPRESSION_LEVEL" ,
117
+ dest = "zip" ,
118
+ type = int ,
119
+ help = "Compress level to be used to create zip file." ,
120
+ choices = choices ,
121
+ default = zlib .Z_DEFAULT_COMPRESSION ,
122
+ )
123
+ monkeypatch .setenv ("AUDITWHEEL_ZIP_COMPRESSION_LEVEL" , "10" )
124
+ with pytest .raises (argparse .ArgumentError ):
125
+ p .add_argument (
126
+ "-z" ,
127
+ "--zip-compression-level" ,
128
+ action = EnvironmentDefault ,
129
+ metavar = "zip" ,
130
+ env = "AUDITWHEEL_ZIP_COMPRESSION_LEVEL" ,
131
+ dest = "zip" ,
132
+ type = int ,
133
+ help = "Compress level to be used to create zip file." ,
134
+ choices = choices ,
135
+ default = zlib .Z_DEFAULT_COMPRESSION ,
136
+ )
137
+
138
+
62
139
def _write_test_permissions_zip (path : Path ) -> None :
63
140
source_zip_xz = Path (__file__ ).parent / "test-permissions.zip.xz"
64
141
with lzma .open (source_zip_xz ) as f :
@@ -92,7 +169,7 @@ def test_zip2dir_round_trip_permissions(tmp_path: Path) -> None:
92
169
_write_test_permissions_zip (source_zip )
93
170
extract_path = tmp_path / "unzip2"
94
171
zip2dir (source_zip , tmp_path / "unzip1" )
95
- dir2zip (tmp_path / "unzip1" , tmp_path / "tmp.zip" )
172
+ dir2zip (tmp_path / "unzip1" , tmp_path / "tmp.zip" , zlib . Z_DEFAULT_COMPRESSION , None )
96
173
zip2dir (tmp_path / "tmp.zip" , extract_path )
97
174
_check_permissions (extract_path )
98
175
@@ -104,7 +181,7 @@ def test_dir2zip_deflate(tmp_path: Path) -> None:
104
181
input_file = input_dir / "zeros.bin"
105
182
input_file .write_bytes (buffer )
106
183
output_file = tmp_path / "ouput.zip"
107
- dir2zip (input_dir , output_file )
184
+ dir2zip (input_dir , output_file , zlib . Z_DEFAULT_COMPRESSION , None )
108
185
assert output_file .stat ().st_size < len (buffer ) / 4
109
186
110
187
@@ -117,7 +194,7 @@ def test_dir2zip_folders(tmp_path: Path) -> None:
117
194
empty_folder = input_dir / "dummy" / "empty"
118
195
empty_folder .mkdir (parents = True )
119
196
output_file = tmp_path / "output.zip"
120
- dir2zip (input_dir , output_file )
197
+ dir2zip (input_dir , output_file , zlib . Z_DEFAULT_COMPRESSION , None )
121
198
expected_dirs = {"dummy/" , "dummy/empty/" , "dummy-1.0.dist-info/" }
122
199
with zipfile .ZipFile (output_file , "r" ) as z :
123
200
assert len (z .filelist ) == 4
0 commit comments