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
1155 lines (1036 loc) · 37.7 KB
/
Copy pathbuild.rs
File metadata and controls
1155 lines (1036 loc) · 37.7 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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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() -> Result<(), String> {
// Only use build_data if the environment variable isn't set.
if 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 env::var("CARGO_MANIFEST_DIR") {
Ok(dir) => PathBuf::from(dir).parent().unwrap().parent().unwrap().to_path_buf(),
Err(_) => 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_success_with_bug_tests(&mut test_file, &test_dir);
generate_compile_failure_tests(&mut test_file, &test_dir);
generate_minimal_execution_success_tests(&mut test_file, &test_dir);
generate_interpret_execution_success_tests(&mut test_file, &test_dir);
generate_interpret_execution_failure_tests(&mut test_file, &test_dir);
generate_comptime_interpret_execution_success_tests(&mut test_file, &test_dir);
generate_comptime_interpret_execution_failure_tests(&mut test_file, &test_dir);
generate_comptime_interpret_noir_test_success_tests(&mut test_file, &test_dir);
generate_comptime_interpret_noir_test_failure_tests(&mut test_file, &test_dir);
generate_brillig_small_stack_execution_success_tests(&mut test_file, &test_dir);
generate_fuzzing_failure_tests(&mut test_file, &test_dir);
generate_nargo_expand_execution_success_tests(&mut test_file, &test_dir);
generate_nargo_expand_compile_tests_with_ignore_list(
"compile_success_empty",
&mut test_file,
&test_dir,
&IGNORED_NARGO_EXPAND_COMPILE_SUCCESS_EMPTY_TESTS,
);
generate_nargo_expand_compile_tests("compile_success_contract", &mut test_file, &test_dir);
generate_nargo_expand_compile_tests_with_ignore_list(
"compile_success_no_bug",
&mut test_file,
&test_dir,
&IGNORED_NARGO_EXPAND_COMPILE_SUCCESS_NO_BUG_TESTS,
);
generate_nargo_expand_compile_tests_with_ignore_list(
"compile_success_with_bug",
&mut test_file,
&test_dir,
&IGNORED_NARGO_EXPAND_COMPILE_SUCCESS_WITH_BUG_TESTS,
);
Ok(())
}
/// Tests expected to fail with `--force-brillig --max-stack-frame-size 64`
/// because they need register spilling (not yet implemented).
/// Remove tests from this list as spilling is implemented.
const IGNORED_BRILLIG_SMALL_STACK_TESTS: [&str; 2] = [
// TODO: Enabling this would require an indirect call convention. We are returning more args than allowed in the stack.
// To enable this code we would need to pass/return call args through a pointer.
"brillig_block_parameter_liveness",
// This test relies on a specific inliner setting, while we only run
// the small stack tests with the default maximally aggressive inliner.
"reference_counts_vectors_inliner_0",
];
/// 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] = [
// 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",
// The output depends on function IDs of lambdas, and with --force-brillig we only get one kind.
"regression_10158",
];
/// Tests which aren't expected to work with the default minimum inliner cases.
const INLINER_MIN_OVERRIDES: [(&str, i64); 1] = [
// 0 works if PoseidonHasher::write is tagged as `inline_always`, otherwise 22.
("eddsa", 0),
];
/// Tests which aren't expected to work with the default maximum inliner cases.
const INLINER_MAX_OVERRIDES: [(&str, i64); 0] = [];
/// These tests should only be run on exactly 1 inliner setting (the one given here)
const INLINER_OVERRIDES: [(&str, i64); 4] = [
("reference_counts_inliner_0", 0),
("reference_counts_inliner_min", i64::MIN),
("reference_counts_inliner_max", i64::MAX),
("reference_counts_vectors_inliner_0", 0),
];
/// Some tests are expected to have warnings
/// These should be fixed and removed from this list.
const TESTS_WITH_EXPECTED_WARNINGS: [&str; 6] = [
// 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",
// We issue a "experimental feature" warning for all enums until they're stabilized
"enums",
"comptime_enums",
// Testing unreachable instructions
"brillig_continue_break",
// Expected - tests the `std::meta::warn` builtin
"comptime_user_warning",
];
/// `nargo interpret` ignored tests, either because they don't currently work or
/// because they are too slow to run.
const IGNORED_INTERPRET_EXECUTION_TESTS: [&str; 2] = [
// slow
"regression_4709",
// Doesn't match Brillig, but the expected ref-count of 5 has comments which
// suggest it's not exactly clear why we get that exact value anyway.
"reference_counts_inliner_max",
];
const IGNORED_COMPTIME_INTERPRET_EXECUTION_TESTS: [&str; 0] = [];
/// `nargo execute --force-comptime` ignored tests because of bugs or because some
/// programs don't behave the same way in comptime (for example: reference counting).
const PANICKING_COMPTIME_INTERPRET_EXECUTION_TESTS: [&str; 6] = [
// These check reference counts, which aren't tracked in comptime code
"reference_counts_inliner_0",
"reference_counts_inliner_max",
"reference_counts_inliner_min",
"reference_counts_vectors_inliner_0",
// Enums (and `match`) are currently unsupported in comptime code
"regression_7323",
"match_struct_pattern_field_order",
];
const PANICKING_COMPTIME_INTERPRET_EXECUTION_FAILURE_TESTS: [&str; 0] = [];
const IGNORED_COMPTIME_INTERPRET_EXECUTION_FAILURE_TESTS: [&str; 0] = [];
/// We usually check that the stdout of `nargo execute --force-comptime` matches
/// that of `nargo execute`, but in some cases the output doesn't match and it's not clear
/// this can be solved.
/// There are two Noir types that show out differently in comptime: functions and references.
const IGNORED_COMPTIME_INTERPRET_EXECUTION_STDOUT_CHECK_TESTS: [&str; 4] =
["debug_logs", "regression_10156", "regression_10158", "regression_9578"];
const IGNORED_COMPTIME_INTERPRET_NOIR_TESTS: [&str; 1] = [
// For some reason at comptime a `comptime` function is considered a constant.
"comptime_globals",
];
/// `nargo execute --minimal-ssa` ignored tests
const IGNORED_MINIMAL_EXECUTION_TESTS: [&str; 18] = [
// internal error: entered unreachable code: unsupported function call type Intrinsic(AssertConstant)
// These tests contain calls to `assert_constant`, which are evaluated and removed in the full SSA
// pipeline, but in the minimal they are untouched, and trying to remove them causes a failure because
// we don't have the other passes that would turn expressions into constants.
"array_to_vector_constant_length",
"static_assert_empty_loop",
"brillig_cow_regression",
"brillig_pedersen",
"import",
"merkle_insert",
"pedersen_check",
"pedersen_hash",
"pedersen_commitment",
"simple_shield",
"strings",
// The minimal SSA pipeline only works with Brillig: \'zeroed_lambda\' needs to be unconstrained
"conditional_black_box_function_pointer_call",
"lambda_from_dynamic_if",
"regression_10156",
// The constrained foreign-function proxy can't run in the Brillig-only minimal pipeline.
"regression_foreign_proxy_generic",
// This relies on maximum inliner setting
"reference_counts_inliner_max",
"reference_counts_inliner_min",
"reference_counts_inliner_0",
];
/// These tests are ignored because making them work involves a more complex test code that
/// might not be worth it.
/// Others are ignored because of existing bugs in `nargo expand`.
/// As the bugs are fixed these tests should be removed from this list.
const IGNORED_NARGO_EXPAND_EXECUTION_TESTS: [&str; 12] = [
// `nargo expand` prints an associated-constant access by its bare name (e.g. `N`),
// dropping the `Box::<Field>::` qualifier, so the expanded source no longer resolves.
"comptime_resolve_associated_constant_scope",
// There's nothing special about this program but making it work with a custom entry would involve
// having to parse the Nargo.toml file, etc., which is not worth it
"custom_entry",
// There's no "src/main.nr" here so it's trickier to make this work
"diamond_deps_0",
// bug
"numeric_type_alias",
"negative_associated_constants",
// There's no "src/main.nr" here so it's trickier to make this work
"overlapping_dep_and_mod",
// bug
"regression_9116",
// bug
"regression_10466",
// bug
"trait_associated_constant",
// Globals evaluate to invalid utf-8 which don't display correctly in a source file
"regression_12269",
// There's no "src/main.nr" here so it's trickier to make this work
"workspace",
// There's no "src/main.nr" here so it's trickier to make this work
"workspace_default_member",
];
/// Tests for which we don't check that stdout matches the expected output.
const TESTS_WITHOUT_STDOUT_CHECK: [&str; 0] = [];
/// These tests are ignored because of existing bugs in `nargo expand`.
/// As the bugs are fixed these tests should be removed from this list.
/// (some are ignored on purpose for the same reason as `IGNORED_NARGO_EXPAND_EXECUTION_TESTS`)
const IGNORED_NARGO_EXPAND_COMPILE_SUCCESS_EMPTY_TESTS: [&str; 11] = [
// A generated associated constant resolves to `<[T; N] as Ser>::N`, which `nargo expand`
// prints as `<(resolved type) as Ser>::N` — not valid syntax to recompile.
"regression_10747_associated_constant",
// There's no "src/main.nr" here so it's trickier to make this work
"overlapping_dep_and_mod",
// this one works, but copying its `Nargo.toml` file to somewhere else doesn't work
// because it references another project by a relative path
"reexports",
// bug
"trait_function_calls",
// bug
"trait_method_mut_self",
// bug
"trait_static_methods",
// There's no "src/main.nr" here so it's trickier to make this work
"workspace_reexport_bug",
// bug
"trait_call_in_global",
// `nargo expand` drops the trait generic arguments on `impl Trait<...>` parameters
"regression_7648",
// The expanded code names a transitive-only dependency (`leaflib`) by path, which isn't
// directly importable when the expansion is recompiled as a standalone program.
"comptime_as_typed_expr_public_type_trait_method",
// The expanded code names a transitive-only dependency (`leaflib`) by path, which isn't
// directly importable when the expansion is recompiled as a standalone program.
"comptime_transitive_public_dependency_type",
];
/// These tests are ignored because of existing bugs in `nargo expand`.
/// As the bugs are fixed these tests should be removed from this list.
const IGNORED_NARGO_EXPAND_COMPILE_SUCCESS_NO_BUG_TESTS: [&str; 17] = [
"noirc_frontend_tests_check_trait_as_type_as_fn_parameter",
"noirc_frontend_tests_check_trait_as_type_as_two_fn_parameters",
"noirc_frontend_tests_enums_match_on_empty_enum",
"noirc_frontend_tests_traits_trait_alias_polymorphic_inheritance",
"noirc_frontend_tests_traits_trait_alias_single_member",
"noirc_frontend_tests_traits_trait_alias_two_members",
"noirc_frontend_tests_traits_trait_impl_with_where_clause_with_trait_with_associated_numeric",
"noirc_frontend_tests_traits_accesses_associated_type_inside_trait_impl_using_self",
"noirc_frontend_tests_traits_accesses_associated_type_inside_trait_using_self",
"noirc_frontend_tests_u32_globals_as_sizes_in_types",
// This creates a struct at comptime which, expanded, gives a visibility error
"noirc_frontend_tests_visibility_visibility_bug_inside_comptime",
"noirc_frontend_tests_aliases_identity_numeric_type_alias_works",
"noirc_frontend_tests_aliases_type_alias_to_numeric_as_generic",
"noirc_frontend_tests_aliases_type_alias_to_numeric_generic",
"noirc_frontend_tests_traits_trait_bound_on_implementing_type",
"function_registry",
"regression_10887", // expands into global struct with private fields
];
const IGNORED_NARGO_EXPAND_COMPILE_SUCCESS_WITH_BUG_TESTS: [&str; 0] = [];
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);
// A missing directory means the test category currently has no tests, so we yield nothing
// rather than panicking. The inner `flatten` also skips any entry that fails to read.
let test_case_dirs =
fs::read_dir(test_data_dir).into_iter().flatten().flatten().filter(|c| c.path().is_dir());
test_case_dirs.into_iter().filter_map(|dir| {
// When switching git branches we might end up with non-empty directories that have a `target`
// directory inside them but no `Nargo.toml`.
// These "tests" would always fail, but it's okay to ignore them so we do that here.
if !dir.path().join("Nargo.toml").exists() {
return None;
}
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 `_`"
);
}
Some((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,
// If there is a non-default maximum inliner aggressiveness to use with the brillig tests.
max_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.
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));
}
if !cases.iter().any(|c| c.value() == matrix_config.max_inliner) {
cases.push(Inliner::Custom(matrix_config.max_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 {
let inliner_range = matrix_config.min_inliner..=matrix_config.max_inliner;
if *brillig && !inliner_range.contains(&inliner.value()) {
continue;
}
test_cases.push(format!(
"#[test_case::test_case(ForceBrillig({brillig}), Inliner({}))]",
inliner.label()
));
}
}
let test_cases = test_cases.join("\n");
write!(
test_file,
r#"
{test_cases}
fn test_{test_name}(force_brillig: ForceBrillig, inliner_aggressiveness: Inliner) {{
let test_program_dir = PathBuf::from("{test_dir}");
#[allow(unused_variables)]
let runtime = if force_brillig.0 {{
Runtime::Brillig
}} else {{
Runtime::Acir
}};
#[allow(unused_mut)]
let (mut nargo, target_dir) = setup_nargo_command(&test_program_dir, "{test_command}", force_brillig, inliner_aggressiveness);
{test_content}
drop(target_dir);
}}
"#
)
.expect("Could not write templated test file.");
}
/// Generate fuzzing tests, where the noir program is fuzzed with one thread for 120 seconds.
/// We expect that a failure is found in that time
fn generate_fuzzing_test_case(
test_file: &mut File,
test_name: &str,
test_dir: &std::path::Display,
test_content: &str,
timeout: usize,
) {
let timeout_str = timeout.to_string();
write!(
test_file,
r#"
#[test]
fn test_{test_name}() {{
let corpus_dir = assert_fs::TempDir::new().unwrap();
let fuzzing_failure_dir = assert_fs::TempDir::new().unwrap();
let test_program_dir = PathBuf::from("{test_dir}");
#[allow(deprecated)]
let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(test_program_dir);
nargo.arg("fuzz").arg("--timeout").arg("{timeout_str}");
nargo.arg("--corpus-dir").arg(corpus_dir.path());
nargo.arg("--fuzzing-failure-dir").arg(fuzzing_failure_dir.path());
{test_content}
}}
"#
)
.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();
let check_stdout = !TESTS_WITHOUT_STDOUT_CHECK.contains(&test_name.as_str());
generate_test_cases(
test_file,
&test_name,
&test_dir,
"execute",
&format!("execution_success(nargo, test_program_dir, {check_stdout});",),
&MatrixConfig {
vary_brillig: !IGNORED_BRILLIG_TESTS.contains(&test_name.as_str()),
vary_inliner: true,
min_inliner: min_inliner(&test_name),
max_inliner: max_inliner(&test_name),
},
);
}
writeln!(test_file, "}}").unwrap();
}
fn max_inliner(test_name: &str) -> i64 {
INLINER_MAX_OVERRIDES
.iter()
.chain(&INLINER_OVERRIDES)
.find(|(n, _)| *n == test_name)
.map_or(i64::MAX, |(_, i)| *i)
}
fn min_inliner(test_name: &str) -> i64 {
INLINER_MIN_OVERRIDES
.iter()
.chain(&INLINER_OVERRIDES)
.find(|(n, _)| *n == test_name)
.map_or(i64::MIN, |(_, i)| *i)
}
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",
"execution_failure(nargo, test_program_dir, runtime);",
&MatrixConfig {
vary_brillig: !IGNORED_BRILLIG_TESTS.contains(&test_name.as_str()),
..Default::default()
},
);
}
writeln!(test_file, "}}").unwrap();
}
fn generate_comptime_interpret_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 comptime_interpret_{test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
if IGNORED_COMPTIME_INTERPRET_EXECUTION_TESTS.contains(&test_name.as_str()) {
continue;
}
let should_panic =
if PANICKING_COMPTIME_INTERPRET_EXECUTION_TESTS.contains(&test_name.as_str()) {
"#[should_panic]"
} else {
""
};
let check_stdout =
!IGNORED_COMPTIME_INTERPRET_EXECUTION_STDOUT_CHECK_TESTS.contains(&test_name.as_str());
let test_dir = test_dir.display();
write!(
test_file,
r#"
#[test]
{should_panic}
fn test_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");
nargo_execute_comptime(test_program_dir, {check_stdout});
}}
"#
)
.unwrap();
}
writeln!(test_file, "}}").unwrap();
}
fn generate_comptime_interpret_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 comptime_interpret_{test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
if IGNORED_COMPTIME_INTERPRET_EXECUTION_FAILURE_TESTS.contains(&test_name.as_str()) {
continue;
}
let should_panic =
if PANICKING_COMPTIME_INTERPRET_EXECUTION_FAILURE_TESTS.contains(&test_name.as_str()) {
"#[should_panic]"
} else {
""
};
let test_dir = test_dir.display();
write!(
test_file,
r#"
#[test]
{should_panic}
fn test_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");
nargo_execute_comptime_expect_failure(test_program_dir);
}}
"#
)
.unwrap();
}
writeln!(test_file, "}}").unwrap();
}
fn generate_comptime_interpret_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, test_type);
writeln!(
test_file,
"mod comptime_interpret_{test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
if IGNORED_COMPTIME_INTERPRET_NOIR_TESTS.contains(&test_name.as_str()) {
continue;
}
let test_dir = test_dir.display();
write!(
test_file,
r#"
#[test]
fn test_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");
nargo_test_comptime(test_program_dir);
}}
"#
)
.unwrap();
}
writeln!(test_file, "}}").unwrap();
}
fn generate_comptime_interpret_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 comptime_interpret_{test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();
write!(
test_file,
r#"
#[test]
fn test_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");
nargo_test_comptime_expect_failure(test_program_dir);
}}
"#
)
.unwrap();
}
writeln!(test_file, "}}").unwrap();
}
fn generate_brillig_small_stack_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 brillig_small_stack_{test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
if IGNORED_BRILLIG_TESTS.contains(&test_name.as_str()) {
continue;
}
let should_panic = if IGNORED_BRILLIG_SMALL_STACK_TESTS.contains(&test_name.as_str()) {
"#[should_panic]"
} else {
""
};
let test_dir = test_dir.display();
write!(
test_file,
r#"
#[test]
{should_panic}
fn test_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");
nargo_execute_brillig_small_stack(test_program_dir);
}}
"#
)
.unwrap();
}
writeln!(test_file, "}}").unwrap();
}
/// Generate tests for fuzzing which find failures in the fuzzed program.
fn generate_fuzzing_failure_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "fuzzing_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_fuzzing_test_case(
test_file,
&test_name,
&test_dir,
r#"
nargo.assert().failure().stderr(
predicate::str::contains("Failing input").and(
predicate::str::contains("got a different failing assertion").not())
);
"#,
240,
);
}
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",
"noir_test_success(nargo);",
&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",
"noir_test_failure(nargo);",
&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();
generate_test_cases(
test_file,
&test_name,
&test_dir,
"info",
&format!(
"compile_success_empty(nargo, {});",
!TESTS_WITH_EXPECTED_WARNINGS.contains(&test_name.as_str())
),
&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",
"compile_success_contract(nargo);",
&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",
"compile_success_no_bug(nargo);",
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
}
/// Generate tests for checking that the contract compiles and there are "bugs" in stderr
fn generate_compile_success_with_bug_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "compile_success_with_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",
"compile_success_with_bug(nargo, test_program_dir);",
&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",
"compile_failure(nargo, test_program_dir);",
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
}
fn generate_interpret_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 interpret_{test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
if IGNORED_INTERPRET_EXECUTION_TESTS.contains(&test_name.as_str()) {
continue;
}
let test_dir = test_dir.display();
generate_test_cases(
test_file,
&test_name,
&test_dir,
"interpret",
r#"
nargo.arg("--validate-between-passes");
interpret_execution_success(nargo);
"#,
&MatrixConfig {
vary_brillig: !IGNORED_BRILLIG_TESTS.contains(&test_name.as_str()),
vary_inliner: true,
min_inliner: min_inliner(&test_name),
max_inliner: max_inliner(&test_name),
},
);
}
writeln!(test_file, "}}").unwrap();
}
fn generate_interpret_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 interpret_{test_type} {{
use super::*;
"
)
.unwrap();