Skip to content

Commit

Permalink
Don't output console links in CI env (#7049)
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Dec 4, 2021
1 parent 1956b85 commit 14dcbc9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 17 deletions.
19 changes: 5 additions & 14 deletions src/Psalm/Internal/Cli/Psalm.php
Expand Up @@ -226,7 +226,7 @@ function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\Class

$config->setIncludeCollector($include_collector);

$in_ci = self::runningInCI(); // disable progressbar on CI
$in_ci = CliUtils::runningInCI(); // disable progressbar on CI

if ($in_ci) {
$options['long-progress'] = true;
Expand Down Expand Up @@ -310,7 +310,7 @@ function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\Class
$progress = self::initProgress($options, $config);
$providers = self::initProviders($options, $config, $current_dir);

$stdout_report_options = self::initStdoutReportOptions($options, $show_info, $output_format);
$stdout_report_options = self::initStdoutReportOptions($options, $show_info, $output_format, $in_ci);

/** @var list<string>|string $report_file_paths type guaranteed by argument to getopt() */
$report_file_paths = $options['report'] ?? [];
Expand Down Expand Up @@ -776,7 +776,8 @@ private static function autoGenerateConfig(
private static function initStdoutReportOptions(
array $options,
bool $show_info,
string $output_format
string $output_format,
bool $in_ci
): ReportOptions {
$stdout_report_options = new \Psalm\Report\ReportOptions();
$stdout_report_options->use_color = !array_key_exists('m', $options);
Expand All @@ -788,6 +789,7 @@ private static function initStdoutReportOptions(
$stdout_report_options->format = $output_format;
$stdout_report_options->show_snippet = !isset($options['show-snippet']) || $options['show-snippet'] !== "false";
$stdout_report_options->pretty = isset($options['pretty-print']) && $options['pretty-print'] !== "false";
$stdout_report_options->in_ci = $in_ci;

return $stdout_report_options;
}
Expand Down Expand Up @@ -817,17 +819,6 @@ private static function clearCache(Config $config): void
exit;
}

private static function runningInCI(): bool
{
return isset($_SERVER['TRAVIS'])
|| isset($_SERVER['CIRCLECI'])
|| isset($_SERVER['APPVEYOR'])
|| isset($_SERVER['JENKINS_URL'])
|| isset($_SERVER['SCRUTINIZER'])
|| isset($_SERVER['GITLAB_CI'])
|| isset($_SERVER['GITHUB_WORKFLOW']);
}

private static function getCurrentDir(array $options): string
{
$cwd = getcwd();
Expand Down
15 changes: 13 additions & 2 deletions src/Psalm/Internal/CliUtils.php
Expand Up @@ -342,9 +342,9 @@ public static function getPsalmHelpText(): string
--no-diff
Turns off Psalm’s diff mode, checks all files regardless of whether they’ve changed.
--php-version=PHP_VERSION
Explicitly set PHP version to analyse code against.
Explicitly set PHP version to analyse code against.
Surfacing issues:
--show-info[=BOOLEAN]
Expand Down Expand Up @@ -647,4 +647,15 @@ public static function initPhpVersion(array $options, Config $config, ProjectAna
$project_analyzer->setPhpVersion($version, $source);
}
}

public static function runningInCI(): bool
{
return isset($_SERVER['TRAVIS'])
|| isset($_SERVER['CIRCLECI'])
|| isset($_SERVER['APPVEYOR'])
|| isset($_SERVER['JENKINS_URL'])
|| isset($_SERVER['SCRUTINIZER'])
|| isset($_SERVER['GITLAB_CI'])
|| isset($_SERVER['GITHUB_WORKFLOW']);
}
}
4 changes: 4 additions & 0 deletions src/Psalm/Report.php
Expand Up @@ -65,6 +65,9 @@ abstract class Report
/** @var bool */
protected $pretty;

/** @var bool */
protected $in_ci;

/** @var int */
protected $mixed_expression_count;

Expand Down Expand Up @@ -98,6 +101,7 @@ function ($issue_data) : bool {
$this->show_snippet = $report_options->show_snippet;
$this->show_info = $report_options->show_info;
$this->pretty = $report_options->pretty;
$this->in_ci = $report_options->in_ci;

$this->mixed_expression_count = $mixed_expression_count;
$this->total_expression_count = $total_expression_count;
Expand Down
4 changes: 4 additions & 0 deletions src/Psalm/Report/ConsoleReport.php
Expand Up @@ -129,6 +129,10 @@ private function getFileReference($data): string
. "\033[0m"
;

if ($this->in_ci) {
return $reference;
}

if (null === $this->link_format) {
// if xdebug is not enabled, use `get_cfg_var` to get the value directly from php.ini
$this->link_format = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format')
Expand Down
3 changes: 3 additions & 0 deletions src/Psalm/Report/ReportOptions.php
Expand Up @@ -39,4 +39,7 @@ class ReportOptions
* @var bool
*/
public $show_suggestions = true;

/** @var bool */
public $in_ci = false;
}
2 changes: 1 addition & 1 deletion tests/EndToEnd/PsalmRunnerTrait.php
Expand Up @@ -42,7 +42,7 @@ private function runPsalm(
$process->mustRun();
} else {
$process->run();
$this->assertEquals(2, $process->getExitCode());
$this->assertEquals(2, $process->getExitCode(), 'Expected Psalm to report errors');
}

return [
Expand Down
18 changes: 18 additions & 0 deletions tests/ReportOutputTest.php
Expand Up @@ -1074,6 +1074,7 @@ public function testConsoleReportWithLinks(): void
$console_report_options = new Report\ReportOptions();
$console_report_options->show_snippet = false;
$console_report_options->use_color = true;
$console_report_options->in_ci = false; // we don't output links in CI

$output = IssueBuffer::getOutput(IssueBuffer::getIssuesData(), $console_report_options);

Expand All @@ -1083,6 +1084,23 @@ public function testConsoleReportWithLinks(): void
);
}

public function testConsoleReportLinksAreDisabledInCI(): void
{
$this->analyzeFileForReport();

$console_report_options = new Report\ReportOptions();
$console_report_options->show_snippet = false;
$console_report_options->use_color = true;
$console_report_options->in_ci = true;

$output = IssueBuffer::getOutput(IssueBuffer::getIssuesData(), $console_report_options);

$this->assertStringNotContainsString(
"\033]8;;file://somefile.php#L3\033\\",
$output
);
}

public function testCompactReport(): void
{
$this->analyzeFileForReport();
Expand Down

0 comments on commit 14dcbc9

Please sign in to comment.