Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Command/SelfUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Php\Pie\Platform;
use Php\Pie\SelfManage\Update\Channel;
use Php\Pie\SelfManage\Update\FetchPieReleaseFromGitHub;
use Php\Pie\SelfManage\Update\IsBrewInstallation;
use Php\Pie\SelfManage\Update\ReleaseIsNewer;
use Php\Pie\SelfManage\Update\ReleaseMetadata;
use Php\Pie\SelfManage\Verify\FailedToVerifyRelease;
Expand All @@ -30,6 +31,8 @@
use Throwable;

use function file_get_contents;
use function is_link;
use function realpath;
use function sprintf;
use function unlink;

Expand Down Expand Up @@ -87,6 +90,15 @@ public function execute(InputInterface $input, OutputInterface $output): int
return Command::FAILURE;
}

$originalPathToSelf = ($this->fullPathToSelf)();
$fullPathToSelf = is_link($originalPathToSelf) ? (realpath($originalPathToSelf) ?: $originalPathToSelf) : $originalPathToSelf;

if ((new IsBrewInstallation())($fullPathToSelf, $originalPathToSelf)) {
$this->io->writeError('<comment>Aborting! PIE was installed with Brew, you should upgrade with `brew upgrade pie`.</comment>');

return Command::FAILURE;
}

$settings = new Settings(Platform::getPieBaseWorkingDirectory());
$updateChannel = $settings->updateChannel();

Expand Down Expand Up @@ -197,7 +209,6 @@ public function execute(InputInterface $input, OutputInterface $output): int
return Command::FAILURE;
}

$fullPathToSelf = ($this->fullPathToSelf)();
$this->io->write(
sprintf('Writing new version to %s', $fullPathToSelf),
verbosity: IOInterface::VERBOSE,
Expand Down
19 changes: 19 additions & 0 deletions src/SelfManage/Update/IsBrewInstallation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Php\Pie\SelfManage\Update;

use function str_contains;

/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
final class IsBrewInstallation
{
public function __invoke(string $resolvedPath, string $originalPath): bool
{
return str_contains($resolvedPath, '/usr/local/Cellar')
|| str_contains($resolvedPath, '/opt/homebrew/Cellar')
|| str_contains($originalPath, '/usr/local/Cellar')
|| str_contains($originalPath, '/opt/homebrew/Cellar');
}
}
2 changes: 1 addition & 1 deletion test/end-to-end/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ COPY --from=build_pie_phar /app/pie.phar /usr/local/bin/pie
RUN pie install -v --auto-install-system-dependencies php/sodium

FROM alpine AS test_pie_installs_system_deps_on_alpine
RUN apk add php php-phar php-mbstring php-iconv php-openssl bzip2-dev libbz2 build-base autoconf bison re2c libtool php84-dev
RUN apk add php php-phar php-mbstring php-iconv php-openssl bzip2-dev libbz2 build-base autoconf bison re2c libtool php85-dev
COPY --from=build_pie_phar /app/pie.phar /usr/local/bin/pie
RUN pie install -v --auto-install-system-dependencies php/sodium

Expand Down
108 changes: 108 additions & 0 deletions test/unit/SelfManage/Update/IsBrewInstallationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Php\PieUnitTest\SelfManage\Update;

use Composer\Util\Filesystem;
use Php\Pie\SelfManage\Update\IsBrewInstallation;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresOperatingSystemFamily;
use PHPUnit\Framework\TestCase;

use function mkdir;
use function realpath;
use function symlink;
use function sys_get_temp_dir;
use function touch;
use function uniqid;

use const DIRECTORY_SEPARATOR;

#[CoversClass(IsBrewInstallation::class)]
final class IsBrewInstallationTest extends TestCase
{
/** @return array<non-empty-string, array{0: string, 1: string, 2: bool}> */
public static function pathProvider(): array
{
return [
'regular-path' => ['/home/user/.local/bin/pie.phar', '/home/user/.local/bin/pie.phar', false],
'both-regular-usr-local' => ['/usr/local/bin/pie', '/usr/local/bin/pie', false],
'intel-mac-cellar-direct' => ['/usr/local/Cellar/pie/1.0/bin/pie', '/usr/local/Cellar/pie/1.0/bin/pie', true],
'apple-silicon-cellar-direct' => ['/opt/homebrew/Cellar/pie/1.0/bin/pie', '/opt/homebrew/Cellar/pie/1.0/bin/pie', true],
'resolved-is-cellar' => ['/opt/homebrew/Cellar/pie/1.0/bin/pie', '/usr/local/bin/pie', true],
'original-is-cellar' => ['/usr/local/bin/pie', '/opt/homebrew/Cellar/pie/1.0/bin/pie', true],
];
}

#[DataProvider('pathProvider')]
public function testIsBrewInstallationWithPaths(
string $resolvedPath,
string $originalPath,
bool $expected,
): void {
self::assertSame($expected, (new IsBrewInstallation())($resolvedPath, $originalPath));
}

#[RequiresOperatingSystemFamily('Linux')]
public function testSymlinkAtRegularPathPointingIntoBrewCellarIsDetected(): void
{
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_brew_test_', true);
$cellarFile = $tmpDir . '/opt/homebrew/Cellar/pie/1.0/pie.phar';
$symlinkPath = $tmpDir . '/pie';

mkdir($tmpDir . '/opt/homebrew/Cellar/pie/1.0', 0777, true);
touch($cellarFile);
symlink($cellarFile, $symlinkPath);

try {
$resolvedPath = realpath($symlinkPath);
self::assertIsString($resolvedPath);
self::assertTrue((new IsBrewInstallation())($resolvedPath, $symlinkPath));
} finally {
(new Filesystem())->remove($tmpDir);
}
}

#[RequiresOperatingSystemFamily('Linux')]
public function testSymlinkAtRegularPathPointingToNonBrewPathIsNotDetected(): void
{
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_brew_test_', true);
$regularFile = $tmpDir . '/regular/pie.phar';
$symlinkPath = $tmpDir . '/pie';

mkdir($tmpDir . '/regular', 0777, true);
touch($regularFile);
symlink($regularFile, $symlinkPath);

try {
$resolvedPath = realpath($symlinkPath);
self::assertIsString($resolvedPath);
self::assertFalse((new IsBrewInstallation())($resolvedPath, $symlinkPath));
} finally {
(new Filesystem())->remove($tmpDir);
}
}

#[RequiresOperatingSystemFamily('Linux')]
public function testSymlinkInCellarPointingToRegularPathIsDetectedViaOriginalPath(): void
{
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_brew_test_', true);
$regularFile = $tmpDir . '/regular/pie.phar';
$symlinkPath = $tmpDir . '/opt/homebrew/Cellar/pie/1.0/pie';

mkdir($tmpDir . '/regular', 0777, true);
mkdir($tmpDir . '/opt/homebrew/Cellar/pie/1.0', 0777, true);
touch($regularFile);
symlink($regularFile, $symlinkPath);

try {
$resolvedPath = realpath($symlinkPath);
self::assertIsString($resolvedPath);
self::assertTrue((new IsBrewInstallation())($resolvedPath, $symlinkPath));
} finally {
(new Filesystem())->remove($tmpDir);
}
}
}
Loading