From 0890512e1c898c9c4c271df12d43353f3b64daf3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 9 Dec 2020 15:07:37 +0300 Subject: Include config into the manual --- crates/rust-analyzer/src/config.rs | 75 +++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 345a56978..bd41a971b 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -637,6 +637,17 @@ macro_rules! _config_data { },)* ]) } + + #[cfg(test)] + fn manual() -> String { + manual(&[ + $({ + let field = stringify!($field); + let ty = stringify!($ty); + (field, ty, &[$($doc),*], $default) + },)* + ]) + } } }; } @@ -753,26 +764,54 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json map.into() } -#[test] -fn schema_in_sync_with_package_json() { - fn remove_ws(text: &str) -> String { - text.replace(char::is_whitespace, "") - } +#[cfg(test)] +fn manual(fields: &[(&'static str, &'static str, &[&str], &str)]) -> String { + fields + .iter() + .map(|(field, _ty, doc, default)| { + let name = field.replace("_", "."); + let name = format!("rust-analyzer.{} (default: `{}`)", name, default); + format!("{}::\n{}\n", name, doc.join(" ")) + }) + .collect::() +} + +#[cfg(test)] +mod tests { + use std::fs; + + use test_utils::project_dir; + + use super::*; - let s = Config::json_schema(); - let schema = format!("{:#}", s); - let schema = schema.trim_start_matches('{').trim_end_matches('}'); + #[test] + fn schema_in_sync_with_package_json() { + let s = Config::json_schema(); + let schema = format!("{:#}", s); + let schema = schema.trim_start_matches('{').trim_end_matches('}'); - let package_json = std::env::current_dir() - .unwrap() - .ancestors() - .nth(2) - .unwrap() - .join("editors/code/package.json"); - let package_json = std::fs::read_to_string(&package_json).unwrap(); + let package_json = project_dir().join("editors/code/package.json"); + let package_json = fs::read_to_string(&package_json).unwrap(); + + let p = remove_ws(&package_json); + let s = remove_ws(&schema); + + assert!(p.contains(&s), "update config in package.json. New config:\n{:#}", schema); + } - let p = remove_ws(&package_json); - let s = remove_ws(&schema); + #[test] + fn schema_in_sync_with_docs() { + let docs_path = project_dir().join("docs/user/generated_config.adoc"); + let current = fs::read_to_string(&docs_path).unwrap(); + let expected = ConfigData::manual(); - assert!(p.contains(&s), "update config in package.json. New config:\n{:#}", schema); + if remove_ws(¤t) != remove_ws(&expected) { + fs::write(&docs_path, expected).unwrap(); + panic!("updated config manual"); + } + } + + fn remove_ws(text: &str) -> String { + text.replace(char::is_whitespace, "") + } } -- cgit v1.2.3