xref: /PHP-8.1/scripts/dev/bless_tests.php (revision ea256a21)
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]:\\\\).+ on line \d+$/m', 'in %s on line %d', $out);
66    $out = preg_replace('/in (\/|[A-Z]:\\\\).+:\d+$/m', 'in %s:%d', $out);
67    $out = preg_replace('/^#(\d+) (\/|[A-Z]:\\\\).+\(\d+\):/m', '#$1 %s(%d):', $out);
68    $out = preg_replace('/Resource id #\d+/', 'Resource id #%d', $out);
69    $out = preg_replace('/resource\(\d+\) of type/', 'resource(%d) of type', $out);
70    $out = preg_replace(
71        '/Resource ID#\d+ used as offset, casting to integer \(\d+\)/',
72        'Resource ID#%d used as offset, casting to integer (%d)',
73        $out);
74    $out = preg_replace('/string\(\d+\) "([^"]*%d)/', 'string(%d) "$1', $out);
75    $out = str_replace("\0", '%0', $out);
76    return $out;
77}
78
79function formatToRegex(string $format): string {
80    $result = preg_quote($format, '/');
81    $result = str_replace('%e', '\\' . DIRECTORY_SEPARATOR, $result);
82    $result = str_replace('%s', '[^\r\n]+', $result);
83    $result = str_replace('%S', '[^\r\n]*', $result);
84    $result = str_replace('%w', '\s*', $result);
85    $result = str_replace('%i', '[+-]?\d+', $result);
86    $result = str_replace('%d', '\d+', $result);
87    $result = str_replace('%x', '[0-9a-fA-F]+', $result);
88    $result = str_replace('%f', '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?', $result);
89    $result = str_replace('%c', '.', $result);
90    $result = str_replace('%0', '\0', $result);
91    return "/^$result$/s";
92}
93
94function generateMinimallyDifferingOutput(string $out, string $oldExpect) {
95    $outLines = explode("\n", $out);
96    $oldExpectLines = explode("\n", $oldExpect);
97    $differ = new Differ(function($oldExpect, $new) {
98        if (strpos($oldExpect, '%') === false) {
99            return $oldExpect === $new;
100        }
101        return preg_match(formatToRegex($oldExpect), $new);
102    });
103    $diff = $differ->diff($oldExpectLines, $outLines);
104
105    $result = [];
106    foreach ($diff as $elem) {
107        if ($elem->type == DiffElem::TYPE_KEEP) {
108            $result[] = $elem->old;
109        } else if ($elem->type == DiffElem::TYPE_ADD) {
110            $result[] = normalizeOutput($elem->new);
111        }
112    }
113    return implode("\n", $result);
114}
115
116function insertOutput(string $phpt, string $out): string {
117    return preg_replace_callback('/--EXPECTF?--.*?(--CLEAN--|$)/sD', function($matches) use($out) {
118        $hasWildcard = preg_match('/%[resSaAwidxfc0]/', $out);
119        $F = $hasWildcard ? 'F' : '';
120        return "--EXPECT$F--\n" . $out . "\n" . $matches[1];
121    }, $phpt);
122}
123
124/**
125 * Implementation of the the Myers diff algorithm.
126 *
127 * Myers, Eugene W. "An O (ND) difference algorithm and its variations."
128 * Algorithmica 1.1 (1986): 251-266.
129 */
130
131class DiffElem
132{
133    const TYPE_KEEP = 0;
134    const TYPE_REMOVE = 1;
135    const TYPE_ADD = 2;
136
137    /** @var int One of the TYPE_* constants */
138    public $type;
139    /** @var mixed Is null for add operations */
140    public $old;
141    /** @var mixed Is null for remove operations */
142    public $new;
143
144    public function __construct(int $type, $old, $new) {
145        $this->type = $type;
146        $this->old = $old;
147        $this->new = $new;
148    }
149}
150
151class Differ
152{
153    private $isEqual;
154
155    /**
156     * Create differ over the given equality relation.
157     *
158     * @param callable $isEqual Equality relation with signature function($a, $b) : bool
159     */
160    public function __construct(callable $isEqual) {
161        $this->isEqual = $isEqual;
162    }
163
164    /**
165     * Calculate diff (edit script) from $old to $new.
166     *
167     * @param array $old Original array
168     * @param array $new New array
169     *
170     * @return DiffElem[] Diff (edit script)
171     */
172    public function diff(array $old, array $new) {
173        list($trace, $x, $y) = $this->calculateTrace($old, $new);
174        return $this->extractDiff($trace, $x, $y, $old, $new);
175    }
176
177    private function calculateTrace(array $a, array $b) {
178        $n = \count($a);
179        $m = \count($b);
180        $max = $n + $m;
181        $v = [1 => 0];
182        $trace = [];
183        for ($d = 0; $d <= $max; $d++) {
184            $trace[] = $v;
185            for ($k = -$d; $k <= $d; $k += 2) {
186                if ($k === -$d || ($k !== $d && $v[$k-1] < $v[$k+1])) {
187                    $x = $v[$k+1];
188                } else {
189                    $x = $v[$k-1] + 1;
190                }
191
192                $y = $x - $k;
193                while ($x < $n && $y < $m && ($this->isEqual)($a[$x], $b[$y])) {
194                    $x++;
195                    $y++;
196                }
197
198                $v[$k] = $x;
199                if ($x >= $n && $y >= $m) {
200                    return [$trace, $x, $y];
201                }
202            }
203        }
204        throw new \Exception('Should not happen');
205    }
206
207    private function extractDiff(array $trace, int $x, int $y, array $a, array $b) {
208        $result = [];
209        for ($d = \count($trace) - 1; $d >= 0; $d--) {
210            $v = $trace[$d];
211            $k = $x - $y;
212
213            if ($k === -$d || ($k !== $d && $v[$k-1] < $v[$k+1])) {
214                $prevK = $k + 1;
215            } else {
216                $prevK = $k - 1;
217            }
218
219            $prevX = $v[$prevK];
220            $prevY = $prevX - $prevK;
221
222            while ($x > $prevX && $y > $prevY) {
223                $result[] = new DiffElem(DiffElem::TYPE_KEEP, $a[$x-1], $b[$y-1]);
224                $x--;
225                $y--;
226            }
227
228            if ($d === 0) {
229                break;
230            }
231
232            while ($x > $prevX) {
233                $result[] = new DiffElem(DiffElem::TYPE_REMOVE, $a[$x-1], null);
234                $x--;
235            }
236
237            while ($y > $prevY) {
238                $result[] = new DiffElem(DiffElem::TYPE_ADD, null, $b[$y-1]);
239                $y--;
240            }
241        }
242        return array_reverse($result);
243    }
244}
245