|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +if ($argc < 2) { |
| 5 | + die("Usage: php bless_tests.php dir/"); |
| 6 | +} |
| 7 | + |
| 8 | +$dir = $argv[1]; |
| 9 | +$it = new RecursiveIteratorIterator( |
| 10 | + new RecursiveDirectoryIterator($dir), |
| 11 | + RecursiveIteratorIterator::LEAVES_ONLY |
| 12 | +); |
| 13 | +foreach ($it as $file) { |
| 14 | + $path = $file->getPathName(); |
| 15 | + if (!preg_match('/^(.*)\.phpt$/', $path, $matches)) { |
| 16 | + // Not a phpt test |
| 17 | + continue; |
| 18 | + } |
| 19 | + |
| 20 | + $outPath = $matches[1] . '.out'; |
| 21 | + if (!file_exists($outPath)) { |
| 22 | + // Test did not fail |
| 23 | + continue; |
| 24 | + } |
| 25 | + |
| 26 | + $phpt = file_get_contents($path); |
| 27 | + if (false !== strpos($phpt, '--XFAIL--')) { |
| 28 | + // Don't modify expected output of XFAIL tests |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + $out = file_get_contents($outPath); |
| 33 | + $out = normalizeOutput($out); |
| 34 | + $phpt = insertOutput($phpt, $out); |
| 35 | + file_put_contents($path, $phpt); |
| 36 | +} |
| 37 | + |
| 38 | +function normalizeOutput(string $out): string { |
| 39 | + $out = preg_replace('/in \/.+ on line \d+/', 'in %s on line %d', $out); |
| 40 | + $out = preg_replace('/Resource id #\d+/', 'Resource id #%d', $out); |
| 41 | + return $out; |
| 42 | +} |
| 43 | + |
| 44 | +function insertOutput(string $phpt, string $out): string { |
| 45 | + return preg_replace_callback('/--EXPECTF?--.*$/s', function($matches) use($out) { |
| 46 | + $F = strpos($out, '%') !== false ? 'F' : ''; |
| 47 | + return "--EXPECT$F--\n" . $out . "\n"; |
| 48 | + }, $phpt); |
| 49 | +} |
0 commit comments