|
1 | 1 | from pathlib import Path |
| 2 | +from typing import Union |
2 | 3 |
|
3 | 4 | import tomlkit |
4 | 5 | from polylith.sync import update |
5 | 6 |
|
6 | 7 |
|
| 8 | +def parse(data: Union[str, None]) -> dict: |
| 9 | + parsed = tomlkit.parse(data) if data else {} |
| 10 | + |
| 11 | + return parsed or {} |
| 12 | + |
| 13 | + |
7 | 14 | def test_brick_to_pyproject_package(): |
8 | 15 | ns = "unit_test" |
9 | 16 | brick = "greet" |
@@ -70,75 +77,126 @@ def test_bricks_to_pyproject_packages(): |
70 | 77 | "components/hello/third": "hello/third", |
71 | 78 | } |
72 | 79 |
|
| 80 | +unsorted_packages = [ |
| 81 | + {"include": "hello/c", "from": "components"}, |
| 82 | + {"include": "hello/a", "from": "bases"}, |
| 83 | + {"include": "hello/b", "from": "components"}, |
| 84 | +] |
| 85 | + |
| 86 | +expected_sorted_pep621_bricks = { |
| 87 | + "bases/hello/first": "hello/first", |
| 88 | + "bases/hello/a": "hello/a", |
| 89 | + "components/hello/b": "hello/b", |
| 90 | + "components/hello/c": "hello/c", |
| 91 | +} |
73 | 92 |
|
74 | | -def test_generate_updated_poetry_project(): |
75 | | - data = tomlkit.parse( |
76 | | - """\ |
| 93 | +poetry_project_data = """\ |
77 | 94 | [tool.poetry] |
78 | 95 | packages = [{include = "hello/first", from = "bases"}] |
79 | 96 |
|
80 | 97 | [build-system] |
81 | 98 | requires = ["poetry-core>=1.0.0"] |
82 | 99 | build-backend = "poetry.core.masonry.api" |
83 | 100 | """ |
84 | | - ) |
85 | 101 |
|
86 | | - updated = update.generate_updated_project(data, packages[1:]) |
87 | | - |
88 | | - res = tomlkit.parse(updated)["tool"]["poetry"]["packages"] |
89 | | - |
90 | | - assert res == packages |
91 | 102 |
|
92 | | - |
93 | | -def test_generate_updated_hatch_project_with_existing_polylith_sections(): |
94 | | - data = tomlkit.parse( |
95 | | - """\ |
| 103 | +hatchling_build_system = """\ |
96 | 104 | [build-system] |
97 | 105 | requires = ["hatchling"] |
98 | 106 | build-backend = "hatchling.build" |
| 107 | +""" |
| 108 | + |
| 109 | +hatchling_project_data = """\ |
| 110 | +{build_system} |
99 | 111 |
|
100 | 112 | [tool.polylith.bricks] |
101 | 113 | "bases/hello/first" = "hello/first" |
102 | | -""" |
103 | | - ) |
| 114 | +""".format( |
| 115 | + build_system=hatchling_build_system |
| 116 | +) |
| 117 | + |
| 118 | +hatch_specific_project_data = """\ |
| 119 | +{build_system} |
| 120 | +
|
| 121 | +[tool.hatch.build.force-include] |
| 122 | +"bases/hello/first" = "hello/first" |
| 123 | +""".format( |
| 124 | + build_system=hatchling_build_system |
| 125 | +) |
| 126 | + |
| 127 | + |
| 128 | +def test_generate_updated_poetry_project(): |
| 129 | + data = tomlkit.parse(poetry_project_data) |
| 130 | + |
| 131 | + updated = update.generate_updated_project(data, packages[1:]) |
| 132 | + |
| 133 | + res = parse(updated)["tool"]["poetry"]["packages"] |
| 134 | + |
| 135 | + assert res == packages |
| 136 | + |
| 137 | + |
| 138 | +def test_generate_updated_poetry_project_with_the_bricks_to_update_sorted(): |
| 139 | + data = tomlkit.parse(poetry_project_data) |
| 140 | + |
| 141 | + expected = [ |
| 142 | + {"include": "hello/first", "from": "bases"}, |
| 143 | + {"include": "hello/a", "from": "bases"}, |
| 144 | + {"include": "hello/b", "from": "components"}, |
| 145 | + {"include": "hello/c", "from": "components"}, |
| 146 | + ] |
| 147 | + |
| 148 | + updated = update.generate_updated_project(data, unsorted_packages) |
| 149 | + |
| 150 | + res = parse(updated)["tool"]["poetry"]["packages"] |
| 151 | + |
| 152 | + assert res == expected |
| 153 | + |
| 154 | + |
| 155 | +def test_generate_updated_hatch_project_with_existing_polylith_sections(): |
| 156 | + data = tomlkit.parse(hatchling_project_data) |
104 | 157 |
|
105 | 158 | updated = update.generate_updated_project(data, packages[1:]) |
106 | 159 |
|
107 | | - res = tomlkit.parse(updated)["tool"]["polylith"]["bricks"] |
| 160 | + res = parse(updated)["tool"]["polylith"]["bricks"] |
108 | 161 |
|
109 | 162 | assert res == expected_hatch_packages |
110 | 163 |
|
111 | 164 |
|
| 165 | +def test_generate_updated_pep621_project_with_the_bricks_to_update_sorted(): |
| 166 | + data = tomlkit.parse(hatchling_project_data) |
| 167 | + |
| 168 | + updated = update.generate_updated_project(data, unsorted_packages) |
| 169 | + |
| 170 | + res = parse(updated)["tool"]["polylith"]["bricks"] |
| 171 | + |
| 172 | + assert list(res.keys()) == list(expected_sorted_pep621_bricks.keys()) |
| 173 | + |
| 174 | + |
112 | 175 | def test_generate_updated_hatch_project_with_missing_brick_config(): |
113 | | - data = tomlkit.parse( |
114 | | - """\ |
115 | | -[build-system] |
116 | | -requires = ["hatchling"] |
117 | | -build-backend = "hatchling.build" |
118 | | -""" |
119 | | - ) |
| 176 | + data = tomlkit.parse(hatchling_build_system) |
120 | 177 |
|
121 | 178 | updated = update.generate_updated_project(data, packages) |
122 | 179 |
|
123 | | - res = tomlkit.parse(updated)["tool"]["polylith"]["bricks"] |
| 180 | + res = parse(updated)["tool"]["polylith"]["bricks"] |
124 | 181 |
|
125 | 182 | assert res == expected_hatch_packages |
126 | 183 |
|
127 | 184 |
|
128 | 185 | def test_generate_updated_hatch_project_with_existing_force_include(): |
129 | | - data = tomlkit.parse( |
130 | | - """\ |
131 | | -[build-system] |
132 | | -requires = ["hatchling"] |
133 | | -build-backend = "hatchling.build" |
134 | | -
|
135 | | -[tool.hatch.build.force-include] |
136 | | -"bases/hello/first" = "hello/first" |
137 | | -""" |
138 | | - ) |
| 186 | + data = tomlkit.parse(hatch_specific_project_data) |
139 | 187 |
|
140 | 188 | updated = update.generate_updated_project(data, packages[1:]) |
141 | 189 |
|
142 | | - res = tomlkit.parse(updated)["tool"]["hatch"]["build"]["force-include"] |
| 190 | + res = parse(updated)["tool"]["hatch"]["build"]["force-include"] |
143 | 191 |
|
144 | 192 | assert res == expected_hatch_packages |
| 193 | + |
| 194 | + |
| 195 | +def test_generate_updated_hatch_project_force_include_with_sorted_bricks(): |
| 196 | + data = tomlkit.parse(hatch_specific_project_data) |
| 197 | + |
| 198 | + updated = update.generate_updated_project(data, unsorted_packages) |
| 199 | + |
| 200 | + res = parse(updated)["tool"]["hatch"]["build"]["force-include"] |
| 201 | + |
| 202 | + assert list(res.keys()) == list(expected_sorted_pep621_bricks.keys()) |
0 commit comments