-
Notifications
You must be signed in to change notification settings - Fork 59
/
YamlTests.php
300 lines (273 loc) · 12 KB
/
YamlTests.php
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
declare(strict_types=1);
/**
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* OpenSearch PHP client
*
* @link https://github.com/opensearch-project/opensearch-php/
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
* the GNU Lesser General Public License, Version 2.1, at your option.
* See the LICENSE file in the project root for more information.
*/
namespace OpenSearch\Util;
use Exception;
use ParseError;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use stdClass;
class YamlTests
{
public const TEMPLATE_UNIT_TEST = __DIR__ . '/template/test/unit-test-oss';
public const TEMPLATE_UNIT_TEST_SKIPPED = __DIR__ . '/template/test/unit-test-skipped';
public const TEMPLATE_FUNCTION_TEST = __DIR__ . '/template/test/function-test';
public const TEMPLATE_FUNCTION_SKIPPED = __DIR__ . '/template/test/function-skipped';
public const OPENSEARCH_GIT_URL = 'https://github.com/opensearch-project/OpenSearch/tree/%s/rest-api-spec/src/main/resources/rest-api-spec/test/%s';
public const SKIPPED_TEST = [
'Cat\Nodeattrs\_10_BasicTest::TestCatNodesAttrsOutput' => 'Regexp error, it seems not compatible with PHP',
'Cat\Shards\_10_BasicTest::TestCatShardsOutput' => 'Regexp error, it seems not compatible with PHP',
'Search\Aggregation\_10_HistogramTest::HistogramProfiler' => "Error reading 'n' field from YAML",
'Indices\GetAlias\_10_BasicTest::GetAliasAgainstClosedIndices' => 'Failed asserting that true is false'
];
public const PHP_RESERVED_WORDS = [
'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch',
'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do',
'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach',
'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final',
'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements',
'include', 'include_once', 'instanceof', 'insteadof', 'interface',
'isset', 'list', 'namespace', 'new', 'or', 'print', 'private',
'protected', 'public', 'require', 'require_once', 'return', 'static',
'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor'
];
private $tests = [];
private $testOutput;
private $testDir;
public static $esVersion;
public static $minorEsVersion;
public static $testSuite;
public function __construct(string $testDir, string $testOutput, string $esVersion, string $testSuite)
{
if (!is_dir($testDir)) {
throw new Exception(sprintf(
"The directory %s specified does not exist",
$testDir
));
}
if (file_exists($testOutput)) {
$this->removeDirectory($testOutput);
}
self::$testSuite = ucfirst($testSuite);
$this->testOutput = sprintf("%s/%s", $testOutput, self::$testSuite);
$this->testDir = $testDir;
$this->tests = $this->getAllTests($testDir);
self::$esVersion = $esVersion;
list($major, $minor, $patch) = explode('.', self::$esVersion);
self::$minorEsVersion = sprintf("%s.%s", $major, $minor);
}
private function getAllTests(string $dir): array
{
$it = new RecursiveDirectoryIterator($dir);
$parsed = [];
// Iterate over the Yaml test files
foreach (new RecursiveIteratorIterator($it) as $file) {
if ($file->getExtension() === 'yml') {
$test = yaml_parse_file($file->getPathname(), -1, $ndocs, [
YAML_MAP_TAG => function ($value, $tag, $flags) {
return empty($value) ? new stdClass() : $value;
}
]);
if (false === $test) {
throw new Exception(sprintf(
"YAML parse error file %s",
$file->getPathname()
));
}
$parsed[$file->getPathname()] = $test;
}
}
return $parsed;
}
public function build(): array
{
$numTest = 0;
$numFile = 0;
foreach ($this->tests as $testFile => $value) {
$namespace = $this->extractTestNamespace($testFile);
$testName = $this->extractTestName($testFile);
$yamlFileName = substr($testFile, strlen($this->testDir) + 1);
# Delete and create the output directory
$testDirName = sprintf("%s/%s", $this->testOutput, str_replace('\\', '/', $namespace));
if (!is_dir($testDirName) && !mkdir($testDirName, 0777, true) && !is_dir($testDirName)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $testDirName));
}
$functions = '';
$setup = '';
$teardown = '';
$alreadyAssignedNames = [];
$allSkipped = false;
foreach ($value as $test) {
if (!is_array($test)) {
continue;
}
foreach ($test as $name => $actions) {
switch ($name) {
case 'setup':
$setup = (string) new ActionTest($actions);
break;
case 'teardown':
$teardown = (string) new ActionTest($actions);
break;
default:
$functionName = $this->filterFunctionName(ucwords($name), $alreadyAssignedNames);
$alreadyAssignedNames[] = $functionName;
$skippedTest = sprintf("%s\\%s::%s", $namespace, $testName, $functionName);
$skippedAllTest = sprintf("%s\\%s::*", $namespace, $testName);
$skippedAllFiles = sprintf("%s\\*", $namespace);
$skip = self::SKIPPED_TEST;
if (isset($skip[$skippedAllFiles]) || isset($skip[$skippedAllTest])) {
$allSkipped = true;
$functions .= self::render(
self::TEMPLATE_FUNCTION_SKIPPED,
[
':name' => $functionName,
':skipped_msg' => $skip[$skippedAllTest]
]
);
} elseif (isset($skip[$skippedTest])) {
$functions .= self::render(
self::TEMPLATE_FUNCTION_SKIPPED,
[
':name' => $functionName,
':skipped_msg' => $skip[$skippedTest]
]
);
} else {
$functions .= self::render(
self::TEMPLATE_FUNCTION_TEST,
[
':name' => $functionName,
':test' => (string) new ActionTest($actions)
]
);
}
$numTest++;
}
}
}
if ($allSkipped) {
$test = self::render(
self::TEMPLATE_UNIT_TEST_SKIPPED,
[
':namespace' => sprintf("OpenSearch\Tests\Yaml\%s\%s", self::$testSuite, $namespace),
':test-name' => $testName,
':tests' => $functions,
':yamlfile' => sprintf(self::OPENSEARCH_GIT_URL, self::$minorEsVersion, $yamlFileName),
':group' => strtolower(self::$testSuite)
]
);
} else {
$test = self::render(
self::TEMPLATE_UNIT_TEST,
[
':namespace' => sprintf("OpenSearch\Tests\Yaml\%s\%s", self::$testSuite, $namespace),
':test-name' => $testName,
':tests' => $functions,
':setup' => $setup,
':teardown' => $teardown,
':yamlfile' => sprintf(self::OPENSEARCH_GIT_URL, self::$minorEsVersion, $yamlFileName),
':group' => strtolower(self::$testSuite)
]
);
}
file_put_contents($testDirName . '/' . $testName . '.php', $test);
try {
eval(substr($test, 5)); // remove <?php header
} catch (ParseError $e) {
throw new Exception(sprintf(
"The PHP code generate in %s not valid: %s",
$testDirName . '/' . $testName . '.php',
$e->getMessage()
));
}
$numFile++;
}
return [
'tests' => $numTest,
'files' => $numFile
];
}
private function extractTestNamespace(string $path)
{
$file = substr($path, strlen($this->testDir) + 1);
$last = strrpos($file, '/', -1);
$namespace = substr($file, 0, $last);
$namespace = ucwords($namespace, '._/-');
$namespace = str_replace(['.', '_', '/', '-'], ['\\', '', '\\', ''], ucwords($namespace, '.'));
// Check if a PHP reserved word is present in the namespace
$parts = explode('\\', $namespace);
foreach ($parts as $part) {
if (in_array(strtolower($part), self::PHP_RESERVED_WORDS)) {
$namespace = str_replace($part, $part . '_', $namespace);
}
}
return $namespace;
}
private function extractTestName(string $path): string
{
$file = substr($path, strlen($this->testDir) + 1);
$last = strrpos($file, '/', -1);
$testName = substr($file, $last + 1, -4);
$testName = ucwords($testName, '_-');
$testName = str_replace('-', '', $testName);
return '_' . $testName . 'Test';
}
public static function render(string $fileName, array $params = []): string
{
if (!is_file($fileName)) {
throw new Exception(sprintf(
"The file %s is not valid",
$fileName
));
}
$output = file_get_contents($fileName);
foreach ($params as $name => $value) {
if (is_array($value)) {
$value = var_export($value, true);
} elseif ($value instanceof \stdClass) {
$value = 'new \stdClass';
} elseif (is_numeric($value)) {
$value = (string) $value;
}
$output = str_replace($name, $value, $output);
}
return $output;
}
private function removeDirectory($directory)
{
foreach (glob("{$directory}/*") as $file) {
if (is_dir($file)) {
$this->removeDirectory($file);
} else {
unlink($file);
}
}
if (is_dir($directory)) {
rmdir($directory);
}
}
private function filterFunctionName(string $name, array $alreadyAssigned = []): string
{
$result = preg_replace("/[^a-zA-Z0-9_]/", "", $name);
while (in_array($result, $alreadyAssigned)) {
$result .= '_';
}
return $result;
}
}