Update Rust tests to use internal KCL samples on CI (#7014)
* Update Rust tests to use internal KCL samples on CI * Regenerate manifest with internal KCL samples * try again Signed-off-by: Jess Frazelle <github@jessfraz.com> * remove the needs Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * features Signed-off-by: Jess Frazelle <github@jessfraz.com> * features Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * secret Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
use std::{
|
||||
fs::read_to_string,
|
||||
panic::{catch_unwind, AssertUnwindSafe},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
@ -21,7 +22,7 @@ struct Test {
|
||||
name: String,
|
||||
/// The name of the KCL file that's the entry point, e.g. "main.kcl", in the
|
||||
/// `input_dir`.
|
||||
entry_point: String,
|
||||
entry_point: PathBuf,
|
||||
/// Input KCL files are in this directory.
|
||||
input_dir: PathBuf,
|
||||
/// Expected snapshot output files are in this directory.
|
||||
@ -34,7 +35,7 @@ impl Test {
|
||||
fn new(name: &str) -> Self {
|
||||
Self {
|
||||
name: name.to_owned(),
|
||||
entry_point: "input.kcl".to_owned(),
|
||||
entry_point: Path::new("tests").join(name).join("input.kcl"),
|
||||
input_dir: Path::new("tests").join(name),
|
||||
output_dir: Path::new("tests").join(name),
|
||||
}
|
||||
@ -66,19 +67,12 @@ where
|
||||
settings.bind(f);
|
||||
}
|
||||
|
||||
fn read<P>(filename: &str, dir: P) -> String
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
std::fs::read_to_string(dir.as_ref().join(filename)).expect("Failed to read file: {filename}")
|
||||
}
|
||||
|
||||
fn parse(test_name: &str) {
|
||||
parse_test(&Test::new(test_name));
|
||||
}
|
||||
|
||||
fn parse_test(test: &Test) {
|
||||
let input = read(&test.entry_point, &test.input_dir);
|
||||
let input = read_to_string(&test.entry_point).unwrap();
|
||||
let tokens = crate::parsing::token::lex(&input, ModuleId::default()).unwrap();
|
||||
|
||||
// Parse the tokens into an AST.
|
||||
@ -98,7 +92,7 @@ async fn unparse(test_name: &str) {
|
||||
|
||||
async fn unparse_test(test: &Test) {
|
||||
// Parse into an AST
|
||||
let input = read(&test.entry_point, &test.input_dir);
|
||||
let input = read_to_string(&test.entry_point).unwrap();
|
||||
let tokens = crate::parsing::token::lex(&input, ModuleId::default()).unwrap();
|
||||
let ast = crate::parsing::parse_tokens(tokens).unwrap();
|
||||
|
||||
@ -111,10 +105,9 @@ async fn unparse_test(test: &Test) {
|
||||
}));
|
||||
|
||||
// Check all the rest of the files in the directory.
|
||||
let entry_point = test.input_dir.join(&test.entry_point);
|
||||
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 kcl_files = kcl_files.into_iter().filter(|f| f != &test.entry_point);
|
||||
let futures = kcl_files
|
||||
.into_iter()
|
||||
.filter(|file| file.extension().is_some_and(|ext| ext == "kcl")) // We only care about kcl
|
||||
@ -154,13 +147,11 @@ async fn execute(test_name: &str, render_to_png: bool) {
|
||||
}
|
||||
|
||||
async fn execute_test(test: &Test, render_to_png: bool, export_step: bool) {
|
||||
let input = read(&test.entry_point, &test.input_dir);
|
||||
let input = read_to_string(&test.entry_point).unwrap();
|
||||
let ast = crate::Program::parse_no_errs(&input).unwrap();
|
||||
|
||||
// Run the program.
|
||||
let exec_res =
|
||||
crate::test_server::execute_and_snapshot_ast(ast, Some(test.input_dir.join(&test.entry_point)), export_step)
|
||||
.await;
|
||||
let exec_res = crate::test_server::execute_and_snapshot_ast(ast, Some(test.entry_point.clone()), export_step).await;
|
||||
match exec_res {
|
||||
Ok((exec_state, env_ref, png, step)) => {
|
||||
let fail_path = test.output_dir.join("execution_error.snap");
|
||||
|
@ -23,7 +23,7 @@ lazy_static::lazy_static! {
|
||||
|
||||
#[kcl_directory_test_macro::test_all_dirs("../public/kcl-samples")]
|
||||
fn parse(dir_name: &str, dir_path: &Path) {
|
||||
let t = test(dir_name, dir_path.join("main.kcl").to_str().unwrap().to_owned());
|
||||
let t = test(dir_name, dir_path.join("main.kcl"));
|
||||
let write_new = matches!(
|
||||
std::env::var("INSTA_UPDATE").as_deref(),
|
||||
Ok("auto" | "always" | "new" | "unseen")
|
||||
@ -37,7 +37,7 @@ fn parse(dir_name: &str, dir_path: &Path) {
|
||||
|
||||
#[kcl_directory_test_macro::test_all_dirs("../public/kcl-samples")]
|
||||
async fn unparse(dir_name: &str, dir_path: &Path) {
|
||||
let t = test(dir_name, dir_path.join("main.kcl").to_str().unwrap().to_owned());
|
||||
let t = test(dir_name, dir_path.join("main.kcl"));
|
||||
unparse_test(&t).await;
|
||||
}
|
||||
|
||||
@ -71,7 +71,7 @@ async fn unparse_test(test: &Test) {
|
||||
|
||||
#[kcl_directory_test_macro::test_all_dirs("../public/kcl-samples")]
|
||||
async fn kcl_test_execute(dir_name: &str, dir_path: &Path) {
|
||||
let t = test(dir_name, dir_path.join("main.kcl").to_str().unwrap().to_owned());
|
||||
let t = test(dir_name, dir_path.join("main.kcl"));
|
||||
super::execute_test(&t, true, true).await;
|
||||
}
|
||||
|
||||
@ -129,12 +129,22 @@ fn test_after_engine_generate_manifest() {
|
||||
generate_kcl_manifest(&INPUTS_DIR).unwrap();
|
||||
}
|
||||
|
||||
fn test(test_name: &str, entry_point: String) -> Test {
|
||||
fn test(test_name: &str, entry_point: std::path::PathBuf) -> Test {
|
||||
let parent = std::fs::canonicalize(entry_point.parent().unwrap()).unwrap();
|
||||
let inputs_dir = std::fs::canonicalize(INPUTS_DIR.as_path()).unwrap();
|
||||
let relative_path = parent.strip_prefix(inputs_dir).unwrap();
|
||||
let output_dir = std::fs::canonicalize(OUTPUTS_DIR.as_path()).unwrap();
|
||||
let relative_output_dir = output_dir.join(relative_path);
|
||||
|
||||
// Ensure the output directory exists.
|
||||
if !relative_output_dir.exists() {
|
||||
std::fs::create_dir_all(&relative_output_dir).unwrap();
|
||||
}
|
||||
Test {
|
||||
name: test_name.to_owned(),
|
||||
entry_point,
|
||||
input_dir: INPUTS_DIR.join(test_name),
|
||||
output_dir: OUTPUTS_DIR.join(test_name),
|
||||
entry_point: entry_point.clone(),
|
||||
input_dir: parent.to_path_buf(),
|
||||
output_dir: relative_output_dir,
|
||||
}
|
||||
}
|
||||
|
||||
@ -173,8 +183,9 @@ fn kcl_samples_inputs() -> Vec<Test> {
|
||||
eprintln!("Found KCL sample: {:?}", dir_name.to_string_lossy());
|
||||
// Look for the entry point inside the directory.
|
||||
let sub_dir = INPUTS_DIR.join(dir_name);
|
||||
let entry_point = if sub_dir.join("main.kcl").exists() {
|
||||
"main.kcl".to_owned()
|
||||
let main_kcl_path = sub_dir.join("main.kcl");
|
||||
let entry_point = if main_kcl_path.exists() {
|
||||
main_kcl_path
|
||||
} else {
|
||||
panic!("No main.kcl found in {:?}", sub_dir);
|
||||
};
|
||||
|
Reference in New Issue
Block a user