Skip to content

Commit

Permalink
fix: cleanup logic to first check config_settings (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored Jun 6, 2023
1 parent 2190101 commit d1f1e9e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,4 @@ caches/
.idea/
__pypackages__
.pdm-python
.pdm-build/
19 changes: 14 additions & 5 deletions src/pdm/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _find_top_packages(root: str) -> list[str]:
class Builder:
"""Base class for building and distributing a package from given path."""

DEFAULT_EXCLUDES = ["build"]
DEFAULT_EXCLUDES = [".pdm-build"]

target: str
hooks: list[BuildHookInterface] = [DynamicVersionBuildHook()]
Expand Down Expand Up @@ -181,11 +181,20 @@ def finalize(self, context: Context, artifact: Path) -> None:
def build(self, build_dir: str, **kwargs: Any) -> Path:
"""Build the package and return the path to the artifact."""
context = self.build_context(Path(build_dir), **kwargs)
if (
not self.config_settings.get("no-clean-build")
or os.getenv("PDM_BUILD_NO_CLEAN", "false").lower() == "false"
):
should_clean = True

if "no-clean-build" in self.config_settings:
should_clean = False
elif "PDM_BUILD_NO_CLEAN" in os.environ:
should_clean = os.getenv("PDM_BUILD_NO_CLEAN", "0").lower() in (
"0",
"false",
"no",
)

if should_clean:
self.clean(context)

self.initialize(context)
files = sorted(self.get_files(context))
artifact = self.build_artifact(context, files)
Expand Down
57 changes: 57 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import email
import os
import sys
import zipfile
from pathlib import Path
Expand Down Expand Up @@ -378,3 +379,59 @@ def test_get_version_from_call(project_with_scm: Path, getter: str) -> None:
with zipfile.ZipFile(wheel_name) as zf:
version = zf.read("foo/__version__.py").decode("utf-8").strip()
assert version == "1.1.1"


@pytest.mark.parametrize(
"settings, cleanup", [("true", False), ("false", True), ("0", True), ("1", False)]
)
def test_clean_not_called_if_envset(
project_with_scm: Path,
monkeypatch: pytest.MonkeyPatch,
settings: str,
cleanup: bool,
) -> None:
monkeypatch.setenv("PDM_BUILD_NO_CLEAN", settings)
builder = WheelBuilder(project_with_scm)
builder.config.data.setdefault("tool", {}).setdefault("pdm", {})["version"] = {
"source": "scm",
"write_to": "foo/__version__.py",
}

test_file = project_with_scm / ".pdm-build" / "testfile"
os.makedirs(project_with_scm / ".pdm-build", exist_ok=True)
test_file.touch()
assert os.path.exists(test_file)

with builder:
builder.build(project_with_scm / "dist")
if cleanup:
assert not os.path.exists(test_file)
else:
assert os.path.exists(test_file)


@pytest.mark.parametrize(
"settings, cleanup", [("", False), (True, False), (None, False)]
)
def test_clean_not_called_if_config_settings_exist(
project_with_scm: Path, settings: bool, cleanup: bool
) -> None:
builder = WheelBuilder(
project_with_scm, config_settings={"no-clean-build": settings}
)
builder.config.data.setdefault("tool", {}).setdefault("pdm", {})["version"] = {
"source": "scm",
"write_to": "foo/__version__.py",
}

test_file = project_with_scm / ".pdm-build" / "testfile"
os.makedirs(project_with_scm / ".pdm-build", exist_ok=True)
test_file.touch()
assert os.path.exists(test_file)

with builder:
builder.build(project_with_scm / "dist")
if cleanup:
assert not os.path.exists(test_file)
else:
assert os.path.exists(test_file)

0 comments on commit d1f1e9e

Please sign in to comment.