1
0
Fork 0

Make use of Phar::running() to get the current phar path

pull/12261/head
Jordi Boggiano 2025-01-08 13:12:54 +01:00
parent fb397acaa0
commit e81df52e53
No known key found for this signature in database
3 changed files with 21 additions and 26 deletions

View File

@ -23,6 +23,7 @@ use Composer\SelfUpdate\Versions;
use Composer\IO\IOInterface;
use Composer\Downloader\FilesystemException;
use Composer\Downloader\TransportException;
use Phar;
use Symfony\Component\Console\Input\InputInterface;
use Composer\Console\Input\InputOption;
use Composer\Console\Input\InputArgument;
@ -116,9 +117,9 @@ EOT
$cacheDir = $config->get('cache-dir');
$rollbackDir = $config->get('data-dir');
$home = $config->get('home');
$localFilename = realpath($_SERVER['argv'][0]);
if (false === $localFilename) {
$localFilename = $_SERVER['argv'][0];
$localFilename = Phar::running(false);
if ('' === $localFilename) {
throw new \RuntimeException('Could not determine the location of the composer.phar file as it appears you are not running this code from a phar archive.');
}
if ($input->getOption('update-keys')) {

View File

@ -96,6 +96,7 @@ class AllFunctionalTest extends TestCase
self::assertFileExists(self::$pharPath);
copy(self::$pharPath, __DIR__.'/../../composer-test.phar');
chmod(__DIR__.'/../../composer-test.phar', 0777);
}
/**

View File

@ -14,6 +14,7 @@ namespace Composer\Test\Command;
use Composer\Composer;
use Composer\Test\TestCase;
use Symfony\Component\Process\Process;
/**
* @group slow
@ -24,23 +25,15 @@ class SelfUpdateCommandTest extends TestCase
/**
* @var string
*/
private $prevArgv;
private $phar;
public function setUp(): void
{
parent::setUp();
$this->prevArgv = $_SERVER['argv'][0];
$dir = $this->initTempComposer();
copy(__DIR__.'/../../../composer-test.phar', $dir.'/composer.phar');
$_SERVER['argv'][0] = $dir.'/composer.phar';
}
public function tearDown(): void
{
parent::tearDown();
$_SERVER['argv'][0] = $this->prevArgv;
$this->phar = $dir.'/composer.phar';
}
public function testSuccessfulUpdate(): void
@ -49,20 +42,20 @@ class SelfUpdateCommandTest extends TestCase
$this->markTestSkipped('On releases this test can fail to upgrade as we are already on latest version');
}
$appTester = $this->getApplicationTester();
$appTester->run(['command' => 'self-update']);
$appTester = new Process([PHP_BINARY, $this->phar, 'self-update']);
$status = $appTester->run();
self::assertSame(0, $status, $appTester->getErrorOutput());
$appTester->assertCommandIsSuccessful();
self::assertStringContainsString('Upgrading to version', $appTester->getDisplay());
self::assertStringContainsString('Upgrading to version', $appTester->getOutput());
}
public function testUpdateToSpecificVersion(): void
{
$appTester = $this->getApplicationTester();
$appTester->run(['command' => 'self-update', 'version' => '2.4.0']);
$appTester = new Process([PHP_BINARY, $this->phar, 'self-update', '2.4.0']);
$status = $appTester->run();
self::assertSame(0, $status, $appTester->getErrorOutput());
$appTester->assertCommandIsSuccessful();
self::assertStringContainsString('Upgrading to version 2.4.0', $appTester->getDisplay());
self::assertStringContainsString('Upgrading to version 2.4.0', $appTester->getOutput());
}
public function testUpdateWithInvalidOptionThrowsException(): void
@ -83,12 +76,12 @@ class SelfUpdateCommandTest extends TestCase
$this->markTestSkipped('On releases this test can fail to upgrade as we are already on latest version');
}
$appTester = $this->getApplicationTester();
$appTester->run(['command' => 'self-update', $option => true]);
$appTester->assertCommandIsSuccessful();
$appTester = new Process([PHP_BINARY, $this->phar, 'self-update', $option]);
$status = $appTester->run();
self::assertSame(0, $status, $appTester->getErrorOutput());
self::assertStringContainsString('Upgrading to version', $appTester->getDisplay());
self::assertStringContainsString($expectedOutput, $appTester->getDisplay());
self::assertStringContainsString('Upgrading to version', $appTester->getOutput());
self::assertStringContainsString($expectedOutput, $appTester->getOutput());
}
/**