1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
use anyhow::{Context, Result, bail};
use indexmap::IndexMap;
use std::fs;
use std::path::Path;
const VALID_SECTIONS: &[&str] = &[
"TEST",
"CONDITION",
"COMPOSER",
"LOCK",
"INSTALLED",
"RUN",
"EXPECT-LOCK",
"EXPECT-INSTALLED",
"EXPECT-OUTPUT",
"EXPECT-OUTPUT-OPTIMIZED",
"EXPECT-EXIT-CODE",
"EXPECT-EXCEPTION",
"EXPECT",
];
const REQUIRED_SECTIONS: &[&str] = &["TEST", "COMPOSER", "RUN", "EXPECT"];
#[derive(Debug, Clone)]
pub struct ParsedTest {
pub test: String,
pub condition: Option<String>,
pub composer: String,
pub lock: Option<String>,
pub installed: Option<String>,
pub run: String,
pub expect_lock: Option<String>,
pub expect_installed: Option<String>,
pub expect_output: Option<String>,
pub expect_output_optimized: Option<String>,
pub expect_exit_code: Option<i32>,
pub expect_exception: Option<String>,
pub expect: String,
}
pub fn parse_test_file(path: &Path) -> Result<ParsedTest> {
let content =
fs::read_to_string(path).with_context(|| format!("failed to read {}", path.display()))?;
parse_test_str(&content).with_context(|| format!("failed to parse {}", path.display()))
}
pub fn parse_test_str(content: &str) -> Result<ParsedTest> {
let mut sections = split_sections(content, VALID_SECTIONS)?;
for required in REQUIRED_SECTIONS {
if !sections.contains_key(*required) {
bail!("missing required section: --{required}--");
}
}
let mut take = |key: &str| sections.shift_remove(key);
let test = take("TEST").unwrap();
let composer = take("COMPOSER").unwrap();
let run = take("RUN").unwrap();
let expect = take("EXPECT").unwrap();
let expect_exit_code = match take("EXPECT-EXIT-CODE") {
Some(s) => Some(
s.trim()
.parse::<i32>()
.with_context(|| format!("invalid EXPECT-EXIT-CODE: {s:?}"))?,
),
None => None,
};
Ok(ParsedTest {
test,
condition: take("CONDITION"),
composer,
lock: take("LOCK"),
installed: take("INSTALLED"),
run,
expect_lock: take("EXPECT-LOCK"),
expect_installed: take("EXPECT-INSTALLED"),
expect_output: take("EXPECT-OUTPUT"),
expect_output_optimized: take("EXPECT-OUTPUT-OPTIMIZED"),
expect_exit_code,
expect_exception: take("EXPECT-EXCEPTION"),
expect,
})
}
/// Split a `.test` fixture into its `--SECTION--` blocks.
///
/// Shared helper for both [`parse_test_str`] and the sibling pool-builder
/// parser; each caller passes its own allowed-section list so unknown
/// headers still surface as parse errors rather than silently ignored.
pub(crate) fn split_sections(
content: &str,
valid_sections: &[&str],
) -> Result<IndexMap<String, String>> {
let header_re = regex::Regex::new(r"^--([A-Z][A-Z-]*)--$").unwrap();
let mut sections: IndexMap<String, String> = IndexMap::new();
let mut current_section: Option<String> = None;
let mut current_body = String::new();
for line in content.split_inclusive('\n') {
let trimmed = line.trim_end_matches('\n').trim_end_matches('\r');
if let Some(caps) = header_re.captures(trimmed) {
let name = caps[1].to_string();
if !valid_sections.contains(&name.as_str()) {
bail!("unknown section: --{name}--");
}
if let Some(prev) = current_section.take() {
let body = trim_trailing_newlines(¤t_body).to_string();
if sections.insert(prev.clone(), body).is_some() {
bail!("duplicate section: --{prev}--");
}
current_body.clear();
}
current_section = Some(name);
} else if current_section.is_some() {
current_body.push_str(line);
}
}
if let Some(name) = current_section.take() {
let body = trim_trailing_newlines(¤t_body).to_string();
if sections.insert(name.clone(), body).is_some() {
bail!("duplicate section: --{name}--");
}
}
Ok(sections)
}
fn trim_trailing_newlines(s: &str) -> &str {
s.trim_end_matches(['\n', '\r'])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_minimal_required_sections() {
let input = "\
--TEST--
A simple test
--COMPOSER--
{\"require\": {\"a/a\": \"1.0.0\"}}
--RUN--
install
--EXPECT--
Installing a/a (1.0.0)
";
let t = parse_test_str(input).unwrap();
assert_eq!(t.test, "A simple test");
assert_eq!(t.composer, "{\"require\": {\"a/a\": \"1.0.0\"}}");
assert_eq!(t.run, "install");
assert_eq!(t.expect, "Installing a/a (1.0.0)");
assert!(t.lock.is_none());
assert!(t.installed.is_none());
assert!(t.expect_output.is_none());
assert!(t.expect_exit_code.is_none());
}
#[test]
fn parses_all_sections() {
let input = "\
--TEST--
desc
--CONDITION--
true
--COMPOSER--
{}
--LOCK--
{\"packages\": []}
--INSTALLED--
[]
--RUN--
update --with-dependencies a/a
--EXPECT-LOCK--
{\"packages\": []}
--EXPECT-INSTALLED--
[]
--EXPECT-OUTPUT--
some output
--EXPECT-OUTPUT-OPTIMIZED--
optimized output
--EXPECT-EXIT-CODE--
2
--EXPECT-EXCEPTION--
SomeException
--EXPECT--
op log
";
let t = parse_test_str(input).unwrap();
assert_eq!(t.test, "desc");
assert_eq!(t.condition.as_deref(), Some("true"));
assert_eq!(t.composer, "{}");
assert_eq!(t.lock.as_deref(), Some("{\"packages\": []}"));
assert_eq!(t.installed.as_deref(), Some("[]"));
assert_eq!(t.run, "update --with-dependencies a/a");
assert_eq!(t.expect_lock.as_deref(), Some("{\"packages\": []}"));
assert_eq!(t.expect_installed.as_deref(), Some("[]"));
assert_eq!(t.expect_output.as_deref(), Some("some output"));
assert_eq!(
t.expect_output_optimized.as_deref(),
Some("optimized output")
);
assert_eq!(t.expect_exit_code, Some(2));
assert_eq!(t.expect_exception.as_deref(), Some("SomeException"));
assert_eq!(t.expect, "op log");
}
#[test]
fn preserves_internal_newlines_in_body() {
let input = "\
--TEST--
multi
--COMPOSER--
{
\"name\": \"a/a\"
}
--RUN--
install
--EXPECT--
line1
line2
line3
";
let t = parse_test_str(input).unwrap();
assert_eq!(t.composer, "{\n \"name\": \"a/a\"\n}");
assert_eq!(t.expect, "line1\nline2\nline3");
}
#[test]
fn rejects_unknown_section() {
let input = "\
--TEST--
x
--MYSTERY--
y
--COMPOSER--
{}
--RUN--
install
--EXPECT--
z
";
let err = parse_test_str(input).unwrap_err();
assert!(err.to_string().contains("unknown section"), "{err}");
}
#[test]
fn rejects_missing_required_section() {
let input = "\
--TEST--
x
--COMPOSER--
{}
--EXPECT--
z
";
let err = parse_test_str(input).unwrap_err();
assert!(err.to_string().contains("RUN"), "{err}");
}
#[test]
fn rejects_duplicate_section() {
let input = "\
--TEST--
first
--COMPOSER--
{}
--RUN--
install
--TEST--
second
--EXPECT--
z
";
let err = parse_test_str(input).unwrap_err();
assert!(err.to_string().contains("duplicate"), "{err}");
}
#[test]
fn rejects_invalid_exit_code() {
let input = "\
--TEST--
x
--COMPOSER--
{}
--RUN--
install
--EXPECT-EXIT-CODE--
not-a-number
--EXPECT--
z
";
let err = parse_test_str(input).unwrap_err();
assert!(err.to_string().contains("EXPECT-EXIT-CODE"), "{err}");
}
#[test]
fn skips_text_before_first_section() {
let input = "\
this is a header comment
that should be ignored
--TEST--
x
--COMPOSER--
{}
--RUN--
install
--EXPECT--
z
";
let t = parse_test_str(input).unwrap();
assert_eq!(t.test, "x");
}
#[test]
fn handles_crlf_line_endings() {
let input =
"--TEST--\r\nx\r\n--COMPOSER--\r\n{}\r\n--RUN--\r\ninstall\r\n--EXPECT--\r\nz\r\n";
let t = parse_test_str(input).unwrap();
assert_eq!(t.test, "x");
assert_eq!(t.composer, "{}");
assert_eq!(t.expect, "z");
}
}
|