From 72909fc7daeb3b041abdd8ecb2184b2e54d43d81 Mon Sep 17 00:00:00 2001 From: Andrew Snare Date: Mon, 15 Jul 2024 15:33:50 +0200 Subject: [PATCH] Fix the .resolve() implementation. The implementation now: - Properly fails on (unsupported) relative paths. - Checks the path exists if the 'strict' argument is true. --- src/databricks/labs/blueprint/paths.py | 6 +++++- tests/unit/test_paths.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/databricks/labs/blueprint/paths.py b/src/databricks/labs/blueprint/paths.py index 227b64c..44b04a1 100644 --- a/src/databricks/labs/blueprint/paths.py +++ b/src/databricks/labs/blueprint/paths.py @@ -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(): diff --git a/tests/unit/test_paths.py b/tests/unit/test_paths.py index d49d145..feee490 100644 --- a/tests/unit/test_paths.py +++ b/tests/unit/test_paths.py @@ -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.