Skip to content

Commit

Permalink
fix: use glob instead of walkdir
Browse files Browse the repository at this point in the history
  • Loading branch information
liv committed Aug 8, 2023
1 parent 304ab0d commit b1fb9dc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7,108 deletions.
10 changes: 9 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ oranda-generate-css = { version = "0.3.0-prerelease.4", path = "generate-css" }
[dev-dependencies]
assert_cmd = "2"
assert_fs = "1.0.7"
insta = { version = "1.31.0", features = ["filters"] }
walkdir = "2.3.3"
insta = { version = "1.31.0", features = ["filters", "glob"] }
glob = "0.3.1"

[build-dependencies]
oranda-generate-css = { version = "0.3.0-prerelease.4", path = "generate-css" }
Expand Down
71 changes: 26 additions & 45 deletions tests/gallery/oranda_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::BTreeMap;
use std::sync::Mutex;

use camino::{Utf8Path, Utf8PathBuf};
Expand Down Expand Up @@ -179,10 +178,9 @@ pub struct OrandaResult {
}

impl<'a> TestContext<'a, Tools> {
/// Run 'cargo dist build -aglobal' with the toml patched
/// and return paths to various files that were generated
/// Run `oranda build` with the JSON configuration that we provided which sets up our fake
/// workspace
pub fn oranda_build(&self, test_name: &str) -> Result<OrandaResult> {
// build installers
eprintln!("running oranda build...");
self.tools.oranda.output_checked(|cmd| cmd.arg("build"))?;

Expand Down Expand Up @@ -245,47 +243,33 @@ impl OrandaResult {
// We make a single uber-snapshot to avoid the annoyances of having multiple snapshots in one test
let mut snapshots = String::new();

Self::append_snapshot_dir(&mut snapshots, "public", self.public_dir.as_deref())?;

let test_name = &self.test_name;
snapshot_settings_with_oranda_css_filter().bind(|| {
insta::assert_snapshot!(format!("{test_name}-public"), &snapshots);
});
Ok(())
}

fn append_snapshot_dir(
out: &mut String,
name: &str,
src_path: Option<&Utf8Path>,
) -> Result<()> {
// Skip snapshotting this folder if absent
let Some(src_path) = src_path else {
let Some(src_path) = &self.public_dir else {
return Ok(());
};

let mut results = BTreeMap::new();

for entry in walkdir::WalkDir::new(src_path) {
let entry = entry.into_diagnostic()?;
if entry.file_type().is_file() {
let path = Utf8Path::from_path(entry.path()).expect("path wasn't utf8!?");
let rel_path = path
.strip_prefix(src_path)
.expect("path's child wasn't its child!?")
.to_owned();
let ext = rel_path.extension().unwrap_or_default();
if ext == "html" {
results.insert(rel_path, path.to_owned());
}
for path in glob::glob(&format!("{}/**/*.html", src_path)).unwrap() {
let path = path.unwrap();

let path = Utf8PathBuf::from_path_buf(path).unwrap();
// We don't want to test another tool's output, so we filter out mdbook files. This
// is kind of a FIXME, the way we do this is very brittle, we should really be disabling
// mdbook from running altogether.
if !&path.to_string().contains("book") {
let rel_path = pathdiff::diff_utf8_paths(&path, src_path).unwrap();
// Normalize Windows slashes to Unix slashes
let rel_path = rel_path.to_string().replace('\\', "/");
Self::append_snapshot_file(
&mut snapshots,
&format!("public/{}", rel_path),
Some(path.as_path()),
)
.unwrap();
}
}
for (rel_path, path) in &results {
// normalize path to unix slashes when emitting to snapshot
let rel_path = rel_path.as_str().replace('\\', "/");
Self::append_snapshot_file(out, &format!("{name}/{rel_path}"), Some(path))?;
}

let test_name = &self.test_name;
snapshot_settings_with_oranda_css_filter().bind(|| {
insta::assert_snapshot!(format!("{test_name}-public"), &snapshots);
});
Ok(())
}

Expand All @@ -294,11 +278,8 @@ impl OrandaResult {
name: &str,
src_path: Option<&Utf8Path>,
) -> Result<()> {
// Skip snapshotting this file if absent
let Some(src_path) = src_path else {
return Ok(());
};

// `insta::glob!` guarantees this path exists
let src_path = src_path.unwrap();
let src = axoasset::LocalAsset::load_string(src_path)?;
Self::append_snapshot_string(out, name, &src)
}
Expand Down
Loading

0 comments on commit b1fb9dc

Please sign in to comment.