niri-config: update wiki parses test to test all codeblocks
Browse files at this commit1af9f9bd95283544b068dbbe33e923ab9e836243Parent: 926451c1 changed file +23 -19
- niri-config/tests/wiki-parses.rs +23 -19
niri-config/tests/wiki-parses.rsmodified+23 -19
| @@ -1,19 +1,18 @@ | |||
| 1 | use std::fs; | 1 | use std::fs; |
| 2 | use std::path::PathBuf; | 2 | use std::path::PathBuf; |
| 3 | 3 | ||
| 4 | -const NO_TEST_COMMENT: &str = "<!-- no-test -->"; | ||
| 5 | - | ||
| 6 | struct KdlCodeBlock { | 4 | struct KdlCodeBlock { |
| 7 | filename: String, | 5 | filename: String, |
| 8 | code: String, | 6 | code: String, |
| 9 | line_number: usize, | 7 | line_number: usize, |
| 8 | + must_fail: bool, | ||
| 10 | } | 9 | } |
| 11 | 10 | ||
| 12 | fn extract_kdl_from_file(file_contents: &str, filename: &str) -> Vec<KdlCodeBlock> { | 11 | fn extract_kdl_from_file(file_contents: &str, filename: &str) -> Vec<KdlCodeBlock> { |
| 13 | - // Removes the > from callouts that might contain ```kdl``` | 12 | + let mut lines = file_contents |
| 14 | - let lines: Vec<_> = file_contents | ||
| 15 | .lines() | 13 | .lines() |
| 16 | .map(|line| { | 14 | .map(|line| { |
| 15 | + // Removes the > from callouts that might contain ```kdl``` | ||
| 17 | let line = line.trim(); | 16 | let line = line.trim(); |
| 18 | if line.starts_with(">") { | 17 | if line.starts_with(">") { |
| 19 | if line.len() == 1 { | 18 | if line.len() == 1 { |
| @@ -25,22 +24,18 @@ fn extract_kdl_from_file(file_contents: &str, filename: &str) -> Vec<KdlCodeBloc | |||
| 25 | line | 24 | line |
| 26 | } | 25 | } |
| 27 | }) | 26 | }) |
| 28 | - .enumerate() | 27 | + .enumerate(); |
| 29 | - .collect(); | ||
| 30 | 28 | ||
| 31 | - let mut lines_iter = lines.iter(); | ||
| 32 | let mut kdl_code_blocks = vec![]; | 29 | let mut kdl_code_blocks = vec![]; |
| 33 | 30 | ||
| 34 | - while let Some((line_number, line)) = lines_iter.next() { | 31 | + while let Some((line_number, line)) = lines.next() { |
| 35 | - let start_snippet = | 32 | + if !line.starts_with("```kdl") { |
| 36 | - line.starts_with("```kdl") && lines[line_number - 1].1 != NO_TEST_COMMENT; | ||
| 37 | - | ||
| 38 | - if !start_snippet { | ||
| 39 | continue; | 33 | continue; |
| 40 | } | 34 | } |
| 41 | 35 | ||
| 42 | let mut snippet = String::new(); | 36 | let mut snippet = String::new(); |
| 43 | - for (_, line) in lines_iter | 37 | + |
| 38 | + for (_, line) in lines | ||
| 44 | .by_ref() | 39 | .by_ref() |
| 45 | .take_while(|(_, line)| !line.starts_with("```")) | 40 | .take_while(|(_, line)| !line.starts_with("```")) |
| 46 | { | 41 | { |
| @@ -50,8 +45,9 @@ fn extract_kdl_from_file(file_contents: &str, filename: &str) -> Vec<KdlCodeBloc | |||
| 50 | 45 | ||
| 51 | kdl_code_blocks.push(KdlCodeBlock { | 46 | kdl_code_blocks.push(KdlCodeBlock { |
| 52 | code: snippet, | 47 | code: snippet, |
| 53 | - line_number: *line_number, | 48 | + line_number, |
| 54 | filename: filename.to_string(), | 49 | filename: filename.to_string(), |
| 50 | + must_fail: line.contains("must-fail"), | ||
| 55 | }); | 51 | }); |
| 56 | } | 52 | } |
| 57 | 53 | ||
| @@ -86,15 +82,23 @@ fn wiki_docs_parses() { | |||
| 86 | code, | 82 | code, |
| 87 | line_number, | 83 | line_number, |
| 88 | filename, | 84 | filename, |
| 85 | + must_fail, | ||
| 89 | } in code_blocks | 86 | } in code_blocks |
| 90 | { | 87 | { |
| 91 | if let Err(error) = niri_config::Config::parse(&filename, &code) { | 88 | if let Err(error) = niri_config::Config::parse(&filename, &code) { |
| 89 | + if !must_fail { | ||
| 90 | + errors.push(format!( | ||
| 91 | + "Error parsing wiki KDL code block at {}:{}: {:?}", | ||
| 92 | + filename, | ||
| 93 | + line_number, | ||
| 94 | + miette::Report::new(error) | ||
| 95 | + )); | ||
| 96 | + } | ||
| 97 | + } else if must_fail { | ||
| 92 | errors.push(format!( | 98 | errors.push(format!( |
| 93 | - "Error parsing wiki KDL code block at {}:{}: {:?}", | 99 | + "Expected error parsing wiki KDL code block at {}:{}", |
| 94 | - filename, | 100 | + filename, line_number |
| 95 | - line_number, | 101 | + )); |
| 96 | - miette::Report::new(error) | ||
| 97 | - )) | ||
| 98 | } | 102 | } |
| 99 | } | 103 | } |
| 100 | 104 | ||