use std::path::{Path, PathBuf};
use crate::config::{Config, is_glob};
use crate::error::WasmCheckError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedFile {
key: String,
path: PathBuf,
}
impl ResolvedFile {
#[must_use]
pub fn key(&self) -> &str {
&self.key
}
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
}
pub fn find_wasm_files(dir: impl AsRef<Path>) -> Result<Vec<PathBuf>, WasmCheckError> {
let mut entries: Vec<PathBuf> = std::fs::read_dir(dir.as_ref())?
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|path| path.extension().is_some_and(|ext| ext == "wasm"))
.collect();
entries.sort();
if entries.is_empty() {
for sub in FALLBACK_DIRS {
let sub = dir.as_ref().join(sub);
if let Ok(found) = find_wasm_files(&sub) {
entries.extend(found);
}
}
for sub in CARGO_WASM_DIRS {
let sub = dir.as_ref().join(sub);
if let Ok(found) = find_wasm_files(&sub) {
entries.extend(found);
}
}
entries.sort();
}
Ok(entries)
}
const FALLBACK_DIRS: [&str; 2] = ["dist", "target"];
const CARGO_WASM_DIRS: [&str; 2] = [
"target/wasm32-unknown-unknown/release",
"target/wasm32-unknown-unknown/debug",
];
pub fn resolve_files(
cli_file: Option<&str>,
config: Option<&Config>,
config_dir: &Path,
) -> Result<Vec<ResolvedFile>, WasmCheckError> {
let base = normalized_dir(config_dir);
if let Some(file) = cli_file {
if is_glob(file) {
let pattern = glob_pattern(&base, file);
let matches = glob::glob(&pattern)
.map_err(|e| WasmCheckError::Io(std::io::Error::other(e.to_string())))?;
let mut resolved = Vec::new();
for path in matches.filter_map(Result::ok).filter(|p| p.is_file()) {
resolved.push(ResolvedFile {
key: file.to_string(),
path: clean(&path),
});
}
resolved.sort_by(|a, b| a.path.cmp(&b.path));
resolved.dedup();
if resolved.is_empty() {
return Err(WasmCheckError::NoWasmFound {
dir: display_dir(&base),
});
}
return Ok(resolved);
}
let path = clean(Path::new(file));
if !path.is_file() {
return Err(WasmCheckError::FileNotFound(path));
}
return Ok(vec![ResolvedFile {
key: key_for(&path, &base),
path,
}]);
}
if let Some(config) = config.filter(|c| !c.files().is_empty()) {
let mut resolved = Vec::new();
for entry in config.files() {
if is_glob(entry) {
let pattern = glob_pattern(&base, entry);
let matches = glob::glob(&pattern)
.map_err(|e| WasmCheckError::Io(std::io::Error::other(e.to_string())))?;
for path in matches.filter_map(Result::ok).filter(|p| p.is_file()) {
resolved.push(ResolvedFile {
key: entry.clone(),
path: clean(&path),
});
}
} else {
let path = clean(&base.join(entry));
if !path.is_file() {
return Err(WasmCheckError::FileNotFound(path));
}
resolved.push(ResolvedFile {
key: entry.clone(),
path,
});
}
}
resolved.sort_by(|a, b| a.path.cmp(&b.path));
resolved.dedup();
if resolved.is_empty() {
return Err(WasmCheckError::NoWasmFound {
dir: display_dir(&base),
});
}
return Ok(resolved);
}
let dir = if base.as_os_str().is_empty() {
PathBuf::from(".")
} else {
base.clone()
};
let found = find_wasm_files(&dir)?;
match found.as_slice() {
[] => Err(WasmCheckError::NoWasmFound {
dir: display_dir(&base),
}),
[only] => {
let path = clean(only);
Ok(vec![ResolvedFile {
key: key_for(&path, &base),
path,
}])
}
many => Err(WasmCheckError::MultipleWasmFound {
found: many.iter().map(|p| p.display().to_string()).collect(),
}),
}
}
fn normalized_dir(dir: &Path) -> PathBuf {
if dir.as_os_str().is_empty() || dir == Path::new(".") {
PathBuf::new()
} else {
dir.to_path_buf()
}
}
fn display_dir(base: &Path) -> PathBuf {
if base.as_os_str().is_empty() {
PathBuf::from(".")
} else {
base.to_path_buf()
}
}
fn glob_pattern(base: &Path, entry: &str) -> String {
if base.as_os_str().is_empty() {
entry.to_string()
} else {
format!("{}/{}", base.to_string_lossy().replace('\\', "/"), entry)
}
}
fn key_for(path: &Path, base: &Path) -> String {
if base.as_os_str().is_empty() {
return path.display().to_string();
}
match path.strip_prefix(base) {
Ok(relative) if !relative.as_os_str().is_empty() => relative.display().to_string(),
_ => path.display().to_string(),
}
}
fn clean(path: &Path) -> PathBuf {
path.strip_prefix(".")
.map(Path::to_path_buf)
.unwrap_or_else(|_| path.to_path_buf())
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
fn write(path: &str, contents: &[u8]) {
if let Some(parent) = Path::new(path).parent() {
fs::create_dir_all(parent).expect("create parent dirs");
}
fs::write(path, contents).expect("write file");
}
#[test]
fn finds_wasm_directly_in_dir() {
let dir = std::env::temp_dir().join("wasmcheck-direct");
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).expect("create dir");
write(&dir.join("app.wasm").display().to_string(), b"\0asm stub");
let found = find_wasm_files(&dir).expect("find");
assert_eq!(found, vec![dir.join("app.wasm")]);
fs::remove_dir_all(&dir).expect("cleanup");
}
#[test]
fn falls_back_one_level_into_dist() {
let dir = std::env::temp_dir().join("wasmcheck-fallback-dist");
let _ = fs::remove_dir_all(&dir);
write(
&dir.join("dist").join("app.wasm").display().to_string(),
b"\0asm stub",
);
let found = find_wasm_files(&dir).expect("find");
assert_eq!(found, vec![dir.join("dist").join("app.wasm")]);
fs::remove_dir_all(&dir).expect("cleanup");
}
#[test]
fn falls_back_one_level_into_target() {
let dir = std::env::temp_dir().join("wasmcheck-fallback-target");
let _ = fs::remove_dir_all(&dir);
write(
&dir.join("target").join("app.wasm").display().to_string(),
b"\0asm stub",
);
let found = find_wasm_files(&dir).expect("find");
assert_eq!(found, vec![dir.join("target").join("app.wasm")]);
fs::remove_dir_all(&dir).expect("cleanup");
}
#[test]
fn does_not_recurse_past_one_level() {
let dir = std::env::temp_dir().join("wasmcheck-too-deep");
let _ = fs::remove_dir_all(&dir);
write(
&dir.join("build")
.join("output")
.join("app.wasm")
.display()
.to_string(),
b"\0asm stub",
);
assert!(find_wasm_files(&dir).expect("find").is_empty());
fs::remove_dir_all(&dir).expect("cleanup");
}
#[test]
fn direct_files_win_over_fallback() {
let dir = std::env::temp_dir().join("wasmcheck-direct-wins");
let _ = fs::remove_dir_all(&dir);
write(&dir.join("top.wasm").display().to_string(), b"\0asm stub");
write(
&dir.join("dist").join("app.wasm").display().to_string(),
b"\0asm stub",
);
let found = find_wasm_files(&dir).expect("find");
assert_eq!(found, vec![dir.join("top.wasm")]);
fs::remove_dir_all(&dir).expect("cleanup");
}
#[test]
fn fallback_key_is_relative_to_the_search_dir() {
let dir = std::env::temp_dir().join("wasmcheck-fallback-key");
let _ = fs::remove_dir_all(&dir);
write(
&dir.join("dist").join("app.wasm").display().to_string(),
b"\0asm stub",
);
let resolved = resolve_files(None, None, &dir).expect("resolve");
assert_eq!(resolved.len(), 1);
assert_eq!(resolved[0].key(), "dist/app.wasm");
fs::remove_dir_all(&dir).expect("cleanup");
}
#[test]
fn finds_the_cargo_wasm_layout() {
let dir = std::env::temp_dir().join("wasmcheck-cargo-wasm-layout");
let _ = fs::remove_dir_all(&dir);
write(
&dir.join("target")
.join("wasm32-unknown-unknown")
.join("release")
.join("app.wasm")
.display()
.to_string(),
b"\0asm stub",
);
let found = find_wasm_files(&dir).expect("find");
assert_eq!(
found,
vec![
dir.join("target")
.join("wasm32-unknown-unknown")
.join("release")
.join("app.wasm")
]
);
fs::remove_dir_all(&dir).expect("cleanup");
}
#[test]
fn deeper_tooling_layouts_stay_out_of_reach() {
let dir = std::env::temp_dir().join("wasmcheck-nested-in-fallback");
let _ = fs::remove_dir_all(&dir);
write(
&dir.join("dist")
.join("assets")
.join("app.wasm")
.display()
.to_string(),
b"\0asm stub",
);
assert!(find_wasm_files(&dir).expect("find").is_empty());
fs::remove_dir_all(&dir).expect("cleanup");
}
#[test]
fn cargo_debug_layout_is_found_too() {
let dir = std::env::temp_dir().join("wasmcheck-cargo-debug-layout");
let _ = fs::remove_dir_all(&dir);
write(
&dir.join("target")
.join("wasm32-unknown-unknown")
.join("debug")
.join("app.wasm")
.display()
.to_string(),
b"\0asm stub",
);
let found = find_wasm_files(&dir).expect("find");
assert_eq!(found.len(), 1);
fs::remove_dir_all(&dir).expect("cleanup");
}
}