Skip to content

Commit d39f346

Browse files
authored
fix(poly diff): list of diff files with paths starting from the workspace-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
1 parent 70a2d89 commit d39f346

File tree

6 files changed

+71
-10
lines changed

6 files changed

+71
-10
lines changed

components/polylith/commands/diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def print_views(root: Path, tag: str, options: dict) -> None:
6666
changed_bases = diff.collect.get_changed_bases(root, files, ns)
6767
changed_components = diff.collect.get_changed_components(root, files, ns)
6868
changed_bricks = set(changed_bases + changed_components)
69-
changed_projects = diff.collect.get_changed_projects(files)
69+
changed_projects = diff.collect.get_changed_projects(root, files)
7070

7171
projects_data = get_projects_data(root, ns)
7272

components/polylith/diff/collect.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,31 @@ def _parse_folder_parts(pattern: str, changed_file: Path) -> str:
1515
return next(p for p in file_path.parts if p != file_path.root)
1616

1717

18-
def _get_changed(pattern: str, changed_files: List[Path]) -> set:
18+
def _is_in_workspace(root: Path, top_dir: str, changed_file: Path) -> bool:
19+
file_path = changed_file.as_posix()
20+
21+
if file_path.startswith(top_dir):
22+
return True
23+
24+
return f"{root.name}/{top_dir}" in file_path
25+
26+
27+
def _is_match(root: Path, top_dir: str, pattern: str, file_path: Path) -> bool:
28+
if re.match(pattern, file_path.as_posix()):
29+
return True
30+
31+
found = re.search(pattern, file_path.as_posix())
32+
33+
return found is not None and _is_in_workspace(root, top_dir, file_path)
34+
35+
36+
def _get_changed(
37+
root: Path, top_dir: str, pattern: str, changed_files: List[Path]
38+
) -> set:
1939
return {
2040
_parse_folder_parts(pattern, f)
2141
for f in changed_files
22-
if re.match(pattern, f.as_posix())
42+
if _is_match(root, top_dir, pattern, f)
2343
}
2444

2545

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

40-
return sorted(_get_changed(pattern, changed_files))
60+
return sorted(_get_changed(root, top_dir, pattern, changed_files))
4161

4262

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

5272

53-
def get_changed_projects(changed_files: List[Path]) -> list:
54-
res = _get_changed(repo.projects_dir, changed_files)
73+
def get_changed_projects(root: Path, changed_files: List[Path]) -> list:
74+
res = _get_changed(root, repo.projects_dir, repo.projects_dir, changed_files)
5575
filtered = {p for p in res if p != repo.projects_dir}
5676
return sorted(filtered)
5777

development/david.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
changed_files = diff.collect.get_files(tag)
4343
changed_components = diff.collect.get_changed_components(root, changed_files, ns)
4444
changed_bases = diff.collect.get_changed_bases(root, changed_files, ns)
45-
changed_projects = diff.collect.get_changed_projects(changed_files)
45+
changed_projects = diff.collect.get_changed_projects(root, changed_files)
4646

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

projects/poetry_polylith_plugin/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "poetry-polylith-plugin"
3-
version = "1.31.0"
3+
version = "1.32.0"
44
description = "A Poetry plugin that adds tooling support for the Polylith Architecture"
55
authors = ["David Vujic"]
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"

projects/polylith_cli/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "polylith-cli"
3-
version = "1.20.2"
3+
version = "1.21.0"
44
description = "Python tooling support for the Polylith Architecture"
55
authors = ['David Vujic']
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"

test/components/polylith/diff/test_collect.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
from polylith.diff import collect
21
from pathlib import Path
2+
33
import pytest
4+
from polylith.diff import collect
45

56
root = Path.cwd()
67
ns = "my_namespace"
78

9+
subfolder = "python"
10+
workspace_root_in_subfolder = Path(subfolder)
811

912
changed_files_loose = [
1013
Path(f"components/{ns}/a/core.py"),
1114
Path(f"some/other/{ns}/file.py"),
1215
Path(f"bases/{ns}/b/core.py"),
1316
Path(f"components/{ns}/b/core.py"),
1417
Path(f"components/{ns}/c/nested/subfolder/core.py"),
18+
Path(f"test/components/{ns}/x/core.py"),
19+
Path("projects/z/pyproject.toml"),
1520
]
1621

1722
changed_files_tdd = [
@@ -21,6 +26,8 @@
2126
Path(f"components/b/src/{ns}/b/core.py"),
2227
Path(f"components/{ns}/x/core.py"),
2328
Path(f"components/c/src/{ns}/c/nested/subfolder/core.py"),
29+
Path(f"components/x/test/{ns}/x/core.py"),
30+
Path("projects/z/pyproject.toml"),
2431
]
2532

2633

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

6673
assert res == ["b"]
74+
75+
76+
def test_get_changed_components_with_workspace_in_sub_folder(setup):
77+
setup(theme="loose")
78+
79+
changes = [Path(f"{subfolder}/{p.as_posix()}") for p in changed_files_loose]
80+
81+
res = collect.get_changed_components(workspace_root_in_subfolder, changes, ns)
82+
83+
assert res == ["a", "b", "c"]
84+
85+
86+
def test_get_changed_components_with_workspace_in_sub_folder_tdd_theme(setup):
87+
setup(theme="tdd")
88+
89+
changes = [Path(f"{subfolder}/{p.as_posix()}") for p in changed_files_tdd]
90+
91+
res = collect.get_changed_components(workspace_root_in_subfolder, changes, ns)
92+
93+
assert res == ["a", "b", "c"]
94+
95+
96+
def test_get_changed_projects():
97+
res = collect.get_changed_projects(root, changed_files_loose)
98+
99+
assert res == ["z"]
100+
101+
102+
def test_get_changed_projects_in_subfolder():
103+
changes = [Path(f"{subfolder}/{p.as_posix()}") for p in changed_files_loose]
104+
105+
res = collect.get_changed_projects(workspace_root_in_subfolder, changes)
106+
107+
assert res == ["z"]

0 commit comments

Comments
 (0)