forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·1051 lines (920 loc) · 28.9 KB
/
run
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
#!/usr/bin/env php
<?php
/**
* Run the test suites in various configurations.
*/
function get_expect_file($test, $type, $options) {
if (isset($options['repo']) && file_exists("$test.$type-repo")) {
return "$test.$type-repo";
}
return "$test.$type";
}
function usage() {
global $argv;
return "usage: $argv[0] [-m jit|interp] [-r] <test/directories>";
}
function help() {
global $argv;
$ztestexample = 'test/zend/good/*/*z*.php'; // sep. for syntax highlighting
$help = <<<EOT
This is the hhvm test-suite runner. For more detailed documentation,
see hphp/test/README.md.
The test argument may be a path to a php test file, a directory name, or
one of a few pre-defined suite names that this script knows about.
If you work with hhvm a lot, you might consider a bash alias:
alias ht="path/to/fbcode/hphp/test/run"
Examples:
# Quick tests in JIT mode:
% $argv[0] test/quick
# Slow tests in interp mode:
% $argv[0] -m interp test/slow
# Slow closure tests in JIT mode:
% $argv[0] test/slow/closure
# Slow closure tests in JIT mode with RepoAuthoritative:
% $argv[0] -r test/slow/closure
# Slow array tests, in RepoAuthoritative:
% $argv[0] -r test/slow/array
# Zend tests with a "z" in their name:
% $argv[0] $ztestexample
# Quick tests in JIT mode with some extra runtime options:
% $argv[0] test/quick -a '-vEval.JitMaxTranslations=120 -vEval.HHIRJumpOpts=0'
# All quick tests except debugger
% $argv[0] -e debugger test/quick
# All tests except those containing a string of 3 digits
% $argv[0] -E '/\d{3}/' all
# All tests whose name containing pdo_mysql
% $argv[0] -i pdo_mysql -m jit -r zend
EOT;
return usage().$help;
}
function error($message) {
print "$message\n";
exit(1);
}
function hphp_home() {
return realpath(__DIR__.'/../..');
}
function idx($array, $key, $default = null) {
return isset($array[$key]) ? $array[$key] : $default;
}
function idx_file($array, $key, $default = null) {
$file = is_file(idx($array, $key)) ? realpath($array[$key]) : $default;
if (!is_file($file)) {
error("$file doesn't exist. Did you forget to build first?");
}
return rel_path($file);
}
function bin_root() {
$dir = hphp_home() . '/' . idx($_ENV, 'FBMAKE_BIN_ROOT', '_bin');
return is_dir($dir) ?
$dir : # fbmake
hphp_home() # github
;
}
function verify_hhbc() {
return idx($_ENV, 'VERIFY_HHBC', bin_root().'/verify.hhbc');
}
function read_file($file) {
return file_exists($file) ?
str_replace('__DIR__', dirname($file),
preg_replace('/\s+/', ' ', (file_get_contents($file))))
: "";
}
// http://stackoverflow.com/questions/2637945/
function rel_path($to) {
$from = explode('/', getcwd().'/');
$to = explode('/', $to);
$relPath = $to;
foreach($from as $depth => $dir) {
// find first non-matching dir
if($dir === $to[$depth]) {
// ignore this directory
array_shift($relPath);
} else {
// get number of remaining dirs to $from
$remaining = count($from) - $depth;
if($remaining > 1) {
// add traversals up to first matching dir
$padLength = (count($relPath) + $remaining - 1) * -1;
$relPath = array_pad($relPath, $padLength, '..');
break;
} else {
$relPath[0] = './' . $relPath[0];
}
}
}
return implode('/', $relPath);
}
function get_options($argv) {
$parameters = array(
'exclude:' => 'e:',
'exclude-pattern:' => 'E:',
'include:' => 'i:',
'include-pattern:' => 'I:',
'repo' => 'r',
'mode:' => 'm:',
'server' => '',
'help' => 'h',
'verbose' => 'v',
'fbmake' => '',
'threads:' => '',
'args:' => 'a:',
'log' => 'l',
'failure-file:' => '',
'arm' => '',
'hhas-round-trip' => '',
'color' => 'c',
);
$options = array();
$files = array();
for ($i = 1; $i < count($argv); $i++) {
$arg = $argv[$i];
$found = false;
if ($arg && $arg[0] == '-') {
foreach ($parameters as $long => $short) {
if ($arg == '-'.str_replace(':', '', $short) ||
$arg == '--'.str_replace(':', '', $long)) {
if (substr($long, -1, 1) == ':') {
$value = $argv[++$i];
} else {
$value = true;
}
$options[str_replace(':', '', $long)] = $value;
$found = true;
break;
}
}
}
if (!$found && $arg) {
$files[] = $arg;
}
}
if (isset($options['repo']) && isset($options['hhas-round-trip'])) {
echo "repo and hhas-round-trip are mutually exclusive options\n";
exit(1);
}
return array($options, $files);
}
/*
* We support some 'special' file names, that just know where the test
* suites are, to avoid typing 'hphp/test/foo'.
*/
function map_convenience_filename($file) {
$mappage = array(
'quick' => 'hphp/test/quick',
'slow' => 'hphp/test/slow',
'debugger' => 'hphp/test/server/debugger/tests',
'zend' => 'hphp/test/zend/good',
'facebook' => 'hphp/facebook/test',
// Subsets of zend tests.
'zend_ext' => 'hphp/test/zend/good/ext',
'zend_Zend' => 'hphp/test/zend/good/Zend',
'zend_tests' => 'hphp/test/zend/good/tests',
'zend_bad' => 'hphp/test/zend/bad',
);
if (!isset($mappage[$file])) {
return $file;
}
return hphp_home().'/'.$mappage[$file];
}
function find_tests($files, array $options = null) {
if (!$files) {
$files = array('quick');
}
if ($files == array('all')) {
$files = array('quick', 'slow', 'zend');
}
foreach ($files as &$file) {
$file = map_convenience_filename($file);
if (!@stat($file)) {
error("Not valid file or directory: '$file'");
}
$file = preg_replace(',//+,', '/', realpath($file));
$file = preg_replace(',^'.getcwd().'/,', '', $file);
}
$files = implode(' ', $files);
$tests = explode("\n", shell_exec(
"find $files -name '*.php' -o -name '*.hhas' | grep -v round_trip.hhas"
));
if (!$tests) {
error(usage());
}
asort($tests);
$tests = array_filter($tests);
if (!empty($options['exclude'])) {
$exclude = $options['exclude'];
$tests = array_filter($tests, function($test) use ($exclude) {
return (false === strpos($test, $exclude));
});
}
if (!empty($options['exclude-pattern'])) {
$exclude = $options['exclude-pattern'];
$tests = array_filter($tests, function($test) use ($exclude) {
return !preg_match($exclude, $test);
});
}
if (!empty($options['include'])) {
$include = $options['include'];
$tests = array_filter($tests, function($test) use ($include) {
return (false !== strpos($test, $include));
});
}
if (!empty($options['include-pattern'])) {
$include = $options['include-pattern'];
$tests = array_filter($tests, function($test) use ($include) {
return preg_match($include, $test);
});
}
return $tests;
}
function find_test_ext($test, $ext) {
if (is_file("{$test}.{$ext}")) {
return "{$test}.{$ext}";
}
return find_file_for_dir(dirname($test), "config.{$ext}");
}
function find_file($test, $name) {
return find_file_for_dir(dirname($test), $name);
}
function find_file_for_dir($dir, $name) {
while (($dir !== '.' && $dir !== '/') && is_dir($dir)) {
$file = "$dir/$name";
if (is_file($file)) {
return $file;
}
$dir = dirname($dir);
}
$file = __DIR__.'/'.$name;
if (file_exists($file)) {
return $file;
}
return null;
}
function find_debug_config($test, $name) {
$debug_config = find_file_for_dir(dirname($test), $name);
if ($debug_config !== null) {
return "-m debug --debug-config ".$debug_config;
}
return "";
}
function mode_cmd($options) {
$repo_args = "-vRepo.Local.Mode=-- -vRepo.Central.Path=".verify_hhbc();
$jit_args = "$repo_args -vEval.Jit=true";
$mode = idx($options, 'mode', '');
switch ($mode) {
case '':
case 'jit':
case 'automain':
return "$jit_args";
case 'pgo':
return "$jit_args -vEval.JitPGO=1 -vEval.JitRegionSelector=hottrace ".
"-vEval.JitPGOHotOnly=0";
case 'interp':
return "$repo_args -vEval.Jit=0";
default:
error("-m must be one of jit | pgo | interp | automain. Got: '$mode'");
}
}
function extra_args($options) {
return idx($options, 'args', '');
}
function hhvm_path() {
return idx_file($_ENV, 'HHVM_BIN', bin_root().'/hphp/hhvm/hhvm');
}
// Return the command and the env to run it in.
function hhvm_cmd($options, $test, $test_run = null) {
$use_automain = false;
if ($test_run === null) {
$use_automain = 'automain' === idx($options, 'mode')
&& 'php' === pathinfo($test, PATHINFO_EXTENSION);
$test_run = $use_automain
? __DIR__.'/pseudomain_wrapper.php'
: $test;
}
$cmd = implode(' ', array(
hhvm_path(),
'--config',
find_test_ext($test, 'hdf'),
find_debug_config($test, 'hphpd.ini'),
mode_cmd($options),
'-vEval.EnableArgsInBacktraces=true',
read_file(find_test_ext($test, 'opts')),
isset($options['arm']) ? '-vEval.SimulateARM=1' : '',
extra_args($options),
'-vResourceLimit.CoreFileSize=0',
'--file',
escapeshellarg($test_run),
$use_automain ? escapeshellarg($test) : ''
));
if (file_exists($test.'.ini')) {
$cmd .= " -c $test.ini";
}
$env = $_ENV;
$in = find_test_ext($test, 'in');
if ($in !== null) {
$cmd .= ' < ' . escapeshellarg($in);
// If we're piping the input into the command then setup a simple
// dumb terminal so hhvm doesn't try to control it and pollute the
// output with control characters, which could change depending on
// a wide variety of terminal settings.
$env["TERM"] = "dumb";
}
return array($cmd, $env);
}
function hphp_cmd($options, $test) {
return implode(" ", array(
"HHVM_DISABLE_HHBBC=1",
hhvm_path(),
'--hphp',
'--config',
find_file($test, 'hphp_config.hdf'),
read_file("$test.hphp_opts"),
"-thhbc -l0 -k1 -o $test.repo $test",
));
}
function hhbbc_cmd($options, $test) {
return implode(" ", array(
hhvm_path(),
'--hhbbc',
'--no-logging',
'--parallel-num-threads=1',
read_file("$test.hhbbc_opts"),
"-o $test.repo/hhvm.hhbbc $test.repo/hhvm.hhbc",
));
}
class Status {
private static $results = array();
private static $mode = 0;
private static $use_color = false;
private static $queue = null;
public static $key;
const MODE_NORMAL = 0;
const MODE_VERBOSE = 1;
const MODE_FBMAKE = 2;
const MSG_EXIT = 1;
const MSG_TEST_PASS = 2;
const MSG_TEST_FAIL = 3;
const MSG_TEST_SKIP = 4;
public static function setMode($mode) {
self::$mode = $mode;
}
public static function setUseColor($use) {
self::$use_color = $use;
}
public static function getMode() {
return self::$mode;
}
public static function pass($test) {
array_push(self::$results, array('name' => $test, 'status' => 'passed'));
msg_send(self::getQueue(), self::MSG_TEST_PASS, $test);
}
public static function skip($test, $reason = null) {
msg_send(self::getQueue(), self::MSG_TEST_SKIP, [$test,$reason]);
}
public static function fail($test) {
array_push(self::$results, array(
'name' => $test,
'status' => 'failed',
'details' => (string)@file_get_contents("$test.diff")
));
msg_send(self::getQueue(), self::MSG_TEST_FAIL, $test);
}
public static function sayFBMake($test, $status) {
$start = array('op' => 'start', 'test' => $test);
$end = array('op' => 'test_done', 'test' => $test, 'status' => $status);
if ($status == 'failed') {
$end['details'] = (string)@file_get_contents("$test.diff");
}
self::say($start, $end);
}
public static function getResults() {
return self::$results;
}
/** Output is in the format expected by JsonTestRunner. */
public static function say(/* ... */) {
$data = array_map(function($row) {
return self::jsonEncode($row) . "\n";
}, func_get_args());
fwrite(STDERR, implode("", $data));
}
public static function hasColor() {
return self::$use_color;
}
public static function hasCursorControl() {
return !getenv("TRAVIS");
}
public static function jsonEncode($data) {
// JSON_UNESCAPED_SLASHES is Zend 5.4+
if (defined("JSON_UNESCAPED_SLASHES")) {
return json_encode($data, JSON_UNESCAPED_SLASHES);
}
$json = json_encode($data);
return str_replace('\\/', '/', $json);
}
private static function getQueue() {
if (!self::$queue)
self::$queue = msg_get_queue(self::$key);
return self::$queue;
}
public static function doExit($code) {
msg_send(self::getQueue(), self::MSG_EXIT, [$code,posix_getpid()]);
exit($code);
}
}
function run($options, $tests, $bad_test_file) {
if (isset($options['verbose'])) {
Status::setMode(Status::MODE_VERBOSE);
}
if (isset($options['fbmake'])) {
Status::setMode(Status::MODE_FBMAKE);
}
Status::setUseColor(isset($options['color']) ? true : posix_isatty(STDOUT));
foreach ($tests as $test) {
$status = run_test($options, $test);
if ($status === 'skip') {
Status::skip($test);
} else if ($status === 'skip-norepo') {
Status::skip($test, 'norepo');
} else if ($status === 'skip-onlyrepo') {
Status::skip($test, 'onlyrepo');
} else if ($status) {
Status::pass($test);
} else {
Status::fail($test);
}
}
file_put_contents($bad_test_file, json_encode(Status::getResults()));
foreach (Status::getResults() as $result) {
if ($result['status'] == 'failed') {
return 1;
}
}
return 0;
}
function skip_test($options, $test) {
$skipif_test = find_test_ext($test, 'skipif');
if (!$skipif_test) {
return false;
}
list($hhvm, $_) = hhvm_cmd($options, $test, $skipif_test);
$out = shell_exec($hhvm . ' 2> /dev/null');
$out = trim($out);
return (bool)strlen($out);
}
function dump_hhas_to_temp($hhvm_cmd, $test) {
$tmp_file = $test . '.round_trip.hhas';
system("$hhvm_cmd -vEval.DumpHhas=1 > $tmp_file", $ret);
if ($ret) { echo "system failed\n"; exit(1); }
return $tmp_file;
}
function run_one_config($options, $test, $hhvm, $hhvm_env) {
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w"),
);
if (isset($options['log'])) {
$hhvm_env['TRACE'] = 'printir:2';
$hhvm_env['HPHP_TRACE_FILE'] = $test . '.log';
}
$pipes = null;
$process = proc_open("$hhvm 2>&1", $descriptorspec, $pipes, null, $hhvm_env);
if (!is_resource($process)) {
file_put_contents("$test.diff", "Couldn't invoke $hhvm");
return false;
}
fclose($pipes[0]);
$output = trim(stream_get_contents($pipes[1]));
file_put_contents("$test.out", $output);
fclose($pipes[1]);
// hhvm redirects errors to stdout, so anything on stderr is really bad
$stderr = stream_get_contents($pipes[2]);
if ($stderr) {
file_put_contents(
"$test.diff",
"Test failed because the process wrote on stderr:\n$stderr"
);
return false;
}
fclose($pipes[2]);
proc_close($process);
// Needed for testing non-hhvm binaries that don't actually run the code
// e.g. parser/test/parse_tester.cpp
if ($output == "FORCE PASS") {
return true;
}
if (file_exists("$test.expect")) {
$file = get_expect_file($test, 'expect', $options);
$diff_cmds = "--text -u";
file_put_contents("$test.expect-trimmed",
trim(file_get_contents($file)));
exec("diff --text -u $test.expect-trimmed $test.out > $test.diff 2>&1",
$_, $status);
// unix 0 == success
unlink("$test.expect-trimmed");
return !$status;
} else if (file_exists("$test.expectf")) {
$file = get_expect_file($test, 'expectf', $options);
$wanted_re = trim(file_get_contents($file));
// do preg_quote, but miss out any %r delimited sections
$temp = "";
$r = "%r";
$startOffset = 0;
$length = strlen($wanted_re);
while($startOffset < $length) {
$start = strpos($wanted_re, $r, $startOffset);
if ($start !== false) {
// we have found a start tag
$end = strpos($wanted_re, $r, $start+2);
if ($end === false) {
// unbalanced tag, ignore it.
$end = $start = $length;
}
} else {
// no more %r sections
$start = $end = $length;
}
// quote a non re portion of the string
$temp = $temp.preg_quote(substr($wanted_re, $startOffset,
($start - $startOffset)), '/');
// add the re unquoted.
if ($end > $start) {
$temp = $temp.'('.substr($wanted_re, $start+2, ($end - $start-2)).')';
}
$startOffset = $end + 2;
}
$wanted_re = $temp;
$wanted_re = str_replace(
array('%binary_string_optional%'),
'string',
$wanted_re
);
$wanted_re = str_replace(
array('%unicode_string_optional%'),
'string',
$wanted_re
);
$wanted_re = str_replace(
array('%unicode\|string%', '%string\|unicode%'),
'string',
$wanted_re
);
$wanted_re = str_replace(
array('%u\|b%', '%b\|u%'),
'',
$wanted_re
);
// Stick to basics
$wanted_re = str_replace('%e', '\\' . DIRECTORY_SEPARATOR, $wanted_re);
$wanted_re = str_replace('%s', '[^\r\n]+', $wanted_re);
$wanted_re = str_replace('%S', '[^\r\n]*', $wanted_re);
$wanted_re = str_replace('%a', '.+', $wanted_re);
$wanted_re = str_replace('%A', '.*', $wanted_re);
$wanted_re = str_replace('%w', '\s*', $wanted_re);
$wanted_re = str_replace('%i', '[+-]?\d+', $wanted_re);
$wanted_re = str_replace('%d', '\d+', $wanted_re);
$wanted_re = str_replace('%x', '[0-9a-fA-F]+', $wanted_re);
$wanted_re = str_replace('%f', '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?',
$wanted_re);
$wanted_re = str_replace('%c', '.', $wanted_re);
// %f allows two points "-.0.0" but that is the best *simple* expression
# a poor man's aide for debugging
shell_exec("diff --text -u $test.expectf $test.out > $test.diff 2>&1");
// Normalize newlines
$wanted_re = preg_replace("/(\r\n?|\n)/", "\n", $wanted_re);
$output = preg_replace("/(\r\n?|\n)/", "\n", $output);
return preg_match("/^$wanted_re\$/s", $output);
} else if (file_exists("$test.expectregex")) {
$file = get_expect_file($test, 'expectregex', $options);
$wanted_re = trim(file_get_contents($file));
# a poor man's aide for debugging
shell_exec("diff --text -u $test.expectregex $test.out > $test.diff 2>&1");
return preg_match("/^$wanted_re\$/s", $output);
}
}
function run_test($options, $test) {
if (skip_test($options, $test)) return 'skip';
$test_ext = pathinfo($test, PATHINFO_EXTENSION);
list($hhvm, $hhvm_env) = hhvm_cmd($options, $test);
$hhvm = __DIR__.'/../tools/timeout.sh -t 300 '.$hhvm;
$output = '';
if (isset($options['repo'])) {
if ($test_ext === 'hhas' ||
strpos($hhvm, '-m debug') !== false ||
file_exists($test.'.norepo')) {
return 'skip-norepo';
}
$repodb1 = "$test.repo/hhvm.hhbc";
$repodb2 = "$test.repo/hhvm.hhbbc";
if (file_exists($repodb1)) unlink($repodb1);
if (file_exists($repodb2)) unlink($repodb2);
$hphp = hphp_cmd($options, $test);
$hhbbc = hhbbc_cmd($options, $test);
$output .= shell_exec("$hphp 2>&1");
$output .= shell_exec("$hhbbc 2>&1");
$hhvm .= ' -vRepo.Authoritative=true -vRepo.Commit=0 ';
$hhvm1 = $hhvm."-vRepo.Central.Path=$repodb1";
$hhvm2 = $hhvm."-vRepo.Central.Path=$repodb2";
if (!($ret = run_one_config($options, $test, $hhvm1, $hhvm_env))) {
return $ret;
}
return run_one_config($options, $test, $hhvm2, $hhvm_env);
}
if (file_exists($test.'.onlyrepo')) {
return 'skip-onlyrepo';
}
if (isset($options['hhas-round-trip'])) {
$hhas_temp = dump_hhas_to_temp($hhvm, $test);
list($hhvm, $hhvm_env) = hhvm_cmd($options, $hhas_temp);
}
return run_one_config($options, $test, $hhvm, $hhvm_env);
}
function num_cpus() {
switch(PHP_OS) {
case 'Linux':
$data = file('/proc/stat');
$cores = 0;
foreach($data as $line) {
if (preg_match('/^cpu[0-9]/', $line)) {
$cores++;
}
}
return $cores;
case 'Darwin':
case 'FreeBSD':
return exec('sysctl -n hw.ncpu');
}
return 2; // default when we don't know how to detect
}
function make_header($str) {
return "\n\033[0;33m".$str."\033[0m\n";
}
function print_commands($tests, $options) {
print make_header("Run these by hand:");
foreach ($tests as $test) {
list($command, $_) = hhvm_cmd($options, $test);
if (!isset($options['repo'])) {
print "$command\n";
continue;
}
// How to run without hhbbc:
$command .= " -vRepo.Authoritative=true ";
$hphpc_hhvm = str_replace(verify_hhbc(), "$test.repo/hhvm.hhbc",
$command);
$hphpc_cmds = hphp_cmd($options, $test)."\n";
$hphpc_cmds .= $hphpc_hhvm."\n";
print "$hphpc_cmds\n\n";
// How to run it with hhbbc:
$hhbbc_hhvm = str_replace(verify_hhbc(), "$test.repo/hhvm.hhbbc",
$command);
$hhbbc_cmds = hphp_cmd($options, $test)."\n";
$hhbbc_cmds .= hhbbc_cmd($options, $test)."\n";
$hhbbc_cmds .= $hhbbc_hhvm."\n";
print "$hhbbc_cmds\n";
}
}
function msg_loop($num_tests, $children, $queue) {
$return_value = 0;
$passed = 0;
$skipped = 0;
$failed = 0;
preg_match_all("/columns.([0-9]+);/", strtolower(exec('stty -a |grep columns')), $output);
$cols = $output[1][0];
while (count($children) > 0) {
msg_receive($queue, 0, $type, 1024, $message);
switch ($type) {
case Status::MSG_EXIT:
list($code, $pid) = $message;
pcntl_waitpid($pid, $status);
$return_value |= pcntl_wexitstatus($status);
unset($children[$pid]);
break;
case Status::MSG_TEST_PASS:
$passed++;
$test = $message;
switch (Status::getMode()) {
case Status::MODE_NORMAL:
if (!Status::hasCursorControl()) {
if (Status::hasColor()) {
print "\033[1;32m.\033[0m";
} else {
print ".";
}
}
break;
case Status::MODE_VERBOSE:
if (Status::hasColor()) {
print "$test \033[1;32mpassed\033[0m\n";
} else {
print "$test passed\n";
}
break;
case Status::MODE_FBMAKE:
Status::sayFBMake($test, 'passed');
break;
}
break;
case Status::MSG_TEST_SKIP:
$skipped++;
list($test, $reason) = $message;
switch (Status::getMode()) {
case Status::MODE_NORMAL:
if (!Status::hasCursorControl()) {
if (Status::hasColor()) {
print "\033[1;33ms\033[0m";
} else {
print 's';
}
}
break;
case Status::MODE_VERBOSE:
if (Status::hasColor()) {
if ($reason !== null) {
print "$test \033[1;33mskipped\033[0m ";
print "\033[1;31m$reason\033[0m\n";
} else {
print "$test \033[1;33mskipped\033[0m\n";
}
} else {
if ($reason !== null) {
print "$test skipped\n";
} else {
print "$test skipped - $reason\n";
}
}
break;
}
break;
case Status::MSG_TEST_FAIL:
$failed++;
$test = $message;
switch (Status::getMode()) {
case Status::MODE_NORMAL:
$diff = (string)@file_get_contents($test.'.diff');
if (Status::hasColor()) {
if (Status::hasCursorControl())
print "\033[2K\033[1G";
print "\n\033[0;31m$test\033[0m\n$diff";
} else {
print "\nFAILED: $test\n$diff";
}
break;
case Status::MODE_VERBOSE:
if (Status::hasColor()) {
print "$test \033[0;31mFAILED\033[0m\n";
} else {
print "$test FAILED\n";
}
break;
case Status::MODE_FBMAKE:
Status::sayFBMake($test, 'failed');
break;
}
break;
}
if (Status::getMode() == Status::MODE_NORMAL && Status::hasCursorControl()) {
$total_run = ($skipped + $failed + $passed);
$bar_cols = ($cols - 45);
$passed_ticks = round($bar_cols * ($passed / $num_tests));
$skipped_ticks = round($bar_cols * ($skipped / $num_tests));
$failed_ticks = round($bar_cols * ($failed / $num_tests));
$fill = $bar_cols - ($passed_ticks + $skipped_ticks + $failed_ticks);
if ($fill < 0) $fill = 0;
$fill = str_repeat('-', $fill);
$passed_ticks = str_repeat('#', $passed_ticks);
$skipped_ticks = str_repeat('#', $skipped_ticks);
$failed_ticks = str_repeat('#', $failed_ticks);
print "\033[2K\033[1G[".
"\033[0;32m$passed_ticks".
"\033[33m$skipped_ticks".
"\033[31m$failed_ticks".
"\033[0m$fill] ($total_run/$num_tests) ($skipped skipped, $failed failed)";
}
}
if (Status::getMode() == Status::MODE_NORMAL && Status::hasCursorControl()) {
print "\033[2K\033[1G";
if ($skipped > 0) {
print "$skipped tests \033[1;33mskipped\033[0m\n";
}
}
return $return_value;
}
function print_success($tests, $options) {
print "\nAll tests passed.\n\n".<<<SHIP
| | |
)_) )_) )_)
)___))___))___)\
)____)____)_____)\\
_____|____|____|____\\\__
---------\ SHIP IT /---------
^^^^^ ^^^^^^^^^^^^^^^^^^^^^
^^^^ ^^^^ ^^^ ^^
^^^^ ^^^
SHIP
."\n";
if (isset($options['verbose'])) {
print_commands($tests, $options);
}
}
function print_failure($argv, $results, $options) {
$failed = array();
foreach ($results as $result) {
if ($result['status'] == 'failed') {
$failed[] = $result['name'];
}
}
asort($failed);
print "\n".count($failed)." tests failed\n";
print "(╯°□°)╯︵ ┻━┻\n";
print make_header("See the diffs:").
implode("\n", array_map(
function($test) { return 'cat '.$test.'.diff'; },
$failed))."\n";
$failing_tests_file = !empty($options['failure-file'])
? $options['failure-file']
: tempnam('/tmp', 'test-failures');
file_put_contents($failing_tests_file, implode("\n", $failed)."\n");
print make_header('For xargs, list of failures is available using:').
'cat '.$failing_tests_file."\n";
print_commands($failed, $options);
print make_header("Re-run just the failing tests:").
$argv[0].' '.implode(' ', $failed)."\n";
if (idx($options, 'mode') == 'automain') {
print make_header(
'Automain caveat: wrapper script may change semantics');
print 'The automain wrapper script ('.__DIR__.'/pseudomain_wrapper.php)'
.' may have changed behavior'."\n"
.'(e.g. extra frames in backtraces) by moving code from '
.'pseudo-main to file top-level and adding a wrapper function.'
."\n";
}
}
function main($argv) {
ini_set('pcre.backtrack_limit', PHP_INT_MAX);
list($options, $files) = get_options($argv);
if (isset($options['help'])) {
error(help());
}
$tests = find_tests($files, $options);
hhvm_path(); // check that binary exists
$threads = min(count($tests), idx($options, 'threads', num_cpus() + 1));
if (!isset($options['fbmake'])) {
print "Running ".count($tests)." tests in $threads threads\n";
}
// Try to construct the buckets so the test results are ready in
// approximately alphabetical order
$test_buckets = array();
$i = 0;
foreach ($tests as $test) {
$test_buckets[$i][] = $test;
$i = ($i + 1) % $threads;
}
if (isset($options['verbose'])) {
Status::setMode(Status::MODE_VERBOSE);
}
if (isset($options['fbmake'])) {
Status::setMode(Status::MODE_FBMAKE);
}