Skip to content

Commit

Permalink
fix: 空列表末尾也应该有换行符
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 committed Nov 13, 2024
1 parent 3ade02d commit 7357b7c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/providers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def dump_json5(path: Path, data: Any) -> None:
content = json.dumps(data, ensure_ascii=False, indent=2)
# 手动添加末尾的逗号和换行符
# 避免合并时出现冲突
content = content.replace("}\n]", "},\n]\n")
content = content.replace("}\n]", "},\n]")
content += "\n"

with open(path, "w", encoding="utf-8") as f:
f.write(content)
19 changes: 19 additions & 0 deletions tests/utils/test_json5.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@


async def test_json5_dump(tmp_path: Path) -> None:
"""输出 JSON5
有尾随逗号
"""
from src.providers.utils import dump_json5

data = [
Expand Down Expand Up @@ -34,3 +38,18 @@ async def test_json5_dump(tmp_path: Path) -> None:
]
"""
)


async def test_json5_dump_empty_list(tmp_path: Path) -> None:
"""空列表
末尾也应该有换行
"""
from src.providers.utils import dump_json5

data = []

test_file = tmp_path / "test.json5"
dump_json5(test_file, data)

assert test_file.read_text() == snapshot("[]\n")

0 comments on commit 7357b7c

Please sign in to comment.