Simplify the KCL stdlib test codegen (#3196)

This will ensure that the KCL snapshot tests all use the same logic, whether they're in `tests/executor/main.rs` or in the KCL stdlib modules.
This commit is contained in:
Adam Chalmers
2024-07-31 09:54:46 -05:00
committed by GitHub
parent 0df28abc4b
commit 115f2fdea2
18 changed files with 184 additions and 1231 deletions

View File

@ -1,8 +1,4 @@
use anyhow::Result;
use kcl_lib::{
executor::{ExecutorContext, ExecutorSettings},
settings::types::UnitLength,
};
use kcl_lib::{settings::types::UnitLength, test_server::execute_and_snapshot};
/// The minimum permissible difference between asserted twenty-twenty images.
/// i.e. how different the current model snapshot can be from the previous saved one.
@ -20,63 +16,6 @@ fn assert_out(test_name: &str, result: &image::DynamicImage) {
twenty_twenty::assert_image(format!("tests/executor/outputs/{test_name}.png"), result, MIN_DIFF);
}
async fn new_context(units: UnitLength) -> Result<ExecutorContext> {
let user_agent = concat!(env!("CARGO_PKG_NAME"), ".rs/", env!("CARGO_PKG_VERSION"),);
let http_client = reqwest::Client::builder()
.user_agent(user_agent)
// For file conversions we need this to be long.
.timeout(std::time::Duration::from_secs(600))
.connect_timeout(std::time::Duration::from_secs(60));
let ws_client = reqwest::Client::builder()
.user_agent(user_agent)
// For file conversions we need this to be long.
.timeout(std::time::Duration::from_secs(600))
.connect_timeout(std::time::Duration::from_secs(60))
.connection_verbose(true)
.tcp_keepalive(std::time::Duration::from_secs(600))
.http1_only();
let token = std::env::var("KITTYCAD_API_TOKEN").expect("KITTYCAD_API_TOKEN not set");
// Create the client.
let mut client = kittycad::Client::new_from_reqwest(token, http_client, ws_client);
// Set a local engine address if it's set.
if let Ok(addr) = std::env::var("LOCAL_ENGINE_ADDR") {
client.set_base_url(addr);
}
let ctx = ExecutorContext::new(
&client,
ExecutorSettings {
units,
highlight_edges: true,
enable_ssao: false,
show_grid: false,
},
)
.await?;
Ok(ctx)
}
/// Executes a kcl program and takes a snapshot of the result.
/// This returns the bytes of the snapshot.
async fn execute_and_snapshot(code: &str, units: UnitLength) -> Result<image::DynamicImage> {
let ctx = new_context(units).await?;
let tokens = kcl_lib::token::lexer(code)?;
let parser = kcl_lib::parser::Parser::new(tokens);
let program = parser.ast()?;
let snapshot = ctx.execute_and_prepare_snapshot(&program).await?;
// Create a temporary file to write the output to.
let output_file = std::env::temp_dir().join(format!("kcl_output_{}.png", uuid::Uuid::new_v4()));
// Save the snapshot locally, to that temporary file.
std::fs::write(&output_file, snapshot.contents.0)?;
// Decode the snapshot, return it.
let img = image::io::Reader::open(output_file).unwrap().decode()?;
Ok(img)
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_sketch_on_face() {
let code = kcl_input!("sketch_on_face");