From 1c5bd6a44f5ccee669eaa0461eb5528bbbca2e74 Mon Sep 17 00:00:00 2001 From: Wey Gu Date: Mon, 22 Jan 2024 10:38:04 +0800 Subject: [PATCH] fix: `package-type fixer` to avoid duplicated empty section (#2578) * 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 --- news/2578.bugfix.md | 1 + src/pdm/cli/commands/fix/fixers.py | 16 ++++++++++--- tests/cli/test_install.py | 7 ++++++ .../test-package-type-fixer/pyproject.toml | 23 +++++++++++++++++++ .../src/test_package_type_fixer/__init__.py | 1 + 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 news/2578.bugfix.md create mode 100644 tests/fixtures/projects/test-package-type-fixer/pyproject.toml create mode 100644 tests/fixtures/projects/test-package-type-fixer/src/test_package_type_fixer/__init__.py diff --git a/news/2578.bugfix.md b/news/2578.bugfix.md new file mode 100644 index 0000000000..6ec0c5a9ca --- /dev/null +++ b/news/2578.bugfix.md @@ -0,0 +1 @@ +fix the package-type fixer won't update toml properly for "Nested Section Ordering Issue in TOML". \ No newline at end of file diff --git a/src/pdm/cli/commands/fix/fixers.py b/src/pdm/cli/commands/fix/fixers.py index 1f9f03e0b6..0c853844ad 100644 --- a/src/pdm/cli/commands/fix/fixers.py +++ b/src/pdm/cli/commands/fix/fixers.py @@ -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) diff --git a/tests/cli/test_install.py b/tests/cli/test_install.py index b1b50c6f80..f07a8d2619 100644 --- a/tests/cli/test_install.py +++ b/tests/cli/test_install.py @@ -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 diff --git a/tests/fixtures/projects/test-package-type-fixer/pyproject.toml b/tests/fixtures/projects/test-package-type-fixer/pyproject.toml new file mode 100644 index 0000000000..550eaf68c6 --- /dev/null +++ b/tests/fixtures/projects/test-package-type-fixer/pyproject.toml @@ -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" +] \ No newline at end of file diff --git a/tests/fixtures/projects/test-package-type-fixer/src/test_package_type_fixer/__init__.py b/tests/fixtures/projects/test-package-type-fixer/src/test_package_type_fixer/__init__.py new file mode 100644 index 0000000000..b1621543f8 --- /dev/null +++ b/tests/fixtures/projects/test-package-type-fixer/src/test_package_type_fixer/__init__.py @@ -0,0 +1 @@ +__verson__ = '0.1.0' \ No newline at end of file