diff --git a/rust/kcl-lib/src/lib.rs b/rust/kcl-lib/src/lib.rs index 255a7e920..0a855eebe 100644 --- a/rust/kcl-lib/src/lib.rs +++ b/rust/kcl-lib/src/lib.rs @@ -96,6 +96,8 @@ pub use modules::ModuleId; pub use parsing::ast::{modify::modify_ast_for_sketch, types::FormatOptions}; pub use settings::types::{project::ProjectConfiguration, Configuration, UnitLength}; pub use source_range::SourceRange; +#[cfg(not(target_arch = "wasm32"))] +pub use unparser::recast_dir; // Rather than make executor public and make lots of it pub(crate), just re-export into a new module. // Ideally we wouldn't export these things at all, they should only be used for testing. diff --git a/rust/kcl-lib/src/simulation_tests.rs b/rust/kcl-lib/src/simulation_tests.rs index 16d6bf29c..9b951a185 100644 --- a/rust/kcl-lib/src/simulation_tests.rs +++ b/rust/kcl-lib/src/simulation_tests.rs @@ -95,11 +95,11 @@ fn parse_test(test: &Test) { }); } -fn unparse(test_name: &str) { - unparse_test(&Test::new(test_name)); +async fn unparse(test_name: &str) { + unparse_test(&Test::new(test_name)).await; } -fn unparse_test(test: &Test) { +async fn unparse_test(test: &Test) { let input = read("ast.snap", &test.output_dir); let ast_res: Result = get(&input); let Ok(ast) = ast_res else { @@ -107,7 +107,31 @@ fn unparse_test(test: &Test) { }; // Check recasting the AST produces the original string. let actual = ast.recast(&Default::default(), 0); - expectorate::assert_contents(test.input_dir.join(&test.entry_point), &actual); + let entry_point = test.input_dir.join(&test.entry_point); + expectorate::assert_contents(&entry_point, &actual); + + // Check all the rest of the files in the directory. + let kcl_files = crate::unparser::walk_dir(&test.input_dir).await.unwrap(); + // Filter out the entry point file. + let kcl_files = kcl_files.into_iter().filter(|f| f != &entry_point); + let futures = kcl_files + .into_iter() + .map(|file| { + tokio::spawn(async move { + let contents = tokio::fs::read_to_string(&file).await?; + let program = crate::Program::parse_no_errs(&contents)?; + let recast = program.recast_with_options(&Default::default()); + expectorate::assert_contents(file, &recast); + + Ok::<(), anyhow::Error>(()) + }) + }) + .collect::>(); + + // Join all futures and await their completion. + for future in futures { + future.await.unwrap().unwrap(); + } } async fn execute(test_name: &str, render_to_png: bool) { @@ -249,9 +273,9 @@ mod cube { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -270,9 +294,9 @@ mod cube_with_error { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -291,9 +315,9 @@ mod artifact_graph_example_code1 { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -312,9 +336,9 @@ mod artifact_graph_example_code_no_3d { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -333,9 +357,9 @@ mod artifact_graph_example_code_offset_planes { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -354,9 +378,9 @@ mod artifact_graph_sketch_on_face_etc { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -375,9 +399,9 @@ mod helix_ccw { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -396,9 +420,9 @@ mod double_map_fn { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -417,9 +441,9 @@ mod property_of_object { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -438,9 +462,9 @@ mod index_of_array { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -459,9 +483,9 @@ mod comparisons { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -480,9 +504,9 @@ mod array_range_expr { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -501,9 +525,9 @@ mod array_range_negative_expr { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -522,9 +546,9 @@ mod sketch_in_object { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -543,9 +567,9 @@ mod if_else { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -564,9 +588,9 @@ mod add_lots { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -588,9 +612,9 @@ mod argument_error { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -609,9 +633,9 @@ mod array_elem_push { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -630,9 +654,9 @@ mod invalid_index_str { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -651,9 +675,9 @@ mod invalid_index_negative { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -672,9 +696,9 @@ mod invalid_index_fractional { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -693,9 +717,9 @@ mod invalid_member_object { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -714,9 +738,9 @@ mod invalid_member_object_prop { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -735,9 +759,9 @@ mod non_string_key_of_object { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -756,9 +780,9 @@ mod array_index_oob { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -777,9 +801,9 @@ mod object_prop_not_found { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -798,9 +822,9 @@ mod pipe_substitution_inside_function_called_from_pipeline { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -819,9 +843,9 @@ mod comparisons_multiple { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -840,9 +864,9 @@ mod import_cycle1 { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -861,9 +885,9 @@ mod import_function_not_sketch { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -882,9 +906,9 @@ mod import_constant { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -903,9 +927,9 @@ mod import_export { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -924,9 +948,9 @@ mod import_glob { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -945,9 +969,9 @@ mod import_whole { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -966,9 +990,9 @@ mod import_side_effect { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -987,9 +1011,9 @@ mod import_foreign { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1008,9 +1032,9 @@ mod assembly_non_default_units { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1030,9 +1054,9 @@ mod array_elem_push_fail { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1051,9 +1075,9 @@ mod sketch_on_face { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1072,9 +1096,9 @@ mod revolve_about_edge { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1093,9 +1117,9 @@ mod poop_chute { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1114,9 +1138,9 @@ mod neg_xz_plane { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1135,9 +1159,9 @@ mod xz_plane { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1156,9 +1180,9 @@ mod sketch_on_face_after_fillets_referencing_face { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1177,9 +1201,9 @@ mod circular_pattern3d_a_pattern { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1198,9 +1222,9 @@ mod linear_pattern3d_a_pattern { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1219,9 +1243,9 @@ mod tangential_arc { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1240,9 +1264,9 @@ mod big_number_angle_to_match_length_x { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1261,9 +1285,9 @@ mod big_number_angle_to_match_length_y { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1282,9 +1306,9 @@ mod sketch_on_face_circle_tagged { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1303,9 +1327,9 @@ mod basic_fillet_cube_start { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1324,9 +1348,9 @@ mod basic_fillet_cube_next_adjacent { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1345,9 +1369,9 @@ mod basic_fillet_cube_previous_adjacent { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1366,9 +1390,9 @@ mod basic_fillet_cube_end { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1387,9 +1411,9 @@ mod basic_fillet_cube_close_opposite { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1408,9 +1432,9 @@ mod sketch_on_face_end { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1429,9 +1453,9 @@ mod sketch_on_face_start { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1450,9 +1474,9 @@ mod sketch_on_face_end_negative_extrude { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1471,9 +1495,9 @@ mod mike_stress_test { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1492,9 +1516,9 @@ mod pentagon_fillet_sugar { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1513,9 +1537,9 @@ mod pipe_as_arg { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1534,9 +1558,9 @@ mod computed_var { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1555,9 +1579,9 @@ mod riddle_small { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1576,9 +1600,9 @@ mod tan_arc_x_line { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1597,9 +1621,9 @@ mod fillet_and_shell { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1618,9 +1642,9 @@ mod sketch_on_chamfer_two_times { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1639,9 +1663,9 @@ mod sketch_on_chamfer_two_times_different_order { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1660,9 +1684,9 @@ mod parametric_with_tan_arc { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1681,9 +1705,9 @@ mod parametric { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1702,9 +1726,9 @@ mod ssi_pattern { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1723,9 +1747,9 @@ mod angled_line { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1744,9 +1768,9 @@ mod function_sketch_with_position { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1765,9 +1789,9 @@ mod function_sketch { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1786,9 +1810,9 @@ mod i_shape { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1807,9 +1831,9 @@ mod kittycad_svg { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1828,9 +1852,9 @@ mod kw_fn { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1849,9 +1873,9 @@ mod kw_fn_too_few_args { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1870,9 +1894,9 @@ mod kw_fn_unlabeled_but_has_label { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1891,9 +1915,9 @@ mod kw_fn_with_defaults { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1912,9 +1936,9 @@ mod boolean_logical_and { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1933,9 +1957,9 @@ mod boolean_logical_or { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1954,9 +1978,9 @@ mod boolean_logical_multiple { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1975,9 +1999,9 @@ mod circle_three_point { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -1996,9 +2020,9 @@ mod array_elem_pop { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -2017,9 +2041,9 @@ mod array_elem_pop_empty_fail { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -2038,9 +2062,9 @@ mod array_elem_pop_fail { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -2059,9 +2083,9 @@ mod helix_simple { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -2081,9 +2105,9 @@ mod import_file_not_exist_error { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -2102,10 +2126,9 @@ mod import_file_parse_error { super::parse(TEST_NAME); } - /// Test that parsing and unparsing KCL produces the original KCL input. #[test] fn unparse() { - super::unparse(TEST_NAME) + // Do nothing since we want to keep the parse error for the test. } /// Test that KCL is executed correctly. @@ -2125,9 +2148,9 @@ mod flush_batch_on_end { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -2147,9 +2170,9 @@ mod multi_transform { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -2169,9 +2192,9 @@ mod import_transform { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -2191,9 +2214,9 @@ mod out_of_band_sketches { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -2213,9 +2236,9 @@ mod crazy_multi_profile { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -2234,9 +2257,9 @@ mod assembly_mixed_units_cubes { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. @@ -2255,9 +2278,9 @@ mod bad_units_in_annotation { } /// Test that parsing and unparsing KCL produces the original KCL input. - #[test] - fn unparse() { - super::unparse(TEST_NAME) + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await } /// Test that KCL is executed correctly. diff --git a/rust/kcl-lib/src/simulation_tests/kcl_samples.rs b/rust/kcl-lib/src/simulation_tests/kcl_samples.rs index 7157ed66b..83e157b48 100644 --- a/rust/kcl-lib/src/simulation_tests/kcl_samples.rs +++ b/rust/kcl-lib/src/simulation_tests/kcl_samples.rs @@ -34,10 +34,10 @@ fn parse(dir_name: &str, dir_path: &Path) { } #[kcl_directory_test_macro::test_all_dirs("../public/kcl-samples")] -fn unparse(dir_name: &str, dir_path: &Path) { - // kcl-samples don't always use correct formatting. We don't ignore the - // test because we want to allow the just command to work. It's actually - // fine when no test runs. +async fn unparse(dir_name: &str, dir_path: &Path) { + // TODO: turn this on when we fix the comments recasting. + // let t = test(dir_name, dir_path.join("main.kcl").to_str().unwrap().to_owned()); + // super::unparse_test(&t).await; } #[kcl_directory_test_macro::test_all_dirs("../public/kcl-samples")] diff --git a/rust/kcl-lib/src/unparser.rs b/rust/kcl-lib/src/unparser.rs index 64005be26..b1511250e 100644 --- a/rust/kcl-lib/src/unparser.rs +++ b/rust/kcl-lib/src/unparser.rs @@ -842,6 +842,103 @@ impl Type { } } +/// Collect all the kcl files in a directory, recursively. +#[cfg(not(target_arch = "wasm32"))] +#[async_recursion::async_recursion] +pub(crate) async fn walk_dir(dir: &std::path::PathBuf) -> Result, anyhow::Error> { + // Make sure we actually have a directory. + if !dir.is_dir() { + anyhow::bail!("`{}` is not a directory", dir.display()); + } + + let mut entries = tokio::fs::read_dir(dir).await?; + + let mut files = Vec::new(); + while let Some(entry) = entries.next_entry().await? { + let path = entry.path(); + + if path.is_dir() { + files.extend(walk_dir(&path).await?); + } else if path.extension().is_some_and(|ext| ext == "kcl") { + files.push(path); + } + } + + Ok(files) +} + +/// Recast all the kcl files in a directory, recursively. +#[cfg(not(target_arch = "wasm32"))] +pub async fn recast_dir(dir: &std::path::Path, options: &crate::FormatOptions) -> Result<(), crate::KclError> { + let files = walk_dir(&dir.to_path_buf()).await.map_err(|err| { + crate::KclError::Internal(crate::errors::KclErrorDetails { + message: format!("Failed to walk directory `{}`: {:?}", dir.display(), err), + source_ranges: vec![crate::SourceRange::default()], + }) + })?; + + let futures = files + .into_iter() + .map(|file| { + let options = options.clone(); + tokio::spawn(async move { + let contents = tokio::fs::read_to_string(&file).await.map_err(|err| { + crate::KclError::Internal(crate::errors::KclErrorDetails { + message: format!("Failed to read file `{}`: {:?}", file.display(), err), + source_ranges: vec![crate::SourceRange::default()], + }) + })?; + let (program, ces) = crate::Program::parse(&contents)?; + for ce in &ces { + if ce.severity != crate::errors::Severity::Warning { + return Err(crate::KclError::Semantic(ce.clone().into())); + } + } + let Some(program) = program else { + return Err(crate::KclError::Internal(crate::errors::KclErrorDetails { + message: format!("Failed to parse file `{}`: {:?}", file.display(), ces), + source_ranges: vec![crate::SourceRange::default()], + })); + }; + let recast = program.recast_with_options(&options); + tokio::fs::write(&file, recast).await.map_err(|err| { + crate::KclError::Internal(crate::errors::KclErrorDetails { + message: format!("Failed to write file `{}`: {:?}", file.display(), err), + source_ranges: vec![crate::SourceRange::default()], + }) + })?; + + Ok::<(), crate::KclError>(()) + }) + }) + .collect::>(); + + // Join all futures and await their completion + let results = futures::future::join_all(futures).await; + + // Check if any of the futures failed. + let mut errors = Vec::new(); + for result in results { + if let Err(err) = result.map_err(|err| { + crate::KclError::Internal(crate::errors::KclErrorDetails { + message: format!("Failed to recast file: {:?}", err), + source_ranges: vec![crate::SourceRange::default()], + }) + })? { + errors.push(err); + } + } + + if !errors.is_empty() { + return Err(crate::KclError::Internal(crate::errors::KclErrorDetails { + message: format!("Failed to recast files: {:?}", errors), + source_ranges: vec![crate::SourceRange::default()], + })); + } + + Ok(()) +} + #[cfg(test)] mod tests { use pretty_assertions::assert_eq; diff --git a/rust/kcl-lib/tests/assembly_mixed_units_cubes/artifact_commands.snap b/rust/kcl-lib/tests/assembly_mixed_units_cubes/artifact_commands.snap index dfa1f5a43..b64071ace 100644 --- a/rust/kcl-lib/tests/assembly_mixed_units_cubes/artifact_commands.snap +++ b/rust/kcl-lib/tests/assembly_mixed_units_cubes/artifact_commands.snap @@ -80,8 +80,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 47, - 66, + 48, + 67, 3 ], "command": { @@ -109,8 +109,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 76, - 113, + 77, + 114, 3 ], "command": { @@ -129,8 +129,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 76, - 113, + 77, + 114, 3 ], "command": { @@ -140,8 +140,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 76, - 113, + 77, + 114, 3 ], "command": { @@ -157,8 +157,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 76, - 113, + 77, + 114, 3 ], "command": { @@ -168,8 +168,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 119, - 136, + 120, + 137, 3 ], "command": { @@ -189,8 +189,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 142, - 160, + 143, + 161, 3 ], "command": { @@ -210,8 +210,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 166, - 184, + 167, + 185, 3 ], "command": { @@ -231,8 +231,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 190, - 246, + 191, + 247, 3 ], "command": { @@ -252,8 +252,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 252, - 259, + 253, + 260, 3 ], "command": { @@ -264,8 +264,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -284,8 +284,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -298,8 +298,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -309,8 +309,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -321,8 +321,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -334,8 +334,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -348,8 +348,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -362,8 +362,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -376,8 +376,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -390,8 +390,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -404,8 +404,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -418,8 +418,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -432,8 +432,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 265, - 287, + 266, + 288, 3 ], "command": { @@ -458,8 +458,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 47, - 66, + 48, + 67, 4 ], "command": { @@ -487,8 +487,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 76, - 111, + 77, + 112, 4 ], "command": { @@ -507,8 +507,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 76, - 111, + 77, + 112, 4 ], "command": { @@ -518,8 +518,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 76, - 111, + 77, + 112, 4 ], "command": { @@ -535,8 +535,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 76, - 111, + 77, + 112, 4 ], "command": { @@ -546,8 +546,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 117, - 134, + 118, + 135, 4 ], "command": { @@ -567,8 +567,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 140, - 158, + 141, + 159, 4 ], "command": { @@ -588,8 +588,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 164, - 182, + 165, + 183, 4 ], "command": { @@ -609,8 +609,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 188, - 244, + 189, + 245, 4 ], "command": { @@ -630,8 +630,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 250, - 257, + 251, + 258, 4 ], "command": { @@ -642,8 +642,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { @@ -662,8 +662,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { @@ -676,8 +676,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { @@ -687,8 +687,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { @@ -699,8 +699,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { @@ -712,8 +712,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { @@ -726,8 +726,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { @@ -740,8 +740,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { @@ -754,8 +754,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { @@ -768,8 +768,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { @@ -782,8 +782,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { @@ -796,8 +796,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { @@ -810,8 +810,8 @@ description: Artifact commands assembly_mixed_units_cubes.kcl { "cmdId": "[uuid]", "range": [ - 263, - 285, + 264, + 286, 4 ], "command": { diff --git a/rust/kcl-lib/tests/assembly_mixed_units_cubes/artifact_graph_flowchart.snap.md b/rust/kcl-lib/tests/assembly_mixed_units_cubes/artifact_graph_flowchart.snap.md index 40ca45918..8a2eae937 100644 --- a/rust/kcl-lib/tests/assembly_mixed_units_cubes/artifact_graph_flowchart.snap.md +++ b/rust/kcl-lib/tests/assembly_mixed_units_cubes/artifact_graph_flowchart.snap.md @@ -1,25 +1,25 @@ ```mermaid flowchart LR subgraph path2 [Path] - 2["Path
[76, 113, 3]"] - 3["Segment
[119, 136, 3]"] - 4["Segment
[142, 160, 3]"] - 5["Segment
[166, 184, 3]"] - 6["Segment
[190, 246, 3]"] - 7["Segment
[252, 259, 3]"] + 2["Path
[77, 114, 3]"] + 3["Segment
[120, 137, 3]"] + 4["Segment
[143, 161, 3]"] + 5["Segment
[167, 185, 3]"] + 6["Segment
[191, 247, 3]"] + 7["Segment
[253, 260, 3]"] 8[Solid2d] end subgraph path25 [Path] - 25["Path
[76, 111, 4]"] - 26["Segment
[117, 134, 4]"] - 27["Segment
[140, 158, 4]"] - 28["Segment
[164, 182, 4]"] - 29["Segment
[188, 244, 4]"] - 30["Segment
[250, 257, 4]"] + 25["Path
[77, 112, 4]"] + 26["Segment
[118, 135, 4]"] + 27["Segment
[141, 159, 4]"] + 28["Segment
[165, 183, 4]"] + 29["Segment
[189, 245, 4]"] + 30["Segment
[251, 258, 4]"] 31[Solid2d] end - 1["Plane
[47, 66, 3]"] - 9["Sweep Extrusion
[265, 287, 3]"] + 1["Plane
[48, 67, 3]"] + 9["Sweep Extrusion
[266, 288, 3]"] 10[Wall] 11[Wall] 12[Wall] @@ -34,8 +34,8 @@ flowchart LR 21["SweepEdge Adjacent"] 22["SweepEdge Opposite"] 23["SweepEdge Adjacent"] - 24["Plane
[47, 66, 4]"] - 32["Sweep Extrusion
[263, 285, 4]"] + 24["Plane
[48, 67, 4]"] + 32["Sweep Extrusion
[264, 286, 4]"] 33[Wall] 34[Wall] 35[Wall] diff --git a/rust/kcl-lib/tests/assembly_mixed_units_cubes/cube-inches.kcl b/rust/kcl-lib/tests/assembly_mixed_units_cubes/cube-inches.kcl index dbd2d6a10..5ee5b97e0 100644 --- a/rust/kcl-lib/tests/assembly_mixed_units_cubes/cube-inches.kcl +++ b/rust/kcl-lib/tests/assembly_mixed_units_cubes/cube-inches.kcl @@ -1,5 +1,6 @@ @settings(defaultLengthUnit = in) + sketch001 = startSketchOn('XY') cubeIn = startProfileAt([-10, -10], sketch001) |> xLine(length = 5) diff --git a/rust/kcl-lib/tests/assembly_mixed_units_cubes/cube-mm.kcl b/rust/kcl-lib/tests/assembly_mixed_units_cubes/cube-mm.kcl index 893cfcf4a..b89620f33 100644 --- a/rust/kcl-lib/tests/assembly_mixed_units_cubes/cube-mm.kcl +++ b/rust/kcl-lib/tests/assembly_mixed_units_cubes/cube-mm.kcl @@ -1,5 +1,6 @@ @settings(defaultLengthUnit = mm) + sketch001 = startSketchOn('XY') cubeMm = startProfileAt([10, 10], sketch001) |> xLine(length = 5) diff --git a/rust/kcl-lib/tests/assembly_mixed_units_cubes/ops.snap b/rust/kcl-lib/tests/assembly_mixed_units_cubes/ops.snap index ad2a89c10..e23fbff25 100644 --- a/rust/kcl-lib/tests/assembly_mixed_units_cubes/ops.snap +++ b/rust/kcl-lib/tests/assembly_mixed_units_cubes/ops.snap @@ -11,16 +11,16 @@ description: Operations executed assembly_mixed_units_cubes.kcl "value": "XY" }, "sourceRange": [ - 61, - 65, + 62, + 66, 3 ] } }, "name": "startSketchOn", "sourceRange": [ - 47, - 66, + 48, + 67, 3 ], "type": "StdLibCall", @@ -43,16 +43,16 @@ description: Operations executed assembly_mixed_units_cubes.kcl } }, "sourceRange": [ - 285, 286, + 287, 3 ] } }, "name": "extrude", "sourceRange": [ - 265, - 287, + 266, + 288, 3 ], "type": "StdLibCall", @@ -64,8 +64,8 @@ description: Operations executed assembly_mixed_units_cubes.kcl } }, "sourceRange": [ - 273, 274, + 275, 3 ] } @@ -78,16 +78,16 @@ description: Operations executed assembly_mixed_units_cubes.kcl "value": "XY" }, "sourceRange": [ - 61, - 65, + 62, + 66, 4 ] } }, "name": "startSketchOn", "sourceRange": [ - 47, - 66, + 48, + 67, 4 ], "type": "StdLibCall", @@ -110,16 +110,16 @@ description: Operations executed assembly_mixed_units_cubes.kcl } }, "sourceRange": [ - 283, 284, + 285, 4 ] } }, "name": "extrude", "sourceRange": [ - 263, - 285, + 264, + 286, 4 ], "type": "StdLibCall", @@ -131,8 +131,8 @@ description: Operations executed assembly_mixed_units_cubes.kcl } }, "sourceRange": [ - 271, 272, + 273, 4 ] } diff --git a/rust/kcl-lib/tests/assembly_non_default_units/artifact_commands.snap b/rust/kcl-lib/tests/assembly_non_default_units/artifact_commands.snap index 672783ebf..90ce8dc10 100644 --- a/rust/kcl-lib/tests/assembly_non_default_units/artifact_commands.snap +++ b/rust/kcl-lib/tests/assembly_non_default_units/artifact_commands.snap @@ -80,8 +80,8 @@ description: Artifact commands assembly_non_default_units.kcl { "cmdId": "[uuid]", "range": [ - 172, - 191, + 173, + 192, 3 ], "command": { @@ -109,8 +109,8 @@ description: Artifact commands assembly_non_default_units.kcl { "cmdId": "[uuid]", "range": [ - 197, - 232, + 198, + 233, 3 ], "command": { @@ -129,8 +129,8 @@ description: Artifact commands assembly_non_default_units.kcl { "cmdId": "[uuid]", "range": [ - 197, - 232, + 198, + 233, 3 ], "command": { @@ -140,8 +140,8 @@ description: Artifact commands assembly_non_default_units.kcl { "cmdId": "[uuid]", "range": [ - 197, - 232, + 198, + 233, 3 ], "command": { @@ -157,8 +157,8 @@ description: Artifact commands assembly_non_default_units.kcl { "cmdId": "[uuid]", "range": [ - 197, - 232, + 198, + 233, 3 ], "command": { @@ -168,8 +168,8 @@ description: Artifact commands assembly_non_default_units.kcl { "cmdId": "[uuid]", "range": [ - 197, - 232, + 198, + 233, 3 ], "command": { @@ -197,8 +197,8 @@ description: Artifact commands assembly_non_default_units.kcl { "cmdId": "[uuid]", "range": [ - 197, - 232, + 198, + 233, 3 ], "command": { diff --git a/rust/kcl-lib/tests/assembly_non_default_units/artifact_graph_flowchart.snap.md b/rust/kcl-lib/tests/assembly_non_default_units/artifact_graph_flowchart.snap.md index dd526491a..e48467a01 100644 --- a/rust/kcl-lib/tests/assembly_non_default_units/artifact_graph_flowchart.snap.md +++ b/rust/kcl-lib/tests/assembly_non_default_units/artifact_graph_flowchart.snap.md @@ -1,8 +1,8 @@ ```mermaid flowchart LR subgraph path2 [Path] - 2["Path
[197, 232, 3]"] - 3["Segment
[197, 232, 3]"] + 2["Path
[198, 233, 3]"] + 3["Segment
[198, 233, 3]"] 4[Solid2d] end subgraph path6 [Path] @@ -10,7 +10,7 @@ flowchart LR 7["Segment
[114, 149, 4]"] 8[Solid2d] end - 1["Plane
[172, 191, 3]"] + 1["Plane
[173, 192, 3]"] 5["Plane
[89, 108, 4]"] 1 --- 2 2 --- 3 diff --git a/rust/kcl-lib/tests/assembly_non_default_units/globals.kcl b/rust/kcl-lib/tests/assembly_non_default_units/globals.kcl index ff6cb9c25..9eb29e200 100644 --- a/rust/kcl-lib/tests/assembly_non_default_units/globals.kcl +++ b/rust/kcl-lib/tests/assembly_non_default_units/globals.kcl @@ -1,3 +1,4 @@ @settings(defaultLengthUnit = in) + export radius = 1 diff --git a/rust/kcl-lib/tests/assembly_non_default_units/ops.snap b/rust/kcl-lib/tests/assembly_non_default_units/ops.snap index d677a69dc..cef9ed660 100644 --- a/rust/kcl-lib/tests/assembly_non_default_units/ops.snap +++ b/rust/kcl-lib/tests/assembly_non_default_units/ops.snap @@ -11,16 +11,16 @@ description: Operations executed assembly_non_default_units.kcl "value": "XZ" }, "sourceRange": [ - 186, - 190, + 187, + 191, 3 ] } }, "name": "startSketchOn", "sourceRange": [ - 172, - 191, + 173, + 192, 3 ], "type": "StdLibCall", diff --git a/rust/kcl-lib/tests/assembly_non_default_units/other1.kcl b/rust/kcl-lib/tests/assembly_non_default_units/other1.kcl index f7949842e..779b2fcf8 100644 --- a/rust/kcl-lib/tests/assembly_non_default_units/other1.kcl +++ b/rust/kcl-lib/tests/assembly_non_default_units/other1.kcl @@ -1,5 +1,6 @@ @settings(defaultLengthUnit = in) + // This is not used, but it triggers the problem. import radius from "globals.kcl" diff --git a/rust/kcl-lib/tests/import_cycle1/import_cycle2.kcl b/rust/kcl-lib/tests/import_cycle1/import_cycle2.kcl index 0881fb91f..7f3b41cd2 100644 --- a/rust/kcl-lib/tests/import_cycle1/import_cycle2.kcl +++ b/rust/kcl-lib/tests/import_cycle1/import_cycle2.kcl @@ -1,4 +1,6 @@ @settings(defaultLengthUnit = mm) import three from "import_cycle3.kcl" -export fn two = () => { return three() - 1 } +export fn two() { + return three() - 1 +} diff --git a/rust/kcl-lib/tests/import_cycle1/import_cycle3.kcl b/rust/kcl-lib/tests/import_cycle1/import_cycle3.kcl index cf4a50cbd..6e9867887 100644 --- a/rust/kcl-lib/tests/import_cycle1/import_cycle3.kcl +++ b/rust/kcl-lib/tests/import_cycle1/import_cycle3.kcl @@ -1,4 +1,6 @@ @settings(defaultLengthUnit = in) import one from "input.kcl" -export fn three = () => { return one() + one() + one() } +export fn three() { + return one() + one() + one() +} diff --git a/rust/kcl-lib/tests/import_function_not_sketch/my_functions.kcl b/rust/kcl-lib/tests/import_function_not_sketch/my_functions.kcl index 56e71bbf0..047c574e2 100644 --- a/rust/kcl-lib/tests/import_function_not_sketch/my_functions.kcl +++ b/rust/kcl-lib/tests/import_function_not_sketch/my_functions.kcl @@ -1,5 +1,6 @@ @settings(defaultLengthUnit = mm) + export part001 = startSketchOn('XY') |> startProfileAt([4, 12], %) |> line(end = [2, 0]) @@ -12,4 +13,6 @@ export part001 = startSketchOn('XY') |> close() |> revolve({ axis = 'y' }, %) // default angle is 360 -export fn two = () => { return 5 } +export fn two() { + return 5 +} diff --git a/rust/kcl-lib/tests/import_function_not_sketch/ops.snap b/rust/kcl-lib/tests/import_function_not_sketch/ops.snap index 8841da17a..ab5a2fcd4 100644 --- a/rust/kcl-lib/tests/import_function_not_sketch/ops.snap +++ b/rust/kcl-lib/tests/import_function_not_sketch/ops.snap @@ -11,16 +11,16 @@ description: Operations executed import_function_not_sketch.kcl "value": "XY" }, "sourceRange": [ - 66, - 70, + 67, + 71, 3 ] } }, "name": "startSketchOn", "sourceRange": [ - 52, - 71, + 53, + 72, 3 ], "type": "StdLibCall", @@ -39,8 +39,8 @@ description: Operations executed import_function_not_sketch.kcl } }, "sourceRange": [ - 312, - 326, + 313, + 327, 3 ] }, @@ -52,16 +52,16 @@ description: Operations executed import_function_not_sketch.kcl } }, "sourceRange": [ - 328, 329, + 330, 3 ] } }, "name": "revolve", "sourceRange": [ - 304, - 330, + 305, + 331, 3 ], "type": "StdLibCall", diff --git a/rust/kcl-lib/tests/import_side_effect/export_side_effect.kcl b/rust/kcl-lib/tests/import_side_effect/export_side_effect.kcl index 890fc7b63..44f1a307b 100644 --- a/rust/kcl-lib/tests/import_side_effect/export_side_effect.kcl +++ b/rust/kcl-lib/tests/import_side_effect/export_side_effect.kcl @@ -1,4 +1,6 @@ -export fn foo = () => { return 0 } +export fn foo() { + return 0 +} // This interacts with the engine. part001 = startSketchOn('XY') diff --git a/rust/kcl-lib/tests/import_side_effect/ops.snap b/rust/kcl-lib/tests/import_side_effect/ops.snap index 378a44805..b8ac58ae7 100644 --- a/rust/kcl-lib/tests/import_side_effect/ops.snap +++ b/rust/kcl-lib/tests/import_side_effect/ops.snap @@ -1,5 +1,5 @@ --- -source: kcl/src/simulation_tests.rs +source: kcl-lib/src/simulation_tests.rs description: Operations executed import_side_effect.kcl --- [ @@ -11,16 +11,16 @@ description: Operations executed import_side_effect.kcl "value": "XY" }, "sourceRange": [ + 91, 95, - 99, 3 ] } }, "name": "startSketchOn", "sourceRange": [ - 81, - 100, + 77, + 96, 3 ], "type": "StdLibCall", diff --git a/rust/kcl-python-bindings/src/lib.rs b/rust/kcl-python-bindings/src/lib.rs index ba0f6685a..53911ffc4 100644 --- a/rust/kcl-python-bindings/src/lib.rs +++ b/rust/kcl-python-bindings/src/lib.rs @@ -490,7 +490,7 @@ async fn execute_code_and_export(code: String, export_format: FileExportFormat) .map_err(|err| pyo3::exceptions::PyException::new_err(err.to_string()))? } -/// Format the kcl code. +/// Format the kcl code. This will return the formatted code. #[pyfunction] fn format(code: String) -> PyResult { let program = kcl_lib::Program::parse_no_errs(&code).map_err(|err| into_miette_for_parse("", &code, err))?; @@ -499,6 +499,19 @@ fn format(code: String) -> PyResult { Ok(recasted) } +/// Format a whole directory of kcl code. +#[pyfunction] +async fn format_dir(dir: String) -> PyResult<()> { + tokio() + .spawn(async move { + kcl_lib::recast_dir(std::path::Path::new(&dir), &Default::default()) + .await + .map_err(|err| pyo3::exceptions::PyException::new_err(err.to_string())) + }) + .await + .map_err(|err| pyo3::exceptions::PyException::new_err(err.to_string()))? +} + /// Lint the kcl code. #[pyfunction] fn lint(code: String) -> PyResult> { @@ -528,6 +541,7 @@ fn kcl(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(execute_and_export, m)?)?; m.add_function(wrap_pyfunction!(execute_code_and_export, m)?)?; m.add_function(wrap_pyfunction!(format, m)?)?; + m.add_function(wrap_pyfunction!(format_dir, m)?)?; m.add_function(wrap_pyfunction!(lint, m)?)?; Ok(()) } diff --git a/rust/kcl-python-bindings/tests/tests.py b/rust/kcl-python-bindings/tests/tests.py index 5dba675de..b63b10d89 100755 --- a/rust/kcl-python-bindings/tests/tests.py +++ b/rust/kcl-python-bindings/tests/tests.py @@ -129,6 +129,9 @@ def test_kcl_format(): assert formatted_code is not None assert len(formatted_code) > 0 +@pytest.mark.asyncio +async def test_kcl_format_dir(): + await kcl.format_dir(walkie_talkie_dir) def test_kcl_lint(): # Read from a file.