forked from hhvm/hhast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHHClientLinterTestTrait.hack
More file actions
59 lines (50 loc) · 1.66 KB
/
Copy pathHHClientLinterTestTrait.hack
File metadata and controls
59 lines (50 loc) · 1.66 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
/*
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
namespace Facebook\HHAST;
use namespace HH\Lib\{C, Str};
trait HHClientLinterTestTrait {
abstract const HHClientLinter::TConfig CONFIG;
/**
* The temporary directory to include testing source files to lint.
*
* Note that the temporary directory must be under the current directory,
* otherwise hh_client will not work.
*/
<<__MemoizeLSB>>
private static function temporarySourceDirectory(): string {
return
__DIR__.'/../.var/tmp/hhast/'.C\lastx(Str\split(static::class, '\\'));
}
protected function getLinter(string $file): HHClientLinter {
$ext = Str\strip_suffix($file, '.in')
|> Str\ends_with($$, '.php')
|> $$ ? 'php' : 'hack';
$hh_client_tmp_file =
self::temporarySourceDirectory().'/'.\bin2hex(\random_bytes(16)).'.'.$ext;
\copy($file, $hh_client_tmp_file);
return
HHClientLinter::fromPathWithConfig($hh_client_tmp_file, static::CONFIG);
}
<<__Override>>
public static async function beforeFirstTestAsync(): Awaitable<void> {
$mode = 0777;
$recursive = true;
\mkdir(self::temporarySourceDirectory(), $mode, $recursive);
}
<<__Override>>
public static async function afterLastTestAsync(): Awaitable<void> {
foreach (\scandir(self::temporarySourceDirectory()) as $file) {
$path_file = self::temporarySourceDirectory().'/'.$file;
if (\is_file($path_file)) {
\unlink($path_file);
}
}
\rmdir(self::temporarySourceDirectory());
}
}