forked from bext-lang/b
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtest.rs
More file actions
957 lines (868 loc) · 36.1 KB
/
btest.rs
File metadata and controls
957 lines (868 loc) · 36.1 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
#![no_main]
#![no_std]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(unused_macros)]
#[macro_use]
pub mod crust;
#[macro_use]
pub mod nob;
pub mod targets;
pub mod flag;
pub mod glob;
pub mod jim;
pub mod jimp;
pub mod ir;
pub mod arena;
pub mod lexer;
pub mod codegen;
pub mod shlex;
pub mod params;
use core::ffi::*;
use core::cmp;
use core::mem::{zeroed, size_of};
use core::ptr;
use crust::libc::*;
use nob::*;
use targets::*;
use flag::*;
use glob::*;
use jim::*;
use jimp::*;
use crust::compar_cstr;
const GARBAGE_FOLDER: *const c_char = c!("./build/tests/");
enum_with_order! {
#[derive(Copy, Clone)]
enum TestState in TEST_STATE_ORDER {
Enabled,
Disabled,
}
}
impl TestState {
unsafe fn name(self) -> *const c_char {
match self {
Self::Enabled => c!("Enabled"),
Self::Disabled => c!("Disabled"),
}
}
unsafe fn from_name(name: *const c_char) -> Option<Self> {
for i in 0..TEST_STATE_ORDER.len() {
let state = (*TEST_STATE_ORDER)[i];
if strcmp(state.name(), name) == 0 {
return Some(state)
}
}
None
}
}
#[derive(Copy, Clone)]
pub enum Outcome {
/// The test didn't even manage to build
BuildFail,
/// The test built, but crashed at runtime
RunFail,
/// The test built, printed something and exited normally
RunSuccess{stdout: *const c_char},
}
enum_with_order! {
#[derive(Copy, Clone)]
enum ReportStatus in REPORT_STATUS_ORDER {
OK,
NeverRecorded,
StdoutMismatch,
BuildFail,
RunFail,
Disabled,
}
}
impl ReportStatus {
fn letter(self) -> *const c_char {
match self {
ReportStatus::OK => c!("K"),
ReportStatus::NeverRecorded => c!("K"),
ReportStatus::StdoutMismatch => c!("K"),
ReportStatus::BuildFail => c!("B"),
ReportStatus::RunFail => c!("R"),
ReportStatus::Disabled => c!("-"),
}
}
fn color(self) -> *const c_char {
match self {
ReportStatus::OK => GREEN,
ReportStatus::NeverRecorded => BLUE,
ReportStatus::StdoutMismatch => RED,
ReportStatus::BuildFail => RED,
ReportStatus::RunFail => RED,
ReportStatus::Disabled => GREY,
}
}
fn description(self) -> *const c_char {
match self {
ReportStatus::OK => c!("passed"),
ReportStatus::NeverRecorded => c!("stdout is not recorded"),
ReportStatus::StdoutMismatch => c!("unexpected stdout"),
ReportStatus::BuildFail => c!("build fail"),
ReportStatus::RunFail => c!("runtime error"),
ReportStatus::Disabled => c!("disabled"),
}
}
}
#[derive(Copy, Clone)]
pub struct Report {
pub name: *const c_char,
pub statuses: Array<ReportStatus>,
}
pub unsafe fn execute_test(
// Inputs
test_folder: *const c_char, name: *const c_char, target: Target, quiet: bool,
// Outputs
cmd: *mut Cmd, sb: *mut String_Builder,
) -> Option<Outcome> {
// TODO: add timeouts for running and building in case they go into infinite loop or something
let input_path = temp_sprintf(c!("%s/%s.b"), test_folder, name);
let program_path = temp_sprintf(c!("%s/%s.%s%s"), GARBAGE_FOLDER, name, target.api.name(), target.file_ext());
let stdout_path = temp_sprintf(c!("%s/%s.%s.stdout.txt"), GARBAGE_FOLDER, name, target.api.name());
cmd_append! {
cmd,
if cfg!(target_os = "windows") {
c!("./build/b.exe")
} else {
c!("./build/b")
},
input_path,
c!("-t"), target.api.name(),
c!("-o"), program_path,
}
if quiet {
cmd_append! { cmd, c!("-q") }
}
if !cmd_run_sync_and_reset(cmd) {
return Some(Outcome::BuildFail);
}
cmd_append! {
cmd,
if cfg!(target_os = "windows") {
c!("./build/b.exe")
} else {
c!("./build/b")
},
input_path,
c!("-t"), target.api.name(),
c!("-o"), program_path,
c!("-q"),
c!("-nobuild"),
c!("-run"),
}
// Hack for Uxn
if strcmp(target.api.name(), c!("uxn")) == 0 {
cmd_append!(cmd, c!("-C"), c!("runner=uxncli"));
}
let mut fdout = fd_open_for_write(stdout_path);
let mut redirect: Cmd_Redirect = zeroed();
redirect.fdout = &mut fdout;
let run_ok = cmd_run_sync_redirect_and_reset(cmd, redirect);
(*sb).count = 0;
read_entire_file(stdout_path, sb)?; // Should always succeed, but may fail if stdout_path is a directory for instance.
da_append(sb, 0); // NULL-terminating the stdout
if !quiet {
printf(c!("%s"), (*sb).items); // Forward stdout for diagnostic purposes
}
if !run_ok {
Some(Outcome::RunFail)
} else {
Some(Outcome::RunSuccess{stdout: strdup((*sb).items)}) // TODO: memory leak
}
}
pub unsafe fn usage() {
fprintf(stderr(), c!("B Compiler Testing Tool\n"));
fprintf(stderr(), c!("Usage: %s [OPTIONS]\n"), flag_program_name());
fprintf(stderr(), c!("OPTIONS:\n"));
flag_print_options(stderr());
}
#[derive(Clone, Copy)]
pub struct ReportStats {
entries: [usize; REPORT_STATUS_ORDER.len()]
}
const RESET: *const c_char = c!("\x1b[0m");
const GREEN: *const c_char = c!("\x1b[32m");
const _YELLOW: *const c_char = c!("\x1b[33m");
const GREY: *const c_char = c!("\x1b[90m");
const RED: *const c_char = c!("\x1b[31m");
const BLUE: *const c_char = c!("\x1b[94m");
pub unsafe fn print_legend(row_width: usize) {
for i in 0..REPORT_STATUS_ORDER.len() {
let status = (*REPORT_STATUS_ORDER)[i];
printf(c!("%*s%s%s%s - %s\n"), row_width + 2, c!(""), status.color(), status.letter(), RESET, status.description());
}
}
pub unsafe fn print_report_stats(stats: ReportStats) {
for i in 0..REPORT_STATUS_ORDER.len() {
let status = (*REPORT_STATUS_ORDER)[i];
printf(c!(" %s%s%s: %-3zu"), status.color(), status.letter(), RESET, stats.entries[i]);
}
printf(c!("\n"));
}
pub unsafe fn print_top_labels(targets: *const [Target], stats_by_target: *const [ReportStats], row_width: usize, col_width: usize) {
assert!(targets.len() == stats_by_target.len());
for j in 0..targets.len() {
let target = (*targets)[j];
let stats = (*stats_by_target)[j];
printf(c!("%*s"), row_width + 2, c!(""));
for _ in 0..j {
printf(c!("│ "));
}
// TODO: these fancy unicode characters don't work well on mingw32 build via wine
printf(c!("┌─%-*s"), col_width - 2*j, target.api.name());
print_report_stats(stats)
}
}
pub unsafe fn print_bottom_labels(targets: *const [Target], stats_by_target: *const [ReportStats], row_width: usize, col_width: usize) {
assert!(targets.len() == stats_by_target.len());
for j in (0..targets.len()).rev() {
let target = (*targets)[j];
let stats = (*stats_by_target)[j];
printf(c!("%*s"), row_width + 2, c!(""));
for _ in 0..j {
printf(c!("│ "));
}
printf(c!("└─%-*s"), col_width - 2*j, target.api.name());
print_report_stats(stats)
}
}
pub unsafe fn matches_glob(pattern: *const c_char, text: *const c_char) -> Option<bool> {
let mark = temp_save();
let result = match glob_utf8(pattern, text) {
Glob_Result::MATCHED => Ok(true),
Glob_Result::UNMATCHED => Ok(false),
Glob_Result::OOM_ERROR => Err(c!("out of memory")),
Glob_Result::ENCODING_ERROR => Err(c!("encoding error")),
Glob_Result::SYNTAX_ERROR => Err(c!("syntax error")),
};
temp_rewind(mark);
match result {
Ok(result) => Some(result),
Err(error) => {
fprintf(stderr(), c!("ERROR: while matching pattern `%s`: %s\n"), pattern, error);
None
}
}
}
pub unsafe fn record_tests(
// Inputs
test_folder: *const c_char, cases: *const [*const c_char], targets: *const [Target], tt: *mut TestTable, quiet: bool,
// Outputs
cmd: *mut Cmd, sb: *mut String_Builder,
reports: *mut Array<Report>, stats_by_target: *mut Array<ReportStats>,
) -> Option<()> {
// TODO: Parallelize the test runner.
// Probably using `cmd_run_async_and_reset`.
// Also don't forget to add the `-j` flag.
for i in 0..cases.len() {
let case_name = (*cases)[i];
let mut report = Report {
name: case_name,
statuses: zeroed(),
};
for j in 0..targets.len() {
let target = (*targets)[j];
if let Some(test_row) = test_table_find_row(tt, case_name, target) {
match (*test_row).state {
TestState::Enabled => {
let outcome = execute_test(
// Inputs
test_folder, case_name, target, quiet,
// Outputs
cmd, sb,
)?;
match outcome {
Outcome::BuildFail => da_append(&mut report.statuses, ReportStatus::BuildFail),
Outcome::RunFail => da_append(&mut report.statuses, ReportStatus::RunFail),
Outcome::RunSuccess{stdout} => {
(*test_row).expected_stdout = stdout;
da_append(&mut report.statuses, ReportStatus::OK);
}
}
}
TestState::Disabled => da_append(&mut report.statuses, ReportStatus::Disabled),
}
} else {
let outcome = execute_test(
// Inputs
test_folder, case_name, target, quiet,
// Outputs
cmd, sb,
)?;
match outcome {
Outcome::BuildFail => {
da_append(tt, TestRow {
case_name,
target,
expected_stdout: c!(""),
state: TestState::Enabled,
comment: c!("Failed to build on record"),
});
da_append(&mut report.statuses, ReportStatus::BuildFail)
},
Outcome::RunFail => {
da_append(tt, TestRow {
case_name,
target,
expected_stdout: c!(""),
state: TestState::Enabled,
comment: c!("Failed to run on record"),
});
da_append(&mut report.statuses, ReportStatus::RunFail)
}
Outcome::RunSuccess{stdout} => {
da_append(tt, TestRow {
case_name,
target,
expected_stdout: stdout,
state: TestState::Enabled,
comment: c!(""),
});
da_append(&mut report.statuses, ReportStatus::OK);
}
}
}
}
da_append(reports, report);
}
collect_stats_by_target(targets, da_slice(*reports), stats_by_target);
generate_report(da_slice(*reports), da_slice(*stats_by_target), targets);
Some(())
}
pub unsafe fn collect_stats_by_target(targets: *const [Target], reports: *const [Report], stats_by_target: *mut Array<ReportStats>) {
for j in 0..targets.len() {
let mut stats: ReportStats = zeroed();
for i in 0..reports.len() {
let report = (*reports)[i];
stats.entries[*report.statuses.items.add(j) as usize] += 1;
}
da_append(stats_by_target, stats);
}
}
pub unsafe fn generate_report(reports: *const [Report], stats_by_target: *const [ReportStats], targets: *const [Target]) {
let mut row_width = 0;
for i in 0..reports.len() {
let report = (*reports)[i];
row_width = cmp::max(row_width, strlen(report.name));
}
let mut col_width = 0;
for j in 0..targets.len() {
let target = (*targets)[j];
let width = 2*(j + 1) + strlen(target.api.name());
col_width = cmp::max(col_width, width);
}
print_legend(row_width);
printf(c!("\n"));
print_top_labels(targets, stats_by_target, row_width, col_width);
for i in 0..reports.len() {
let report = (*reports)[i];
printf(c!("%*s:"), row_width, report.name);
for j in 0..report.statuses.count {
let status = *report.statuses.items.add(j);
printf(c!(" %s%s%s"), status.color(), status.letter(), RESET);
}
printf(c!("\n"));
}
print_bottom_labels(targets, stats_by_target, row_width, col_width);
printf(c!("\n"));
print_legend(row_width);
}
pub struct TestRow {
pub case_name: *const c_char,
pub target: Target,
pub expected_stdout: *const c_char,
pub state: TestState,
pub comment: *const c_char,
}
type TestTable = Array<TestRow>;
// TODO: test_table_find_row is O(n) which usually causes no problems on small arrays, but TestTable by the nature of the data
// it holds has a tendency to grow rather fast (the growth is O(Cases*Targets), basically every time we add a case it adds
// Targets amount of rows). We should invest into improving the performance of this operation rather soon. HashMap<K, V> is
// out of the question of course since we are torturing ourselves with Crust. We could implement our own HashMap or we could
// maybe sort this array on loading it from the JSON file and do Binary Search. Sorting has additional benefit of keeping
// the JSON file tidy.
pub unsafe fn test_table_find_row(tt: *mut TestTable, case_name: *const c_char, target: Target) -> Option<*mut TestRow> {
for i in 0..(*tt).count {
let row = (*tt).items.add(i);
if strcmp((*row).target.api.name(), target.api.name()) == 0 && strcmp((*row).case_name, case_name) == 0 {
return Some(row)
}
}
None
}
pub unsafe fn load_tt_from_json_file_if_exists(
all_targets: *const [Target], json_path: *const c_char, test_folder: *const c_char,
sb: *mut String_Builder, jimp: *mut Jimp
) -> Option<TestTable> {
let mut tt: TestTable = zeroed();
if file_exists(json_path)? {
log(Log_Level::INFO, c!("loading file %s..."), json_path);
// TODO: file may stop existing between file_exists() and read_entire_file() cools
// It would be much better if read_entire_file() returned the reason of failure so
// it's easy to check if it failed due to ENOENT, but that requires significant
// changes to nob.h rn.
(*sb).count = 0;
read_entire_file(json_path, sb)?;
jimp_begin(jimp, json_path, (*sb).items, (*sb).count);
jimp_array_begin(jimp)?;
'table: while jimp_array_item(jimp) {
let saved_point = (*jimp).point;
let mut case_name: *const c_char = ptr::null();
let mut target: Option<Target> = None;
let mut expected_stdout: *const c_char = c!("");
let mut state = TestState::Enabled;
let mut comment: *const c_char = c!("");
jimp_object_begin(jimp)?;
'row: while jimp_object_member(jimp) {
if strcmp((*jimp).string, c!("case")) == 0 {
jimp_string(jimp)?;
case_name = strdup((*jimp).string); // TODO: memory leak
continue 'row;
}
if strcmp((*jimp).string, c!("target")) == 0 {
jimp_string(jimp)?;
if let Some(parsed_target) = Target::by_name(all_targets, (*jimp).string) {
target = Some(parsed_target);
} else {
jimp_diagf(jimp, c!("WARNING: invalid target name `%s`\n"), (*jimp).string);
}
continue 'row;
}
if strcmp((*jimp).string, c!("expected_stdout")) == 0 {
jimp_string(jimp)?;
expected_stdout = strdup((*jimp).string); // TODO: memory leak
continue 'row;
}
if strcmp((*jimp).string, c!("state")) == 0 {
jimp_string(jimp)?;
if let Some(parsed_state) = TestState::from_name((*jimp).string) {
state = parsed_state;
} else {
jimp_diagf(jimp, c!("WARNING: invalid state name `%s`\n"), (*jimp).string);
}
continue 'row;
}
if strcmp((*jimp).string, c!("comment")) == 0 {
jimp_string(jimp)?;
comment = strdup((*jimp).string); // TODO: memory leak
continue 'row;
}
jimp_diagf(jimp, c!("ERROR: unknown test row field `%s`\n"), (*jimp).string);
return None;
}
jimp_object_end(jimp)?;
let Some(target) = target else {
(*jimp).token_start = saved_point;
jimp_diagf(jimp, c!("WARNING: no valid `target` field is defined for this test row. Ignoring the entire row...\n"));
// TODO: memory leak, we are dropping the whole row here
continue 'table;
};
if case_name.is_null() {
(*jimp).token_start = saved_point;
jimp_diagf(jimp, c!("WARNING: no valid `case_name` field is defined for this test row. Ignoring the entire row...\n"));
// TODO: memory leak, we are dropping the whole row here
continue 'table;
}
if let Some(_existing_row) = test_table_find_row(&mut tt, case_name, target) {
(*jimp).token_start = saved_point;
// TODO: report the location of existing_row here as a NOTE
// This requires keeping track of location in TestRow structure. Which requires location tracking capabilities
// comparable to lexer.rs but in jim/jimp.
jimp_diagf(jimp, c!("WARNING: Redefinition of the row case `%s`, target `%s`. We are using only the first definition. All the rest are gonna be prunned"), case_name, target.api.name());
// TODO: memory leak, we are dropping the whole row here
continue 'table;
}
let case_path = temp_sprintf(c!("%s/%s.b"), test_folder, case_name);
if !file_exists(case_path)? {
(*jimp).token_start = saved_point;
jimp_diagf(jimp, c!("WARNING: %s does not exist. Ignoring case `%s`, target `%s` ...\n"), case_path, case_name, target.api.name());
// TODO: memory leak, we are dropping the whole row here
continue 'table;
}
da_append(&mut tt, TestRow {
case_name,
target,
expected_stdout,
state,
comment,
});
}
jimp_array_end(jimp)?;
} else {
log(Log_Level::INFO, c!("%s doesn't exist. Nothing to load."), json_path);
}
Some(tt)
}
pub unsafe fn save_tt_to_json_file(
json_path: *const c_char, tt: TestTable,
jim: *mut Jim,
) -> Option<()> {
log(Log_Level::INFO, c!("saving file %s..."), json_path);
jim_begin(jim);
jim_array_begin(jim);
for i in 0..tt.count {
let row = tt.items.add(i);
jim_object_begin(jim);
jim_member_key(jim, c!("case"));
jim_string(jim, (*row).case_name);
jim_member_key(jim, c!("target"));
jim_string(jim, (*row).target.api.name());
jim_member_key(jim, c!("expected_stdout"));
jim_string(jim, (*row).expected_stdout);
jim_member_key(jim, c!("state"));
jim_string(jim, (*row).state.name());
jim_member_key(jim, c!("comment"));
jim_string(jim, (*row).comment);
jim_object_end(jim);
}
jim_array_end(jim);
write_entire_file(json_path, (*jim).sink as *const c_void, (*jim).sink_count)
}
pub unsafe fn replay_tests(
// TODO: The Inputs and the Outputs want to be their own entity. But what should they be called?
// Inputs
test_folder: *const c_char, cases: *const [*const c_char], targets: *const [Target], mut tt: TestTable, quiet: bool,
// Outputs
cmd: *mut Cmd, sb: *mut String_Builder, reports: *mut Array<Report>, stats_by_target: *mut Array<ReportStats>, jim: *mut Jim,
) -> Option<()> {
// TODO: Parallelize the test runner.
// Probably using `cmd_run_async_and_reset`.
// Also don't forget to add the `-j` flag.
for i in 0..cases.len() {
let case_name = (*cases)[i];
let mut report = Report {
name: case_name,
statuses: zeroed(),
};
for j in 0..targets.len() {
let target = (*targets)[j];
if let Some(row) = test_table_find_row(&mut tt, case_name, target) {
match (*row).state {
TestState::Enabled => {
let outcome = execute_test(
// Inputs
test_folder, case_name, target, quiet,
// Outputs
cmd, sb,
)?;
match outcome {
Outcome::RunSuccess{stdout} =>
if strcmp((*row).expected_stdout, stdout) != 0 {
fprintf(stderr(), c!("UNEXPECTED OUTCOME!!!\n"));
jim_begin(jim);
jim_string(jim, (*row).expected_stdout);
fprintf(stderr(), c!("EXPECTED: %.*s\n"), (*jim).sink_count, (*jim).sink);
jim_begin(jim);
jim_string(jim, stdout);
fprintf(stderr(), c!("ACTUAL: %.*s\n"), (*jim).sink_count, (*jim).sink);
da_append(&mut report.statuses, ReportStatus::StdoutMismatch);
} else {
da_append(&mut report.statuses, ReportStatus::OK);
},
Outcome::BuildFail => da_append(&mut report.statuses, ReportStatus::BuildFail),
Outcome::RunFail => da_append(&mut report.statuses, ReportStatus::RunFail),
}
}
TestState::Disabled => da_append(&mut report.statuses, ReportStatus::Disabled),
}
} else {
let outcome = execute_test(
// Inputs
test_folder, case_name, target, quiet,
// Outputs
cmd, sb,
)?;
match outcome {
Outcome::RunSuccess{..} => {
fprintf(stderr(), c!("UNEXPECTED OUTCOME!!! The outcome was never recorded. Please use -record flag to record what is expected for this test case at this target\n"));
da_append(&mut report.statuses, ReportStatus::NeverRecorded);
}
Outcome::BuildFail => da_append(&mut report.statuses, ReportStatus::BuildFail),
Outcome::RunFail => da_append(&mut report.statuses, ReportStatus::RunFail),
}
}
}
da_append(reports, report);
}
collect_stats_by_target(targets, da_slice(*reports), stats_by_target);
generate_report(da_slice(*reports), da_slice(*stats_by_target), targets);
Some(())
}
enum_with_order! {
#[derive(Copy, Clone)]
enum Action in ACTION_ORDER {
Replay,
Record,
Prune,
Disable,
Count,
}
}
impl Action {
unsafe fn name(self) -> *const c_char {
match self {
Self::Replay => c!("replay"),
Self::Record => c!("record"),
Self::Prune => c!("prune"),
Self::Disable => c!("disable"),
Self::Count => c!("count"),
}
}
unsafe fn from_name(name: *const c_char) -> Option<Self> {
for i in 0..ACTION_ORDER.len() {
let action = (*ACTION_ORDER)[i];
if strcmp(name, action.name()) == 0 {
return Some(action)
}
}
None
}
}
pub unsafe fn main(argc: i32, argv: *mut*mut c_char) -> Option<()> {
let all_targets = codegen::load_targets()?;
let default_action = Action::Replay;
let target_flags = flag_list(c!("t"), c!("Compilation targets to select for testing. Can be a glob pattern."));
let exclude_target_flags = flag_list(c!("xt"), c!("Compilation targets to exclude from selected ones. Can be a glob pattern"));
let list_targets = flag_bool(c!("tlist"), false, c!("Print the list of selected compilation targets."));
let cases_flags = flag_list(c!("c"), c!("Test cases to select for testing. Can be a glob pattern."));
let exclude_cases_flags = flag_list(c!("xc"), c!("Test cases to exclude from selected ones. Can be a glob pattern"));
let list_cases = flag_bool(c!("clist"), false, c!("Print the list of selected test cases"));
let action_flag = flag_str(c!("a"), default_action.name(), c!("Action to perform. Use -alist to get the list of available actions"));
let list_actions = flag_bool(c!("alist"), false, c!("Print the list of all available actions."));
let record = flag_bool(c!("record"), false, temp_sprintf(c!("DEPRECATED! Please use `-%s %s` flag instead."), flag_name(action_flag), Action::Record.name()));
let comment = flag_str(c!("comment"), ptr::null(), temp_sprintf(c!("Set the comment on disabled test cases when you do `-%s %s`"), flag_name(action_flag), Action::Disable.name()));
let test_folder = flag_str(c!("dir"), c!("./tests/"), c!("Test folder"));
let quiet = flag_bool(c!("q"), false, c!("Makes the test runner yap less about what it's doing"));
let help = flag_bool(c!("help"), false, c!("Print this help message"));
if !flag_parse(argc, argv) {
usage();
flag_print_error(stderr());
return None;
}
if *help {
usage();
return Some(());
}
let Some(action) = Action::from_name(*action_flag) else {
fprintf(stderr(), c!("ERROR: unknown action `%s`\n"), *action_flag);
return None;
};
let json_path = c!("tests.json");
if *list_actions {
fprintf(stderr(), c!("Available actions:\n"));
let mut width = 0;
for i in 0..ACTION_ORDER.len() {
width = cmp::max(width, strlen((*ACTION_ORDER)[i].name()));
}
for i in 0..ACTION_ORDER.len() {
let action = (*ACTION_ORDER)[i];
match action {
Action::Replay => {
printf(c!(" %-*s - Replay the selected Test Matrix slice with expected outputs from %s.\n"), width, action.name(), json_path);
}
Action::Record => {
printf(c!(" %-*s - Record the selected Test Matrix slice into %s.\n"), width, action.name(), json_path);
}
Action::Prune => {
printf(c!(" %-*s - Prune all the recordings from %s that are outside of the selected Test Matrix slice.\n"), width, action.name(), json_path);
printf(c!(" %-*s Useful when you delete targets or test cases. Just delete a target or a test case and\n"), width, c!(""));
printf(c!(" %-*s run `%s -%s %s` without any additional flags.\n"), width, c!(""), flag_program_name(), flag_name(action_flag), Action::Prune.name());
}
Action::Disable => {
printf(c!(" %-*s - Disable all the tests in the selected Test Matrix slice.\n"), width, action.name());
printf(c!(" %-*s You can optionally set the comment with the -%s flag.\n"), width, c!(""), flag_name(comment));
}
Action::Count => {
printf(c!(" %-*s - Count the amount of rows in %s.\n"), width, action.name(), json_path);
}
};
}
return Some(());
}
if *quiet {
minimal_log_level = Log_Level::WARNING;
}
let mut sb: String_Builder = zeroed();
let mut cmd: Cmd = zeroed();
let mut jim: Jim = zeroed();
jim.pp = 4;
let mut jimp: Jimp = zeroed();
let mut reports: Array<Report> = zeroed();
let mut stats_by_target: Array<ReportStats> = zeroed();
let mut selected_targets: Array<Target> = zeroed();
if (*target_flags).count == 0 {
da_append_many(&mut selected_targets, da_slice(all_targets));
} else {
for j in 0..(*target_flags).count {
let mut added_anything = false;
let pattern = *(*target_flags).items.add(j);
for j in 0..all_targets.count {
let target = *all_targets.items.add(j);
let name = target.api.name();
if matches_glob(pattern, name)? {
da_append(&mut selected_targets, target);
added_anything = true;
}
}
if !added_anything {
fprintf(stderr(), c!("ERROR: unknown target `%s`\n"), pattern);
return None;
}
}
}
let mut targets: Array<Target> = zeroed();
for i in 0..selected_targets.count {
let target = *selected_targets.items.add(i);
let mut matches_any = false;
'exclude: for j in 0..(*exclude_target_flags).count {
let pattern = *(*exclude_target_flags).items.add(j);
if matches_glob(pattern, target.api.name())? {
matches_any = true;
break 'exclude;
}
}
if !matches_any {
da_append(&mut targets, target);
}
}
let mut all_cases: Array<*const c_char> = zeroed();
let mut test_files: File_Paths = zeroed();
if !read_entire_dir(*test_folder, &mut test_files) { return None; }
qsort(test_files.items as *mut c_void, test_files.count, size_of::<*const c_char>(), compar_cstr);
for i in 0..test_files.count {
let test_file = *test_files.items.add(i);
if *test_file == '.' as c_char { continue; }
let Some(case_name) = temp_strip_suffix(test_file, c!(".b")) else { continue; };
da_append(&mut all_cases, case_name);
}
let mut selected_cases: Array<*const c_char> = zeroed();
if (*cases_flags).count == 0 {
selected_cases = all_cases;
} else {
for i in 0..(*cases_flags).count {
let saved_count = selected_cases.count;
let pattern = *(*cases_flags).items.add(i);
for i in 0..all_cases.count {
let case_name = *all_cases.items.add(i);
if matches_glob(pattern, case_name)? {
da_append(&mut selected_cases, case_name);
}
}
if selected_cases.count == saved_count {
fprintf(stderr(), c!("ERROR: unknown test case `%s`\n"), pattern);
return None;
}
}
}
let mut cases: Array<*const c_char> = zeroed();
for i in 0..selected_cases.count {
let case = *selected_cases.items.add(i);
let mut matches_any = false;
'exclude: for j in 0..(*exclude_cases_flags).count {
let pattern = *(*exclude_cases_flags).items.add(j);
if matches_glob(pattern, case)? {
matches_any = true;
break 'exclude;
}
}
if !matches_any {
da_append(&mut cases, case);
}
}
// TODO: maybe merge -tlist and -clist outputs if they are provided together
if *list_targets {
fprintf(stderr(), c!("Compilation targets:\n"));
for i in 0..targets.count {
let target = *targets.items.add(i);
fprintf(stderr(), c!(" %s\n"), target.api.name());
}
return Some(());
}
if *list_cases {
fprintf(stderr(), c!("Test cases:\n"));
for i in 0..cases.count {
let case = *cases.items.add(i);
fprintf(stderr(), c!(" %s\n"), case);
}
return Some(());
}
if !mkdir_if_not_exists(GARBAGE_FOLDER) { return None; }
if *record {
fprintf(stderr(), c!("ERROR: Flag `-%s` is DEPRECATED! Please use `-%s %s` instead.\n"), flag_name(record), flag_name(action_flag), Action::Record.name());
return None
}
match action {
Action::Record => {
let mut tt = load_tt_from_json_file_if_exists(da_slice(all_targets), json_path, *test_folder, &mut sb, &mut jimp)?;
record_tests(
// Inputs
*test_folder, da_slice(cases), da_slice(targets), &mut tt, *quiet,
// Outputs
&mut cmd, &mut sb, &mut reports, &mut stats_by_target,
)?;
save_tt_to_json_file(json_path, tt, &mut jim)?;
}
Action::Replay => {
let tt = load_tt_from_json_file_if_exists(da_slice(all_targets), json_path, *test_folder, &mut sb, &mut jimp)?;
replay_tests(
// Inputs
*test_folder, da_slice(cases), da_slice(targets), tt, *quiet,
// Outputs
&mut cmd, &mut sb, &mut reports, &mut stats_by_target, &mut jim,
);
}
Action::Prune => {
let tt = load_tt_from_json_file_if_exists(da_slice(all_targets), json_path, *test_folder, &mut sb, &mut jimp)?;
save_tt_to_json_file(json_path, tt, &mut jim)?;
}
Action::Disable => {
let mut case_width = 0;
for i in 0..cases.count {
let case_name = *cases.items.add(i);
case_width = cmp::max(case_width, strlen(case_name));
}
let mut target_width = 0;
for j in 0..targets.count {
let target = *targets.items.add(j);
target_width = cmp::max(target_width, strlen(target.api.name()));
}
let mut tt = load_tt_from_json_file_if_exists(da_slice(all_targets), json_path, *test_folder, &mut sb, &mut jimp)?;
for i in 0..cases.count {
let case_name = *cases.items.add(i);
for j in 0..targets.count {
let target = *targets.items.add(j);
log(Log_Level::INFO, c!("disabling %-*s for %-*s"), case_width, case_name, target_width, target.api.name());
if let Some(row) = test_table_find_row(&mut tt, case_name, target) {
(*row).state = TestState::Disabled;
if !(*comment).is_null() {
(*row).comment = *comment;
}
} else {
da_append(&mut tt, TestRow {
case_name,
target,
expected_stdout: c!(""),
state: TestState::Disabled,
comment: if (*comment).is_null() {
c!("")
} else {
*comment
},
});
}
}
}
save_tt_to_json_file(json_path, tt, &mut jim)?;
}
Action::Count => {
let tt = load_tt_from_json_file_if_exists(da_slice(all_targets), json_path, *test_folder, &mut sb, &mut jimp)?;
printf(c!("%zu\n"), tt.count);
}
}
Some(())
}
// TODO: generate HTML reports and deploy them somewhere automatically
// TODO: Make btest test historical mode