From 03c7923ce7825b1f963c345edf236b62c8ff801c Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Fri, 6 Oct 2023 21:11:30 +0100 Subject: [PATCH] chore: codegen formatter test cases (#3006) --- tooling/nargo_fmt/build.rs | 68 +++++++++++++++++++ tooling/nargo_fmt/src/lib.rs | 48 ------------- tooling/nargo_fmt/tests/execute.rs | 5 ++ .../{nested-if-else.nr => nested_if_else.nr} | 0 .../{nested-if-else.nr => nested_if_else.nr} | 0 5 files changed, 73 insertions(+), 48 deletions(-) create mode 100644 tooling/nargo_fmt/build.rs create mode 100644 tooling/nargo_fmt/tests/execute.rs rename tooling/nargo_fmt/tests/expected/{nested-if-else.nr => nested_if_else.nr} (100%) rename tooling/nargo_fmt/tests/input/{nested-if-else.nr => nested_if_else.nr} (100%) diff --git a/tooling/nargo_fmt/build.rs b/tooling/nargo_fmt/build.rs new file mode 100644 index 00000000000..0ff39e99129 --- /dev/null +++ b/tooling/nargo_fmt/build.rs @@ -0,0 +1,68 @@ +use std::fs::File; +use std::io::Write; +use std::path::{Path, PathBuf}; +use std::{env, fs}; + +fn main() { + let out_dir = env::var("OUT_DIR").unwrap(); + let destination = Path::new(&out_dir).join("execute.rs"); + let mut test_file = File::create(destination).unwrap(); + + // Try to find the directory that Cargo sets when it is running; otherwise fallback to assuming the CWD + // is the root of the repository and append the crate path + let manifest_dir = match std::env::var("CARGO_MANIFEST_DIR") { + Ok(dir) => PathBuf::from(dir), + Err(_) => std::env::current_dir().unwrap().join("crates").join("nargo_cli"), + }; + let test_dir = manifest_dir.join("tests"); + + generate_formatter_tests(&mut test_file, &test_dir); +} + +fn generate_formatter_tests(test_file: &mut File, test_data_dir: &Path) { + let inputs_dir = test_data_dir.join("input"); + let outputs_dir = test_data_dir.join("expected"); + + let test_case_files = + fs::read_dir(inputs_dir).unwrap().flatten().filter(|c| c.path().is_file()); + + for file in test_case_files { + let file_path = file.path(); + let file_name = file_path.file_name().unwrap(); + let test_name = file_path.file_stem().unwrap().to_str().unwrap(); + + if test_name.contains('-') { + panic!( + "Invalid test directory: {test_name}. Cannot include `-`, please convert to `_`" + ); + }; + + let input_source_path = file.path(); + let input_source = std::fs::read_to_string(input_source_path).unwrap(); + + let output_source_path = outputs_dir.join(file_name); + let output_source = std::fs::read_to_string(output_source_path).unwrap(); + + write!( + test_file, + r##" +#[test] +fn format_{test_name}() {{ + let input = r#"{input_source}"#; + let expected_output = r#"{output_source}"#; + + + let (parsed_module, errors) = noirc_frontend::parse_program(&input); + assert!(errors.is_empty()); + + let config = nargo_fmt::Config::default(); + let fmt_text = nargo_fmt::format(&input, parsed_module, &config); + + + assert_eq!(fmt_text, expected_output); +}} + "## + ) + .expect("Could not write templated test file."); + } +} diff --git a/tooling/nargo_fmt/src/lib.rs b/tooling/nargo_fmt/src/lib.rs index 754449aa425..c938a70e7a5 100644 --- a/tooling/nargo_fmt/src/lib.rs +++ b/tooling/nargo_fmt/src/lib.rs @@ -33,51 +33,3 @@ pub fn format(source: &str, parsed_module: ParsedModule, config: &Config) -> Str fmt.visit_module(parsed_module); fmt.finish() } - -#[cfg(test)] -mod tests { - use std::{ffi::OsStr, path::PathBuf}; - - use crate::Config; - - #[test] - fn test() { - let files = std::fs::read_dir("tests/input").unwrap(); - for file in files { - let file = file.unwrap(); - - let config = Config::default(); - - let source_path = file.path(); - let source = std::fs::read_to_string(&source_path).unwrap(); - - let (parsed_module, errors) = noirc_frontend::parse_program(&source); - let fmt_text = crate::format(&source, parsed_module, &config); - - assert!(errors.is_empty()); - - let target_path: PathBuf = source_path - .components() - .map(|component| { - if component.as_os_str() == "input" { - OsStr::new("expected") - } else { - component.as_os_str() - } - }) - .collect(); - - let target = match std::fs::read_to_string(&target_path) { - Ok(t) => t, - Err(err) if err.kind() == std::io::ErrorKind::NotFound => { - std::fs::write(target_path, fmt_text.clone()).unwrap(); - fmt_text.clone() - } - Err(err) => unreachable!("{err}"), - }; - - // FIXME: better diff - assert_eq!(fmt_text, target); - } - } -} diff --git a/tooling/nargo_fmt/tests/execute.rs b/tooling/nargo_fmt/tests/execute.rs new file mode 100644 index 00000000000..d8802fd926b --- /dev/null +++ b/tooling/nargo_fmt/tests/execute.rs @@ -0,0 +1,5 @@ +#[cfg(test)] +mod tests { + // include tests generated by `build.rs` + include!(concat!(env!("OUT_DIR"), "/execute.rs")); +} diff --git a/tooling/nargo_fmt/tests/expected/nested-if-else.nr b/tooling/nargo_fmt/tests/expected/nested_if_else.nr similarity index 100% rename from tooling/nargo_fmt/tests/expected/nested-if-else.nr rename to tooling/nargo_fmt/tests/expected/nested_if_else.nr diff --git a/tooling/nargo_fmt/tests/input/nested-if-else.nr b/tooling/nargo_fmt/tests/input/nested_if_else.nr similarity index 100% rename from tooling/nargo_fmt/tests/input/nested-if-else.nr rename to tooling/nargo_fmt/tests/input/nested_if_else.nr