@@ -85,6 +85,29 @@ def test_write_stac(tmp_path, dataset, write_datasets, pre_existing_catalog):
8585 }
8686
8787
88+ orig_import = __import__
89+ def import_mock (name , * args ):
90+ if name == 'pystac' :
91+ raise ModuleNotFoundError ("No module named 'pystac'" )
92+ return orig_import (name , * args )
93+
94+
95+ def test_write_stac_no_pystac (tmp_path , dataset ):
96+ # Import hooks are the recommended "clean" way to do this, but don't work
97+ # in this case.
98+ with mock .patch ('builtins.__import__' , side_effect = import_mock ):
99+ # pytest imports xcengine.util long before we can patch __import__,
100+ # so we delete pystac from util's namespace (if present) instead.
101+ # This gives a NameError on access rather than a ModuleNotFoundError
102+ # on import, but the important thing is to break any implementation
103+ # that tries to import pystac without checking if it's available.
104+ import xcengine .util
105+ xcengine .util .__dict__ .pop ("pystac" , None )
106+ from xcengine .util import write_stac
107+ write_stac ({"ds1" : dataset }, tmp_path )
108+ # We want nothing to happen here, so no explicit assertions.
109+
110+
88111@pytest .mark .parametrize ("eoap_mode" , [False , True ])
89112@pytest .mark .parametrize ("ds2_format" , [None , "zarr" , "netcdf" ])
90113def test_save_datasets (tmp_path , dataset , eoap_mode , ds2_format ):
0 commit comments