44import stat
55import sys
66import urllib .request as urllib_request
7+ from contextlib import nullcontext
78
89import numpy as np
910import pytest
1011
1112import asdf
1213from asdf import exceptions , generic_io
1314from asdf .config import config_context
15+ from asdf .exceptions import AsdfDeprecationWarning
1416
1517from . import _helpers as helpers
1618from . import create_large_tree , create_small_tree
@@ -21,6 +23,26 @@ def tree(request):
2123 return request .param ()
2224
2325
26+ @pytest .fixture (params = [True , False ])
27+ def has_fsspec (request , monkeypatch ):
28+ if request .param :
29+ yield True
30+ else :
31+ pytest .importorskip ("fsspec" )
32+ monkeypatch .setitem (sys .modules , "fsspec" , None )
33+ yield False
34+
35+
36+ @pytest .fixture ()
37+ def warn_no_fsspec (has_fsspec ):
38+ if has_fsspec :
39+ yield nullcontext ()
40+ else :
41+ yield pytest .warns (
42+ AsdfDeprecationWarning , match = r"Opening http urls without fsspec is deprecated. Please install fsspec"
43+ )
44+
45+
2446def _roundtrip (tree , get_write_fd , get_read_fd , write_options = None , read_options = None ):
2547 write_options = {} if write_options is None else write_options
2648 read_options = {} if read_options is None else read_options
@@ -259,7 +281,7 @@ def get_read_fd():
259281
260282
261283@pytest .mark .remote_data ()
262- def test_http_connection (tree , httpserver ):
284+ def test_http_connection (tree , httpserver , warn_no_fsspec ):
263285 path = os .path .join (httpserver .tmpdir , "test.asdf" )
264286
265287 def get_write_fd ():
@@ -274,9 +296,10 @@ def get_read_fd():
274296 fd .read (0 )
275297 return fd
276298
277- with _roundtrip (tree , get_write_fd , get_read_fd ) as ff :
278- assert len (ff ._blocks .blocks ) == 2
279- assert (ff .tree ["science_data" ] == tree ["science_data" ]).all ()
299+ with warn_no_fsspec :
300+ with _roundtrip (tree , get_write_fd , get_read_fd ) as ff :
301+ assert len (ff ._blocks .blocks ) == 2
302+ assert (ff .tree ["science_data" ] == tree ["science_data" ]).all ()
280303
281304
282305def test_exploded_filesystem (tree , tmp_path ):
@@ -313,7 +336,7 @@ def get_read_fd():
313336
314337
315338@pytest .mark .remote_data ()
316- def test_exploded_http (tree , httpserver ):
339+ def test_exploded_http (tree , httpserver , warn_no_fsspec ):
317340 path = os .path .join (httpserver .tmpdir , "test.asdf" )
318341
319342 def get_write_fd ():
@@ -322,8 +345,9 @@ def get_write_fd():
322345 def get_read_fd ():
323346 return generic_io .get_file (httpserver .url + "test.asdf" )
324347
325- with _roundtrip (tree , get_write_fd , get_read_fd , write_options = {"all_array_storage" : "external" }) as ff :
326- assert len (list (ff ._blocks .blocks )) == 0
348+ with warn_no_fsspec :
349+ with _roundtrip (tree , get_write_fd , get_read_fd , write_options = {"all_array_storage" : "external" }) as ff :
350+ assert len (list (ff ._blocks .blocks )) == 0
327351
328352
329353def test_exploded_stream_write (small_tree ):
@@ -378,7 +402,7 @@ def test_open_stdout():
378402 pass
379403
380404
381- def test_invalid_obj (tmp_path ):
405+ def test_invalid_obj (tmp_path , has_fsspec ):
382406 with pytest .raises (ValueError , match = r"Can't handle .* as a file for mode 'r'" ):
383407 generic_io .get_file (42 )
384408
@@ -392,8 +416,14 @@ def test_invalid_obj(tmp_path):
392416 ):
393417 generic_io .get_file (fd , "r" )
394418
395- with pytest .raises (ValueError , match = r"HTTP connections can not be opened for writing" ):
396- generic_io .get_file ("http://www.google.com" , "w" )
419+ url = "http://www.google.com"
420+ mode = "w"
421+ if has_fsspec :
422+ raises_ctx = pytest .raises (ValueError , match = f"Unable to open { url } with mode { mode } " )
423+ else :
424+ raises_ctx = pytest .raises (ValueError , match = r"HTTP connections can not be opened for writing" )
425+ with raises_ctx :
426+ generic_io .get_file (url , mode )
397427
398428 with pytest .raises (TypeError , match = r"io.StringIO objects are not supported. Use io.BytesIO instead." ):
399429 generic_io .get_file (io .StringIO ())
0 commit comments