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

normalize databricks paths as part of resolving them #157

Merged
merged 4 commits into from
Oct 10, 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
18 changes: 17 additions & 1 deletion src/databricks/labs/blueprint/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,23 @@ def resolve(self: P, strict: bool = False) -> P:
if strict and not absolute.exists():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't self.absolute() fail because self.cwd() is not available for _DatabricksPath?

    def absolute(self: P) -> P:
        if self.is_absolute():
            return self
        return self.with_segments(self.cwd(), self)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will but that's not the problem being addressed by this PR

msg = f"Path does not exist: {self}"
raise FileNotFoundError(msg)
return absolute
# pylint: disable=protected-access
return absolute._normalize()

def _normalize(self: P) -> P:
if ".." not in self._path_parts:
return self
segments: list[str] = []
for part in self._path_parts:
if part == "..":
if segments:
segments.pop()
elif part is None or part == '.':
continue
Comment on lines +534 to +535
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed: by the time we get here a part can neither be None nor .?

else:
segments.append(part)
# pylint: disable=protected-access
return self.with_segments(self.anchor, *segments)._normalize()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit unclear to me why we need to also call ._normalize() on the new instance?


def absolute(self: P) -> P:
if self.is_absolute():
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ def test_replace_file(ws, make_random, cls):
tmp_dir.rmdir(recursive=True)


@pytest.mark.parametrize("cls", DATABRICKS_PATHLIKE)
def test_resolve_is_consistent(ws, cls):
path = cls(ws, "/a/b/c") / Path("../../d")
asnare marked this conversation as resolved.
Show resolved Hide resolved
resolved = path.resolve()
assert resolved == cls(ws, "/a/d")
asnare marked this conversation as resolved.
Show resolved Hide resolved
path = cls(ws, "/a/b/c") / "../../d"
resolved = path.resolve()
assert resolved == cls(ws, "/a/d")
resolved = cls(ws, "/a/b/c/../../d").resolve()
assert resolved == cls(ws, "/a/d")
resolved = cls(ws, "/../d").resolve()
assert resolved == cls(ws, "/d")
resolved = cls(ws, "/a/b/c/./../../d").resolve()
assert resolved == cls(ws, "/a/d")


def test_workspace_as_fuse(ws):
wsp = WorkspacePath(ws, "/Users/foo/bar/baz")
assert Path("/Workspace/Users/foo/bar/baz") == wsp.as_fuse()
Expand Down
Loading