]> git.lizzy.rs Git - rust.git/commitdiff
Update configuration tests
authortopecongiro <seuchida@gmail.com>
Fri, 23 Feb 2018 12:54:59 +0000 (21:54 +0900)
committertopecongiro <seuchida@gmail.com>
Fri, 23 Feb 2018 12:54:59 +0000 (21:54 +0900)
This commit adds following functionalities to `configuration_snippet_tests`:

1. Error if there is an unknown configuration option in Configuration.md.
2. Error if there are multiple guides for the same configuration option in
   Configuration.md.
3. Error if an user-facing configuration option does not have its guide in
   Configuration.md.

We will be able to catch outdated Configuration.md. Should prevent issues
like #2459.

rustfmt-config/src/config_type.rs
rustfmt-core/tests/lib.rs

index 2bc9ceace87e3c8c703ae5b5f5d7c36df64a298e..bace9dba8ff17965e56e6702c7c5ddcd803dd798 100644 (file)
@@ -74,6 +74,8 @@ macro_rules! is_nightly_channel {
 
 macro_rules! create_config {
     ($($i:ident: $ty:ty, $def:expr, $stb:expr, $( $dstring:expr ),+ );+ $(;)*) => (
+        use std::collections::HashSet;
+
         #[derive(Clone)]
         pub struct Config {
             // For each config item, we store a bool indicating whether it has
@@ -190,6 +192,24 @@ fn fill_from_parsed_config(mut self, parsed: PartialConfig) -> Config {
                 self
             }
 
+            /// Returns a hash set initialized with every user-facing config option name.
+            pub fn hash_set() -> HashSet<String> {
+                let mut hash_set = HashSet::new();
+                $(
+                    hash_set.insert(stringify!($i).to_owned());
+                )+
+                hash_set
+            }
+
+            pub fn is_valid_name(name: &str) -> bool {
+                match name {
+                    $(
+                        stringify!($i) => true,
+                    )+
+                        _ => false,
+                }
+            }
+
             pub fn from_toml(toml: &str) -> Result<Config, String> {
                 let parsed: toml::Value =
                     toml.parse().map_err(|e| format!("Could not parse TOML: {}", e))?;
@@ -199,15 +219,9 @@ pub fn from_toml(toml: &str) -> Result<Config, String> {
                         .as_table()
                         .ok_or(String::from("Parsed config was not table"))?;
                     for key in table.keys() {
-                        match &**key {
-                            $(
-                                stringify!($i) => (),
-                            )+
-                            _ => {
-                                let msg =
-                                    &format!("Warning: Unknown configuration option `{}`\n", key);
-                                err.push_str(msg)
-                            }
+                        if !Config::is_valid_name(key) {
+                            let msg = &format!("Warning: Unknown configuration option `{}`\n", key);
+                            err.push_str(msg)
                         }
                     }
                 }
@@ -324,10 +338,13 @@ fn resolve_project_file(dir: &Path) -> Result<Option<PathBuf>, Error> {
                 }
             }
 
+            pub fn is_hidden_option(name: &str) -> bool {
+                const HIDE_OPTIONS: [&str; 3] = ["verbose", "file_lines", "width_heuristics"];
+                HIDE_OPTIONS.contains(&name)
+            }
 
             pub fn print_docs() {
                 use std::cmp;
-                const HIDE_OPTIONS: [&str; 3] = ["verbose", "file_lines", "width_heuristics"];
                 let max = 0;
                 $( let max = cmp::max(max, stringify!($i).len()+1); )+
                 let mut space_str = String::with_capacity(max);
@@ -338,7 +355,7 @@ pub fn print_docs() {
                 $(
                     let name_raw = stringify!($i);
 
-                    if !HIDE_OPTIONS.contains(&name_raw) {
+                    if !Config::is_hidden_option(name_raw) {
                         let mut name_out = String::with_capacity(max);
                         for _ in name_raw.len()..max-1 {
                             name_out.push(' ')
index f219cf3212b9b0d5065a7bfc01da55075eb32352..00988dc6fc5ce40d4e9d466770790c5e4b7fd773 100644 (file)
@@ -17,7 +17,7 @@
 extern crate rustfmt_core as rustfmt;
 extern crate term;
 
-use std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
 use std::fs;
 use std::io::{self, BufRead, BufReader, Read};
 use std::iter::{Enumerate, Peekable};
@@ -795,6 +795,7 @@ fn formatted_is_idempotent(&self) -> bool {
     fn extract<I: Iterator<Item = String>>(
         file: &mut Enumerate<I>,
         prev: Option<&ConfigCodeBlock>,
+        hash_set: &mut HashSet<String>,
     ) -> Option<ConfigCodeBlock> {
         let mut code_block = ConfigCodeBlock::new();
         code_block.config_name = prev.and_then(|cb| cb.config_name.clone());
@@ -806,6 +807,16 @@ fn extract<I: Iterator<Item = String>>(
                     break;
                 }
                 Some(ConfigurationSection::ConfigName(name)) => {
+                    assert!(
+                        Config::is_valid_name(&name),
+                        "an unknown configuration option was found: {}",
+                        name
+                    );
+                    assert!(
+                        hash_set.remove(&name),
+                        "multiple configuration guides found for option {}",
+                        name
+                    );
                     code_block.set_config_name(Some(name));
                 }
                 Some(ConfigurationSection::ConfigValue(value)) => {
@@ -831,11 +842,20 @@ fn get_code_blocks() -> Vec<ConfigCodeBlock> {
             .map(|l| l.unwrap())
             .enumerate();
         let mut code_blocks: Vec<ConfigCodeBlock> = Vec::new();
+        let mut hash_set = Config::hash_set();
 
-        while let Some(cb) = ConfigCodeBlock::extract(&mut file_iter, code_blocks.last()) {
+        while let Some(cb) =
+            ConfigCodeBlock::extract(&mut file_iter, code_blocks.last(), &mut hash_set)
+        {
             code_blocks.push(cb);
         }
 
+        for name in hash_set {
+            if !Config::is_hidden_option(&name) {
+                panic!("{} does not have a configuration guide", name);
+            }
+        }
+
         code_blocks
     }