Skip to content

Commit

Permalink
fix: package-type fixer to avoid duplicated empty section (#2578)
Browse files Browse the repository at this point in the history
* fix: packagetype fixer avoid dupl. empty section

close: #2577

* test: add test for the fixer fix

* doc: add news

* Update src/pdm/cli/commands/fix/fixers.py

* Update tests/cli/test_install.py

* fix: test project missing requires-python

* address ming's review comment

* Update tests/cli/test_install.py

---------

Co-authored-by: Frost Ming <[email protected]>
  • Loading branch information
wey-gu and frostming authored Jan 22, 2024
1 parent 01f6a8d commit 1c5bd6a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions news/2578.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix the package-type fixer won't update toml properly for "Nested Section Ordering Issue in TOML".
16 changes: 13 additions & 3 deletions src/pdm/cli/commands/fix/fixers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ def check(self) -> bool:
return "package-type" in self.project.pyproject.settings

def fix(self) -> None:
package_type = self.project.pyproject.settings.pop("package-type")
dist = bool(package_type == "library")
self.project.pyproject.settings["distribution"] = dist
# Copy the project settings
settings = self.project.pyproject.settings.copy()

# Pop the package type and convert it to a distribution type
package_type = settings.pop("package-type")
dist = package_type == "library"
settings["distribution"] = dist

# Update the project settings with the new distribution type
self.project.pyproject._data["tool"].pop("pdm")
self.project.pyproject.settings.update(settings)

# Write the updated settings back to the project
self.project.pyproject.write(False)
7 changes: 7 additions & 0 deletions tests/cli/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,10 @@ def test_install_requirement_with_extras(project, pdm, working_set):
pdm(["lock", "-Gsocks"], obj=project, strict=True)
pdm(["sync", "-Gsocks"], obj=project, strict=True)
assert "pysocks" in working_set


def test_fix_package_type_and_update(fixture_project, pdm, working_set):
project = fixture_project("test-package-type-fixer")
pdm(["fix", "package-type"], obj=project, strict=True)
pdm(["update"], obj=project, strict=True)
assert "test-package-type-fixer" not in working_set
23 changes: 23 additions & 0 deletions tests/fixtures/projects/test-package-type-fixer/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"

[project]

version = "0.0.1"
dependencies = []
name = "test-package-type-fixer"
requires-python = ">=3.8"

[tool.pdm.version]
source = "file"
path = "src/test_package_type_fixer/__init__.py"

[tool.pdm]
package-type = "application"

[tool.pdm.dev-dependencies]

dev = [
"requests==2.19.1"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__verson__ = '0.1.0'

0 comments on commit 1c5bd6a

Please sign in to comment.