Skip to content

Commit

Permalink
WorkspacePath fixes for the .resolve() implementation (#129)
Browse files Browse the repository at this point in the history
This PR updates the `.resolve()` implementation for `WorkspacePath` so
that:

- It now properly fails on (unsupported) relative paths instead of
returning the path as-is.
- The `strict` argument is now checked: `FileNotFoundError` will be
raised if `strict` is true and the path doesn't exist.
  • Loading branch information
asnare authored Jul 15, 2024
1 parent 5ca08fc commit 3e76b65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/databricks/labs/blueprint/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,11 @@ def _return_false(self) -> bool:

def resolve(self, strict=False):
"""Return the absolute path of the file or directory in Databricks Workspace."""
return self
absolute = self.absolute()
if strict and not absolute.exists():
msg = f"Path does not exist: {self}"
raise FileNotFoundError(msg)
return absolute

def absolute(self):
if self.is_absolute():
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,24 @@ def test_home_directory() -> None:
assert str(result) == "/Users/test_user"


def test_resolve() -> None:
"""This is only supported for absolute paths.
Otherwise it depends on the current working directory which isn't supported."""
ws = create_autospec(WorkspaceClient)
ws.workspace.get_status.side_effect = (
ObjectInfo(path="/path/that/exists", object_type=ObjectType.FILE),
NotFound("Simulated NotFound"),
)

assert WorkspacePath(ws, "/absolute/path").resolve() == WorkspacePath(ws, "/absolute/path")
assert WorkspacePath(ws, "/path/that/exists").resolve(strict=True) == WorkspacePath(ws, "/path/that/exists")
with pytest.raises(FileNotFoundError):
_ = WorkspacePath(ws, "/path/that/does/not/exist").resolve(strict=True)
with pytest.raises(NotImplementedError):
_ = WorkspacePath(ws, "relative/path").resolve()


def test_absolute() -> None:
"""This is only supported for absolute paths.
Expand Down

0 comments on commit 3e76b65

Please sign in to comment.