Skip to content

Commit 7c988a6

Browse files
ISSOtmzesterer
authored andcommitted
Treat empty input as one line still
Otherwise no labels can be printed over an empty input, not even empty ones! (`get_source_groups` never returns anything if there are no source lines.)
1 parent 603afb2 commit 7c988a6

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

src/source.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ impl<I: AsRef<str>> From<I> for Source<I> {
108108
///
109109
/// Note that this function can be expensive for long strings. Use an implementor of [`Cache`] where possible.
110110
fn from(input: I) -> Self {
111+
// `input.split_inclusive()` will not iterate at all,
112+
// but an empty input still ought to count as a single empty line.
113+
if input.as_ref().is_empty() {
114+
return Self {
115+
text: input,
116+
lines: vec![Line {
117+
offset: 0,
118+
char_len: 0,
119+
byte_offset: 0,
120+
byte_len: 0,
121+
}],
122+
len: 0,
123+
byte_len: 0,
124+
};
125+
}
126+
111127
let mut char_offset = 0;
112128
let mut byte_offset = 0;
113129
let mut lines = Vec::new();
@@ -383,7 +399,7 @@ mod tests {
383399

384400
#[test]
385401
fn source_from_empty() {
386-
test_with_lines(vec![]); // Empty string
402+
test_with_lines(vec![""]); // Empty string
387403
}
388404

389405
#[test]

src/write.rs

+96
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,102 @@ mod tests {
10971097
"###);
10981098
}
10991099

1100+
#[test]
1101+
fn empty_input() {
1102+
let source = "";
1103+
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
1104+
.with_config(no_color_and_ascii())
1105+
.with_message("unexpected end of file")
1106+
.with_label(Label::new(0..0).with_message("No more fruit!"))
1107+
.finish()
1108+
.write_to_string(Source::from(source));
1109+
1110+
assert_snapshot!(msg, @r###"
1111+
Error: unexpected end of file
1112+
,-[<unknown>:1:1]
1113+
|
1114+
1 |
1115+
| |
1116+
| `- No more fruit!
1117+
---'
1118+
"###);
1119+
}
1120+
1121+
#[test]
1122+
fn empty_input_help() {
1123+
let source = "";
1124+
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
1125+
.with_config(no_color_and_ascii())
1126+
.with_message("unexpected end of file")
1127+
.with_label(Label::new(0..0).with_message("No more fruit!"))
1128+
.with_help("have you tried going to the farmer's market?")
1129+
.finish()
1130+
.write_to_string(Source::from(source));
1131+
1132+
assert_snapshot!(msg, @r###"
1133+
Error: unexpected end of file
1134+
,-[<unknown>:1:1]
1135+
|
1136+
1 |
1137+
| |
1138+
| `- No more fruit!
1139+
|
1140+
| Help: have you tried going to the farmer's market?
1141+
---'
1142+
"###);
1143+
}
1144+
1145+
#[test]
1146+
fn empty_input_note() {
1147+
let source = "";
1148+
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
1149+
.with_config(no_color_and_ascii())
1150+
.with_message("unexpected end of file")
1151+
.with_label(Label::new(0..0).with_message("No more fruit!"))
1152+
.with_note("eat your greens!")
1153+
.finish()
1154+
.write_to_string(Source::from(source));
1155+
1156+
assert_snapshot!(msg, @r###"
1157+
Error: unexpected end of file
1158+
,-[<unknown>:1:1]
1159+
|
1160+
1 |
1161+
| |
1162+
| `- No more fruit!
1163+
|
1164+
| Note: eat your greens!
1165+
---'
1166+
"###);
1167+
}
1168+
1169+
#[test]
1170+
fn empty_input_help_note() {
1171+
let source = "";
1172+
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
1173+
.with_config(no_color_and_ascii())
1174+
.with_message("unexpected end of file")
1175+
.with_label(Label::new(0..0).with_message("No more fruit!"))
1176+
.with_note("eat your greens!")
1177+
.with_help("have you tried going to the farmer's market?")
1178+
.finish()
1179+
.write_to_string(Source::from(source));
1180+
1181+
assert_snapshot!(msg, @r###"
1182+
Error: unexpected end of file
1183+
,-[<unknown>:1:1]
1184+
|
1185+
1 |
1186+
| |
1187+
| `- No more fruit!
1188+
|
1189+
| Help: have you tried going to the farmer's market?
1190+
|
1191+
| Note: eat your greens!
1192+
---'
1193+
"###);
1194+
}
1195+
11001196
#[test]
11011197
fn byte_spans_never_crash() {
11021198
let source = "apple\np\n\nempty\n";

0 commit comments

Comments
 (0)