-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
main.rs
289 lines (266 loc) · 8.68 KB
/
main.rs
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
//! Test runner for the semver compatibility doc chapter.
//!
//! This extracts all the "rust" annotated code blocks and tests that they
//! either fail or succeed as expected. This also checks that the examples are
//! formatted correctly.
//!
//! An example with the word "MINOR" at the top is expected to successfully
//! build against the before and after. Otherwise it should fail. A comment of
//! "// Error:" will check that the given message appears in the error output.
use std::error::Error;
use std::fs;
use std::path::Path;
use std::process::{Command, Output};
fn main() {
if let Err(e) = doit() {
println!("error: {}", e);
std::process::exit(1);
}
}
const SEPARATOR: &str = "///////////////////////////////////////////////////////////";
fn doit() -> Result<(), Box<dyn Error>> {
let filename = std::env::args()
.nth(1)
.unwrap_or_else(|| "../src/reference/semver.md".to_string());
let contents = fs::read_to_string(filename)?;
let mut lines = contents.lines().enumerate();
loop {
// Find a rust block.
let (block_start, run_program, deny_warnings) = loop {
match lines.next() {
Some((lineno, line)) => {
if line.trim().starts_with("```rust") && !line.contains("skip") {
break (
lineno + 1,
line.contains("run-fail"),
!line.contains("dont-deny"),
);
}
}
None => return Ok(()),
}
};
// Read in the code block.
let mut block = Vec::new();
loop {
match lines.next() {
Some((_, line)) => {
if line.trim() == "```" {
break;
}
block.push(line);
}
None => {
return Err(format!(
"rust block did not end for example starting on line {}",
block_start
)
.into());
}
}
}
// Split it into the separate source files.
let parts: Vec<_> = block.split(|line| line.trim() == SEPARATOR).collect();
if parts.len() != 4 {
return Err(format!(
"expected 4 sections in example starting on line {}, got {}:\n{:?}",
block_start,
parts.len(),
parts
)
.into());
}
let join = |part: &[&str]| {
let mut result = String::new();
result.push_str("#![allow(unused)]\n");
if deny_warnings {
result.push_str("#![deny(warnings)]\n");
}
result.push_str(&part.join("\n"));
if !result.ends_with('\n') {
result.push('\n');
}
result
};
let expect_success = parts[0][0].contains("MINOR");
println!("Running test from line {}", block_start);
let result = run_test(
join(parts[1]),
join(parts[2]),
join(parts[3]),
expect_success,
run_program,
);
if let Err(e) = result {
return Err(format!(
"test failed for example starting on line {}: {}",
block_start, e
)
.into());
}
}
}
const CRATE_NAME: &str = "updated_crate";
fn run_test(
before: String,
after: String,
example: String,
expect_success: bool,
run_program: bool,
) -> Result<(), Box<dyn Error>> {
let tempdir = tempfile::TempDir::new()?;
let before_p = tempdir.path().join("before.rs");
let after_p = tempdir.path().join("after.rs");
let example_p = tempdir.path().join("example.rs");
let check_fn = if run_program {
run_check
} else {
compile_check
};
compile_check(before, &before_p, CRATE_NAME, false, true)?;
check_fn(example.clone(), &example_p, "example", true, true)?;
compile_check(after, &after_p, CRATE_NAME, false, true)?;
check_fn(example, &example_p, "example", true, expect_success)?;
Ok(())
}
fn check_formatting(path: &Path) -> Result<(), Box<dyn Error>> {
match Command::new("rustfmt")
.args(&["--edition=2018", "--check"])
.arg(path)
.status()
{
Ok(status) => {
if !status.success() {
return Err(format!("failed to run rustfmt: {}", status).into());
}
Ok(())
}
Err(e) => Err(format!("failed to run rustfmt: {}", e).into()),
}
}
fn compile(
contents: &str,
path: &Path,
crate_name: &str,
extern_path: bool,
) -> Result<Output, Box<dyn Error>> {
let crate_type = if contents.contains("fn main()") {
"bin"
} else {
"rlib"
};
fs::write(path, &contents)?;
check_formatting(path)?;
let out_dir = path.parent().unwrap();
let mut cmd = Command::new("rustc");
cmd.args(&[
"--edition=2021",
"--crate-type",
crate_type,
"--crate-name",
crate_name,
"--out-dir",
]);
cmd.arg(&out_dir);
if extern_path {
let epath = out_dir.join(format!("lib{}.rlib", CRATE_NAME));
cmd.arg("--extern")
.arg(format!("{}={}", CRATE_NAME, epath.display()));
}
cmd.arg(path);
cmd.output().map_err(Into::into)
}
fn compile_check(
mut contents: String,
path: &Path,
crate_name: &str,
extern_path: bool,
expect_success: bool,
) -> Result<(), Box<dyn Error>> {
// If the example has an error message, remove it so that it can be
// compared with the actual output, and also to avoid issues with rustfmt
// moving it around.
let expected_error = match contents.find("// Error:") {
Some(index) => {
let start = contents[..index].rfind(|ch| ch != ' ').unwrap();
let end = contents[index..].find('\n').unwrap();
let error = contents[index + 9..index + end].trim().to_string();
contents.replace_range(start + 1..index + end, "");
Some(error)
}
None => None,
};
let output = compile(&contents, path, crate_name, extern_path)?;
let stderr = std::str::from_utf8(&output.stderr).unwrap();
match (output.status.success(), expect_success) {
(true, true) => Ok(()),
(true, false) => Err(format!(
"expected failure, got success {}\n===== Contents:\n{}\n===== Output:\n{}\n",
path.display(),
contents,
stderr
)
.into()),
(false, true) => Err(format!(
"expected success, got error {}\n===== Contents:\n{}\n===== Output:\n{}\n",
path.display(),
contents,
stderr
)
.into()),
(false, false) => {
if expected_error.is_none() {
return Err("failing test should have an \"// Error:\" annotation ".into());
}
let expected_error = expected_error.unwrap();
if !stderr.contains(&expected_error) {
Err(format!(
"expected error message not found in compiler output\nExpected: {}\nGot:\n{}\n",
expected_error, stderr
)
.into())
} else {
Ok(())
}
}
}
}
fn run_check(
contents: String,
path: &Path,
crate_name: &str,
extern_path: bool,
expect_success: bool,
) -> Result<(), Box<dyn Error>> {
let compile_output = compile(&contents, path, crate_name, extern_path)?;
if !compile_output.status.success() {
let stderr = std::str::from_utf8(&compile_output.stderr).unwrap();
return Err(format!(
"expected success, got error {}\n===== Contents:\n{}\n===== Output:\n{}\n",
path.display(),
contents,
stderr
)
.into());
}
let binary_path = path.parent().unwrap().join(crate_name);
let output = Command::new(binary_path).output()?;
let stderr = std::str::from_utf8(&output.stderr).unwrap();
match (output.status.success(), expect_success) {
(true, false) => Err(format!(
"expected panic, got success {}\n===== Contents:\n{}\n===== Output:\n{}\n",
path.display(),
contents,
stderr
)
.into()),
(false, true) => Err(format!(
"expected success, got panic {}\n===== Contents:\n{}\n===== Output:\n{}\n",
path.display(),
contents,
stderr,
)
.into()),
(_, _) => Ok(()),
}
}