
Previously compiletest's `runtest.rs` was a massive 4700 lines file that made reading and navigation very awkward. This commit intentionally does not neatly reorganize where all the methods on `TestCx` goes, that is intended for a follow-up PR.
104 lines
3.5 KiB
Rust
104 lines
3.5 KiB
Rust
use std::fs;
|
|
|
|
use super::{ProcRes, ReadFrom, TestCx};
|
|
use crate::util::logv;
|
|
|
|
impl TestCx<'_> {
|
|
pub(super) fn run_pretty_test(&self) {
|
|
if self.props.pp_exact.is_some() {
|
|
logv(self.config, "testing for exact pretty-printing".to_owned());
|
|
} else {
|
|
logv(self.config, "testing for converging pretty-printing".to_owned());
|
|
}
|
|
|
|
let rounds = match self.props.pp_exact {
|
|
Some(_) => 1,
|
|
None => 2,
|
|
};
|
|
|
|
let src = fs::read_to_string(&self.testpaths.file).unwrap();
|
|
let mut srcs = vec![src];
|
|
|
|
let mut round = 0;
|
|
while round < rounds {
|
|
logv(
|
|
self.config,
|
|
format!("pretty-printing round {} revision {:?}", round, self.revision),
|
|
);
|
|
let read_from =
|
|
if round == 0 { ReadFrom::Path } else { ReadFrom::Stdin(srcs[round].to_owned()) };
|
|
|
|
let proc_res = self.print_source(read_from, &self.props.pretty_mode);
|
|
if !proc_res.status.success() {
|
|
self.fatal_proc_rec(
|
|
&format!(
|
|
"pretty-printing failed in round {} revision {:?}",
|
|
round, self.revision
|
|
),
|
|
&proc_res,
|
|
);
|
|
}
|
|
|
|
let ProcRes { stdout, .. } = proc_res;
|
|
srcs.push(stdout);
|
|
round += 1;
|
|
}
|
|
|
|
let mut expected = match self.props.pp_exact {
|
|
Some(ref file) => {
|
|
let filepath = self.testpaths.file.parent().unwrap().join(file);
|
|
fs::read_to_string(&filepath).unwrap()
|
|
}
|
|
None => srcs[srcs.len() - 2].clone(),
|
|
};
|
|
let mut actual = srcs[srcs.len() - 1].clone();
|
|
|
|
if self.props.pp_exact.is_some() {
|
|
// Now we have to care about line endings
|
|
let cr = "\r".to_owned();
|
|
actual = actual.replace(&cr, "");
|
|
expected = expected.replace(&cr, "");
|
|
}
|
|
|
|
if !self.config.bless {
|
|
self.compare_source(&expected, &actual);
|
|
} else if expected != actual {
|
|
let filepath_buf;
|
|
let filepath = match &self.props.pp_exact {
|
|
Some(file) => {
|
|
filepath_buf = self.testpaths.file.parent().unwrap().join(file);
|
|
&filepath_buf
|
|
}
|
|
None => &self.testpaths.file,
|
|
};
|
|
fs::write(filepath, &actual).unwrap();
|
|
}
|
|
|
|
// If we're only making sure that the output matches then just stop here
|
|
if self.props.pretty_compare_only {
|
|
return;
|
|
}
|
|
|
|
// Finally, let's make sure it actually appears to remain valid code
|
|
let proc_res = self.typecheck_source(actual);
|
|
if !proc_res.status.success() {
|
|
self.fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
|
|
}
|
|
|
|
if !self.props.pretty_expanded {
|
|
return;
|
|
}
|
|
|
|
// additionally, run `-Zunpretty=expanded` and try to build it.
|
|
let proc_res = self.print_source(ReadFrom::Path, "expanded");
|
|
if !proc_res.status.success() {
|
|
self.fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
|
|
}
|
|
|
|
let ProcRes { stdout: expanded_src, .. } = proc_res;
|
|
let proc_res = self.typecheck_source(expanded_src);
|
|
if !proc_res.status.success() {
|
|
self.fatal_proc_rec("pretty-printed source (expanded) does not typecheck", &proc_res);
|
|
}
|
|
}
|
|
}
|