Skip to content

Commit

Permalink
test: Add testing the artifact graph when there's an execution error (#…
Browse files Browse the repository at this point in the history
…5154)

* Add testing the artifact graph snapshots when there's an execution error

* Update output to check artifact graph in error cases

* Rename helper function to be clearer

* Add test that has meaningful output, followed by an error
  • Loading branch information
jtran authored Jan 31, 2025
1 parent e0ef10e commit cffeb52
Show file tree
Hide file tree
Showing 78 changed files with 1,899 additions and 41 deletions.
112 changes: 71 additions & 41 deletions src/wasm-lib/kcl/src/simulation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use insta::rounded_redaction;

use crate::{
errors::KclError,
exec::ArtifactCommand,
execution::{ArtifactGraph, Operation},
parsing::ast::types::{Node, Program},
source_range::ModuleId,
};
Expand Down Expand Up @@ -104,36 +106,12 @@ async fn execute(test_name: &str, render_to_png: bool) {
".environments[].**[].z[]" => rounded_redaction(4),
});
});
assert_snapshot(test_name, "Operations executed", || {
insta::assert_json_snapshot!("ops", exec_state.mod_local.operations);
});
assert_snapshot(test_name, "Artifact commands", || {
insta::assert_json_snapshot!("artifact_commands", exec_state.global.artifact_commands, {
"[].command.segment.*.x" => rounded_redaction(4),
"[].command.segment.*.y" => rounded_redaction(4),
"[].command.segment.*.z" => rounded_redaction(4),
});
});
assert_snapshot(test_name, "Artifact graph flowchart", || {
let flowchart = exec_state
.global
.artifact_graph
.to_mermaid_flowchart()
.unwrap_or_else(|e| format!("Failed to convert artifact graph to flowchart: {e}"));
// Change the snapshot suffix so that it is rendered as a
// Markdown file in GitHub.
insta::assert_binary_snapshot!("artifact_graph_flowchart.md", flowchart.as_bytes().to_owned());
});
assert_snapshot(test_name, "Artifact graph mind map", || {
let mind_map = exec_state
.global
.artifact_graph
.to_mermaid_mind_map()
.unwrap_or_else(|e| format!("Failed to convert artifact graph to mind map: {e}"));
// Change the snapshot suffix so that it is rendered as a
// Markdown file in GitHub.
insta::assert_binary_snapshot!("artifact_graph_mind_map.md", mind_map.as_bytes().to_owned());
});
assert_common_snapshots(
test_name,
exec_state.mod_local.operations,
exec_state.global.artifact_commands,
exec_state.global.artifact_graph,
);
}
Err(e) => {
match e.error {
Expand All @@ -153,17 +131,12 @@ async fn execute(test_name: &str, render_to_png: bool) {
insta::assert_snapshot!("execution_error", report);
});

assert_snapshot(test_name, "Operations executed", || {
insta::assert_json_snapshot!("ops", error.operations);
});

assert_snapshot(test_name, "Artifact commands", || {
insta::assert_json_snapshot!("artifact_commands", error.artifact_commands, {
"[].command.segment.*.x" => rounded_redaction(4),
"[].command.segment.*.y" => rounded_redaction(4),
"[].command.segment.*.z" => rounded_redaction(4),
});
});
assert_common_snapshots(
test_name,
error.operations,
error.artifact_commands,
error.artifact_graph,
);
}
e => {
// These kinds of errors aren't expected to occur. We don't
Expand All @@ -176,6 +149,42 @@ async fn execute(test_name: &str, render_to_png: bool) {
}
}

/// Assert snapshots that should happen both when KCL execution succeeds and
/// when it results in an error.
fn assert_common_snapshots(
test_name: &str,
operations: Vec<Operation>,
artifact_commands: Vec<ArtifactCommand>,
artifact_graph: ArtifactGraph,
) {
assert_snapshot(test_name, "Operations executed", || {
insta::assert_json_snapshot!("ops", operations);
});
assert_snapshot(test_name, "Artifact commands", || {
insta::assert_json_snapshot!("artifact_commands", artifact_commands, {
"[].command.segment.*.x" => rounded_redaction(4),
"[].command.segment.*.y" => rounded_redaction(4),
"[].command.segment.*.z" => rounded_redaction(4),
});
});
assert_snapshot(test_name, "Artifact graph flowchart", || {
let flowchart = artifact_graph
.to_mermaid_flowchart()
.unwrap_or_else(|e| format!("Failed to convert artifact graph to flowchart: {e}"));
// Change the snapshot suffix so that it is rendered as a Markdown file
// in GitHub.
insta::assert_binary_snapshot!("artifact_graph_flowchart.md", flowchart.as_bytes().to_owned());
});
assert_snapshot(test_name, "Artifact graph mind map", || {
let mind_map = artifact_graph
.to_mermaid_mind_map()
.unwrap_or_else(|e| format!("Failed to convert artifact graph to mind map: {e}"));
// Change the snapshot suffix so that it is rendered as a Markdown file
// in GitHub.
insta::assert_binary_snapshot!("artifact_graph_mind_map.md", mind_map.as_bytes().to_owned());
});
}

mod cube {
const TEST_NAME: &str = "cube";

Expand All @@ -197,6 +206,27 @@ mod cube {
super::execute(TEST_NAME, true).await
}
}
mod cube_with_error {
const TEST_NAME: &str = "cube_with_error";

/// Test parsing KCL.
#[test]
fn parse() {
super::parse(TEST_NAME)
}

/// Test that parsing and unparsing KCL produces the original KCL input.
#[test]
fn unparse() {
super::unparse(TEST_NAME)
}

/// Test that KCL is executed correctly.
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_execute() {
super::execute(TEST_NAME, true).await
}
}
mod artifact_graph_example_code1 {
const TEST_NAME: &str = "artifact_graph_example_code1";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Artifact graph flowchart argument_error.kcl
extension: md
snapshot_kind: binary
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```mermaid
flowchart LR
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Artifact graph mind map argument_error.kcl
extension: md
snapshot_kind: binary
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```mermaid
mindmap
root
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Artifact graph flowchart array_elem_pop_empty_fail.kcl
extension: md
snapshot_kind: binary
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```mermaid
flowchart LR
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Artifact graph mind map array_elem_pop_empty_fail.kcl
extension: md
snapshot_kind: binary
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```mermaid
mindmap
root
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Artifact graph flowchart array_elem_pop_fail.kcl
extension: md
snapshot_kind: binary
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```mermaid
flowchart LR
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Artifact graph mind map array_elem_pop_fail.kcl
extension: md
snapshot_kind: binary
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```mermaid
mindmap
root
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Artifact graph flowchart array_elem_push_fail.kcl
extension: md
snapshot_kind: binary
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```mermaid
flowchart LR
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Artifact graph mind map array_elem_push_fail.kcl
extension: md
snapshot_kind: binary
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```mermaid
mindmap
root
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Artifact graph flowchart array_index_oob.kcl
extension: md
snapshot_kind: binary
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```mermaid
flowchart LR
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Artifact graph mind map array_index_oob.kcl
extension: md
snapshot_kind: binary
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```mermaid
mindmap
root
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Artifact graph flowchart comparisons_multiple.kcl
extension: md
snapshot_kind: binary
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```mermaid
flowchart LR
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: kcl/src/simulation_tests.rs
description: Artifact graph mind map comparisons_multiple.kcl
extension: md
snapshot_kind: binary
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```mermaid
mindmap
root
```
Loading

0 comments on commit cffeb52

Please sign in to comment.