]> git.lizzy.rs Git - rust.git/blob - src/tools/rustfmt/src/test/configuration_snippet.rs
Link to the LLVM issue from a comment on `SpecOptionPartialEq`
[rust.git] / src / tools / rustfmt / src / test / configuration_snippet.rs
1 use std::collections::{HashMap, HashSet};
2 use std::fs;
3 use std::io::{BufRead, BufReader, Write};
4 use std::iter::Enumerate;
5 use std::path::{Path, PathBuf};
6
7 use super::{print_mismatches, write_message, DIFF_CONTEXT_SIZE};
8 use crate::config::{Config, EmitMode, Verbosity};
9 use crate::rustfmt_diff::{make_diff, Mismatch};
10 use crate::{Input, Session};
11
12 const CONFIGURATIONS_FILE_NAME: &str = "Configurations.md";
13
14 // This enum is used to represent one of three text features in Configurations.md: a block of code
15 // with its starting line number, the name of a rustfmt configuration option, or the value of a
16 // rustfmt configuration option.
17 enum ConfigurationSection {
18     CodeBlock((String, u32)), // (String: block of code, u32: line number of code block start)
19     ConfigName(String),
20     ConfigValue(String),
21 }
22
23 impl ConfigurationSection {
24     fn get_section<I: Iterator<Item = String>>(
25         file: &mut Enumerate<I>,
26     ) -> Option<ConfigurationSection> {
27         lazy_static! {
28             static ref CONFIG_NAME_REGEX: regex::Regex =
29                 regex::Regex::new(r"^## `([^`]+)`").expect("failed creating configuration pattern");
30             static ref CONFIG_VALUE_REGEX: regex::Regex =
31                 regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#)
32                     .expect("failed creating configuration value pattern");
33         }
34
35         loop {
36             match file.next() {
37                 Some((i, line)) => {
38                     if line.starts_with("```rust") {
39                         // Get the lines of the code block.
40                         let lines: Vec<String> = file
41                             .map(|(_i, l)| l)
42                             .take_while(|l| !l.starts_with("```"))
43                             .collect();
44                         let block = format!("{}\n", lines.join("\n"));
45
46                         // +1 to translate to one-based indexing
47                         // +1 to get to first line of code (line after "```")
48                         let start_line = (i + 2) as u32;
49
50                         return Some(ConfigurationSection::CodeBlock((block, start_line)));
51                     } else if let Some(c) = CONFIG_NAME_REGEX.captures(&line) {
52                         return Some(ConfigurationSection::ConfigName(String::from(&c[1])));
53                     } else if let Some(c) = CONFIG_VALUE_REGEX.captures(&line) {
54                         return Some(ConfigurationSection::ConfigValue(String::from(&c[1])));
55                     }
56                 }
57                 None => return None, // reached the end of the file
58             }
59         }
60     }
61 }
62
63 // This struct stores the information about code blocks in the configurations
64 // file, formats the code blocks, and prints formatting errors.
65 struct ConfigCodeBlock {
66     config_name: Option<String>,
67     config_value: Option<String>,
68     code_block: Option<String>,
69     code_block_start: Option<u32>,
70 }
71
72 impl ConfigCodeBlock {
73     fn new() -> ConfigCodeBlock {
74         ConfigCodeBlock {
75             config_name: None,
76             config_value: None,
77             code_block: None,
78             code_block_start: None,
79         }
80     }
81
82     fn set_config_name(&mut self, name: Option<String>) {
83         self.config_name = name;
84         self.config_value = None;
85     }
86
87     fn set_config_value(&mut self, value: Option<String>) {
88         self.config_value = value;
89     }
90
91     fn set_code_block(&mut self, code_block: String, code_block_start: u32) {
92         self.code_block = Some(code_block);
93         self.code_block_start = Some(code_block_start);
94     }
95
96     fn get_block_config(&self) -> Config {
97         let mut config = Config::default();
98         config.set().verbose(Verbosity::Quiet);
99         if self.config_name.is_some() && self.config_value.is_some() {
100             config.override_value(
101                 self.config_name.as_ref().unwrap(),
102                 self.config_value.as_ref().unwrap(),
103             );
104         }
105         config
106     }
107
108     fn code_block_valid(&self) -> bool {
109         // We never expect to not have a code block.
110         assert!(self.code_block.is_some() && self.code_block_start.is_some());
111
112         // See if code block begins with #![rustfmt::skip].
113         let fmt_skip = self.fmt_skip();
114
115         if self.config_name.is_none() && !fmt_skip {
116             write_message(&format!(
117                 "No configuration name for {}:{}",
118                 CONFIGURATIONS_FILE_NAME,
119                 self.code_block_start.unwrap()
120             ));
121             return false;
122         }
123         if self.config_value.is_none() && !fmt_skip {
124             write_message(&format!(
125                 "No configuration value for {}:{}",
126                 CONFIGURATIONS_FILE_NAME,
127                 self.code_block_start.unwrap()
128             ));
129             return false;
130         }
131         true
132     }
133
134     /// True if the code block starts with #![rustfmt::skip]
135     fn fmt_skip(&self) -> bool {
136         self.code_block
137             .as_ref()
138             .unwrap()
139             .lines()
140             .nth(0)
141             .unwrap_or("")
142             == "#![rustfmt::skip]"
143     }
144
145     fn has_parsing_errors<T: Write>(&self, session: &Session<'_, T>) -> bool {
146         if session.has_parsing_errors() {
147             write_message(&format!(
148                 "\u{261d}\u{1f3fd} Cannot format {}:{}",
149                 CONFIGURATIONS_FILE_NAME,
150                 self.code_block_start.unwrap()
151             ));
152             return true;
153         }
154
155         false
156     }
157
158     fn print_diff(&self, compare: Vec<Mismatch>) {
159         let mut mismatches = HashMap::new();
160         mismatches.insert(PathBuf::from(CONFIGURATIONS_FILE_NAME), compare);
161         print_mismatches(mismatches, |line_num| {
162             format!(
163                 "\nMismatch at {}:{}:",
164                 CONFIGURATIONS_FILE_NAME,
165                 line_num + self.code_block_start.unwrap() - 1
166             )
167         });
168     }
169
170     fn formatted_has_diff(&self, text: &str) -> bool {
171         let compare = make_diff(self.code_block.as_ref().unwrap(), text, DIFF_CONTEXT_SIZE);
172         if !compare.is_empty() {
173             self.print_diff(compare);
174             return true;
175         }
176
177         false
178     }
179
180     // Return a bool indicating if formatting this code block is an idempotent
181     // operation. This function also triggers printing any formatting failure
182     // messages.
183     fn formatted_is_idempotent(&self) -> bool {
184         // Verify that we have all of the expected information.
185         if !self.code_block_valid() {
186             return false;
187         }
188
189         let input = Input::Text(self.code_block.as_ref().unwrap().to_owned());
190         let mut config = self.get_block_config();
191         config.set().emit_mode(EmitMode::Stdout);
192         let mut buf: Vec<u8> = vec![];
193
194         {
195             let mut session = Session::new(config, Some(&mut buf));
196             session.format(input).unwrap();
197             if self.has_parsing_errors(&session) {
198                 return false;
199             }
200         }
201
202         !self.formatted_has_diff(&String::from_utf8(buf).unwrap())
203     }
204
205     // Extract a code block from the iterator. Behavior:
206     // - Rust code blocks are identifed by lines beginning with "```rust".
207     // - One explicit configuration setting is supported per code block.
208     // - Rust code blocks with no configuration setting are illegal and cause an
209     //   assertion failure, unless the snippet begins with #![rustfmt::skip].
210     // - Configuration names in Configurations.md must be in the form of
211     //   "## `NAME`".
212     // - Configuration values in Configurations.md must be in the form of
213     //   "#### `VALUE`".
214     fn extract<I: Iterator<Item = String>>(
215         file: &mut Enumerate<I>,
216         prev: Option<&ConfigCodeBlock>,
217         hash_set: &mut HashSet<String>,
218     ) -> Option<ConfigCodeBlock> {
219         let mut code_block = ConfigCodeBlock::new();
220         code_block.config_name = prev.and_then(|cb| cb.config_name.clone());
221
222         loop {
223             match ConfigurationSection::get_section(file) {
224                 Some(ConfigurationSection::CodeBlock((block, start_line))) => {
225                     code_block.set_code_block(block, start_line);
226                     break;
227                 }
228                 Some(ConfigurationSection::ConfigName(name)) => {
229                     assert!(
230                         Config::is_valid_name(&name),
231                         "an unknown configuration option was found: {}",
232                         name
233                     );
234                     assert!(
235                         hash_set.remove(&name),
236                         "multiple configuration guides found for option {}",
237                         name
238                     );
239                     code_block.set_config_name(Some(name));
240                 }
241                 Some(ConfigurationSection::ConfigValue(value)) => {
242                     code_block.set_config_value(Some(value));
243                 }
244                 None => return None, // end of file was reached
245             }
246         }
247
248         Some(code_block)
249     }
250 }
251
252 #[test]
253 fn configuration_snippet_tests() {
254     super::init_log();
255     let blocks = get_code_blocks();
256     let failures = blocks
257         .iter()
258         .filter(|block| !block.fmt_skip())
259         .map(ConfigCodeBlock::formatted_is_idempotent)
260         .fold(0, |acc, r| acc + (!r as u32));
261
262     // Display results.
263     println!("Ran {} configurations tests.", blocks.len());
264     assert_eq!(failures, 0, "{} configurations tests failed", failures);
265 }
266
267 // Read Configurations.md and build a `Vec` of `ConfigCodeBlock` structs with one
268 // entry for each Rust code block found.
269 fn get_code_blocks() -> Vec<ConfigCodeBlock> {
270     let mut file_iter = BufReader::new(
271         fs::File::open(Path::new(CONFIGURATIONS_FILE_NAME))
272             .unwrap_or_else(|_| panic!("couldn't read file {}", CONFIGURATIONS_FILE_NAME)),
273     )
274     .lines()
275     .map(Result::unwrap)
276     .enumerate();
277     let mut code_blocks: Vec<ConfigCodeBlock> = Vec::new();
278     let mut hash_set = Config::hash_set();
279
280     while let Some(cb) = ConfigCodeBlock::extract(&mut file_iter, code_blocks.last(), &mut hash_set)
281     {
282         code_blocks.push(cb);
283     }
284
285     for name in hash_set {
286         if !Config::is_hidden_option(&name) {
287             panic!("{} does not have a configuration guide", name);
288         }
289     }
290
291     code_blocks
292 }
293
294 #[test]
295 fn check_unstable_option_tracking_issue_numbers() {
296     // Ensure that tracking issue links point to the correct issue number
297     let tracking_issue =
298         regex::Regex::new(r"\(tracking issue: \[#(?P<number>\d+)\]\((?P<link>\S+)\)\)")
299             .expect("failed creating configuration pattern");
300
301     let lines = BufReader::new(
302         fs::File::open(Path::new(CONFIGURATIONS_FILE_NAME))
303             .unwrap_or_else(|_| panic!("couldn't read file {}", CONFIGURATIONS_FILE_NAME)),
304     )
305     .lines()
306     .map(Result::unwrap)
307     .enumerate();
308
309     for (idx, line) in lines {
310         if let Some(capture) = tracking_issue.captures(&line) {
311             let number = capture.name("number").unwrap().as_str();
312             let link = capture.name("link").unwrap().as_str();
313             assert!(
314                 link.ends_with(number),
315                 "{} on line {} does not point to issue #{}",
316                 link,
317                 idx + 1,
318                 number,
319             );
320         }
321     }
322 }