|
4 | 4 |
|
5 | 5 | from codemodder.codetf import DiffSide |
6 | 6 | from codemodder.dependency import DefusedXML, Security |
7 | | -from codemodder.dependency_management.pyproject_writer import PyprojectWriter |
| 7 | +from codemodder.dependency_management.pyproject_writer import ( |
| 8 | + TYPE_CHECKER_LIBRARIES, |
| 9 | + PyprojectWriter, |
| 10 | +) |
8 | 11 | from codemodder.project_analysis.file_parsers.package_store import ( |
9 | 12 | FileType, |
10 | 13 | PackageStore, |
@@ -408,3 +411,195 @@ def test_pyproject_poetry_no_declared_deps(tmpdir): |
408 | 411 | """ |
409 | 412 |
|
410 | 413 | assert pyproject_toml.read() == dedent(updated_pyproject) |
| 414 | + |
| 415 | + |
| 416 | +@pytest.mark.parametrize("type_checker", TYPE_CHECKER_LIBRARIES) |
| 417 | +def test_pyproject_poetry_with_type_checker_tool(tmpdir, type_checker): |
| 418 | + orig_pyproject = f"""\ |
| 419 | + [tool.poetry] |
| 420 | + name = "example-project" |
| 421 | + version = "0.1.0" |
| 422 | + description = "An example project to demonstrate Poetry configuration." |
| 423 | + authors = ["Your Name <[email protected]>"] |
| 424 | +
|
| 425 | + [build-system] |
| 426 | + requires = ["poetry-core>=1.0.0"] |
| 427 | + build-backend = "poetry.core.masonry.api" |
| 428 | +
|
| 429 | + [tool.poetry.dependencies] |
| 430 | + python = "~=3.11.0" |
| 431 | + requests = ">=2.25.1,<3.0.0" |
| 432 | + pandas = "^1.2.3" |
| 433 | + libcst = ">1.0" |
| 434 | + {type_checker} = "==1.0" |
| 435 | + """ |
| 436 | + |
| 437 | + pyproject_toml = tmpdir.join("pyproject.toml") |
| 438 | + pyproject_toml.write(dedent(orig_pyproject)) |
| 439 | + |
| 440 | + store = PackageStore( |
| 441 | + type=FileType.TOML, |
| 442 | + file=pyproject_toml, |
| 443 | + dependencies=set(), |
| 444 | + py_versions=["~=3.11.0"], |
| 445 | + ) |
| 446 | + |
| 447 | + writer = PyprojectWriter(store, tmpdir) |
| 448 | + dependencies = [Security, DefusedXML] |
| 449 | + writer.write(dependencies) |
| 450 | + |
| 451 | + defusedxml_type_stub = DefusedXML.type_stubs[0] |
| 452 | + updated_pyproject = f"""\ |
| 453 | + [tool.poetry] |
| 454 | + name = "example-project" |
| 455 | + version = "0.1.0" |
| 456 | + description = "An example project to demonstrate Poetry configuration." |
| 457 | + authors = ["Your Name <[email protected]>"] |
| 458 | +
|
| 459 | + [build-system] |
| 460 | + requires = ["poetry-core>=1.0.0"] |
| 461 | + build-backend = "poetry.core.masonry.api" |
| 462 | +
|
| 463 | + [tool.poetry.dependencies] |
| 464 | + python = "~=3.11.0" |
| 465 | + requests = ">=2.25.1,<3.0.0" |
| 466 | + pandas = "^1.2.3" |
| 467 | + libcst = ">1.0" |
| 468 | + {type_checker} = "==1.0" |
| 469 | + {Security.requirement.name} = "{str(Security.requirement.specifier)}" |
| 470 | + {DefusedXML.requirement.name} = "{str(DefusedXML.requirement.specifier)}" |
| 471 | + {defusedxml_type_stub.requirement.name} = "{str(defusedxml_type_stub.requirement.specifier)}" |
| 472 | + """ |
| 473 | + |
| 474 | + assert pyproject_toml.read() == dedent(updated_pyproject) |
| 475 | + |
| 476 | + |
| 477 | +@pytest.mark.parametrize( |
| 478 | + "dependency_section", |
| 479 | + [ |
| 480 | + "[tool.poetry.test.dependencies]", |
| 481 | + "[tool.poetry.dev-dependencies]", |
| 482 | + "[tool.poetry.dev.dependencies]", |
| 483 | + "[tool.poetry.group.test.dependencies]", |
| 484 | + ], |
| 485 | +) |
| 486 | +@pytest.mark.parametrize("type_checker", TYPE_CHECKER_LIBRARIES) |
| 487 | +def test_pyproject_poetry_with_type_checker_tool_without_poetry_deps_section( |
| 488 | + tmpdir, type_checker, dependency_section |
| 489 | +): |
| 490 | + orig_pyproject = f"""\ |
| 491 | + [tool.poetry] |
| 492 | + name = "example-project" |
| 493 | + version = "0.1.0" |
| 494 | + description = "An example project to demonstrate Poetry configuration." |
| 495 | + authors = ["Your Name <[email protected]>"] |
| 496 | +
|
| 497 | + [build-system] |
| 498 | + requires = ["poetry-core>=1.0.0"] |
| 499 | + build-backend = "poetry.core.masonry.api" |
| 500 | + |
| 501 | + {dependency_section} |
| 502 | + {type_checker} = "==1.0" |
| 503 | + """ |
| 504 | + |
| 505 | + pyproject_toml = tmpdir.join("pyproject.toml") |
| 506 | + pyproject_toml.write(dedent(orig_pyproject)) |
| 507 | + |
| 508 | + store = PackageStore( |
| 509 | + type=FileType.TOML, |
| 510 | + file=pyproject_toml, |
| 511 | + dependencies=set(), |
| 512 | + py_versions=["~=3.11.0"], |
| 513 | + ) |
| 514 | + |
| 515 | + writer = PyprojectWriter(store, tmpdir) |
| 516 | + dependencies = [Security, DefusedXML] |
| 517 | + writer.write(dependencies) |
| 518 | + |
| 519 | + defusedxml_type_stub = DefusedXML.type_stubs[0] |
| 520 | + updated_pyproject = f"""\ |
| 521 | + [tool.poetry] |
| 522 | + name = "example-project" |
| 523 | + version = "0.1.0" |
| 524 | + description = "An example project to demonstrate Poetry configuration." |
| 525 | + authors = ["Your Name <[email protected]>"] |
| 526 | +
|
| 527 | + [tool.poetry.dependencies] |
| 528 | + {Security.requirement.name} = "{str(Security.requirement.specifier)}" |
| 529 | + {DefusedXML.requirement.name} = "{str(DefusedXML.requirement.specifier)}" |
| 530 | +
|
| 531 | + [build-system] |
| 532 | + requires = ["poetry-core>=1.0.0"] |
| 533 | + build-backend = "poetry.core.masonry.api" |
| 534 | + |
| 535 | + {dependency_section} |
| 536 | + {type_checker} = "==1.0" |
| 537 | + {defusedxml_type_stub.requirement.name} = "{str(defusedxml_type_stub.requirement.specifier)}" |
| 538 | + """ |
| 539 | + |
| 540 | + assert pyproject_toml.read() == dedent(updated_pyproject) |
| 541 | + |
| 542 | + |
| 543 | +@pytest.mark.parametrize("type_checker", TYPE_CHECKER_LIBRARIES) |
| 544 | +def test_pyproject_poetry_with_type_checker_tool_multiple(tmpdir, type_checker): |
| 545 | + orig_pyproject = f"""\ |
| 546 | + [build-system] |
| 547 | + requires = ["poetry-core>=1.0.0"] |
| 548 | + build-backend = "poetry.core.masonry.api" |
| 549 | + |
| 550 | + [tool.poetry] |
| 551 | + name = "example-project" |
| 552 | + version = "0.1.0" |
| 553 | + description = "An example project to demonstrate Poetry configuration." |
| 554 | + authors = ["Your Name <[email protected]>"] |
| 555 | +
|
| 556 | + [tool.poetry.dependencies] |
| 557 | + python = "~=3.11.0" |
| 558 | + requests = ">=2.25.1,<3.0.0" |
| 559 | + pandas = "^1.2.3" |
| 560 | + libcst = ">1.0" |
| 561 | + |
| 562 | + [tool.poetry.group.test.dependencies] |
| 563 | + {type_checker} = "==1.0" |
| 564 | + """ |
| 565 | + |
| 566 | + pyproject_toml = tmpdir.join("pyproject.toml") |
| 567 | + pyproject_toml.write(dedent(orig_pyproject)) |
| 568 | + |
| 569 | + store = PackageStore( |
| 570 | + type=FileType.TOML, |
| 571 | + file=pyproject_toml, |
| 572 | + dependencies=set(), |
| 573 | + py_versions=["~=3.11.0"], |
| 574 | + ) |
| 575 | + |
| 576 | + writer = PyprojectWriter(store, tmpdir) |
| 577 | + dependencies = [Security, DefusedXML] |
| 578 | + writer.write(dependencies) |
| 579 | + |
| 580 | + defusedxml_type_stub = DefusedXML.type_stubs[0] |
| 581 | + updated_pyproject = f"""\ |
| 582 | + [build-system] |
| 583 | + requires = ["poetry-core>=1.0.0"] |
| 584 | + build-backend = "poetry.core.masonry.api" |
| 585 | + |
| 586 | + [tool.poetry] |
| 587 | + name = "example-project" |
| 588 | + version = "0.1.0" |
| 589 | + description = "An example project to demonstrate Poetry configuration." |
| 590 | + authors = ["Your Name <[email protected]>"] |
| 591 | +
|
| 592 | + [tool.poetry.dependencies] |
| 593 | + python = "~=3.11.0" |
| 594 | + requests = ">=2.25.1,<3.0.0" |
| 595 | + pandas = "^1.2.3" |
| 596 | + libcst = ">1.0" |
| 597 | + {Security.requirement.name} = "{str(Security.requirement.specifier)}" |
| 598 | + {DefusedXML.requirement.name} = "{str(DefusedXML.requirement.specifier)}" |
| 599 | +
|
| 600 | + [tool.poetry.group.test.dependencies] |
| 601 | + {type_checker} = "==1.0" |
| 602 | + {defusedxml_type_stub.requirement.name} = "{str(defusedxml_type_stub.requirement.specifier)}" |
| 603 | + """ |
| 604 | + |
| 605 | + assert pyproject_toml.read() == dedent(updated_pyproject) |
0 commit comments