@@ -35,59 +35,61 @@ def parser_and_data(all_parsers, csv1):
3535
3636
3737@pytest .mark .parametrize ("compression" , ["zip" , "infer" , "zip2" ])
38- def test_zip (parser_and_data , compression ):
38+ def test_zip (tmp_path , parser_and_data , compression ):
3939 parser , data , expected = parser_and_data
4040
41- with tm . ensure_clean ( "test_file.zip" ) as path :
42- with zipfile .ZipFile (path , mode = "w" ) as tmp :
43- tmp .writestr ("test_file" , data )
41+ path = tmp_path / "test_file.zip"
42+ with zipfile .ZipFile (path , mode = "w" ) as tmp :
43+ tmp .writestr ("test_file" , data )
4444
45- if compression == "zip2" :
46- with open (path , "rb" ) as f :
47- result = parser .read_csv (f , compression = "zip" )
48- else :
49- result = parser .read_csv (path , compression = compression )
45+ if compression == "zip2" :
46+ with open (path , "rb" ) as f :
47+ result = parser .read_csv (f , compression = "zip" )
48+ else :
49+ result = parser .read_csv (path , compression = compression )
5050
51- tm .assert_frame_equal (result , expected )
51+ tm .assert_frame_equal (result , expected )
5252
5353
5454@pytest .mark .parametrize ("compression" , ["zip" , "infer" ])
55- def test_zip_error_multiple_files (parser_and_data , compression ):
55+ def test_zip_error_multiple_files (tmp_path , parser_and_data , compression ):
5656 parser , data , expected = parser_and_data
5757
58- with tm . ensure_clean ( "combined_zip.zip" ) as path :
59- inner_file_names = ["test_file" , "second_file" ]
58+ path = tmp_path / "combined_zip.zip"
59+ inner_file_names = ["test_file" , "second_file" ]
6060
61- with zipfile .ZipFile (path , mode = "w" ) as tmp :
62- for file_name in inner_file_names :
63- tmp .writestr (file_name , data )
61+ with zipfile .ZipFile (path , mode = "w" ) as tmp :
62+ for file_name in inner_file_names :
63+ tmp .writestr (file_name , data )
6464
65- with pytest .raises (ValueError , match = "Multiple files" ):
66- parser .read_csv (path , compression = compression )
65+ with pytest .raises (ValueError , match = "Multiple files" ):
66+ parser .read_csv (path , compression = compression )
6767
6868
69- def test_zip_error_no_files (parser_and_data ):
69+ def test_zip_error_no_files (tmp_path , parser_and_data ):
7070 parser , _ , _ = parser_and_data
7171
72- with tm . ensure_clean () as path :
73- with zipfile .ZipFile (path , mode = "w" ):
74- pass
72+ path = tmp_path / "test_file.zip"
73+ with zipfile .ZipFile (path , mode = "w" ):
74+ pass
7575
76- with pytest .raises (ValueError , match = "Zero files" ):
77- parser .read_csv (path , compression = "zip" )
76+ with pytest .raises (ValueError , match = "Zero files" ):
77+ parser .read_csv (path , compression = "zip" )
7878
7979
80- def test_zip_error_invalid_zip (parser_and_data ):
80+ def test_zip_error_invalid_zip (tmp_path , parser_and_data ):
8181 parser , _ , _ = parser_and_data
8282
83- with tm .ensure_clean () as path :
84- with open (path , "rb" ) as f :
85- with pytest .raises (zipfile .BadZipFile , match = "File is not a zip file" ):
86- parser .read_csv (f , compression = "zip" )
83+ path = tmp_path / "invalid_file.zip"
84+ path .touch ()
85+ with open (path , "rb" ) as f :
86+ with pytest .raises (zipfile .BadZipFile , match = "File is not a zip file" ):
87+ parser .read_csv (f , compression = "zip" )
8788
8889
8990@pytest .mark .parametrize ("filename" , [None , "test.{ext}" ])
9091def test_compression (
92+ tmp_path ,
9193 request ,
9294 parser_and_data ,
9395 compression_only ,
@@ -108,17 +110,17 @@ def test_compression(
108110 )
109111 )
110112
111- with tm . ensure_clean ( filename = filename ) as path :
112- tm .write_to_compressed (compress_type , path , data )
113- compression = "infer" if filename else compress_type
113+ path = tmp_path / filename if filename else tmp_path / "test_file"
114+ tm .write_to_compressed (compress_type , path , data )
115+ compression = "infer" if filename else compress_type
114116
115- if buffer :
116- with open (path , "rb" ) as f :
117- result = parser .read_csv (f , compression = compression )
118- else :
119- result = parser .read_csv (path , compression = compression )
117+ if buffer :
118+ with open (path , "rb" ) as f :
119+ result = parser .read_csv (f , compression = compression )
120+ else :
121+ result = parser .read_csv (path , compression = compression )
120122
121- tm .assert_frame_equal (result , expected )
123+ tm .assert_frame_equal (result , expected )
122124
123125
124126@pytest .mark .parametrize ("ext" , [None , "gz" , "bz2" ])
@@ -175,37 +177,38 @@ def test_compression_tar_archive(all_parsers, csv_dir_path):
175177 assert list (df .columns ) == ["a" ]
176178
177179
178- def test_ignore_compression_extension (all_parsers ):
180+ def test_ignore_compression_extension (tmp_path , all_parsers ):
179181 parser = all_parsers
180182 df = DataFrame ({"a" : [0 , 1 ]})
181- with tm .ensure_clean ("test.csv" ) as path_csv :
182- with tm .ensure_clean ("test.csv.zip" ) as path_zip :
183- # make sure to create un-compressed file with zip extension
184- df .to_csv (path_csv , index = False )
185- Path (path_zip ).write_text (
186- Path (path_csv ).read_text (encoding = "utf-8" ), encoding = "utf-8"
187- )
188183
189- tm .assert_frame_equal (parser .read_csv (path_zip , compression = None ), df )
184+ path_csv = tmp_path / "test.csv"
185+ path_zip = tmp_path / "test.csv.zip"
186+ # make sure to create un-compressed file with zip extension
187+ df .to_csv (path_csv , index = False )
188+ Path (path_zip ).write_text (
189+ Path (path_csv ).read_text (encoding = "utf-8" ), encoding = "utf-8"
190+ )
191+
192+ tm .assert_frame_equal (parser .read_csv (path_zip , compression = None ), df )
190193
191194
192- def test_writes_tar_gz (all_parsers ):
195+ def test_writes_tar_gz (tmp_path , all_parsers ):
193196 parser = all_parsers
194197 data = DataFrame (
195198 {
196199 "Country" : ["Venezuela" , "Venezuela" ],
197200 "Twitter" : ["Hugo Chávez Frías" , "Henrique Capriles R." ],
198201 }
199202 )
200- with tm . ensure_clean ( "test.tar.gz" ) as tar_path :
201- data .to_csv (tar_path , index = False )
203+ tar_path = tmp_path / "test.tar.gz"
204+ data .to_csv (tar_path , index = False )
202205
203- # test that read_csv infers .tar.gz to gzip:
204- tm .assert_frame_equal (parser .read_csv (tar_path ), data )
206+ # test that read_csv infers .tar.gz to gzip:
207+ tm .assert_frame_equal (parser .read_csv (tar_path ), data )
205208
206- # test that file is indeed gzipped:
207- with tarfile .open (tar_path , "r:gz" ) as tar :
208- result = parser .read_csv (
209- tar .extractfile (tar .getnames ()[0 ]), compression = "infer"
210- )
211- tm .assert_frame_equal (result , data )
209+ # test that file is indeed gzipped:
210+ with tarfile .open (tar_path , "r:gz" ) as tar :
211+ result = parser .read_csv (
212+ tar .extractfile (tar .getnames ()[0 ]), compression = "infer"
213+ )
214+ tm .assert_frame_equal (result , data )
0 commit comments