xref: /php-src/scripts/dev/bless_tests.php (revision 1c30c5e7)
1#!/usr/bin/env php
2<?php
3
4if ($argc < 2) {
5    die("Usage: php bless_tests.php dir/\n");
6}
7
8$files = getFiles(array_slice($argv, 1));
9foreach ($files as $path) {
10    if (!preg_match('/^(.*)\.phpt$/', $path, $matches)) {
11        // Not a phpt test
12        continue;
13    }
14
15    $outPath = $matches[1] . '.out';
16    if (!file_exists($outPath)) {
17        // Test did not fail
18        continue;
19    }
20
21    $phpt = file_get_contents($path);
22    $out = file_get_contents($outPath);
23
24    if (false !== strpos($phpt, '--XFAIL--')) {
25        // Don't modify expected output of XFAIL tests
26        continue;
27    }
28
29    // Don't update EXPECTREGEX tests
30    if (!preg_match('/--EXPECT(F?)--(.*)$/s', $phpt, $matches)) {
31        continue;
32    }
33
34    $oldExpect = trim($matches[2]);
35    $isFormat = $matches[1] == 'F';
36    if ($isFormat) {
37        $out = generateMinimallyDifferingOutput($out, $oldExpect);
38    } else {
39        $out = normalizeOutput($out);
40    }
41
42    $phpt = insertOutput($phpt, $out);
43    file_put_contents($path, $phpt);
44}
45
46function getFiles(array $dirsOrFiles): \Iterator {
47    foreach ($dirsOrFiles as $dirOrFile) {
48        if (is_dir($dirOrFile)) {
49            $it = new RecursiveIteratorIterator(
50                new RecursiveDirectoryIterator($dirOrFile),
51                RecursiveIteratorIterator::LEAVES_ONLY
52            );
53            foreach ($it as $file) {
54                yield $file->getPathName();
55            }
56        } else if (is_file($dirOrFile)) {
57            yield $dirOrFile;
58        } else {
59            die("$dirOrFile is not a directory or file\n");
60        }
61    }
62}
63
64function normalizeOutput(string $out): string {
65    $out = preg_replace('/in (\/|[A-Z]:\\\\)\S+ on line \d+/m', 'in %s on line %d', $out);
66    $out = preg_replace('/in (\/|[A-Z]:\\\\)\S+:\d+/m', 'in %s:%d', $out);
67    $out = preg_replace('/\{closure:(\/|[A-Z]:\\\\)\S+:\d+\}/', '{closure:%s:%d}', $out);
68    $out = preg_replace('/object\(([A-Za-z0-9]*)\)#\d+/', 'object($1)#%d', $out);
69    $out = preg_replace('/^#(\d+) (\/|[A-Z]:\\\\)\S+\(\d+\):/m', '#$1 %s(%d):', $out);
70    $out = preg_replace('/Resource id #\d+/', 'Resource id #%d', $out);
71    $out = preg_replace('/resource\(\d+\) of type/', 'resource(%d) of type', $out);
72    $out = preg_replace(
73        '/Resource ID#\d+ used as offset, casting to integer \(\d+\)/',
74        'Resource ID#%d used as offset, casting to integer (%d)',
75        $out);
76    $out = preg_replace('/string\(\d+\) "([^"]*%d)/', 'string(%d) "$1', $out);
77    $out = str_replace("\0", '%0', $out);
78    return $out;
79}
80
81function formatToRegex(string $format): string {
82    $result = preg_quote($format, '/');
83    $result = str_replace('%e', '\\' . DIRECTORY_SEPARATOR, $result);
84    $result = str_replace('%s', '[^\r\n]+', $result);
85    $result = str_replace('%S', '[^\r\n]*', $result);
86    $result = str_replace('%w', '\s*', $result);
87    $result = str_replace('%i', '[+-]?\d+', $result);
88    $result = str_replace('%d', '\d+', $result);
89    $result = str_replace('%x', '[0-9a-fA-F]+', $result);
90    $result = str_replace('%f', '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?', $result);
91    $result = str_replace('%c', '.', $result);
92    $result = str_replace('%0', '\0', $result);
93    return "/^$result$/s";
94}
95
96function generateMinimallyDifferingOutput(string $out, string $oldExpect) {
97    $outLines = explode("\n", $out);
98    $oldExpectLines = explode("\n", $oldExpect);
99    $differ = new Differ(function($oldExpect, $new) {
100        if (strpos($oldExpect, '%') === false) {
101            return $oldExpect === $new;
102        }
103        return preg_match(formatToRegex($oldExpect), $new);
104    });
105    $diff = $differ->diff($oldExpectLines, $outLines);
106
107    $result = [];
108    foreach ($diff as $elem) {
109        if ($elem->type == DiffElem::TYPE_KEEP) {
110            $result[] = $elem->old;
111        } else if ($elem->type == DiffElem::TYPE_ADD) {
112            $result[] = normalizeOutput($elem->new);
113        }
114    }
115    return implode("\n", $result);
116}
117
118function insertOutput(string $phpt, string $out): string {
119    return preg_replace_callback('/--EXPECTF?--.*?(--CLEAN--|$)/sD', function($matches) use($out) {
120        $hasWildcard = preg_match('/%[resSaAwidxfc0]/', $out);
121        $F = $hasWildcard ? 'F' : '';
122        return "--EXPECT$F--\n" . $out . "\n" . $matches[1];
123    }, $phpt);
124}
125
126/**
127 * Implementation of the the Myers diff algorithm.
128 *
129 * Myers, Eugene W. "An O (ND) difference algorithm and its variations."
130 * Algorithmica 1.1 (1986): 251-266.
131 */
132
133class DiffElem
134{
135    const TYPE_KEEP = 0;
136    const TYPE_REMOVE = 1;
137    const TYPE_ADD = 2;
138
139    /** @var int One of the TYPE_* constants */
140    public $type;
141    /** @var mixed Is null for add operations */
142    public $old;
143    /** @var mixed Is null for remove operations */
144    public $new;
145
146    public function __construct(int $type, $old, $new) {
147        $this->type = $type;
148        $this->old = $old;
149        $this->new = $new;
150    }
151}
152
153class Differ
154{
155    private $isEqual;
156
157    /**
158     * Create differ over the given equality relation.
159     *
160     * @param callable $isEqual Equality relation with signature function($a, $b) : bool
161     */
162    public function __construct(callable $isEqual) {
163        $this->isEqual = $isEqual;
164    }
165
166    /**
167     * Calculate diff (edit script) from $old to $new.
168     *
169     * @param array $old Original array
170     * @param array $new New array
171     *
172     * @return DiffElem[] Diff (edit script)
173     */
174    public function diff(array $old, array $new) {
175        list($trace, $x, $y) = $this->calculateTrace($old, $new);
176        return $this->extractDiff($trace, $x, $y, $old, $new);
177    }
178
179    private function calculateTrace(array $a, array $b) {
180        $n = \count($a);
181        $m = \count($b);
182        $max = $n + $m;
183        $v = [1 => 0];
184        $trace = [];
185        for ($d = 0; $d <= $max; $d++) {
186            $trace[] = $v;
187            for ($k = -$d; $k <= $d; $k += 2) {
188                if ($k === -$d || ($k !== $d && $v[$k-1] < $v[$k+1])) {
189                    $x = $v[$k+1];
190                } else {
191                    $x = $v[$k-1] + 1;
192                }
193
194                $y = $x - $k;
195                while ($x < $n && $y < $m && ($this->isEqual)($a[$x], $b[$y])) {
196                    $x++;
197                    $y++;
198                }
199
200                $v[$k] = $x;
201                if ($x >= $n && $y >= $m) {
202                    return [$trace, $x, $y];
203                }
204            }
205        }
206        throw new \Exception('Should not happen');
207    }
208
209    private function extractDiff(array $trace, int $x, int $y, array $a, array $b) {
210        $result = [];
211        for ($d = \count($trace) - 1; $d >= 0; $d--) {
212            $v = $trace[$d];
213            $k = $x - $y;
214
215            if ($k === -$d || ($k !== $d && $v[$k-1] < $v[$k+1])) {
216                $prevK = $k + 1;
217            } else {
218                $prevK = $k - 1;
219            }
220
221            $prevX = $v[$prevK];
222            $prevY = $prevX - $prevK;
223
224            while ($x > $prevX && $y > $prevY) {
225                $result[] = new DiffElem(DiffElem::TYPE_KEEP, $a[$x-1], $b[$y-1]);
226                $x--;
227                $y--;
228            }
229
230            if ($d === 0) {
231                break;
232            }
233
234            while ($x > $prevX) {
235                $result[] = new DiffElem(DiffElem::TYPE_REMOVE, $a[$x-1], null);
236                $x--;
237            }
238
239            while ($y > $prevY) {
240                $result[] = new DiffElem(DiffElem::TYPE_ADD, null, $b[$y-1]);
241                $y--;
242            }
243        }
244        return array_reverse($result);
245    }
246}
247