From 6e113599e3ef80e56990e48fa29bddb953fe3a17 Mon Sep 17 00:00:00 2001 From: Max R Date: Wed, 25 Nov 2026 17:56:59 -0500 Subject: [PATCH 1/5] Add pre-commit try-repo end-to-end test --- .pre-commit-config.yaml | 3 +++ pyproject.toml | 1 + tests/try_repo_test.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 tests/try_repo_test.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8145fe7..9c2da9a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,6 +28,7 @@ repos: - id: ty additional_dependencies: - coverage + - pre-commit - pytest - types-setuptools - typing_extensions; python_version < '3.12' @@ -37,6 +38,7 @@ repos: - id: pyright additional_dependencies: - coverage + - pre-commit - pytest - types-setuptools - typing_extensions; python_version < '3.12' @@ -50,6 +52,7 @@ repos: - id: mypy additional_dependencies: - coverage + - pre-commit - pytest - types-setuptools - typing_extensions; python_version < '3.12' diff --git a/pyproject.toml b/pyproject.toml index 615112b..852edb3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,7 @@ pyright = "pyright_pre_commit.__main__:main" dev = [ "covdefaults>=2.1.0", "coverage", + "pre-commit", "pytest", ] diff --git a/tests/try_repo_test.py b/tests/try_repo_test.py new file mode 100644 index 0000000..7f0315e --- /dev/null +++ b/tests/try_repo_test.py @@ -0,0 +1,31 @@ +from __future__ import annotations + +import os +import subprocess +import sys +from pathlib import Path + +REPO_ROOT = Path(__file__).parent.parent + + +def test_try_repo(tmp_path, subtests): + target = tmp_path / "target" + target.mkdir() + (target / "good.py").write_text("x: int = 1\n") + (target / "bad.py").write_text('x: int = "a"\n') + subprocess.check_call(("git", "init", "-q", str(target))) + subprocess.check_call(("git", "-C", str(target), "add", ".")) + + # one try-repo run for all cases: each run builds a fresh shadow commit, so the hook env is never reused across runs + env = {**os.environ, "PRE_COMMIT_HOME": str(tmp_path / "pre-commit-home")} + proc = subprocess.run((sys.executable, "-m", "pre_commit", "try-repo", str(REPO_ROOT), "pyright", "--all-files"), cwd=target, env=env, capture_output=True, text=True) + output = proc.stdout + proc.stderr + + with subtests.test("exit code"): + assert proc.returncode == 1, output + with subtests.test("bad file reported"): + assert "bad.py:1:10 - error" in output + with subtests.test("good file clean"): + assert "good.py:" not in output + with subtests.test("summary"): + assert "1 error, 0 warnings" in output From 6176aef68c44f6257ff0b4e07f5996c52924708c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:58:40 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/try_repo_test.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/try_repo_test.py b/tests/try_repo_test.py index 7f0315e..6ff4725 100644 --- a/tests/try_repo_test.py +++ b/tests/try_repo_test.py @@ -18,7 +18,21 @@ def test_try_repo(tmp_path, subtests): # one try-repo run for all cases: each run builds a fresh shadow commit, so the hook env is never reused across runs env = {**os.environ, "PRE_COMMIT_HOME": str(tmp_path / "pre-commit-home")} - proc = subprocess.run((sys.executable, "-m", "pre_commit", "try-repo", str(REPO_ROOT), "pyright", "--all-files"), cwd=target, env=env, capture_output=True, text=True) + proc = subprocess.run( + ( + sys.executable, + "-m", + "pre_commit", + "try-repo", + str(REPO_ROOT), + "pyright", + "--all-files", + ), + cwd=target, + env=env, + capture_output=True, + text=True, + ) output = proc.stdout + proc.stderr with subtests.test("exit code"): From b9c892691552ed966a7dc6ef0ac9834ea042eb9a Mon Sep 17 00:00:00 2001 From: Max R Date: Wed, 25 Nov 2026 18:01:35 -0500 Subject: [PATCH 3/5] Explicitly add test files in try-repo test --- tests/try_repo_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/try_repo_test.py b/tests/try_repo_test.py index 6ff4725..e122baa 100644 --- a/tests/try_repo_test.py +++ b/tests/try_repo_test.py @@ -14,7 +14,7 @@ def test_try_repo(tmp_path, subtests): (target / "good.py").write_text("x: int = 1\n") (target / "bad.py").write_text('x: int = "a"\n') subprocess.check_call(("git", "init", "-q", str(target))) - subprocess.check_call(("git", "-C", str(target), "add", ".")) + subprocess.check_call(("git", "-C", str(target), "add", "good.py", "bad.py")) # one try-repo run for all cases: each run builds a fresh shadow commit, so the hook env is never reused across runs env = {**os.environ, "PRE_COMMIT_HOME": str(tmp_path / "pre-commit-home")} @@ -28,6 +28,7 @@ def test_try_repo(tmp_path, subtests): "pyright", "--all-files", ), + check=False, cwd=target, env=env, capture_output=True, From 39b04b2492a3ab2e9e7f1cc8d479707d2f441c27 Mon Sep 17 00:00:00 2001 From: Max R Date: Wed, 25 Nov 2026 18:02:59 -0500 Subject: [PATCH 4/5] Simplify try-repo test --- tests/try_repo_test.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/tests/try_repo_test.py b/tests/try_repo_test.py index e122baa..bfd7e83 100644 --- a/tests/try_repo_test.py +++ b/tests/try_repo_test.py @@ -8,7 +8,7 @@ REPO_ROOT = Path(__file__).parent.parent -def test_try_repo(tmp_path, subtests): +def test_try_repo(tmp_path): target = tmp_path / "target" target.mkdir() (target / "good.py").write_text("x: int = 1\n") @@ -16,8 +16,6 @@ def test_try_repo(tmp_path, subtests): subprocess.check_call(("git", "init", "-q", str(target))) subprocess.check_call(("git", "-C", str(target), "add", "good.py", "bad.py")) - # one try-repo run for all cases: each run builds a fresh shadow commit, so the hook env is never reused across runs - env = {**os.environ, "PRE_COMMIT_HOME": str(tmp_path / "pre-commit-home")} proc = subprocess.run( ( sys.executable, @@ -30,17 +28,13 @@ def test_try_repo(tmp_path, subtests): ), check=False, cwd=target, - env=env, + env={**os.environ, "PRE_COMMIT_HOME": str(tmp_path / "pre-commit-home")}, capture_output=True, text=True, ) output = proc.stdout + proc.stderr - with subtests.test("exit code"): - assert proc.returncode == 1, output - with subtests.test("bad file reported"): - assert "bad.py:1:10 - error" in output - with subtests.test("good file clean"): - assert "good.py:" not in output - with subtests.test("summary"): - assert "1 error, 0 warnings" in output + assert proc.returncode == 1, output + assert "bad.py:1:10 - error" in output + assert "good.py:" not in output + assert "1 error, 0 warnings" in output From 41a5af4dd9f77731ada8ea856712f2976237d74b Mon Sep 17 00:00:00 2001 From: Max R Date: Wed, 25 Nov 2026 18:05:12 -0500 Subject: [PATCH 5/5] Fix try-repo test across Windows drives --- tests/try_repo_test.py | 46 ++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/tests/try_repo_test.py b/tests/try_repo_test.py index bfd7e83..a51b932 100644 --- a/tests/try_repo_test.py +++ b/tests/try_repo_test.py @@ -3,35 +3,37 @@ import os import subprocess import sys +import tempfile from pathlib import Path REPO_ROOT = Path(__file__).parent.parent def test_try_repo(tmp_path): - target = tmp_path / "target" - target.mkdir() - (target / "good.py").write_text("x: int = 1\n") - (target / "bad.py").write_text('x: int = "a"\n') - subprocess.check_call(("git", "init", "-q", str(target))) - subprocess.check_call(("git", "-C", str(target), "add", "good.py", "bad.py")) + # pre-commit relpaths the repo against cwd, which fails across Windows drives + with tempfile.TemporaryDirectory(dir=REPO_ROOT) as d: + target = Path(d) + (target / "good.py").write_text("x: int = 1\n") + (target / "bad.py").write_text('x: int = "a"\n') + subprocess.check_call(("git", "init", "-q", str(target))) + subprocess.check_call(("git", "-C", str(target), "add", "good.py", "bad.py")) - proc = subprocess.run( - ( - sys.executable, - "-m", - "pre_commit", - "try-repo", - str(REPO_ROOT), - "pyright", - "--all-files", - ), - check=False, - cwd=target, - env={**os.environ, "PRE_COMMIT_HOME": str(tmp_path / "pre-commit-home")}, - capture_output=True, - text=True, - ) + proc = subprocess.run( + ( + sys.executable, + "-m", + "pre_commit", + "try-repo", + str(REPO_ROOT), + "pyright", + "--all-files", + ), + check=False, + cwd=target, + env={**os.environ, "PRE_COMMIT_HOME": str(tmp_path / "pre-commit-home")}, + capture_output=True, + text=True, + ) output = proc.stdout + proc.stderr assert proc.returncode == 1, output