Skip to content

Commit 73770d2

Browse files
Support empty .pre-commit-config.yaml files (#906)
* Support empty `.pre-commit-config.yaml files` * Don't modify empty files if unchanged
1 parent f8b9614 commit 73770d2

3 files changed

Lines changed: 27 additions & 12 deletions

File tree

src/usethis/_integrations/file/yaml/io_.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,12 @@ def edit_yaml(
474474
except YAMLError as err:
475475
msg = f"Error reading '{yaml_path}':\n{err}"
476476
raise YAMLDecodeError(msg) from None
477+
478+
start_empty = not yaml_document.content
477479
yield yaml_document
480+
if start_empty and not yaml_document.content:
481+
# No change
482+
return
478483
yaml_document.roundtripper.dump(yaml_document.content, stream=yaml_path)
479484

480485

src/usethis/_integrations/pre_commit/io_.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import TYPE_CHECKING
66

77
from pydantic import ValidationError
8+
from ruamel.yaml.comments import CommentedMap
89

910
from usethis._config import usethis_config
1011
from usethis._console import tick_print
@@ -15,8 +16,6 @@
1516
if TYPE_CHECKING:
1617
from collections.abc import Generator
1718

18-
from ruamel.yaml.comments import CommentedMap
19-
2019
from usethis._integrations.file.yaml.io_ import YAMLLiteral
2120

2221

@@ -62,6 +61,9 @@ def edit_pre_commit_config_yaml() -> Generator[PreCommitConfigYAMLDocument, None
6261

6362

6463
def _validate_config(ruamel_content: YAMLLiteral) -> JsonSchemaForPreCommitConfigYaml:
64+
if isinstance(ruamel_content, CommentedMap) and not ruamel_content:
65+
ruamel_content = CommentedMap({"repos": []})
66+
6567
try:
6668
return JsonSchemaForPreCommitConfigYaml.model_validate(ruamel_content)
6769
except ValidationError as err:

tests/usethis/_integrations/pre_commit/test_pre_commit_io_.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
from pathlib import Path
22

3-
import pytest
4-
5-
from usethis._integrations.pre_commit.io_ import (
6-
PreCommitConfigYAMLConfigError,
7-
edit_pre_commit_config_yaml,
8-
)
3+
from usethis._integrations.pre_commit.io_ import edit_pre_commit_config_yaml
94
from usethis._test import change_cwd
105

116

@@ -35,14 +30,27 @@ def test_unchanged(self, tmp_path: Path):
3530
# Assert
3631
assert (tmp_path / ".pre-commit-config.yaml").read_text() == content_str
3732

38-
def test_empty_is_invalid(self, tmp_path: Path):
33+
def test_start_with_empty_file(self, tmp_path: Path):
3934
# Arrange
4035
(tmp_path / ".pre-commit-config.yaml").write_text("")
4136

42-
# Act, Assert
37+
# Act
4338
with (
4439
change_cwd(tmp_path),
45-
pytest.raises(PreCommitConfigYAMLConfigError),
46-
edit_pre_commit_config_yaml(),
40+
edit_pre_commit_config_yaml() as doc,
4741
):
42+
doc.content["repos"] = []
43+
44+
# Assert
45+
assert (tmp_path / ".pre-commit-config.yaml").read_text() == "repos: []\n"
46+
47+
def test_empty_valid_but_unchanged(self, tmp_path: Path):
48+
# Arrange
49+
(tmp_path / ".pre-commit-config.yaml").write_text("")
50+
51+
# Act
52+
with change_cwd(tmp_path), edit_pre_commit_config_yaml():
4853
pass
54+
55+
# Assert
56+
assert (tmp_path / ".pre-commit-config.yaml").read_text() == ""

0 commit comments

Comments
 (0)