@@ -188,24 +188,66 @@ def test_build_engines_sorted() -> None:
188188 "xarray.backends.plugins.list_engines" ,
189189 mock .MagicMock (return_value = {"dummy" : DummyBackendEntrypointArgs ()}),
190190)
191- def test_no_matching_engine_found () -> None :
192- with pytest .raises (ValueError , match = r"did not find a match in any" ):
191+ def test_no_matching_engine_found (tmp_path ) -> None :
192+ # Non-existent local file raises FileNotFoundError
193+ with pytest .raises (FileNotFoundError , match = r"No such file" ):
193194 plugins .guess_engine ("not-valid" )
194195
196+ # Existing file with unrecognized extension raises ValueError
197+ existing_file = tmp_path / "test.unknown"
198+ existing_file .write_bytes (b"" )
199+ with pytest .raises (ValueError , match = r"did not find a match in any" ):
200+ plugins .guess_engine (str (existing_file ))
201+
202+ # Existing file with recognized magic number raises ValueError
203+ nc_file = tmp_path / "foo.nc"
204+ nc_file .write_bytes (b"CDF\x01 \x00 \x00 \x00 \x00 " )
195205 with pytest .raises (ValueError , match = r"found the following matches with the input" ):
196- plugins .guess_engine ("foo.nc" )
206+ plugins .guess_engine (str ( nc_file ) )
197207
198208
199209@mock .patch (
200210 "xarray.backends.plugins.list_engines" ,
201211 mock .MagicMock (return_value = {}),
202212)
203- def test_engines_not_installed () -> None :
204- with pytest .raises (ValueError , match = r"xarray is unable to open" ):
213+ def test_engines_not_installed (tmp_path ) -> None :
214+ # Non-existent local file raises FileNotFoundError
215+ with pytest .raises (FileNotFoundError , match = r"No such file" ):
205216 plugins .guess_engine ("not-valid" )
206217
218+ # Existing file with no matching engine raises ValueError
219+ existing_file = tmp_path / "test.unknown"
220+ existing_file .write_bytes (b"" )
221+ with pytest .raises (ValueError , match = r"xarray is unable to open" ):
222+ plugins .guess_engine (str (existing_file ))
223+
224+ # Existing file with recognized magic number raises ValueError
225+ nc_file = tmp_path / "foo.nc"
226+ nc_file .write_bytes (b"CDF\x01 \x00 \x00 \x00 \x00 " )
207227 with pytest .raises (ValueError , match = r"found the following matches with the input" ):
208- plugins .guess_engine ("foo.nc" )
228+ plugins .guess_engine (str (nc_file ))
229+
230+
231+ @mock .patch (
232+ "xarray.backends.plugins.list_engines" ,
233+ mock .MagicMock (return_value = {"dummy" : DummyBackendEntrypointArgs ()}),
234+ )
235+ def test_guess_engine_file_not_found () -> None :
236+ # Non-existent local file path (string)
237+ with pytest .raises (
238+ FileNotFoundError , match = r"No such file: '/nonexistent/path.h5'"
239+ ):
240+ plugins .guess_engine ("/nonexistent/path.h5" )
241+
242+ # Non-existent local file path (PathLike)
243+ from pathlib import Path
244+
245+ with pytest .raises (FileNotFoundError , match = r"No such file" ):
246+ plugins .guess_engine (Path ("/nonexistent/path.h5" ))
247+
248+ # Remote URIs should not raise FileNotFoundError (raises ValueError instead)
249+ with pytest .raises (ValueError ):
250+ plugins .guess_engine ("https://example.com/missing.h5" )
209251
210252
211253@pytest .mark .parametrize ("engine" , common .BACKEND_ENTRYPOINTS .keys ())
0 commit comments