11import tarfile
22import zipfile
3- from pathlib import Path
43from shutil import rmtree
54
65import pytest
109
1110
1211@pytest .fixture (scope = "function" )
13- def tarfile_path (tmpdir ):
14- with open (
15- tmpdir .join ("test_file.txt" ).strpath , "w" , encoding = "utf-8"
16- ) as write_file :
12+ def tarfile_path (tmp_path ):
13+ with open (tmp_path .joinpath ("test_file.txt" ), "w" , encoding = "utf-8" ) as write_file :
1714 write_file .write ("test file" )
1815 with tarfile .open (
19- tmpdir . join ("test_tar.tar.gz" ). strpath , mode = "w:gz" , encoding = "utf-8"
16+ tmp_path . joinpath ("test_tar.tar.gz" ), mode = "w:gz" , encoding = "utf-8"
2017 ) as archive :
21- archive .add (tmpdir . join ("test_file.txt" ). strpath )
22- return Path ( tmpdir . join ("test_tar.tar.gz" ). strpath )
18+ archive .add (tmp_path . joinpath ("test_file.txt" ))
19+ return tmp_path . joinpath ("test_tar.tar.gz" )
2320
2421
2522@pytest .fixture (scope = "function" )
26- def tarfile_uncompressed_path (tmpdir ):
27- with open (
28- tmpdir .join ("test_file.txt" ).strpath , "w" , encoding = "utf-8"
29- ) as write_file :
23+ def tarfile_uncompressed_path (tmp_path ):
24+ with open (tmp_path .joinpath ("test_file.txt" ), "w" , encoding = "utf-8" ) as write_file :
3025 write_file .write ("test file" )
3126 with tarfile .open (
32- tmpdir . join ("test_tar.tar" ). strpath , mode = "w" , encoding = "utf-8"
27+ tmp_path . joinpath ("test_tar.tar" ), mode = "w" , encoding = "utf-8"
3328 ) as archive :
34- archive .add (tmpdir . join ("test_file.txt" ). strpath )
35- return Path ( tmpdir . join ("test_tar.tar" ). strpath )
29+ archive .add (tmp_path . joinpath ("test_file.txt" ))
30+ return tmp_path . joinpath ("test_tar.tar" )
3631
3732
3833@pytest .fixture (scope = "function" )
39- def zipfile_path (tmpdir ):
40- with open (
41- tmpdir .join ("test_file.txt" ).strpath , "w" , encoding = "utf-8"
42- ) as write_file :
34+ def zipfile_path (tmp_path ):
35+ with open (tmp_path .joinpath ("test_file.txt" ), "w" , encoding = "utf-8" ) as write_file :
4336 write_file .write ("test file" )
44- with zipfile .ZipFile (tmpdir . join ("test_zip.zip" ). strpath , "w" ) as archive :
45- archive .write (tmpdir . join ("test_file.txt" ). strpath )
46- return Path ( tmpdir . join ("test_zip.zip" ). strpath )
37+ with zipfile .ZipFile (tmp_path . joinpath ("test_zip.zip" ), "w" ) as archive :
38+ archive .write (tmp_path . joinpath ("test_file.txt" ))
39+ return tmp_path . joinpath ("test_zip.zip" )
4740
4841
49- def test_download_untrusted_archive_host (tmpdir , requests_mock ):
42+ def test_download_untrusted_archive_host (tmp_path , requests_mock ):
5043 archive_url = "https://www.pyhfthisdoesnotexist.org"
5144 requests_mock .get (archive_url )
5245
5346 with pytest .raises (InvalidArchiveHost ):
54- download (archive_url , tmpdir . join ("likelihoods" ). strpath )
47+ download (archive_url , tmp_path . joinpath ("likelihoods" ))
5548
5649
57- def test_download_invalid_archive (tmpdir , requests_mock ):
50+ def test_download_invalid_archive (tmp_path , requests_mock ):
5851 archive_url = "https://www.hepdata.net/record/resource/1408476?view=true"
5952 requests_mock .get (archive_url , status_code = 404 )
6053
6154 with pytest .raises (InvalidArchive ):
62- download (archive_url , tmpdir . join ("likelihoods" ). strpath )
55+ download (archive_url , tmp_path . joinpath ("likelihoods" ))
6356
6457
65- def test_download_compress (tmpdir , requests_mock ):
58+ def test_download_compress (tmp_path , requests_mock ):
6659 archive_url = "https://www.hepdata.net/record/resource/1408476?view=true"
6760 requests_mock .get (archive_url )
6861
69- download (archive_url , tmpdir . join ("likelihoods" ). strpath , compress = True )
62+ download (archive_url , tmp_path . joinpath ("likelihoods" ), compress = True )
7063
7164
7265def test_download_archive_type (
73- tmpdir , mocker , requests_mock , tarfile_path , tarfile_uncompressed_path , zipfile_path
66+ tmp_path ,
67+ mocker ,
68+ requests_mock ,
69+ tarfile_path ,
70+ tarfile_uncompressed_path ,
71+ zipfile_path ,
7472):
7573 archive_url = "https://www.hepdata.net/record/resource/1408476?view=true"
76- output_directory = tmpdir . join ("likelihoods" ). strpath
74+ output_directory = tmp_path . joinpath ("likelihoods" )
7775 # Give BytesIO a tarfile
7876 requests_mock .get (archive_url , content = open (tarfile_path , "rb" ).read ())
7977 download (archive_url , output_directory )
@@ -86,7 +84,7 @@ def test_download_archive_type(
8684 requests_mock .get (archive_url , content = open (zipfile_path , "rb" ).read ())
8785 # Run without and with existing output_directory to cover both
8886 # cases of the shutil.rmtree logic
89- rmtree (Path ( output_directory ) )
87+ rmtree (output_directory )
9088 download (archive_url , output_directory ) # without
9189 download (archive_url , output_directory ) # with
9290
@@ -97,13 +95,13 @@ def test_download_archive_type(
9795 download (archive_url , output_directory )
9896
9997
100- def test_download_archive_force (tmpdir , requests_mock , tarfile_path ):
98+ def test_download_archive_force (tmp_path , requests_mock , tarfile_path ):
10199 archive_url = "https://www.cern.ch/record/resource/123456789"
102100 requests_mock .get (
103101 archive_url , content = open (tarfile_path , "rb" ).read (), status_code = 200
104102 )
105103
106104 with pytest .raises (InvalidArchiveHost ):
107- download (archive_url , tmpdir . join ("likelihoods" ). strpath , force = False )
105+ download (archive_url , tmp_path . joinpath ("likelihoods" ), force = False )
108106
109- download (archive_url , tmpdir . join ("likelihoods" ). strpath , force = True )
107+ download (archive_url , tmp_path . joinpath ("likelihoods" ), force = True )
0 commit comments