|
18 | 18 |
|
19 | 19 | import static org.assertj.core.api.Assertions.assertThat; |
20 | 20 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
| 21 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
21 | 22 |
|
22 | 23 | import io.github.guacsec.trustifyda.Api; |
23 | 24 | import io.github.guacsec.trustifyda.ExhortTest; |
|
28 | 29 | import java.nio.file.Path; |
29 | 30 | import org.junit.jupiter.api.Assumptions; |
30 | 31 | import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.io.TempDir; |
31 | 33 |
|
32 | 34 | class Python_Uv_Provider_Test extends ExhortTest { |
33 | 35 |
|
@@ -238,6 +240,171 @@ void test_ignored_dependencies_in_uv_project() throws IOException { |
238 | 240 | } |
239 | 241 | } |
240 | 242 |
|
| 243 | + @Test |
| 244 | + void test_parseUvExport_includes_editable_installs(@TempDir Path tempDir) throws IOException { |
| 245 | + // Create a workspace member with its own pyproject.toml |
| 246 | + Path memberDir = tempDir.resolve("packages").resolve("my-lib"); |
| 247 | + Files.createDirectories(memberDir); |
| 248 | + Files.writeString( |
| 249 | + memberDir.resolve("pyproject.toml"), |
| 250 | + "[project]\nname = \"my-lib\"\nversion = \"2.0.0\"\ndependencies = []\n"); |
| 251 | + |
| 252 | + Path pyprojectPath = Path.of(UV_FIXTURE, "pyproject.toml"); |
| 253 | + var provider = new PythonUvProvider(pyprojectPath); |
| 254 | + |
| 255 | + // Simulate uv export output with an editable install |
| 256 | + String exportOutput = |
| 257 | + "# This file was autogenerated by uv\n" |
| 258 | + + "-e file://" |
| 259 | + + memberDir.toUri().getPath() |
| 260 | + + "\n" |
| 261 | + + " # via test-project\n" |
| 262 | + + "anyio==3.6.2\n" |
| 263 | + + " # via my-lib\n"; |
| 264 | + |
| 265 | + var data = provider.parseUvExport(exportOutput); |
| 266 | + |
| 267 | + // The editable install should be in the graph with name/version from pyproject.toml |
| 268 | + assertThat(data.graph()).containsKey("my-lib"); |
| 269 | + assertThat(data.graph().get("my-lib").name()).isEqualTo("my-lib"); |
| 270 | + assertThat(data.graph().get("my-lib").version()).isEqualTo("2.0.0"); |
| 271 | + |
| 272 | + // It should be identified as a direct dependency (via test-project) |
| 273 | + assertThat(data.directDeps()).contains("my-lib"); |
| 274 | + |
| 275 | + // anyio should be a child of my-lib |
| 276 | + assertThat(data.graph().get("my-lib").children()).contains("anyio"); |
| 277 | + } |
| 278 | + |
| 279 | + @Test |
| 280 | + void test_parseUvExport_editable_skips_self_reference(@TempDir Path tempDir) throws IOException { |
| 281 | + // Create a member whose name matches the root project (test-project) |
| 282 | + Path memberDir = tempDir.resolve("packages").resolve("self"); |
| 283 | + Files.createDirectories(memberDir); |
| 284 | + Files.writeString( |
| 285 | + memberDir.resolve("pyproject.toml"), |
| 286 | + "[project]\nname = \"test-project\"\nversion = \"0.1.0\"\ndependencies = []\n"); |
| 287 | + |
| 288 | + Path pyprojectPath = Path.of(UV_FIXTURE, "pyproject.toml"); |
| 289 | + var provider = new PythonUvProvider(pyprojectPath); |
| 290 | + |
| 291 | + String exportOutput = |
| 292 | + "# This file was autogenerated by uv\n" |
| 293 | + + "-e file://" |
| 294 | + + memberDir.toUri().getPath() |
| 295 | + + "\n" |
| 296 | + + "anyio==3.6.2\n" |
| 297 | + + " # via test-project\n"; |
| 298 | + |
| 299 | + var data = provider.parseUvExport(exportOutput); |
| 300 | + |
| 301 | + // The root project should NOT appear in the graph as its own dependency |
| 302 | + assertThat(data.graph()).doesNotContainKey("test-project"); |
| 303 | + // anyio should still be a direct dep |
| 304 | + assertThat(data.directDeps()).contains("anyio"); |
| 305 | + } |
| 306 | + |
| 307 | + @Test |
| 308 | + void test_parseUvExport_editable_skips_missing_version(@TempDir Path tempDir) throws IOException { |
| 309 | + // Create a member with no version |
| 310 | + Path memberDir = tempDir.resolve("packages").resolve("no-version"); |
| 311 | + Files.createDirectories(memberDir); |
| 312 | + Files.writeString( |
| 313 | + memberDir.resolve("pyproject.toml"), |
| 314 | + "[project]\nname = \"no-version-lib\"\ndependencies = []\n"); |
| 315 | + |
| 316 | + Path pyprojectPath = Path.of(UV_FIXTURE, "pyproject.toml"); |
| 317 | + var provider = new PythonUvProvider(pyprojectPath); |
| 318 | + |
| 319 | + String exportOutput = |
| 320 | + "# This file was autogenerated by uv\n" |
| 321 | + + "-e file://" |
| 322 | + + memberDir.toUri().getPath() |
| 323 | + + "\n" |
| 324 | + + " # via test-project\n" |
| 325 | + + "anyio==3.6.2\n" |
| 326 | + + " # via test-project\n"; |
| 327 | + |
| 328 | + var data = provider.parseUvExport(exportOutput); |
| 329 | + |
| 330 | + // Package with no version should be skipped |
| 331 | + assertThat(data.graph()).doesNotContainKey("no-version-lib"); |
| 332 | + // anyio should still be parsed |
| 333 | + assertThat(data.graph()).containsKey("anyio"); |
| 334 | + } |
| 335 | + |
| 336 | + @Test |
| 337 | + void test_parseUvExport_editable_falls_back_to_poetry_name(@TempDir Path tempDir) |
| 338 | + throws IOException { |
| 339 | + // Create a member with Poetry-style name/version only |
| 340 | + Path memberDir = tempDir.resolve("packages").resolve("poetry-lib"); |
| 341 | + Files.createDirectories(memberDir); |
| 342 | + Files.writeString( |
| 343 | + memberDir.resolve("pyproject.toml"), |
| 344 | + "[tool.poetry]\nname = \"poetry-lib\"\nversion = \"1.5.0\"\n"); |
| 345 | + |
| 346 | + Path pyprojectPath = Path.of(UV_FIXTURE, "pyproject.toml"); |
| 347 | + var provider = new PythonUvProvider(pyprojectPath); |
| 348 | + |
| 349 | + String exportOutput = |
| 350 | + "# This file was autogenerated by uv\n" |
| 351 | + + "-e file://" |
| 352 | + + memberDir.toUri().getPath() |
| 353 | + + "\n" |
| 354 | + + " # via test-project\n" |
| 355 | + + "anyio==3.6.2\n" |
| 356 | + + " # via poetry-lib\n"; |
| 357 | + |
| 358 | + var data = provider.parseUvExport(exportOutput); |
| 359 | + |
| 360 | + // Poetry name/version should be used as fallback |
| 361 | + assertThat(data.graph()).containsKey("poetry-lib"); |
| 362 | + assertThat(data.graph().get("poetry-lib").name()).isEqualTo("poetry-lib"); |
| 363 | + assertThat(data.graph().get("poetry-lib").version()).isEqualTo("1.5.0"); |
| 364 | + |
| 365 | + // It should be a direct dep and anyio should be its child |
| 366 | + assertThat(data.directDeps()).contains("poetry-lib"); |
| 367 | + assertThat(data.graph().get("poetry-lib").children()).contains("anyio"); |
| 368 | + } |
| 369 | + |
| 370 | + @Test |
| 371 | + void test_parseUvExport_via_skips_non_bare_package_names() throws IOException { |
| 372 | + Path pyprojectPath = Path.of(UV_FIXTURE, "pyproject.toml"); |
| 373 | + var provider = new PythonUvProvider(pyprojectPath); |
| 374 | + |
| 375 | + String exportOutput = |
| 376 | + "# This file was autogenerated by uv\n" |
| 377 | + + "anyio==3.6.2\n" |
| 378 | + + " # via test-project\n" |
| 379 | + + "idna==3.4\n" |
| 380 | + + " # via foo (>=1.0)\n" |
| 381 | + + "sniffio==1.3.0\n" |
| 382 | + + " # via foo[extra]\n"; |
| 383 | + |
| 384 | + var data = provider.parseUvExport(exportOutput); |
| 385 | + |
| 386 | + // anyio is direct (via test-project) |
| 387 | + assertThat(data.directDeps()).contains("anyio"); |
| 388 | + // idna and sniffio should be in the graph but with no parent edges |
| 389 | + assertThat(data.graph()).containsKey("idna"); |
| 390 | + assertThat(data.graph()).containsKey("sniffio"); |
| 391 | + // No package should have children since the via names were invalid |
| 392 | + assertThat(data.graph().values().stream().allMatch(p -> p.children().isEmpty())).isTrue(); |
| 393 | + } |
| 394 | + |
| 395 | + @Test |
| 396 | + void test_parseUvExport_throws_on_unpinned_version() { |
| 397 | + Path pyprojectPath = Path.of(UV_FIXTURE, "pyproject.toml"); |
| 398 | + var provider = new PythonUvProvider(pyprojectPath); |
| 399 | + |
| 400 | + String exportOutput = |
| 401 | + "# This file was autogenerated by uv\n" + "anyio>=3.6.2\n" + " # via test-project\n"; |
| 402 | + |
| 403 | + assertThatThrownBy(() -> provider.parseUvExport(exportOutput)) |
| 404 | + .isInstanceOf(IOException.class) |
| 405 | + .hasMessageContaining("has no pinned version"); |
| 406 | + } |
| 407 | + |
241 | 408 | @Test |
242 | 409 | void test_canonicalize() { |
243 | 410 | assertThat(PyprojectTomlUtils.canonicalize("charset_normalizer")) |
|
0 commit comments