-
Notifications
You must be signed in to change notification settings - Fork 219
/
build.rs
472 lines (422 loc) · 15.1 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::{env, fs};
const GIT_COMMIT: &&str = &"GIT_COMMIT";
fn main() {
// Only use build_data if the environment variable isn't set.
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 root_dir = match std::env::var("CARGO_MANIFEST_DIR") {
Ok(dir) => PathBuf::from(dir).parent().unwrap().parent().unwrap().to_path_buf(),
Err(_) => std::env::current_dir().unwrap(),
};
let test_dir = root_dir.join("test_programs");
// Rebuild if the tests have changed
println!("cargo:rerun-if-changed=tests");
println!("cargo:rerun-if-changed={}", test_dir.as_os_str().to_str().unwrap());
generate_execution_success_tests(&mut test_file, &test_dir);
generate_execution_failure_tests(&mut test_file, &test_dir);
generate_noir_test_success_tests(&mut test_file, &test_dir);
generate_noir_test_failure_tests(&mut test_file, &test_dir);
generate_compile_success_empty_tests(&mut test_file, &test_dir);
generate_compile_success_contract_tests(&mut test_file, &test_dir);
generate_compile_success_no_bug_tests(&mut test_file, &test_dir);
generate_compile_failure_tests(&mut test_file, &test_dir);
}
/// Some tests are explicitly ignored in brillig due to them failing.
/// These should be fixed and removed from this list.
const IGNORED_BRILLIG_TESTS: [&str; 11] = [
// Takes a very long time to execute as large loops do not get simplified.
"regression_4709",
// bit sizes for bigint operation doesn't match up.
"bigint",
// ICE due to looking for function which doesn't exist.
"fold_after_inlined_calls",
"fold_basic",
"fold_basic_nested_call",
"fold_call_witness_condition",
"fold_complex_outputs",
"fold_distinct_return",
"fold_fibonacci",
"fold_numeric_generic_poseidon",
// Expected to fail as test asserts on which runtime it is in.
"is_unconstrained",
];
/// Tests which aren't expected to work with the default inliner cases.
const INLINER_MIN_OVERRIDES: [(&str, i64); 1] = [
// 0 works if PoseidonHasher::write is tagged as `inline_always`, otherwise 22.
("eddsa", 0),
];
/// Some tests are expected to have warnings
/// These should be fixed and removed from this list.
const TESTS_WITH_EXPECTED_WARNINGS: [&str; 2] = [
// TODO(https://github.com/noir-lang/noir/issues/6238): remove from list once issue is closed
"brillig_cast",
// TODO(https://github.com/noir-lang/noir/issues/6238): remove from list once issue is closed
"macros_in_comptime",
];
fn read_test_cases(
test_data_dir: &Path,
test_sub_dir: &str,
) -> impl Iterator<Item = (String, PathBuf)> {
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());
test_case_dirs.into_iter().map(|dir| {
let test_name =
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 `_`"
);
}
(test_name, dir.path())
})
}
#[derive(Default)]
struct MatrixConfig {
// Only used with execution, and only on selected tests.
vary_brillig: bool,
// Only seems to have an effect on the `execute_success` cases.
vary_inliner: bool,
// If there is a non-default minimum inliner aggressiveness to use with the brillig tests.
min_inliner: i64,
}
// Enum to be able to preserve readable test labels and also compare to numbers.
enum Inliner {
Min,
Default,
Max,
Custom(i64),
}
impl Inliner {
fn value(&self) -> i64 {
match self {
Inliner::Min => i64::MIN,
Inliner::Default => 0,
Inliner::Max => i64::MAX,
Inliner::Custom(i) => *i,
}
}
fn label(&self) -> String {
match self {
Inliner::Min => "i64::MIN".to_string(),
Inliner::Default => "0".to_string(),
Inliner::Max => "i64::MAX".to_string(),
Inliner::Custom(i) => i.to_string(),
}
}
}
/// Generate all test cases for a given test name (expected to be unique for the test directory),
/// based on the matrix configuration. These will be executed serially, but concurrently with
/// other test directories. Running multiple tests on the same directory would risk overriding
/// each others compilation artifacts, which is why this method injects a mutex shared by
/// all cases in the test matrix, as long as the test name and directory has a 1-to-1 relationship.
fn generate_test_cases(
test_file: &mut File,
test_name: &str,
test_dir: &std::path::Display,
test_command: &str,
test_content: &str,
matrix_config: &MatrixConfig,
) {
let brillig_cases = if matrix_config.vary_brillig { vec![false, true] } else { vec![false] };
let inliner_cases = if matrix_config.vary_inliner {
let mut cases = vec![Inliner::Min, Inliner::Default, Inliner::Max];
if !cases.iter().any(|c| c.value() == matrix_config.min_inliner) {
cases.push(Inliner::Custom(matrix_config.min_inliner));
}
cases
} else {
vec![Inliner::Default]
};
// We can't use a `#[test_matrix(brillig_cases, inliner_cases)` if we only want to limit the
// aggressiveness range for the brillig tests, and let them go full range on the ACIR case.
let mut test_cases = Vec::new();
for brillig in &brillig_cases {
for inliner in &inliner_cases {
if *brillig && inliner.value() < matrix_config.min_inliner {
continue;
}
test_cases.push(format!("#[test_case::test_case({brillig}, {})]", inliner.label()));
}
}
let test_cases = test_cases.join("\n");
// We need to isolate test cases in the same group, otherwise they overwrite each other's artifacts.
// On CI we use `cargo nextest`, which runs tests in different processes; for this we use a file lock.
// Locally we might be using `cargo test`, which run tests in the same process; in this case the file lock
// wouldn't work, becuase the process itself has the lock, and it looks like it can have N instances without
// any problems; for this reason we also use a `Mutex`.
let mutex_name = format! {"TEST_MUTEX_{}", test_name.to_uppercase()};
write!(
test_file,
r#"
lazy_static::lazy_static! {{
/// Prevent concurrent tests in the matrix from overwriting the compilation artifacts in {test_dir}
static ref {mutex_name}: std::sync::Mutex<()> = std::sync::Mutex::new(());
}}
{test_cases}
fn test_{test_name}(force_brillig: bool, inliner_aggressiveness: i64) {{
let test_program_dir = PathBuf::from("{test_dir}");
// Ignore poisoning errors if some of the matrix cases failed.
let mutex_guard = {mutex_name}.lock().unwrap_or_else(|e| e.into_inner());
let file_guard = file_lock::FileLock::lock(
test_program_dir.join("Nargo.toml"),
true,
file_lock::FileOptions::new().read(true).write(true).append(true)
).expect("failed to lock Nargo.toml");
let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(test_program_dir);
nargo.arg("{test_command}").arg("--force");
nargo.arg("--inliner-aggressiveness").arg(inliner_aggressiveness.to_string());
if force_brillig {{
nargo.arg("--force-brillig");
}}
{test_content}
drop(file_guard);
drop(mutex_guard);
}}
"#
)
.expect("Could not write templated test file.");
}
fn generate_execution_success_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "execution_success";
let test_cases = read_test_cases(test_data_dir, test_type);
writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();
generate_test_cases(
test_file,
&test_name,
&test_dir,
"execute",
r#"
nargo.assert().success();
"#,
&MatrixConfig {
vary_brillig: !IGNORED_BRILLIG_TESTS.contains(&test_name.as_str()),
vary_inliner: true,
min_inliner: INLINER_MIN_OVERRIDES
.iter()
.find(|(n, _)| *n == test_name.as_str())
.map(|(_, i)| *i)
.unwrap_or(i64::MIN),
},
);
}
writeln!(test_file, "}}").unwrap();
}
fn generate_execution_failure_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "execution_failure";
let test_cases = read_test_cases(test_data_dir, test_type);
writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();
generate_test_cases(
test_file,
&test_name,
&test_dir,
"execute",
r#"
nargo.assert().failure().stderr(predicate::str::contains("The application panicked (crashed).").not());
"#,
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
}
fn generate_noir_test_success_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "noir_test_success";
let test_cases = read_test_cases(test_data_dir, "noir_test_success");
writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();
generate_test_cases(
test_file,
&test_name,
&test_dir,
"test",
r#"
nargo.assert().success();
"#,
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
}
fn generate_noir_test_failure_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "noir_test_failure";
let test_cases = read_test_cases(test_data_dir, test_type);
writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();
generate_test_cases(
test_file,
&test_name,
&test_dir,
"test",
r#"
nargo.assert().failure();
"#,
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
}
fn generate_compile_success_empty_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "compile_success_empty";
let test_cases = read_test_cases(test_data_dir, test_type);
writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();
let mut assert_zero_opcodes = r#"
let output = nargo.output().expect("Failed to execute command");
if !output.status.success() {{
panic!("`nargo info` failed with: {}", String::from_utf8(output.stderr).unwrap_or_default());
}}
"#.to_string();
if !TESTS_WITH_EXPECTED_WARNINGS.contains(&test_name.as_str()) {
assert_zero_opcodes += r#"
nargo.assert().success().stderr(predicate::str::contains("warning:").not());
"#;
}
assert_zero_opcodes += r#"
// `compile_success_empty` tests should be able to compile down to an empty circuit.
let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap_or_else(|e| {{
panic!("JSON was not well-formatted {:?}\n\n{:?}", e, std::str::from_utf8(&output.stdout))
}});
let num_opcodes = &json["programs"][0]["functions"][0]["opcodes"];
assert_eq!(num_opcodes.as_u64().expect("number of opcodes should fit in a u64"), 0);
"#;
generate_test_cases(
test_file,
&test_name,
&test_dir,
"info",
&format!(
r#"
nargo.arg("--json");
{assert_zero_opcodes}
"#,
),
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
}
fn generate_compile_success_contract_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "compile_success_contract";
let test_cases = read_test_cases(test_data_dir, test_type);
writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();
generate_test_cases(
test_file,
&test_name,
&test_dir,
"compile",
r#"
nargo.assert().success().stderr(predicate::str::contains("warning:").not());
"#,
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
}
/// Generate tests for checking that the contract compiles and there are no "bugs" in stderr
fn generate_compile_success_no_bug_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "compile_success_no_bug";
let test_cases = read_test_cases(test_data_dir, test_type);
writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();
generate_test_cases(
test_file,
&test_name,
&test_dir,
"compile",
r#"
nargo.assert().success().stderr(predicate::str::contains("bug:").not());
"#,
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
}
fn generate_compile_failure_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "compile_failure";
let test_cases = read_test_cases(test_data_dir, test_type);
writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();
generate_test_cases(
test_file,
&test_name,
&test_dir,
"compile",
r#"
nargo.assert().failure().stderr(predicate::str::contains("The application panicked (crashed).").not());
"#,
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
}