Skip to content

Commit

Permalink
niri-config: add test to see if all snippets inside of the wiki compile
Browse files Browse the repository at this point in the history
Signed-off-by: Suyashtnt <[email protected]>
  • Loading branch information
Suyashtnt authored and YaLTeR committed Jul 8, 2024
1 parent 3be6e38 commit 7b3bef1
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 1 deletion.
149 changes: 148 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions niri-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ tracy-client.workspace = true

[dev-dependencies]
pretty_assertions = "1.4.0"
miette = { version = "5.10.0", features = ["fancy"] }
108 changes: 108 additions & 0 deletions niri-config/tests/wiki-parses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use std::fs;
use std::path::PathBuf;

const NO_TEST_COMMENT: &str = "<!-- no-test -->";

struct KdlCodeBlock {
filename: String,
code: String,
line_number: usize,
}

fn extract_kdl_from_file(file_contents: &str, filename: &str) -> Vec<KdlCodeBlock> {
// Removes the > from callouts that might contain ```kdl```
let lines: Vec<_> = file_contents
.lines()
.map(|line| {
let line = line.trim();
if line.starts_with(">") {
if line.len() == 1 {
""
} else {
&line[2..]
}
} else {
line
}
})
.enumerate()
.collect();

let mut lines_iter = lines.iter();
let mut kdl_code_blocks = vec![];

while let Some((line_number, line)) = lines_iter.next() {
let start_snippet =
line.starts_with("```kdl") && lines[line_number - 1].1 != NO_TEST_COMMENT;

if !start_snippet {
continue;
}

let mut snippet = String::new();
for (_, line) in lines_iter
.by_ref()
.take_while(|(_, line)| !line.starts_with("```"))
{
snippet.push_str(line);
snippet.push('\n');
}

kdl_code_blocks.push(KdlCodeBlock {
code: snippet,
line_number: *line_number,
filename: filename.to_string(),
});
}

kdl_code_blocks
}

#[test]
fn wiki_docs_parses() {
let wiki_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../wiki");

let code_blocks = fs::read_dir(&wiki_dir)
.unwrap()
.filter_map(|entry| entry.ok())
.filter(|entry| entry.file_type().is_ok_and(|ft| ft.is_file()))
.filter(|file| {
file.path()
.extension()
.map(|ext| ext == "md")
.unwrap_or(false)
})
.map(|file| {
let file_contents = fs::read_to_string(file.path()).unwrap();
let file_path = file.path();
let filename = file_path.to_str().unwrap();
extract_kdl_from_file(&file_contents, filename)
})
.flatten();

let mut errors = vec![];

for KdlCodeBlock {
code,
line_number,
filename,
} in code_blocks
{
if let Err(error) = niri_config::Config::parse(&filename, &code) {
errors.push(format!(
"Error parsing wiki KDL code block at {}:{}: {:?}",
filename,
line_number,
miette::Report::new(error)
))
}
}

if !errors.is_empty() {
panic!(
"Errors parsing {} wiki KDL code blocks:\n{}",
errors.len(),
errors.join("\n")
);
}
}

0 comments on commit 7b3bef1

Please sign in to comment.