Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WorkspacePath fixes for the .resolve() implementation #129

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading