Skip to content

Commit

Permalink
fix(poly diff): list of diff files with paths starting from the works…
Browse files Browse the repository at this point in the history
…pace-root (#289)

* fix(poly diff): calculate diff for bricks and projects where the workspace is in a repo subfolder

* bump Poetry plugin to 1.32.0

* bump CLI to 1.21.0

* refactor: try first with exact match when checking file path with pattern
  • Loading branch information
DavidVujic authored Nov 16, 2024
1 parent 70a2d89 commit d39f346
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
2 changes: 1 addition & 1 deletion components/polylith/commands/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def print_views(root: Path, tag: str, options: dict) -> None:
changed_bases = diff.collect.get_changed_bases(root, files, ns)
changed_components = diff.collect.get_changed_components(root, files, ns)
changed_bricks = set(changed_bases + changed_components)
changed_projects = diff.collect.get_changed_projects(files)
changed_projects = diff.collect.get_changed_projects(root, files)

projects_data = get_projects_data(root, ns)

Expand Down
30 changes: 25 additions & 5 deletions components/polylith/diff/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,31 @@ def _parse_folder_parts(pattern: str, changed_file: Path) -> str:
return next(p for p in file_path.parts if p != file_path.root)


def _get_changed(pattern: str, changed_files: List[Path]) -> set:
def _is_in_workspace(root: Path, top_dir: str, changed_file: Path) -> bool:
file_path = changed_file.as_posix()

if file_path.startswith(top_dir):
return True

return f"{root.name}/{top_dir}" in file_path


def _is_match(root: Path, top_dir: str, pattern: str, file_path: Path) -> bool:
if re.match(pattern, file_path.as_posix()):
return True

found = re.search(pattern, file_path.as_posix())

return found is not None and _is_in_workspace(root, top_dir, file_path)


def _get_changed(
root: Path, top_dir: str, pattern: str, changed_files: List[Path]
) -> set:
return {
_parse_folder_parts(pattern, f)
for f in changed_files
if re.match(pattern, f.as_posix())
if _is_match(root, top_dir, pattern, f)
}


Expand All @@ -37,7 +57,7 @@ def _get_changed_bricks(
) -> list:
pattern = _parse_path_pattern(root, top_dir, namespace)

return sorted(_get_changed(pattern, changed_files))
return sorted(_get_changed(root, top_dir, pattern, changed_files))


def get_changed_components(
Expand All @@ -50,8 +70,8 @@ def get_changed_bases(root: Path, changed_files: List[Path], namespace: str) ->
return _get_changed_bricks(root, repo.bases_dir, changed_files, namespace)


def get_changed_projects(changed_files: List[Path]) -> list:
res = _get_changed(repo.projects_dir, changed_files)
def get_changed_projects(root: Path, changed_files: List[Path]) -> list:
res = _get_changed(root, repo.projects_dir, repo.projects_dir, changed_files)
filtered = {p for p in res if p != repo.projects_dir}
return sorted(filtered)

Expand Down
2 changes: 1 addition & 1 deletion development/david.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
changed_files = diff.collect.get_files(tag)
changed_components = diff.collect.get_changed_components(root, changed_files, ns)
changed_bases = diff.collect.get_changed_bases(root, changed_files, ns)
changed_projects = diff.collect.get_changed_projects(changed_files)
changed_projects = diff.collect.get_changed_projects(root, changed_files)

projects_data = info.get_bricks_in_projects(root, changed_components, changed_bases, ns)

Expand Down
2 changes: 1 addition & 1 deletion projects/poetry_polylith_plugin/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "poetry-polylith-plugin"
version = "1.31.0"
version = "1.32.0"
description = "A Poetry plugin that adds tooling support for the Polylith Architecture"
authors = ["David Vujic"]
homepage = "https://davidvujic.github.io/python-polylith-docs/"
Expand Down
2 changes: 1 addition & 1 deletion projects/polylith_cli/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "polylith-cli"
version = "1.20.2"
version = "1.21.0"
description = "Python tooling support for the Polylith Architecture"
authors = ['David Vujic']
homepage = "https://davidvujic.github.io/python-polylith-docs/"
Expand Down
43 changes: 42 additions & 1 deletion test/components/polylith/diff/test_collect.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
from polylith.diff import collect
from pathlib import Path

import pytest
from polylith.diff import collect

root = Path.cwd()
ns = "my_namespace"

subfolder = "python"
workspace_root_in_subfolder = Path(subfolder)

changed_files_loose = [
Path(f"components/{ns}/a/core.py"),
Path(f"some/other/{ns}/file.py"),
Path(f"bases/{ns}/b/core.py"),
Path(f"components/{ns}/b/core.py"),
Path(f"components/{ns}/c/nested/subfolder/core.py"),
Path(f"test/components/{ns}/x/core.py"),
Path("projects/z/pyproject.toml"),
]

changed_files_tdd = [
Expand All @@ -21,6 +26,8 @@
Path(f"components/b/src/{ns}/b/core.py"),
Path(f"components/{ns}/x/core.py"),
Path(f"components/c/src/{ns}/c/nested/subfolder/core.py"),
Path(f"components/x/test/{ns}/x/core.py"),
Path("projects/z/pyproject.toml"),
]


Expand Down Expand Up @@ -64,3 +71,37 @@ def test_get_changed_bases_with_tdd_theme(setup):
res = collect.get_changed_bases(root, changed_files_tdd, ns)

assert res == ["b"]


def test_get_changed_components_with_workspace_in_sub_folder(setup):
setup(theme="loose")

changes = [Path(f"{subfolder}/{p.as_posix()}") for p in changed_files_loose]

res = collect.get_changed_components(workspace_root_in_subfolder, changes, ns)

assert res == ["a", "b", "c"]


def test_get_changed_components_with_workspace_in_sub_folder_tdd_theme(setup):
setup(theme="tdd")

changes = [Path(f"{subfolder}/{p.as_posix()}") for p in changed_files_tdd]

res = collect.get_changed_components(workspace_root_in_subfolder, changes, ns)

assert res == ["a", "b", "c"]


def test_get_changed_projects():
res = collect.get_changed_projects(root, changed_files_loose)

assert res == ["z"]


def test_get_changed_projects_in_subfolder():
changes = [Path(f"{subfolder}/{p.as_posix()}") for p in changed_files_loose]

res = collect.get_changed_projects(workspace_root_in_subfolder, changes)

assert res == ["z"]

0 comments on commit d39f346

Please sign in to comment.