Skip to content

Commit 82ce3c2

Browse files
authored
Improve pdm_build.py (#273)
* Improve `pdm_build.py` Signed-off-by: Yu Ishikawa <[email protected]> * Add a nox test Signed-off-by: Yu Ishikawa <[email protected]> --------- Signed-off-by: Yu Ishikawa <[email protected]>
1 parent 4dc319e commit 82ce3c2

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

noxfile.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,27 @@ def run_cli_deprecations(session):
5050
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
5151
)
5252
session.run("dbt-autofix", "deprecations")
53+
54+
55+
@nox.session(python=["3.10", "3.11", "3.12", "3.13"], venv_backend="uv")
56+
def test_pre_commit_installation(session):
57+
"""Test that dbt-autofix can be installed as a pre-commit hook"""
58+
session.run_install(
59+
"uv",
60+
"sync",
61+
"--extra=test",
62+
f"--python={session.virtualenv.location}",
63+
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
64+
)
65+
# Use try-repo with a non-existent file to test installation without execution.
66+
# This avoids the requirement for a dbt_project.yml file while still
67+
# triggering the pdm_build.py logic we want to verify.
68+
session.run(
69+
"pre-commit",
70+
"try-repo",
71+
".",
72+
"dbt-autofix-check",
73+
"--files",
74+
"non_existent_file",
75+
"--verbose",
76+
)

pdm_build.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13
from pdm.backend.hooks.version import SCMVersion
24

35

@@ -14,6 +16,32 @@ def format_version(version: SCMVersion) -> str:
1416
def pdm_build_initialize(context):
1517
metadata = context.config.metadata
1618
if metadata["name"] == "dbt-autofix" and "dbt-fusion-package-tools" in metadata["dependencies"]:
17-
new_package_version = metadata["version"]
18-
dependency_idx = metadata["dependencies"].index("dbt-fusion-package-tools")
19-
metadata["dependencies"][dependency_idx] = f"dbt-fusion-package-tools=={new_package_version}"
19+
version = str(metadata["version"])
20+
# Use stderr to ensure visibility in pip output
21+
sys.stderr.write(f"[dbt-autofix build] Detected version: {version}\n")
22+
23+
# Only pin strictly for real releases.
24+
# Fallback versions (0.0...) or development versions (.post, .dev, .rc, +, alpha, beta, etc.)
25+
# should not be pinned as they likely won't exist on PyPI.
26+
is_release = not (
27+
".post" in version
28+
or ".dev" in version
29+
or "rc" in version
30+
or "+" in version
31+
or "a" in version
32+
or "b" in version
33+
or "alpha" in version
34+
or "beta" in version
35+
or version.startswith("0.0")
36+
)
37+
38+
if is_release:
39+
sys.stderr.write(
40+
f"[dbt-autofix build] Release version detected. Pinning dbt-fusion-package-tools to =={version}\n"
41+
)
42+
dependency_idx = metadata["dependencies"].index("dbt-fusion-package-tools")
43+
metadata["dependencies"][dependency_idx] = f"dbt-fusion-package-tools=={version}"
44+
else:
45+
sys.stderr.write(
46+
"[dbt-autofix build] Non-release/fallback version detected. Skipping strict dependency pin.\n"
47+
)

0 commit comments

Comments
 (0)