Skip to content

Commit

Permalink
Fixed init-project command (#65)
Browse files Browse the repository at this point in the history
Fix #41
  • Loading branch information
nfx authored Mar 9, 2024
1 parent 779ed28 commit cfb2c32
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/databricks/labs/blueprint/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from pathlib import Path

from databricks.labs.blueprint.cli import App
Expand Down Expand Up @@ -80,6 +81,7 @@ def init_project(target):
".databricks-login.json",
"coverage.xml",
"dist",
"docs",
}
queue: list[Path] = [src_dir] # type: ignore[annotation-unchecked]
while queue:
Expand All @@ -90,7 +92,7 @@ def init_project(target):
relative_file_name = current.as_posix().replace("blueprint", project_name)
dst_file = dst_dir / relative_file_name
dst_file.parent.mkdir(exist_ok=True, parents=True)
with current.open("r") as r, dst_file.open("w") as w:
with current.open("r", encoding=sys.getdefaultencoding()) as r, dst_file.open("w") as w:
content = r.read().replace("blueprint", project_name)
content = content.replace("databricks-sdk", "databricks-labs-blueprint")
w.write(content)
Expand Down
5 changes: 4 additions & 1 deletion src/databricks/labs/blueprint/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ def relative_paths(*maybe_paths) -> list[Path]:
:param *maybe_paths: string-like arguments
"""
all_paths = [Path(str(_)) for _ in maybe_paths]
all_paths = [Path(str(_)).absolute() for _ in maybe_paths]
common_path = Path(os.path.commonpath([_.as_posix() for _ in all_paths]))
# if common path is root, return absolute paths
if common_path.absolute().as_posix() == common_path.root:
return all_paths
return [_.relative_to(common_path) for _ in all_paths]
42 changes: 42 additions & 0 deletions tests/unit/test_entrypoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import inspect
from pathlib import Path
from unittest.mock import MagicMock

import pytest

from databricks.labs.blueprint.entrypoint import (
find_project_root,
relative_paths,
run_main,
)
from databricks.labs.blueprint.wheels import ProductInfo


def test_common_paths_in_root():
a, b = relative_paths("/etc/hosts", "/usr/bin/python3")
assert a.as_posix() == "/etc/hosts"
assert b.as_posix() == "/usr/bin/python3"


def test_common_paths_in_subdir():
a, b = relative_paths(__file__, inspect.getfile(ProductInfo))
assert a.as_posix() == "tests/unit/test_entrypoint.py"
assert b.as_posix() == "src/databricks/labs/blueprint/wheels.py"


def test_find_project_root():
root = find_project_root(__file__)

this_file = Path(__file__)
assert this_file.parent.parent.parent == root


def test_find_project_root_in_tmp_dir_fails(tmp_path):
with pytest.raises(NotADirectoryError):
find_project_root((tmp_path / "foo.py").as_posix())


def test_run_main():
main = MagicMock()
run_main(main)
main.assert_called_once()

0 comments on commit cfb2c32

Please sign in to comment.