KCL: Show beautiful Miette errors when a KCL example test fails (#4829)

* KCL: Show beautiful Miette errors when a KCL example test fails

Background: KCL example tests are generated from the stdlib KCL examples in the `#[stdlib]` macro in derive-docs.

Problem: When these tests fail, they output a really unhelpful error message like Kcl(Semantic(KclErrorDetails { source_ranges: [156, 160, 0], message: "Expected a sketch but found array" } )).

Solution: Use miette. Now the errors highlight the KCL code that failed and show exactly what went wrong, on which line, presenting nice diagnostics that look like cargo/rustc output.

* Update helix snapshots
This commit is contained in:
Adam Chalmers
2024-12-18 08:52:17 -06:00
committed by GitHub
parent 7193b4110a
commit 93891422f7
19 changed files with 431 additions and 124 deletions

View File

@ -1,7 +1,7 @@
#[cfg(test)]
mod test_examples_my_func {
#[tokio::test(flavor = "multi_thread")]
async fn test_mock_example_my_func0() {
async fn test_mock_example_my_func0() -> miette::Result<()> {
let program =
crate::Program::parse_no_errs("This is another code block.\nyes sirrr.\nmyFunc")
.unwrap();
@ -16,30 +16,47 @@ mod test_examples_my_func {
settings: Default::default(),
context_type: crate::execution::ContextType::Mock,
};
ctx.run(program.into(), &mut crate::ExecState::new())
.await
.unwrap();
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
return Err(miette::Report::new(crate::errors::Report {
error: e,
filename: format!("{}{}", "my_func", 0usize),
kcl_source: "This is another code block.\nyes sirrr.\nmyFunc".to_string(),
}));
}
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 5)]
async fn kcl_test_example_my_func0() {
async fn kcl_test_example_my_func0() -> miette::Result<()> {
let code = "This is another code block.\nyes sirrr.\nmyFunc";
let result = crate::test_server::execute_and_snapshot(
let result = match crate::test_server::execute_and_snapshot(
code,
crate::settings::types::UnitLength::Mm,
None,
)
.await
.unwrap();
{
Err(crate::errors::ExecError::Kcl(e)) => {
return Err(miette::Report::new(crate::errors::Report {
error: e,
filename: format!("{}{}", "my_func", 0usize),
kcl_source: "This is another code block.\nyes sirrr.\nmyFunc".to_string(),
}));
}
Err(other_err) => panic!("{}", other_err),
Ok(img) => img,
};
twenty_twenty::assert_image(
&format!("tests/outputs/{}.png", "serial_test_example_my_func0"),
&result,
0.99,
);
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_mock_example_my_func1() {
async fn test_mock_example_my_func1() -> miette::Result<()> {
let program =
crate::Program::parse_no_errs("This is code.\nIt does other shit.\nmyFunc").unwrap();
let ctx = crate::ExecutorContext {
@ -53,26 +70,43 @@ mod test_examples_my_func {
settings: Default::default(),
context_type: crate::execution::ContextType::Mock,
};
ctx.run(program.into(), &mut crate::ExecState::new())
.await
.unwrap();
if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new()).await {
return Err(miette::Report::new(crate::errors::Report {
error: e,
filename: format!("{}{}", "my_func", 1usize),
kcl_source: "This is code.\nIt does other shit.\nmyFunc".to_string(),
}));
}
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 5)]
async fn kcl_test_example_my_func1() {
async fn kcl_test_example_my_func1() -> miette::Result<()> {
let code = "This is code.\nIt does other shit.\nmyFunc";
let result = crate::test_server::execute_and_snapshot(
let result = match crate::test_server::execute_and_snapshot(
code,
crate::settings::types::UnitLength::Mm,
None,
)
.await
.unwrap();
{
Err(crate::errors::ExecError::Kcl(e)) => {
return Err(miette::Report::new(crate::errors::Report {
error: e,
filename: format!("{}{}", "my_func", 1usize),
kcl_source: "This is code.\nIt does other shit.\nmyFunc".to_string(),
}));
}
Err(other_err) => panic!("{}", other_err),
Ok(img) => img,
};
twenty_twenty::assert_image(
&format!("tests/outputs/{}.png", "serial_test_example_my_func1"),
&result,
0.99,
);
Ok(())
}
}