Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit eac7fcd

Browse files
committed
Add a test for CLI when an exception is thrown
1 parent f9bcd53 commit eac7fcd

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

test-data/hhast-lint.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
2-
"roots": [],
2+
"roots": [ "." ],
3+
"extraLinters": [
4+
"Facebook\\HHAST\\Tests\\ValidConfigForLinter",
5+
"Facebook\\HHAST\\Tests\\InvalidConfigForLinter",
6+
"Facebook\\HHAST\\FinalOrAbstractClassLinter",
7+
"Facebook\\HHAST\\Tests\\ConfigTypeIsNotSupportedByTypeAssertLinter"
8+
],
39
"linterConfigs": {
410
"Facebook\\HHAST\\Tests\\ValidConfigForLinter": {
511
"the answer": 42,

tests/LinterConfigTest.hack

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99

1010
namespace Facebook\HHAST\Tests;
1111

12-
use type Facebook\HackTest\HackTest;
1312
use function Facebook\FBExpect\expect;
1413
use type Facebook\HHAST\{
14+
LinterCLITestTrait,
1515
PreferRequireOnceLinter,
1616
SingleRuleLintError,
1717
SingleRuleLinter,
18+
TestCase,
1819
};
1920
use type Facebook\HHAST\__Private\LintRunConfig;
2021

2122
abstract class EmptyBaseLinter extends SingleRuleLinter {
2223
<<__Override>>
23-
final public async function getLintErrorsAsync(): Awaitable<vec<SingleRuleLintError>> {
24+
final public async function getLintErrorsAsync(
25+
): Awaitable<vec<SingleRuleLintError>> {
2426
return vec[];
2527
}
2628
final public function getConfigPublic(): ?this::TConfig {
@@ -54,9 +56,13 @@ final class ConfigTypeIsNotSupportedByTypeAssertLinter extends EmptyBaseLinter {
5456
const type TConfig = shape('impossible' => vec<nothing>);
5557
}
5658

57-
final class LinterConfigTest extends HackTest {
59+
final class LinterConfigTest extends TestCase {
60+
use LinterCLITestTrait;
61+
62+
const string TEST_DIRECTORY = __DIR__.'/../test-data';
63+
5864
private static function getLintRunConfig(): LintRunConfig {
59-
return LintRunConfig::getForPath(__DIR__.'/../test-data');
65+
return LintRunConfig::getForPath(self::TEST_DIRECTORY);
6066
}
6167

6268
public function testValidConfigForLinter(): void {
@@ -87,6 +93,26 @@ final class LinterConfigTest extends HackTest {
8793
)->toThrow(\Exception::class, 'is not of the correct type');
8894
}
8995

96+
public async function testInvalidConfigForCLI(): Awaitable<void> {
97+
list($cli, $_, $stdout, $stderr) =
98+
$this->getCLI('--config-file', self::TEST_DIRECTORY.'/hhast-lint.json', self::TEST_DIRECTORY);
99+
$exit_code = await $cli->mainAsync();
100+
expect($stderr->getBuffer())->toContainSubstring(
101+
'A linter threw an exception:',
102+
);
103+
expect($stderr->getBuffer())->toContainSubstring(
104+
'Linter: Facebook\HHAST\Tests\InvalidConfigForLinter',
105+
);
106+
expect($stderr->getBuffer())->toMatchRegExp(
107+
re'#File\\: .*/test-data/unparsable_php_file\\.php#',
108+
);
109+
expect($stderr->getBuffer())->toContainSubstring(
110+
'Message: Configuration for Facebook\HHAST\Tests\InvalidConfigForLinter is not of the correct type. See previous exception:',
111+
);
112+
expect($exit_code)->toBeSame(2);
113+
expect($stdout->getBuffer())->toBeEmpty();
114+
}
115+
90116
public function testLinterWithoutATConfigAndNoConfigSupplied(): void {
91117
$lrc = static::getLintRunConfig();
92118

0 commit comments

Comments
 (0)