Skip to content

Commit 2df9cf1

Browse files
committed
Emit empty document for empty multi streams and add regression tests
1 parent fd10e67 commit 2df9cf1

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ fn format_yaml_impl(value: &Yaml<'static>, multi: bool) -> Result<String> {
10071007
}
10081008
};
10091009
if docs.is_empty() {
1010-
return Ok(String::new());
1010+
return Ok(String::from("---\n"));
10111011
}
10121012
emit_yaml_documents(&docs, true)
10131013
} else {

tests_py/test_format_parse.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ def test_format_yaml_round_trips_multi_document_streams():
104104
assert yaml12.parse_yaml(encoded, multi=True) == docs
105105

106106

107+
def test_format_yaml_multi_empty_sequence_emits_empty_document():
108+
encoded = yaml12.format_yaml([], multi=True)
109+
110+
assert encoded == "---\n...\n"
111+
assert yaml12.parse_yaml(encoded, multi=True) == [None]
112+
113+
107114
def test_format_yaml_rejects_non_sequence_for_multi():
108115
with pytest.raises(TypeError, match="`value` must be a sequence when `multi=True`"):
109116
yaml12.format_yaml({"foo": 1}, multi=True)

tests_py/test_read_write.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ def test_write_and_read_multi_document_streams(tmp_path: Path):
5454
assert yaml12.read_yaml(str(path), multi=True) == docs
5555

5656

57+
def test_write_yaml_multi_empty_sequence_emits_empty_document(tmp_path: Path):
58+
path = tmp_path / "yaml12-empty-multi.yaml"
59+
60+
yaml12.write_yaml([], str(path), multi=True)
61+
62+
assert path.read_text(encoding="utf-8") == "---\n...\n"
63+
assert yaml12.read_yaml(str(path), multi=True) == [None]
64+
65+
5766
def test_write_yaml_flushes_final_newline_for_files(tmp_path: Path):
5867
path = tmp_path / "yaml12-flush.yaml"
5968
value = {"foo": 1}

0 commit comments

Comments
 (0)