Skip to content

Commit

Permalink
Improve error message on inexistent files with local server (#754)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
greschd authored Jan 9, 2025
1 parent f784f32 commit 5a38a4d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ansys/acp/core/_server/acp_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions tests/unittests/test_acp_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 5a38a4d

Please sign in to comment.