-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ci testing for markdown toml fences
- Loading branch information
1 parent
4a858e5
commit 05b4a3b
Showing
9 changed files
with
195 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
mod toml; | ||
|
||
use std::{ | ||
ffi::OsStr, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use once_cell::sync::OnceCell; | ||
use serde::Deserialize; | ||
|
||
static WORKSPACE: OnceCell<PathBuf> = OnceCell::new(); | ||
|
||
/// Returns the cargo workspace for the manifest | ||
pub fn get_cargo_workspace() -> &'static Path { | ||
let manifest_dir = env!("CARGO_MANIFEST_DIR"); | ||
WORKSPACE.get_or_init(|| { | ||
#[derive(Deserialize)] | ||
struct Manifest { | ||
workspace_root: PathBuf, | ||
} | ||
let output = std::process::Command::new( | ||
std::env::var("CARGO") | ||
.ok() | ||
.unwrap_or_else(|| "cargo".to_string()), | ||
) | ||
.arg("metadata") | ||
.arg("--format-version=1") | ||
.arg("--no-deps") | ||
.current_dir(manifest_dir) | ||
.output() | ||
.unwrap(); | ||
let manifest: Manifest = serde_json::from_slice(&output.stdout).unwrap(); | ||
manifest.workspace_root | ||
}) | ||
} | ||
|
||
pub fn walk_dir<'a>( | ||
root: &'_ Path, | ||
skip: &'a [impl AsRef<OsStr>], | ||
) -> impl Iterator<Item = Result<walkdir::DirEntry, walkdir::Error>> + 'a { | ||
walkdir::WalkDir::new(root).into_iter().filter_entry(|e| { | ||
if skip | ||
.iter() | ||
.map(|s| -> &std::ffi::OsStr { s.as_ref() }) | ||
.any(|dir| e.file_name() == dir) | ||
{ | ||
return false; | ||
} else if e.file_type().is_dir() { | ||
return true; | ||
} | ||
e.path().extension() == Some("md".as_ref()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::io::Read; | ||
|
||
use once_cell::sync::Lazy; | ||
use regex::{Regex, RegexBuilder}; | ||
|
||
static TOML_REGEX: Lazy<Regex> = Lazy::new(|| { | ||
RegexBuilder::new(r#"```toml\n(.*?)```"#) | ||
.multi_line(true) | ||
.dot_matches_new_line(true) | ||
.build() | ||
.unwrap() | ||
}); | ||
|
||
#[test] | ||
fn toml_check() -> Result<(), Box<dyn std::error::Error>> { | ||
let workspace_root = super::get_cargo_workspace(); | ||
let walk = super::walk_dir( | ||
workspace_root, | ||
&[ | ||
"target", | ||
".git", | ||
"src", | ||
"CODE_OF_CONDUCT.md", | ||
"CHANGELOG.md", | ||
], | ||
); | ||
|
||
for dir_entry in walk { | ||
let dir_entry = dir_entry?; | ||
if dir_entry.file_type().is_dir() { | ||
continue; | ||
} | ||
eprintln!("File: {:?}", dir_entry.path().display()); | ||
let mut file = std::fs::File::open(dir_entry.path()).unwrap(); | ||
let mut contents = String::new(); | ||
file.read_to_string(&mut contents).unwrap(); | ||
for matches in TOML_REGEX.captures_iter(&contents) { | ||
let fence = matches.get(1).unwrap(); | ||
eprintln!( | ||
"testing snippet at: {}:{:?}", | ||
dir_entry.path().display(), | ||
text_line_no(&contents, fence.range().start), | ||
); | ||
assert!(crate::cross_toml::CrossToml::parse(fence.as_str())? | ||
.1 | ||
.is_empty()); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn text_line_no(text: &str, index: usize) -> usize { | ||
let mut line_no = 0; | ||
let mut count = 0; | ||
for line in text.split('\n') { | ||
line_no += 1; | ||
count += line.as_bytes().len() + 1; | ||
if count >= index { | ||
break; | ||
} | ||
} | ||
line_no | ||
} |