Skip to content

Commit 4d773fc

Browse files
authored
refactor(workspace): issues raised by SonarQube (#426)
* refactor(unit tests): us Pytest tmp_path instead of constructing a custom path * fix: low severity issues raised by SonarQube * refactor(tests): simplify conftest that creates a workspace
1 parent 2d7b54f commit 4d773fc

9 files changed

Lines changed: 42 additions & 68 deletions

File tree

components/polylith/check/report.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ def extract_collected_imports(
9191

9292

9393
def collect_all_imports(root: Path, ns: str, project_data: dict) -> dict:
94-
bases = {b for b in project_data.get("bases", [])}
95-
components = {c for c in project_data.get("components", [])}
94+
bases = set(project_data.get("bases", []))
95+
components = set(project_data.get("components", []))
9696

9797
bases_paths = workspace.paths.collect_bases_paths(root, ns, bases)
9898
components_paths = workspace.paths.collect_components_paths(root, ns, components)
@@ -141,8 +141,8 @@ def create_report(
141141
third_party_libs: Set,
142142
is_strict: bool = False,
143143
) -> dict:
144-
bases = {b for b in project_data.get("bases", [])}
145-
components = {c for c in project_data.get("components", [])}
144+
bases = set(project_data.get("bases", []))
145+
components = set(project_data.get("components", []))
146146

147147
brick_imports = collected_imports["brick_imports"]
148148
third_party_imports = collected_imports["third_party_imports"]

components/polylith/diff/collect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def get_latest_tag(root: Path, key: Union[str, None]) -> Union[str, None]:
9292
capture_output=True,
9393
)
9494

95-
return next((tag for tag in res.stdout.decode("utf-8").split()), None)
95+
return next(iter(res.stdout.decode("utf-8").split()), None)
9696

9797

9898
def get_files(tag: str) -> List[Path]:

components/polylith/diff/report.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def print_detected_changes_in_projects(projects: List[str], short: bool) -> None
7878

7979

8080
def print_projects_affected_by_changes(projects: Set[str], short: bool) -> None:
81-
sorted_projects = sorted(list(projects))
81+
sorted_projects = sorted(projects)
8282

8383
print_detected_changes(sorted_projects, "proj", short)
8484

components/polylith/info/collect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def get_matching_bricks(
1313

1414
res = set(bricks).intersection(paths_in_namespace)
1515

16-
return sorted(list(res))
16+
return sorted(res)
1717

1818

1919
def get_project_bricks(project_packages: List[dict], components, bases, namespace: str):

components/polylith/libs/report.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414

1515
def get_third_party_imports(root: Path, ns: str, project_data: dict) -> dict:
16-
bases = {b for b in project_data.get("bases", [])}
17-
components = {c for c in project_data.get("components", [])}
16+
bases = set(project_data.get("bases", []))
17+
components = set(project_data.get("components", []))
1818

1919
bases_paths = workspace.paths.collect_bases_paths(root, ns, bases)
2020
components_paths = workspace.paths.collect_components_paths(root, ns, components)

components/polylith/parsing/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ def copy_brick(
6565
def parse_brick_namespace_from_path(bricks: dict) -> str:
6666
parts = {str.split(v, "/")[0] for v in bricks.values()}
6767

68-
return next(part for part in parts)
68+
return next(iter(parts))

test/components/polylith/bricks/test_base.py

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,9 @@
11
from pathlib import Path
22

3-
import pytest
43
from polylith.bricks.base import create_base, get_bases_data
54

6-
create_base_params = [
7-
(
8-
"1. Creates expected directories and files",
9-
set(["bases", "test"]),
10-
set(
11-
[
12-
Path("test/temp/bases/test_namespace/test_package/core.py"),
13-
Path("test/temp/test/bases/test_namespace/test_package/test_core.py"),
14-
]
15-
),
16-
)
17-
]
18-
create_base_ids = [x[0] for x in create_base_params]
195

20-
21-
@pytest.mark.parametrize(
22-
"id, expected_dirs, expected_dir_structure",
23-
create_base_params,
24-
ids=create_base_ids,
25-
)
26-
def test_create_base(handle_workspace_files, id, expected_dirs, expected_dir_structure):
6+
def test_create_base(handle_workspace_files, tmp_path: Path):
277
options = {
288
"namespace": "test_namespace",
299
"package": "test_package",
@@ -32,16 +12,12 @@ def test_create_base(handle_workspace_files, id, expected_dirs, expected_dir_str
3212
}
3313
create_base(path=handle_workspace_files, options=options)
3414

35-
results = [
36-
x for x in handle_workspace_files.iterdir() if x.name != "workspace.toml"
15+
expected_dirs = [
16+
Path(tmp_path / "bases/test_namespace/test_package/core.py"),
17+
Path(tmp_path / "test/bases/test_namespace/test_package/test_core.py"),
3718
]
3819

39-
assert all([item.is_dir() for item in results if item in expected_dirs])
40-
assert (
41-
set([item.name for item in results]).intersection(expected_dirs)
42-
== expected_dirs
43-
)
44-
assert all([item.exists for item in expected_dir_structure])
20+
assert all(item.exists() for item in expected_dirs)
4521

4622

4723
def test_get_bases_data_valid_with_test_file_structure(create_test_base):

test/conftest.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
1-
import shutil
21
from pathlib import Path
32

43
import pytest
54
from polylith.bricks.base import create_base
65

6+
source_file = """
7+
[tool.polylith]
8+
namespace = "test_space"
79
8-
@pytest.fixture(scope="function")
9-
def handle_workspace_files():
10-
"""Creates a temporary directory with a valid workspace file and removes the directory in tear-down.
10+
[tool.polylith.structure]
11+
theme = "loose"
1112
12-
Yields:
13-
Path: The temp directory path
14-
"""
15-
temp_dir = Path("test/temp")
16-
temp_dir.mkdir(parents=True, exist_ok=True)
17-
workspace_file = temp_dir / "workspace.toml"
13+
[tool.polylith.tag.patterns]
14+
stable = "stable-*"
15+
release = "v[0-9]*"
16+
17+
[tool.polylith.test]
18+
enabled = true
19+
20+
[tool.polylith.resources]
21+
brick_docs_enabled = false
22+
"""
23+
24+
25+
@pytest.fixture(scope="function")
26+
def handle_workspace_files(tmp_path: Path) -> Path:
27+
"""Returns a temporary directory with a valid workspace file."""
28+
workspace_file = tmp_path / "workspace.toml"
1829
workspace_file.touch()
19-
source_file = Path("test/test_data/workspace.toml")
20-
workspace_file.write_text(source_file.read_text())
21-
yield temp_dir
22-
shutil.rmtree(temp_dir)
30+
31+
workspace_file.write_text(source_file)
32+
33+
return tmp_path
2334

2435

2536
@pytest.fixture(scope="function")
@@ -42,5 +53,7 @@ def create_test_base(handle_workspace_files):
4253
"description": "test desc",
4354
"modulename": "core",
4455
}
56+
4557
create_base(path=handle_workspace_files, options=options)
58+
4659
yield handle_workspace_files

test/test_data/workspace.toml

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)