@@ -197,6 +197,49 @@ def test_unpack_tar_filter(self) -> None:
197
197
198
198
assert "is outside the destination" in str (e .value )
199
199
200
+ @pytest .mark .parametrize (
201
+ ("input_prefix" , "unpack_prefix" ),
202
+ [
203
+ ("" , "" ),
204
+ ("dir/" , "" ), # pip ignores a common leading directory
205
+ ("dir/sub/" , "sub/" ), # pip ignores *one* common leading directory
206
+ ],
207
+ )
208
+ def test_unpack_tar_links (self , input_prefix : str , unpack_prefix : str ) -> None :
209
+ """
210
+ Test unpacking a *.tar with file containing hard & soft links
211
+ """
212
+ test_tar = os .path .join (self .tempdir , "test_tar_links.tar" )
213
+ content = b"file content"
214
+ with tarfile .open (test_tar , "w" ) as mytar :
215
+ file_tarinfo = tarfile .TarInfo (input_prefix + "regular_file.txt" )
216
+ file_tarinfo .size = len (content )
217
+ mytar .addfile (file_tarinfo , io .BytesIO (content ))
218
+
219
+ hardlink_tarinfo = tarfile .TarInfo (input_prefix + "hardlink.txt" )
220
+ hardlink_tarinfo .type = tarfile .LNKTYPE
221
+ hardlink_tarinfo .linkname = input_prefix + "regular_file.txt"
222
+ mytar .addfile (hardlink_tarinfo )
223
+
224
+ symlink_tarinfo = tarfile .TarInfo (input_prefix + "symlink.txt" )
225
+ symlink_tarinfo .type = tarfile .SYMTYPE
226
+ symlink_tarinfo .linkname = "regular_file.txt"
227
+ mytar .addfile (symlink_tarinfo )
228
+
229
+ untar_file (test_tar , self .tempdir )
230
+
231
+ os .system (f"ls -alR { self .tempdir } " )
232
+
233
+ unpack_dir = os .path .join (self .tempdir , unpack_prefix )
234
+ with open (os .path .join (unpack_dir , "regular_file.txt" ), "rb" ) as f :
235
+ assert f .read () == content
236
+
237
+ with open (os .path .join (unpack_dir , "hardlink.txt" ), "rb" ) as f :
238
+ assert f .read () == content
239
+
240
+ with open (os .path .join (unpack_dir , "symlink.txt" ), "rb" ) as f :
241
+ assert f .read () == content
242
+
200
243
201
244
def test_unpack_tar_unicode (tmpdir : Path ) -> None :
202
245
test_tar = tmpdir / "test.tar"
0 commit comments