Skip to content

Commit d7a5418

Browse files
Implement a get_minimum_pre_commit_version function
1 parent 7b53c75 commit d7a5418

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from usethis._config import usethis_config
2+
from usethis._integrations.pre_commit.io_ import read_pre_commit_config_yaml
3+
4+
5+
def get_minimum_pre_commit_version() -> str | None:
6+
"""Get the declared minimum supported pre-commit version from the configuration.
7+
8+
This is in a dot-separated digit format e.g. `3.6.2`.
9+
10+
Returns `None` if no minimum version is explicitly declared.
11+
"""
12+
path = usethis_config.cpd() / ".pre-commit-config.yaml"
13+
14+
if not path.exists():
15+
return None
16+
17+
with read_pre_commit_config_yaml() as doc:
18+
return doc.model.minimum_pre_commit_version
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from pathlib import Path
2+
3+
from usethis._integrations.pre_commit.version import get_minimum_pre_commit_version
4+
from usethis._test import change_cwd
5+
6+
7+
class TestGetMinimumPreCommitVersion:
8+
def test_happy_path(self, tmp_path: Path):
9+
# Arrange
10+
(tmp_path / ".pre-commit-config.yaml").write_text("""\
11+
minimum_pre_commit_version: '4.4.0'
12+
repos:
13+
- repo: https://github.com/abravalheri/validate-pyproject
14+
rev: v0.23
15+
hooks:
16+
- id: validate-pyproject
17+
""")
18+
19+
# Act
20+
with change_cwd(tmp_path):
21+
result = get_minimum_pre_commit_version()
22+
23+
# Assert
24+
assert result == "4.4.0"
25+
26+
def test_config_doesnt_exist(self, tmp_path: Path):
27+
# Act
28+
with change_cwd(tmp_path):
29+
result = get_minimum_pre_commit_version()
30+
31+
# Assert
32+
assert result is None
33+
34+
def test_minimum_version_not_declared(self, tmp_path: Path):
35+
# Arrange
36+
(tmp_path / ".pre-commit-config.yaml").write_text("""\
37+
repos:
38+
- repo: https://github.com/abravalheri/validate-pyproject
39+
rev: v0.23
40+
hooks:
41+
- id: validate-pyproject
42+
""")
43+
44+
# Act
45+
with change_cwd(tmp_path):
46+
result = get_minimum_pre_commit_version()
47+
48+
# Assert
49+
assert result is None

0 commit comments

Comments
 (0)