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