Skip to content

Commit

Permalink
add ci testing for markdown toml fences
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilgardis committed Apr 6, 2022
1 parent 4f98001 commit 778af2d
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --locked
args: --locked --all-targets --workspace
timeout-minutes: 5

generate-matrix:
Expand Down
67 changes: 67 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ dunce = "1"

[profile.release]
lto = true

[workspace]
default-members = ["."]
members = [".", "ci"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ non-standard targets (i.e. something not reported by rustc/rustup). However,
you can use the `build.xargo` or `target.{{TARGET}}.xargo` field in
`Cross.toml` to force the use of `xargo`:

``` toml
```toml
# all the targets will use `xargo`
[build]
xargo = true
```

Or,

``` toml
```toml
# only this target will use `xargo`
[target.aarch64-unknown-linux-gnu]
xargo = true
Expand Down
16 changes: 16 additions & 0 deletions ci/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "ci"
version = "0.0.0"
edition = "2021"
publish = false

[lib]
path = "lib.rs"

[dependencies]
regex = "1"
serde = "1"
serde_json = "1"
once_cell = "1"
walkdir = "2"
cross = { path = ".." }
48 changes: 48 additions & 0 deletions ci/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::{path::{Path, PathBuf}, ffi::OsStr};

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())
})
}
63 changes: 63 additions & 0 deletions ci/tests/toml.rs
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 = ci::get_cargo_workspace();
let walk = ci::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!(cross::cross_toml::CrossToml::from_str(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
}

0 comments on commit 778af2d

Please sign in to comment.