compiletest/runtest/
pretty.rs1use std::fs;
2
3use super::{ProcRes, ReadFrom, TestCx};
4
5impl TestCx<'_> {
6 pub(super) fn run_pretty_test(&self) {
7 if self.props.pp_exact.is_some() {
8 self.logv("testing for exact pretty-printing");
9 } else {
10 self.logv("testing for converging pretty-printing");
11 }
12
13 let rounds = match self.props.pp_exact {
14 Some(_) => 1,
15 None => 2,
16 };
17
18 let src = fs::read_to_string(&self.testpaths.file).unwrap();
19 let mut srcs = vec![src];
20
21 let mut round = 0;
22 while round < rounds {
23 self.logv(format_args!("pretty-printing round {round} revision {:?}", self.revision));
24 let read_from =
25 if round == 0 { ReadFrom::Path } else { ReadFrom::Stdin(srcs[round].to_owned()) };
26
27 let proc_res = self.print_source(read_from, &self.props.pretty_mode);
28 if !proc_res.status.success() {
29 self.fatal_proc_rec(
30 &format!(
31 "pretty-printing failed in round {} revision {:?}",
32 round, self.revision
33 ),
34 &proc_res,
35 );
36 }
37
38 let ProcRes { stdout, .. } = proc_res;
39 srcs.push(stdout);
40 round += 1;
41 }
42
43 let mut expected = match self.props.pp_exact {
44 Some(ref file) => {
45 let filepath = self.testpaths.file.parent().unwrap().join(file);
46 fs::read_to_string(&filepath).unwrap()
47 }
48 None => srcs[srcs.len() - 2].clone(),
49 };
50 let mut actual = srcs[srcs.len() - 1].clone();
51
52 if self.props.pp_exact.is_some() {
53 let cr = "\r".to_owned();
55 actual = actual.replace(&cr, "");
56 expected = expected.replace(&cr, "");
57 }
58
59 if !self.config.bless {
60 self.compare_source(&expected, &actual);
61 } else if expected != actual {
62 let filepath_buf;
63 let filepath = match &self.props.pp_exact {
64 Some(file) => {
65 filepath_buf = self.testpaths.file.parent().unwrap().join(file);
66 &filepath_buf
67 }
68 None => &self.testpaths.file,
69 };
70 fs::write(filepath, &actual).unwrap();
71 }
72
73 if self.props.pretty_compare_only {
75 return;
76 }
77
78 let proc_res = self.typecheck_source(actual);
80 if !proc_res.status.success() {
81 self.fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
82 }
83 }
84}