From 5a38a4dfd58f7a8cab4b7a0f33482c3df8a93df9 Mon Sep 17 00:00:00 2001 From: Dominik Gresch Date: Thu, 9 Jan 2025 16:42:15 +0100 Subject: [PATCH] Improve error message on inexistent files with local server (#754) Add a check that the file exists when calling the (no-op) `upload_file` method on the local file transfer strategy. This ensures a nice error is produced when trying to import an inexistent ACPH5 file, instead of failing on H5Open. Resolves #748. --- src/ansys/acp/core/_server/acp_instance.py | 2 ++ tests/unittests/test_acp_instance.py | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/ansys/acp/core/_server/acp_instance.py b/src/ansys/acp/core/_server/acp_instance.py index d96aac794..22a471a22 100644 --- a/src/ansys/acp/core/_server/acp_instance.py +++ b/src/ansys/acp/core/_server/acp_instance.py @@ -58,6 +58,8 @@ def __init__(self, working_directory: _PATH) -> None: self._working_directory = pathlib.Path(working_directory) def upload_file(self, local_path: _PATH) -> pathlib.Path: + if not pathlib.Path(local_path).exists(): + raise FileNotFoundError(f"No such file or directory: '{local_path}'") return self._get_remote_path(local_path) def download_file(self, remote_path: _PATH, local_path: _PATH) -> None: diff --git a/tests/unittests/test_acp_instance.py b/tests/unittests/test_acp_instance.py index 5600e6cd3..4eee70588 100644 --- a/tests/unittests/test_acp_instance.py +++ b/tests/unittests/test_acp_instance.py @@ -75,3 +75,14 @@ def test_import_from_differenct_cwd(acp_instance, model_data_dir): assert (tmp_dir_path / export_filename).exists() # Check that the exported file does not exist on the original CWD assert not pathlib.Path(export_filename).exists() + + +def test_import_inexistent(acp_instance): + """Test that inexistent files raise a FileNotFoundError. + + Regression test for #748 (when run with 'direct' launch mode). + """ + filename = "inexistent_file.acph5" + with pytest.raises(FileNotFoundError) as exc: + acp_instance.import_model(filename) + assert filename in str(exc.value)