forked from noir-lang/noir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
96 lines (76 loc) · 3.33 KB
/
Copy pathbuild.rs
File metadata and controls
96 lines (76 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
fn main() {
let out_dir = std::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("tooling").join("nargo_fmt"),
};
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 =
std::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 config = input_source
.lines()
.filter_map(|line| line.strip_prefix("//@"))
.collect::<Vec<_>>()
.join("\n");
let output_source_path = outputs_dir.join(file_name).display().to_string();
let output_source =
std::fs::read_to_string(output_source_path.clone()).unwrap_or_else(|_| {
panic!("expected output source at {output_source_path:?} was not found")
});
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_with_dummy_file(input);
let config = nargo_fmt::Config::of("{config}", &std::path::PathBuf::new()).unwrap();
let fmt_text = nargo_fmt::format(input, parsed_module, &config);
if std::env::var("UPDATE_EXPECT").is_ok() {{
std::fs::write("{output_source_path}", fmt_text.clone()).unwrap();
}}
similar_asserts::assert_eq!(fmt_text, expected_output);
}}
"##
)
.expect("Could not write templated test file.");
write!(
test_file,
r##"
#[test]
fn format_idempotent_{test_name}() {{
let expected_output = r#"{output_source}"#;
let (parsed_module, _errors) = noirc_frontend::parse_program_with_dummy_file(expected_output);
let config = nargo_fmt::Config::of("{config}", &std::path::PathBuf::new()).unwrap();
let fmt_text = nargo_fmt::format(expected_output, parsed_module, &config);
similar_asserts::assert_eq!(fmt_text, expected_output);
}}
"##
)
.expect("Could not write templated test file.");
}
}