This repository was archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathreplace_expect.php
More file actions
executable file
·112 lines (85 loc) · 3.42 KB
/
Copy pathreplace_expect.php
File metadata and controls
executable file
·112 lines (85 loc) · 3.42 KB
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
#!/usr/bin/env php
<?php declare(strict_types=1);
/**
* This file is part of the phpv8/php-v8 PHP extension.
*
* Copyright (c) 2015-2018 Bogdan Padalko <thepinepain@gmail.com>
*
* Licensed under the MIT license: http://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the
* LICENSE file that was distributed with this source or visit
* http://opensource.org/licenses/MIT
*/
$tests_dir = realpath(__DIR__ . '/../tests');
$mode = 'write';
$args = $argv;
unset($args[0]);
foreach ($argv as $i => $item) {
if ($item == '--pretend') {
$mode = 'pretend';
unset($args[$i]);
}
}
if (count($args) > 1) {
echo 'Invalid options', PHP_EOL;
exit(1);
}
if ($args) {
$mask = str_replace(['tests/', '.phpt', '.diff'], '', array_pop($args));
} else {
$mask = '*';
}
$iterator = new GlobIterator($tests_dir . "/{$mask}.out", FilesystemIterator::KEY_AS_FILENAME);
foreach ($iterator as $item) {
//var_dump($item);
$out_file = $iterator->key();
$base_name = preg_replace('/\.out$/i', '', $iterator->key());
$test_file = $base_name . '.phpt';
$test_content = file_get_contents($tests_dir . '/' . $test_file);
if (false !== ($pos = strpos($test_content, '--EXPECT--'))) {
printf("--EXPECT-- [%s]" . PHP_EOL, $iterator->key());
$test_content = substr($test_content, 0, $pos);
$test_content .= '--EXPECT--' . PHP_EOL;
$test_content .= file_get_contents($tests_dir . '/' . $out_file);
$test_content .= PHP_EOL;
file_put_contents($tests_dir . '/' . $test_file, $test_content);
foreach (['.diff', '.exp', '.log', '.mem', '.out', '.php', '.sh'] as $ext) {
@unlink($tests_dir . '/' . $base_name . $ext);
}
continue;
//} elseif (0) {
} elseif (false !== ($pos = strpos($test_content, '--EXPECTF--'))) {
printf("--EXPECTF-- [%s]" . PHP_EOL, $iterator->key());
// get replacements
$tests = substr($test_content, 0, $pos);
$result = file_get_contents($tests_dir . '/' . $out_file);
preg_match_all('#// EXPECTF: \-\-\-(.+)#', $tests, $expectf_search);
preg_match_all('#// EXPECTF: \+\+\+(.+)#', $tests, $expectf_replace);
if (count($expectf_search) != count($expectf_replace)) {
printf("please, edit manually [%s]: searches and replaces count doesn't match" . PHP_EOL, $iterator->key());
continue;
}
foreach (array_combine($expectf_search[1], $expectf_replace[1]) as $search => $replace) {
$replace = str_replace('\\\\n', '!!(ಠ_ಠ)!!', $replace);
$replace = str_replace('\\n', "\n", $replace);
$replace = str_replace('!!(ಠ_ಠ)!!', '\\\\n', $replace);
$result = preg_replace($search, $replace, $result);
}
$test_content = $tests;
$test_content .= '--EXPECTF--' . PHP_EOL;
$test_content .= $result;
$test_content .= PHP_EOL;
if ($mode == 'pretend') {
echo $result, PHP_EOL;
echo PHP_EOL;
} elseif ($mode = 'write') {
file_put_contents($tests_dir . '/' . $test_file, $test_content);
foreach (['.diff', '.exp', '.log', '.mem', '.out', '.php', '.sh'] as $ext) {
@unlink($tests_dir . '/' . $base_name . $ext);
}
}
continue;
}
printf("please, edit manually [%s]" . PHP_EOL, $iterator->key());
}