Skip to content

Commit

Permalink
Fix broken link format detection (#16)
Browse files Browse the repository at this point in the history
This broke in #10. The condition was wrong.

Added tests to assure this won't break again.
  • Loading branch information
ruudk authored Oct 3, 2024
1 parent 9c3abdb commit 7caf309
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
1 change: 1 addition & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ services:
relativePathHelper: '@simpleRelativePathHelper'
ciDetectedErrorFormatter: '@PHPStan\Command\ErrorFormatter\CiDetectedErrorFormatter'
editorUrl: '%editorUrl%'
environmentVariables: '%env%'
20 changes: 14 additions & 6 deletions src/TicketSwapErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,39 @@ final class TicketSwapErrorFormatter implements ErrorFormatter
private ErrorFormatter $ciDetectedErrorFormatter;
private ?string $editorUrl;

/**
* @param RelativePathHelper $relativePathHelper
* @param ErrorFormatter $ciDetectedErrorFormatter
* @param string|null $editorUrl
* @param array<string, mixed> $environmentVariables
*/
public function __construct(
RelativePathHelper $relativePathHelper,
ErrorFormatter $ciDetectedErrorFormatter,
?string $editorUrl = null
?string $editorUrl,
array $environmentVariables
) {
$this->relativePathHelper = $relativePathHelper;
$this->ciDetectedErrorFormatter = $ciDetectedErrorFormatter;
$this->editorUrl = $editorUrl;
$this->linkFormat = self::getLinkFormatFromEnv();
$this->linkFormat = self::getLinkFormatFromEnv($environmentVariables);
}

/**
* @param array<string, mixed> $environmentVariables
* @return self::LINK_FORMAT_*
*/
public static function getLinkFormatFromEnv() : string
public static function getLinkFormatFromEnv(array $environmentVariables) : string
{
if (getenv('GITHUB_ACTIONS') !== false) {
if (isset($environmentVariables['GITHUB_ACTIONS'])) {
return self::LINK_FORMAT_GITHUB_ACTIONS;
}

if (getenv('TERMINAL_EMULATOR') !== 'JetBrains-JediTerm') {
if (isset($environmentVariables['TERMINAL_EMULATOR']) && $environmentVariables['TERMINAL_EMULATOR'] === 'JetBrains-JediTerm') {
return self::LINK_FORMAT_PHPSTORM;
}

if (getenv('TERM_PROGRAM') !== 'WarpTerminal') {
if (isset($environmentVariables['TERM_PROGRAM']) && $environmentVariables['TERM_PROGRAM'] === 'WarpTerminal') {
return self::LINK_FORMAT_WARP;
}

Expand Down
41 changes: 40 additions & 1 deletion tests/TicketSwapErrorFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,46 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
return 0;
}
},
self::PHPSTOR_EDITOR_URL
self::PHPSTOR_EDITOR_URL,
[]
);
}

/**
* @return iterable<array{TicketSwapErrorFormatter::LINK_FORMAT_*, array<string, string>}>
*/
public static function provideLinkFormatFromEnv() : iterable
{
yield 'GitHub Actions' => [
TicketSwapErrorFormatter::LINK_FORMAT_GITHUB_ACTIONS,
['GITHUB_ACTIONS' => 'true'],
];
yield 'JetBrains' => [
TicketSwapErrorFormatter::LINK_FORMAT_PHPSTORM,
['TERMINAL_EMULATOR' => 'JetBrains-JediTerm'],
];
yield 'Warp' => [
TicketSwapErrorFormatter::LINK_FORMAT_WARP,
['TERM_PROGRAM' => 'WarpTerminal'],
];
yield 'Ghostty' => [
TicketSwapErrorFormatter::LINK_FORMAT_DEFAULT,
['TERM_PROGRAM' => 'ghostty'],
];
yield 'Default' => [
TicketSwapErrorFormatter::LINK_FORMAT_DEFAULT,
[],
];
}

/**
* @dataProvider provideLinkFormatFromEnv
*/
public function testGetLinkFormatFromEnv(string $expected, array $environmentVariables) : void
{
self::assertSame(
$expected,
TicketSwapErrorFormatter::getLinkFormatFromEnv($environmentVariables)
);
}

Expand Down

0 comments on commit 7caf309

Please sign in to comment.