Skip to content

Commit 8ecd22b

Browse files
authored
Use current project config for uv (#79)
1 parent b64db63 commit 8ecd22b

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

src/pre_commit_uv/__init__.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,40 @@ def _install_environment(
5959
) -> None:
6060
import logging # noqa: PLC0415
6161

62+
from pre_commit.git import get_root # noqa: PLC0415
6263
from pre_commit.lang_base import environment_dir, setup_cmd # noqa: PLC0415
6364
from pre_commit.util import cmd_output_b # noqa: PLC0415
6465

66+
project_root_dir = get_root()
67+
6568
logger = logging.getLogger("pre_commit")
6669
logger.info("Using pre-commit with uv %s via pre-commit-uv %s", uv_version(), self_version())
6770
uv = _uv()
6871
py = python.norm_version(version) or os.environ.get("UV_PYTHON", sys.executable)
69-
venv_cmd = [uv, "venv", environment_dir(prefix, python.ENVIRONMENT_DIR, version), "-p", py]
72+
venv_cmd = [
73+
uv,
74+
"--project",
75+
project_root_dir,
76+
"venv",
77+
environment_dir(prefix, python.ENVIRONMENT_DIR, version),
78+
"-p",
79+
py,
80+
]
7081
cmd_output_b(*venv_cmd, cwd="/")
7182

7283
with python.in_env(prefix, version):
73-
setup_cmd(prefix, (uv, "pip", "install", ".", *additional_dependencies))
84+
setup_cmd(
85+
prefix,
86+
(
87+
uv,
88+
"--project",
89+
project_root_dir,
90+
"pip",
91+
"install",
92+
".",
93+
*additional_dependencies,
94+
),
95+
)
7496

7597
@cache
7698
def _uv() -> str:

tests/test_main.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,97 @@ def test_install(git_repo: Path, caplog: pytest.LogCaptureFixture, monkeypatch:
6666
"This may take a few minutes...",
6767
f"Using pre-commit with uv {uv} via pre-commit-uv {self}",
6868
]
69+
70+
71+
test_install_with_uv_config_cases: list[tuple[str, str]] = [
72+
(
73+
"pyproject.toml",
74+
"""
75+
[[tool.uv.index]]
76+
name = "internal"
77+
url = "https://pypi.org/simple/"
78+
default = true
79+
""",
80+
),
81+
(
82+
"uv.toml",
83+
"""
84+
[[index]]
85+
name = "internal"
86+
url = "https://pypi.org/simple/"
87+
default = true
88+
""",
89+
),
90+
]
91+
92+
93+
@pytest.mark.parametrize(
94+
("file_name", "content"),
95+
test_install_with_uv_config_cases,
96+
)
97+
def test_install_with_uv_config(
98+
git_repo: Path,
99+
caplog: pytest.LogCaptureFixture,
100+
monkeypatch: pytest.MonkeyPatch,
101+
file_name: str,
102+
content: str,
103+
) -> None:
104+
(git_repo / file_name).write_text(dedent(content))
105+
106+
monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")
107+
108+
import pre_commit_uv # noqa: PLC0415
109+
110+
pre_commit_uv._patch() # noqa: SLF001
111+
main.main(["install-hooks", "-c", str(git_repo / precommit_file)])
112+
113+
assert caplog.messages == [
114+
"Initializing environment for https://github.com/tox-dev/pyproject-fmt.",
115+
"Installing environment for https://github.com/tox-dev/pyproject-fmt.",
116+
"Once installed this environment will be reused.",
117+
"This may take a few minutes...",
118+
f"Using pre-commit with uv {uv} via pre-commit-uv {self}",
119+
]
120+
121+
122+
test_install_with_uv_config_raises_error_cases: list[tuple[str, str]] = [
123+
(
124+
"pyproject.toml",
125+
"""
126+
[[tool.uv.index]]
127+
name = "internal"
128+
url = "https://pypi.example/simple/"
129+
default = true
130+
""",
131+
),
132+
(
133+
"uv.toml",
134+
"""
135+
[[index]]
136+
name = "internal"
137+
url = "https://pypi.example/simple/"
138+
default = true
139+
""",
140+
),
141+
]
142+
143+
144+
@pytest.mark.parametrize(("file_name", "content"), test_install_with_uv_config_raises_error_cases)
145+
def test_install_with_uv_config_raises_error(
146+
git_repo: Path,
147+
monkeypatch: pytest.MonkeyPatch,
148+
file_name: str,
149+
content: str,
150+
) -> None:
151+
"""Test to make sure that uv config is used for non default pypi repos."""
152+
(git_repo / file_name).write_text(dedent(content))
153+
154+
monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")
155+
156+
import pre_commit_uv # noqa: PLC0415
157+
158+
pre_commit_uv._patch() # noqa: SLF001
159+
160+
# would raise SystemExit due to bad config
161+
with pytest.raises(SystemExit):
162+
main.main(["install-hooks", "-c", str(git_repo / precommit_file)])

0 commit comments

Comments
 (0)