Skip to content

Commit

Permalink
chore: codegen formatter test cases (#3006)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Oct 6, 2023
1 parent 9893dc7 commit 03c7923
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 48 deletions.
68 changes: 68 additions & 0 deletions tooling/nargo_fmt/build.rs
Original file line number Diff line number Diff line change
@@ -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.");
}
}
48 changes: 0 additions & 48 deletions tooling/nargo_fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
5 changes: 5 additions & 0 deletions tooling/nargo_fmt/tests/execute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[cfg(test)]
mod tests {
// include tests generated by `build.rs`
include!(concat!(env!("OUT_DIR"), "/execute.rs"));
}

0 comments on commit 03c7923

Please sign in to comment.