Skip to content

Commit 95c8eae

Browse files
authored
Replace MockInstallation with MockPathLookup for testing fixtures (#3215)
closes #3115
1 parent b075724 commit 95c8eae

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

tests/unit/__init__.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from databricks.labs.ucx.source_code.graph import SourceContainer
2323

2424
logging.getLogger("tests").setLevel("DEBUG")
25+
logger = logging.getLogger(__name__)
2526

2627
DEFAULT_CONFIG = {
2728
"config.yml": {
@@ -121,16 +122,6 @@ def _id_list(cls: type, ids=None):
121122
return [installation.load(cls, filename=_) for _ in ids]
122123

123124

124-
def _load_sources(cls: type, *filenames: str):
125-
# TODO: remove the usage of it in favor of MockPathLookup
126-
if not filenames:
127-
return []
128-
installation = MockInstallation(DEFAULT_CONFIG | {_: _load_source(f'{_FOLDERS[cls]}/{_}') for _ in filenames})
129-
# cleanly avoid mypy error
130-
setattr(installation, "_unmarshal_type", lambda as_dict, filename, type_ref: as_dict)
131-
return [installation.load(cls, filename=_) for _ in filenames]
132-
133-
134125
def _samples_path(cls: type) -> str:
135126
return (__dir / _FOLDERS[cls]).as_posix()
136127

tests/unit/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,16 @@ def simple_dependency_resolver(mock_path_lookup: PathLookup) -> DependencyResolv
219219
notebook_resolver = NotebookResolver(NotebookLoader())
220220
import_resolver = ImportFileResolver(FileLoader(), allow_list)
221221
return DependencyResolver(library_resolver, notebook_resolver, import_resolver, import_resolver, mock_path_lookup)
222+
223+
224+
def _load_sources(*filenames: str):
225+
# Load the contents of the files into a list of strings
226+
contents = []
227+
path_lookup = MockPathLookup()
228+
for filename in filenames:
229+
path = path_lookup.resolve(Path(filename))
230+
# Read local file into a string
231+
if path:
232+
with open(path.as_posix(), 'r', encoding='utf-8') as f:
233+
contents.append(f.read())
234+
return contents

tests/unit/source_code/test_notebook.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from databricks.sdk.service.workspace import Language, ObjectType, ObjectInfo
66

77
from databricks.labs.ucx.source_code.base import CurrentSessionState
8-
from databricks.labs.ucx.source_code.graph import DependencyGraph, SourceContainer, DependencyResolver
8+
from databricks.labs.ucx.source_code.graph import DependencyGraph, DependencyResolver
99
from databricks.labs.ucx.source_code.known import KnownList
1010
from databricks.labs.ucx.source_code.linters.files import ImportFileResolver, FileLoader
1111
from databricks.labs.ucx.source_code.linters.imports import DbutilsPyLinter
@@ -16,7 +16,7 @@
1616
NotebookLoader,
1717
)
1818
from databricks.labs.ucx.source_code.python_libraries import PythonLibraryResolver
19-
from tests.unit import _load_sources
19+
from tests.unit.conftest import _load_sources
2020

2121
# fmt: off
2222
# the following samples are real samples from https://github.com/databricks-industry-solutions
@@ -78,7 +78,7 @@
7878
)
7979
def test_notebook_splits_source_into_cells(source: tuple[str, Language, list[str]]) -> None:
8080
path = source[0]
81-
sources: list[str] = _load_sources(SourceContainer, path)
81+
sources: list[str] = _load_sources(path)
8282
assert len(sources) == 1
8383
notebook = Notebook.parse(Path(path), sources[0], source[1])
8484
assert notebook is not None
@@ -98,7 +98,7 @@ def test_notebook_splits_source_into_cells(source: tuple[str, Language, list[str
9898
)
9999
def test_notebook_rebuilds_same_code(source: tuple[str, Language, list[str]]) -> None:
100100
path = source[0]
101-
sources: list[str] = _load_sources(SourceContainer, path)
101+
sources: list[str] = _load_sources(path)
102102
assert len(sources) == 1
103103
notebook = Notebook.parse(Path(path), sources[0], source[1])
104104
assert notebook is not None
@@ -121,7 +121,7 @@ def test_notebook_rebuilds_same_code(source: tuple[str, Language, list[str]]) ->
121121
)
122122
def test_notebook_generates_runnable_cells(source: tuple[str, Language, list[str]]) -> None:
123123
path = source[0]
124-
sources: list[str] = _load_sources(SourceContainer, path)
124+
sources: list[str] = _load_sources(path)
125125
assert len(sources) == 1
126126
notebook = Notebook.parse(Path(path), sources[0], source[1])
127127
assert notebook is not None

0 commit comments

Comments
 (0)