-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pip cell installs to dependency graph (#1698)
## Changes Logic for registering import installed in a pip cell ### Linked issues #1642 Follow up on #1692 ### Functionality - [ ] added relevant user documentation - [ ] added new CLI command - [ ] modified existing command: `databricks labs ucx ...` - [ ] added a new workflow - [ ] modified existing workflow: `...` - [ ] added a new table - [ ] modified existing table: `...` ### Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [ ] manually tested - [ ] added unit tests - [ ] added integration tests - [ ] verified on staging environment (screenshot attached)
- Loading branch information
1 parent
c8fc07e
commit 68ef91f
Showing
4 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from pathlib import Path | ||
from unittest.mock import create_autospec | ||
|
||
from databricks.labs.ucx.source_code.graph import Dependency, DependencyGraph, DependencyResolver | ||
from databricks.labs.ucx.source_code.files import FileLoader | ||
from databricks.labs.ucx.source_code.notebooks.cells import CellLanguage, PipCell | ||
from databricks.labs.ucx.source_code.notebooks.loaders import ( | ||
NotebookResolver, | ||
NotebookLoader, | ||
) | ||
from databricks.labs.ucx.source_code.site_packages import PipResolver | ||
|
||
|
||
def test_pip_cell_language_is_pip(): | ||
assert PipCell("code").language == CellLanguage.PIP | ||
|
||
|
||
def test_pip_cell_build_dependency_graph_invokes_register_library(): | ||
graph = create_autospec(DependencyGraph) | ||
|
||
code = "%pip install databricks" | ||
cell = PipCell(code) | ||
|
||
problems = cell.build_dependency_graph(graph) | ||
|
||
assert len(problems) == 0 | ||
graph.register_library.assert_called_once_with("databricks") | ||
|
||
|
||
def test_pip_cell_build_dependency_graph_pip_registers_missing_library(): | ||
graph = create_autospec(DependencyGraph) | ||
|
||
code = "%pip install" | ||
cell = PipCell(code) | ||
|
||
problems = cell.build_dependency_graph(graph) | ||
|
||
assert len(problems) == 1 | ||
assert problems[0].code == "library-install-failed" | ||
assert problems[0].message == "Missing arguments in '%pip install'" | ||
graph.register_library.assert_not_called() | ||
|
||
|
||
def test_pip_cell_build_dependency_graph_reports_incorrect_syntax(): | ||
graph = create_autospec(DependencyGraph) | ||
|
||
code = "%pip installl pytest" # typo on purpose | ||
cell = PipCell(code) | ||
|
||
problems = cell.build_dependency_graph(graph) | ||
|
||
assert len(problems) == 1 | ||
assert problems[0].code == "library-install-failed" | ||
assert problems[0].message == "Unsupported %pip command: installl" | ||
graph.register_library.assert_not_called() | ||
|
||
|
||
def test_pip_cell_build_dependency_graph_reports_unknown_library(mock_path_lookup): | ||
dependency = Dependency(FileLoader(), Path("test")) | ||
notebook_loader = NotebookLoader() | ||
notebook_resolver = NotebookResolver(notebook_loader) | ||
dependency_resolver = DependencyResolver(notebook_resolver, [PipResolver()], mock_path_lookup) | ||
graph = DependencyGraph(dependency, None, dependency_resolver, mock_path_lookup) | ||
|
||
code = "%pip install unknown-library-name" | ||
cell = PipCell(code) | ||
|
||
problems = cell.build_dependency_graph(graph) | ||
|
||
assert len(problems) == 1 | ||
assert problems[0].code == "library-install-failed" | ||
assert problems[0].message.startswith("Failed to install unknown-library-name") | ||
|
||
|
||
def test_pip_cell_build_dependency_graph_resolves_installed_library(mock_path_lookup): | ||
dependency = Dependency(FileLoader(), Path("test")) | ||
notebook_loader = NotebookLoader() | ||
notebook_resolver = NotebookResolver(notebook_loader) | ||
dependency_resolver = DependencyResolver(notebook_resolver, [PipResolver()], mock_path_lookup) | ||
graph = DependencyGraph(dependency, None, dependency_resolver, mock_path_lookup) | ||
|
||
code = "%pip install pytest" | ||
cell = PipCell(code) | ||
|
||
problems = cell.build_dependency_graph(graph) | ||
|
||
assert len(problems) == 0 | ||
assert graph.path_lookup.resolve(Path("pytest")).exists() |