5
5
6
6
import pytest
7
7
8
+ from tornado .httpclient import HTTPClientError
9
+
8
10
9
11
@pytest .mark .parametrize (
10
12
"followSymlinks, download_hidden, file_list" ,
@@ -111,6 +113,36 @@ async def test_download(jp_fetch, jp_root_dir, followSymlinks, download_hidden,
111
113
assert set (map (lambda m : m .name , tf .getmembers ())) == file_list
112
114
113
115
116
+ def _create_archive_file (root_dir , file_name , format , mode ):
117
+ # Create a dummy directory.
118
+ archive_dir_path = root_dir / file_name
119
+ archive_dir_path .mkdir (parents = True )
120
+
121
+ (archive_dir_path / "extract-test1.txt" ).write_text ("hello1" )
122
+ (archive_dir_path / "extract-test2.txt" ).write_text ("hello2" )
123
+ (archive_dir_path / "extract-test3.md" ).write_text ("hello3" )
124
+
125
+ # Make an archive
126
+ archive_dir_path = root_dir / file_name
127
+ # The request should fail when the extension has an unnecessary prefix.
128
+ archive_path = archive_dir_path .parent / f"{ archive_dir_path .name } .{ format } "
129
+ if format == "zip" :
130
+ with zipfile .ZipFile (archive_path , mode = mode ) as writer :
131
+ for file_path in archive_dir_path .rglob ("*" ):
132
+ if file_path .is_file ():
133
+ writer .write (file_path , file_path .relative_to (root_dir ))
134
+ else :
135
+ with tarfile .open (str (archive_path ), mode = mode ) as writer :
136
+ for file_path in archive_dir_path .rglob ("*" ):
137
+ if file_path .is_file ():
138
+ writer .add (file_path , file_path .relative_to (root_dir ))
139
+
140
+ # Remove the directory
141
+ shutil .rmtree (archive_dir_path )
142
+
143
+ return archive_dir_path , archive_path
144
+
145
+
114
146
@pytest .mark .parametrize (
115
147
"file_name" ,
116
148
[
@@ -134,34 +166,36 @@ async def test_download(jp_fetch, jp_root_dir, followSymlinks, download_hidden,
134
166
],
135
167
)
136
168
async def test_extract (jp_fetch , jp_root_dir , file_name , format , mode ):
137
- # Create a dummy directory.
138
- archive_dir_path = jp_root_dir / file_name
139
- archive_dir_path .mkdir (parents = True )
140
-
141
- (archive_dir_path / "extract-test1.txt" ).write_text ("hello1" )
142
- (archive_dir_path / "extract-test2.txt" ).write_text ("hello2" )
143
- (archive_dir_path / "extract-test3.md" ).write_text ("hello3" )
144
-
145
- # Make an archive
146
- archive_dir_path = jp_root_dir / file_name
147
- archive_path = archive_dir_path .with_suffix ("." + format )
148
- if format == "zip" :
149
- with zipfile .ZipFile (archive_path , mode = mode ) as writer :
150
- for file_path in archive_dir_path .rglob ("*" ):
151
- if file_path .is_file ():
152
- writer .write (file_path , file_path .relative_to (jp_root_dir ))
153
- else :
154
- with tarfile .open (str (archive_path ), mode = mode ) as writer :
155
- for file_path in archive_dir_path .rglob ("*" ):
156
- if file_path .is_file ():
157
- writer .add (file_path , file_path .relative_to (jp_root_dir ))
158
-
159
- # Remove the directory
160
- shutil .rmtree (archive_dir_path )
169
+ archive_dir_path , archive_path = _create_archive_file (jp_root_dir , file_name , format , mode )
161
170
162
171
r = await jp_fetch ("extract-archive" , archive_path .relative_to (jp_root_dir ).as_posix (), method = "GET" )
163
172
assert r .code == 200
164
173
assert archive_dir_path .is_dir ()
165
174
166
175
n_files = len (list (archive_dir_path .glob ("*" )))
167
176
assert n_files == 3
177
+
178
+
179
+ @pytest .mark .parametrize (
180
+ "format, mode" ,
181
+ [
182
+ ("zip" , "w" ),
183
+ ("tgz" , "w|gz" ),
184
+ ("tar.gz" , "w|gz" ),
185
+ ("tbz" , "w|bz2" ),
186
+ ("tbz2" , "w|bz2" ),
187
+ ("tar.bz" , "w|bz2" ),
188
+ ("tar.bz2" , "w|bz2" ),
189
+ ("txz" , "w|xz" ),
190
+ ("tar.xz" , "w|xz" ),
191
+ ],
192
+ )
193
+ async def test_extract_failure (jp_fetch , jp_root_dir , format , mode ):
194
+ # The request should fail when the extension has an unnecessary prefix.
195
+ prefixed_format = f"prefix{ format } "
196
+ archive_dir_path , archive_path = _create_archive_file (jp_root_dir , "extract-archive-dir" , prefixed_format , mode )
197
+
198
+ with pytest .raises (Exception ) as e :
199
+ await jp_fetch ("extract-archive" , archive_path .relative_to (jp_root_dir ).as_posix (), method = "GET" )
200
+ assert e .type == HTTPClientError
201
+ assert not archive_dir_path .exists ()
0 commit comments