test the wasm side (#6726)

Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2025-05-06 20:04:34 -07:00
committed by GitHub
parent 17c326e654
commit e373d285fe
30 changed files with 594 additions and 330 deletions

View File

@ -1,4 +1,4 @@
use std::{ffi::OsStr, path::Path, str::FromStr};
use std::str::FromStr;
use anyhow::Result;
use kcmc::{
@ -15,7 +15,7 @@ use uuid::Uuid;
use crate::{
errors::{KclError, KclErrorDetails},
execution::{annotations, types::UnitLen, ExecState, ExecutorContext, ImportedGeometry},
execution::{annotations, typed_path::TypedPath, types::UnitLen, ExecState, ExecutorContext, ImportedGeometry},
fs::FileSystem,
parsing::ast::types::{Annotation, Node},
source_range::SourceRange,
@ -29,7 +29,7 @@ use crate::{
pub const ZOO_COORD_SYSTEM: System = *KITTYCAD;
pub async fn import_foreign(
file_path: &Path,
file_path: &TypedPath,
format: Option<InputFormat3d>,
exec_state: &mut ExecState,
ctxt: &ExecutorContext,
@ -43,19 +43,18 @@ pub async fn import_foreign(
}));
}
let ext_format =
get_import_format_from_extension(file_path.extension().and_then(OsStr::to_str).ok_or_else(|| {
KclError::Semantic(KclErrorDetails {
message: format!("No file extension found for `{}`", file_path.display()),
source_ranges: vec![source_range],
})
})?)
.map_err(|e| {
KclError::Semantic(KclErrorDetails {
message: e.to_string(),
source_ranges: vec![source_range],
})
})?;
let ext_format = get_import_format_from_extension(file_path.extension().ok_or_else(|| {
KclError::Semantic(KclErrorDetails {
message: format!("No file extension found for `{}`", file_path.display()),
source_ranges: vec![source_range],
})
})?)
.map_err(|e| {
KclError::Semantic(KclErrorDetails {
message: e.to_string(),
source_ranges: vec![source_range],
})
})?;
// Get the format type from the extension of the file.
let format = if let Some(format) = format {
@ -80,15 +79,12 @@ pub async fn import_foreign(
})?;
// We want the file_path to be without the parent.
let file_name = std::path::Path::new(&file_path)
.file_name()
.map(|p| p.to_string_lossy().to_string())
.ok_or_else(|| {
KclError::Semantic(KclErrorDetails {
message: format!("Could not get the file name from the path `{}`", file_path.display()),
source_ranges: vec![source_range],
})
})?;
let file_name = file_path.file_name().map(|p| p.to_string()).ok_or_else(|| {
KclError::Semantic(KclErrorDetails {
message: format!("Could not get the file name from the path `{}`", file_path.display()),
source_ranges: vec![source_range],
})
})?;
let mut import_files = vec![kcmc::ImportFile {
path: file_name.to_string(),
data: file_contents.clone(),
@ -112,19 +108,12 @@ pub async fn import_foreign(
if let Some(uri) = &buffer.uri {
if !uri.starts_with("data:") {
// We want this path relative to the file_path given.
let bin_path = std::path::Path::new(&file_path)
.parent()
.map(|p| p.join(uri))
.map(|p| p.to_string_lossy().to_string())
.ok_or_else(|| {
KclError::Semantic(KclErrorDetails {
message: format!(
"Could not get the parent path of the file `{}`",
file_path.display()
),
source_ranges: vec![source_range],
})
})?;
let bin_path = file_path.parent().map(|p| p.join(uri)).ok_or_else(|| {
KclError::Semantic(KclErrorDetails {
message: format!("Could not get the parent path of the file `{}`", file_path.display()),
source_ranges: vec![source_range],
})
})?;
let bin_contents = ctxt.fs.read(&bin_path, source_range).await.map_err(|e| {
KclError::Semantic(KclErrorDetails {
@ -154,7 +143,7 @@ pub async fn import_foreign(
pub(super) fn format_from_annotations(
annotations: &[Node<Annotation>],
path: &Path,
path: &TypedPath,
import_source_range: SourceRange,
) -> Result<Option<InputFormat3d>, KclError> {
if annotations.is_empty() {
@ -184,7 +173,6 @@ pub(super) fn format_from_annotations(
let mut result = result
.or_else(|| {
path.extension()
.and_then(OsStr::to_str)
.and_then(|ext| get_import_format_from_extension(ext).ok())
})
.ok_or(KclError::Semantic(KclErrorDetails {
@ -397,7 +385,7 @@ mod test {
fn annotations() {
// no annotations
assert!(
format_from_annotations(&[], Path::new("../foo.txt"), SourceRange::default(),)
format_from_annotations(&[], &TypedPath::from("../foo.txt"), SourceRange::default(),)
.unwrap()
.is_none()
);
@ -406,7 +394,7 @@ mod test {
let text = "@()\nimport '../foo.gltf' as foo";
let parsed = crate::Program::parse_no_errs(text).unwrap().ast;
let attrs = parsed.body[0].get_attrs();
let fmt = format_from_annotations(attrs, Path::new("../foo.gltf"), SourceRange::default())
let fmt = format_from_annotations(attrs, &TypedPath::from("../foo.gltf"), SourceRange::default())
.unwrap()
.unwrap();
assert_eq!(
@ -418,7 +406,7 @@ mod test {
let text = "@(format = gltf)\nimport '../foo.txt' as foo";
let parsed = crate::Program::parse_no_errs(text).unwrap().ast;
let attrs = parsed.body[0].get_attrs();
let fmt = format_from_annotations(attrs, Path::new("../foo.txt"), SourceRange::default())
let fmt = format_from_annotations(attrs, &TypedPath::from("../foo.txt"), SourceRange::default())
.unwrap()
.unwrap();
assert_eq!(
@ -427,7 +415,7 @@ mod test {
);
// format, no extension (wouldn't parse but might some day)
let fmt = format_from_annotations(attrs, Path::new("../foo"), SourceRange::default())
let fmt = format_from_annotations(attrs, &TypedPath::from("../foo"), SourceRange::default())
.unwrap()
.unwrap();
assert_eq!(
@ -439,7 +427,7 @@ mod test {
let text = "@(format = obj, coords = vulkan, lengthUnit = ft)\nimport '../foo.txt' as foo";
let parsed = crate::Program::parse_no_errs(text).unwrap().ast;
let attrs = parsed.body[0].get_attrs();
let fmt = format_from_annotations(attrs, Path::new("../foo.txt"), SourceRange::default())
let fmt = format_from_annotations(attrs, &TypedPath::from("../foo.txt"), SourceRange::default())
.unwrap()
.unwrap();
assert_eq!(
@ -454,7 +442,7 @@ mod test {
let text = "@(coords = vulkan, lengthUnit = ft)\nimport '../foo.obj' as foo";
let parsed = crate::Program::parse_no_errs(text).unwrap().ast;
let attrs = parsed.body[0].get_attrs();
let fmt = format_from_annotations(attrs, Path::new("../foo.obj"), SourceRange::default())
let fmt = format_from_annotations(attrs, &TypedPath::from("../foo.obj"), SourceRange::default())
.unwrap()
.unwrap();
assert_eq!(
@ -507,7 +495,7 @@ mod test {
fn assert_annotation_error(src: &str, path: &str, expected: &str) {
let parsed = crate::Program::parse_no_errs(src).unwrap().ast;
let attrs = parsed.body[0].get_attrs();
let err = format_from_annotations(attrs, Path::new(path), SourceRange::default()).unwrap_err();
let err = format_from_annotations(attrs, &TypedPath::from(path), SourceRange::default()).unwrap_err();
assert!(
err.message().contains(expected),
"Expected: `{expected}`, found `{}`",