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