From 1b54c3ed7f50176f32b7ae2fd9d281adf0a9fd12 Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Fri, 14 Jan 2022 00:42:24 +0000 Subject: [PATCH] DX: Application - better display version when displaying gitSha --- src/Console/Application.php | 18 +++++++++--------- tests/Smoke/PharTest.php | 5 ++++- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Console/Application.php b/src/Console/Application.php index 3a23ec61463..a4feef2ab46 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -119,20 +119,20 @@ public function doRun(InputInterface $input, OutputInterface $output): int */ public function getLongVersion(): string { - $version = implode('', [ + $commit = '@git-commit@'; + $versionCommit = ''; + + if ('@'.'git-commit@' !== $commit) { /** @phpstan-ignore-line as `$commit` is replaced during phar building */ + $versionCommit = substr($commit, 0, 7); + } + + return implode('', [ parent::getLongVersion(), + $versionCommit ? sprintf(' (%s)', $versionCommit) : '', // @phpstan-ignore-line to avoid `Ternary operator condition is always true|false.` self::VERSION_CODENAME ? sprintf(' %s', self::VERSION_CODENAME) : '', // @phpstan-ignore-line to avoid `Ternary operator condition is always true|false.` ' by Fabien Potencier and Dariusz Ruminski.', "\nPHP runtime: ".PHP_VERSION.'', ]); - - $commit = '@git-commit@'; - - if ('@'.'git-commit@' !== $commit) { // @phpstan-ignore-line as `$commit` is replaced during phar building - $version .= ' ('.substr($commit, 0, 7).')'; - } - - return $version; } /** diff --git a/tests/Smoke/PharTest.php b/tests/Smoke/PharTest.php index 633c26bf52e..d1057e10198 100644 --- a/tests/Smoke/PharTest.php +++ b/tests/Smoke/PharTest.php @@ -55,8 +55,11 @@ public static function setUpBeforeClass(): void public function testVersion(): void { + /** @phpstan-ignore-next-line to avoid `Ternary operator condition is always true|false.` */ + $shouldExpectCodename = Application::VERSION_CODENAME ? 1 : 0; + static::assertMatchesRegularExpression( - sprintf("/^.* %s(?: %s)? by .*\nPHP runtime: \\d\\.\\d+\\..*\$/", Application::VERSION, Application::VERSION_CODENAME), + sprintf("/^PHP CS Fixer (?%s)(? \\([a-z0-9]+\\))?(? %s){%d}(? by .*)\nPHP runtime: (?\\d\\.\\d+\\..*)$/", Application::VERSION, Application::VERSION_CODENAME, $shouldExpectCodename), self::executePharCommand('--version')->getOutput() ); }