compiletest/runtest/
pretty.rs

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