-
Notifications
You must be signed in to change notification settings - Fork 220
/
build.rs
157 lines (131 loc) · 5 KB
/
build.rs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use rustc_version::{version, Version};
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::{env, fs};
fn check_rustc_version() {
assert!(
version().unwrap() >= Version::parse("1.66.0").unwrap(),
"The minimal supported rustc version is 1.66.0."
);
}
const GIT_COMMIT: &&str = &"GIT_COMMIT";
fn main() {
// Rebuild if the tests have changed
println!("cargo:rerun-if-changed=tests");
check_rustc_version();
// Only use build_data if the environment variable isn't set
// The environment variable is always set when working via Nix
if std::env::var(GIT_COMMIT).is_err() {
build_data::set_GIT_COMMIT();
build_data::set_GIT_DIRTY();
build_data::no_debug_rebuilds();
}
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_execution_success_tests(&mut test_file, &test_dir);
generate_compile_success_tests(&mut test_file, &test_dir);
generate_compile_failure_tests(&mut test_file, &test_dir);
}
fn generate_execution_success_tests(test_file: &mut File, test_data_dir: &Path) {
let test_sub_dir = "execution_success";
let test_data_dir = test_data_dir.join(test_sub_dir);
let test_case_dirs =
fs::read_dir(test_data_dir).unwrap().flatten().filter(|c| c.path().is_dir());
for test_dir in test_case_dirs {
let test_name =
test_dir.file_name().into_string().expect("Directory can't be converted to string");
if test_name.contains('-') {
panic!(
"Invalid test directory: {test_name}. Cannot include `-`, please convert to `_`"
);
};
let test_dir = &test_dir.path();
write!(
test_file,
r#"
#[test]
fn execution_success_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");
let mut cmd = Command::cargo_bin("nargo").unwrap();
cmd.arg("--program-dir").arg(test_program_dir);
cmd.arg("execute");
cmd.assert().success();
}}
"#,
test_dir = test_dir.display(),
)
.expect("Could not write templated test file.");
}
}
fn generate_compile_success_tests(test_file: &mut File, test_data_dir: &Path) {
let test_sub_dir = "compile_success";
let test_data_dir = test_data_dir.join(test_sub_dir);
let test_case_dirs =
fs::read_dir(test_data_dir).unwrap().flatten().filter(|c| c.path().is_dir());
for test_dir in test_case_dirs {
let test_name =
test_dir.file_name().into_string().expect("Directory can't be converted to string");
if test_name.contains('-') {
panic!(
"Invalid test directory: {test_name}. Cannot include `-`, please convert to `_`"
);
};
let test_dir = &test_dir.path();
write!(
test_file,
r#"
#[test]
fn compile_success_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");
let mut cmd = Command::cargo_bin("nargo").unwrap();
cmd.arg("--program-dir").arg(test_program_dir);
cmd.arg("info");
// `compile_success` tests should be able to compile down to an empty circuit.
cmd.assert().stdout(predicate::str::contains("Total ACIR opcodes generated for language PLONKCSat {{ width: 3 }}: 0"));
}}
"#,
test_dir = test_dir.display(),
)
.expect("Could not write templated test file.");
}
}
fn generate_compile_failure_tests(test_file: &mut File, test_data_dir: &Path) {
let test_sub_dir = "compile_failure";
let test_data_dir = test_data_dir.join(test_sub_dir);
let test_case_dirs =
fs::read_dir(test_data_dir).unwrap().flatten().filter(|c| c.path().is_dir());
for test_dir in test_case_dirs {
let test_name =
test_dir.file_name().into_string().expect("Directory can't be converted to string");
if test_name.contains('-') {
panic!(
"Invalid test directory: {test_name}. Cannot include `-`, please convert to `_`"
);
};
let test_dir = &test_dir.path();
write!(
test_file,
r#"
#[test]
fn compile_failure_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");
let mut cmd = Command::cargo_bin("nargo").unwrap();
cmd.arg("--program-dir").arg(test_program_dir);
cmd.arg("execute");
cmd.assert().failure();
}}
"#,
test_dir = test_dir.display(),
)
.expect("Could not write templated test file.");
}
}