-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathTest.php
More file actions
457 lines (381 loc) · 10.2 KB
/
Test.php
File metadata and controls
457 lines (381 loc) · 10.2 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
declare(strict_types=1);
require __DIR__ . '/FileDetector.php';
/** @var string[] */
$FailingTests = [];
/** @var array<string, array<string, string|string[]>>|false */
$Rulesets = parse_ini_file( __DIR__ . '/../rules.ini', true, INI_SCANNER_RAW );
$AllowedNameCharacters = '/^[a-zA-Z0-9_-]+$/';
if( empty( $Rulesets ) )
{
throw new \RuntimeException( 'rules.ini failed to parse' );
}
foreach( $Rulesets as $Type => $Rules )
{
$SortTest = TestSorting( $Rules );
if( $SortTest !== null )
{
$FailingTests[] = "{$Type}: Rules should be sorted in case insensitive natural order, {$SortTest}";
}
foreach( $Rules as $Name => $RuleRegexes )
{
if( preg_match( '/^[a-zA-Z0-9_-]+$/', $Name, $Matches ) !== 1 )
{
$FailingTests[] = "$Type.$Name has disallowed characters (allowed: {$AllowedNameCharacters})";
}
if( !is_array( $RuleRegexes ) )
{
$RuleRegexes = [ $RuleRegexes ];
}
else if( count( $RuleRegexes ) === 1 )
{
$FailingTests[] = "$Type.$Name is an array for no reason, remove []";
}
foreach( $RuleRegexes as $Regex )
{
if( RegexHasCapturingGroups( $Regex ) )
{
$FailingTests[] = "$Type.$Name: Regex \"$Regex\" contains a capturing group";
}
$RegexIssue = CheckRegexPerformance( $Regex );
if( $RegexIssue !== null )
{
$FailingTests[] = "$Type.$Name: Regex \"$Regex\" $RegexIssue";
}
}
}
}
$Detector = new FileDetector( $Rulesets, null );
$Detector->FilterEvidenceMatches = false;
/** @var array<string, bool> */
$SeenTestTypes = [];
TestBasicRules( $Detector, $SeenTestTypes, $FailingTests );
TestFilelists( $Detector, $SeenTestTypes, $FailingTests );
TestDescriptions( $SeenTestTypes, $FailingTests );
TestExtra( $Detector, $FailingTests );
// Really basic code to find extra detections that aren't specified in rules.ini
$Code = file_get_contents( __DIR__ . '/FileDetector.php' );
if( $Code === false )
{
throw new \RuntimeException( 'Failed to read FileDetector.php' );
}
preg_match_all( '/[\'"](?<string>(?:' . implode( '|', array_keys( $Rulesets ) ) . ')\.\w+)[\'"]/', $Code, $Matches );
$AllFoundTestTypes = array_unique( array_merge( $Detector->Map, $Matches[ 'string' ] ?? [] ) );
foreach( $AllFoundTestTypes as $TestType )
{
if( !isset( $SeenTestTypes[ $TestType ] ) )
{
$FailingTests[] = "\"$TestType\" does not have any tests";
}
$File = __DIR__ . '/../descriptions/' . $TestType . '.md';
if( !file_exists( $File ) )
{
if( !str_starts_with( $TestType, 'Evidence.' ) )
{
$FailingTests[] = "\"descriptions/{$TestType}.md\" does not exist";
}
continue;
}
$Text = file_get_contents( $File );
if( $Text === false )
{
throw new \RuntimeException( 'Failed to read ' . $File );
}
if( str_contains( $Text, 'steamdb.info' ) )
{
$FailingTests[] = "\"descriptions/{$TestType}.md\" - SteamDB links in descriptions should be relative (do not include the domain)";
}
}
if( !empty( $FailingTests ) )
{
echo count( $FailingTests ) . " tests failed.\n";
foreach( $FailingTests as $Test )
{
err( $Test );
}
exit( 1 );
}
else
{
echo "All tests have passed.\n";
}
function RegexHasCapturingGroups( string $regex ) : bool
{
// From https://github.com/nikic/FastRoute/blob/dafa1911fd7c1560c64d19556cbd4c599fed15ea/src/DataGenerator/RegexBasedAbstract.php#L181
if( strpos( $regex, '(' ) === false )
{
// Needs to have at least a ( to contain a capturing group
return false;
}
// Semi-accurate detection for capturing groups
return (bool)preg_match(
'~
(?:
\(\?\(
| \[ [^\]\\\\]* (?: \\\\ . [^\]\\\\]* )* \]
| \\\\ .
) (*SKIP)(*FAIL) |
\(
(?!
\? (?! <(?![!=]) | P< | \' )
| \*
)
~x',
$regex
);
}
function CheckRegexPerformance( string $regex ) : ?string
{
// Check for .+ or .*, skip if it's inside a character class like [.+]
if( preg_match( '/(?<!\[)\.[\+\*]/', $regex ) )
{
return 'uses .+ or .* which is inefficient';
}
// Patterns like (.+)+ or (a*)*
if( preg_match( '/\([^)]*[\+\*][^)]*\)[\+\*]/', $regex ) )
{
return 'has nested quantifiers';
}
// Quantifier applied to anchor
if( preg_match( '/[\^\$][\+\*\?]/', $regex ) )
{
return 'has quantifier on anchor';
}
// Empty alternation like (|foo) or (foo|)
if( preg_match( '/\(\||\|\)/', $regex ) )
{
return 'has empty alternation';
}
return null;
}
/**
* @param array<string, string|string[]> $Rulesets
*/
function TestSorting( array $Rulesets ) : ?string
{
$Sorted = $Rulesets;
uksort( $Sorted, fn( string $a, string $b ) : int => strnatcasecmp( $a, $b ) );
if( $Rulesets !== $Sorted )
{
$gamesKeys = array_keys( $Rulesets );
$gamesSortedKeys = array_keys( $Sorted );
$cachedCount = count( $gamesKeys );
for( $i = 0; $i < $cachedCount; ++$i )
{
if( $gamesKeys[ $i ] === $gamesSortedKeys[ $i ] )
{
continue;
}
$sortedPosition = array_search( $gamesKeys[ $i ], $gamesSortedKeys, true );
$actualPosition = array_search( $gamesSortedKeys[ $i ], $gamesKeys, true );
if( $sortedPosition === false || $actualPosition === false )
{
return "Failed to find position of \"{$gamesKeys[ $i ]}\" in sorted array";
}
$shouldBe = $gamesSortedKeys[ $sortedPosition - 1 ];
if( $actualPosition > $sortedPosition )
{
return "\"{$shouldBe}\" should be before \"{$gamesKeys[ $i ]}\"";
}
return "\"{$gamesKeys[ $i ]}\" should be after \"{$shouldBe}\"";
}
}
return null;
}
/**
* @param array<string, bool> $SeenTestTypes
* @param string[] $FailingTests
*/
function TestDescriptions( array $SeenTestTypes, array &$FailingTests ) : void
{
$TestsIterator = new DirectoryIterator( __DIR__ . '/../descriptions' );
foreach( $TestsIterator as $File )
{
if( $File->isDot() )
{
continue;
}
if( $File->getExtension() !== 'md' )
{
$FailingTests[] = "There is an unexpected file: descriptions/{$File->getFilename()}";
continue;
}
if( !isset( $SeenTestTypes[ $File->getBasename( '.md' ) ] ) )
{
$FailingTests[] = "There is a description for something we have no tests for: types/{$File->getFilename()}";
}
}
}
/**
* @param array<string, bool> $SeenTestTypes
* @param string[] $FailingTests
*/
function TestBasicRules( FileDetector $Detector, array &$SeenTestTypes, array &$FailingTests ) : void
{
$TestsIterator = new DirectoryIterator( __DIR__ . '/types' );
foreach( $TestsIterator as $File )
{
if( $File->isDot() )
{
continue;
}
if( $File->getExtension() !== 'txt' )
{
$FailingTests[] = "There is an unexpected file: types/{$File->getFilename()}";
continue;
}
$TestFilePaths = file( $File->getPathname(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$ExpectedType = $File->getBasename( '.txt' );
if( $TestFilePaths === false )
{
throw new \RuntimeException( "Failed to read file: types/{$File->getFilename()}" );
}
if( empty( $TestFilePaths ) )
{
$FailingTests[] = "File is empty: types/{$File->getFilename()}";
continue;
}
else
{
$SeenTestTypes[ $ExpectedType ] = true;
}
if( $ExpectedType === '_NonMatchingTests' )
{
$ExpectedType = null;
}
/** @var array<string, bool> */
$AlreadySeenStrings = [];
foreach( $TestFilePaths as $Path )
{
if( isset( $AlreadySeenStrings[ $Path ] ) )
{
$FailingTests[] = "Path \"$Path\" in \"$File\" is defined more than once";
continue;
}
$AlreadySeenStrings[ $Path ] = true;
$Actual = $Detector->GetMatchesForFileList( [ $Path ] );
if( preg_last_error() !== PREG_NO_ERROR )
{
err( 'Regex is failing: ' . preg_last_error_msg() );
exit( 2 );
}
if( $ExpectedType === null )
{
if( empty( $Actual ) )
{
continue;
}
foreach( $Actual as $Match => $Count )
{
if( str_starts_with( $Match, 'Evidence.' ) )
{
// Evidence tests get ignored when matching non-matching tests
continue;
}
else
{
$FailingTests[] = "Path \"$Path\" returned \"$Match\" but it should not have matched anything";
}
}
}
else
{
if( isset( $Actual[ $ExpectedType ] ) )
{
continue;
}
$FailingTests[] = "Path \"$Path\" does not match for \"$ExpectedType\"";
}
}
}
}
/**
* @param array<string, bool> $SeenTestTypes
* @param string[] $FailingTests
*/
function TestFilelists( FileDetector $Detector, array &$SeenTestTypes, array &$FailingTests ) : void
{
$TestsIterator = new DirectoryIterator( __DIR__ . '/filelists' );
foreach( $TestsIterator as $File )
{
if( $File->isDot() )
{
continue;
}
if( $File->getExtension() !== 'txt' )
{
$FailingTests[] = "There is an unexpected file: filelists/{$File->getFilename()}";
continue;
}
$TestFilePaths = file( $File->getPathname(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$BaseName = $File->getBasename( '.txt' );
$Bits = explode( '.', $BaseName, 3 );
$ExpectedType = $Bits[ 0 ] . '.' . $Bits[ 1 ];
if( $TestFilePaths === false )
{
throw new \RuntimeException( "Failed to read file: filelists/{$File->getFilename()}" );
}
if( empty( $TestFilePaths ) )
{
$FailingTests[] = "File is empty: filelists/{$File->getFilename()}";
continue;
}
else
{
$SeenTestTypes[ $ExpectedType ] = true;
}
$Matches = $Detector->GetMatchesForFileList( $TestFilePaths );
if( !isset( $Matches[ $ExpectedType ] ) )
{
$FailingTests[] = "Failed to match $ExpectedType for filelists/$BaseName (matched as " . implode( ', ', array_keys( $Matches ) ) . ")";
}
foreach( $Matches as $Key => $Count )
{
if( $Key !== $ExpectedType && str_starts_with( $Key, 'Engine.' ) )
{
$FailingTests[] = "FALSE Positive? $ExpectedType for filelists/$BaseName (matched as " . implode( ', ', array_keys( $Matches ) ) . ")";
break;
}
}
}
}
/**
* @param string[] $FailingTests
*/
function TestExtra( FileDetector $Detector, array &$FailingTests ) : void
{
$GodotTests =
[
[
'Game.exe',
'Sound/Game.pck',
],
[
'Folder/Game',
'Sound/Game.pck',
],
[
'Folder/Game.exe',
'Game.pck',
],
];
foreach( $GodotTests as $Files )
{
$Matches = $Detector->GetMatchesForFileList( $Files );
if( count( $Matches ) !== 1 || !isset( $Matches[ 'Evidence.PCK' ] ) )
{
$FailingTests[] = "Incorrectly matched Godot";
}
}
}
function err( string $Message ) : void
{
if( getenv( 'CI' ) !== false )
{
echo "::error::" . $Message . PHP_EOL;
}
else
{
fwrite( STDERR, $Message . PHP_EOL );
}
}