Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add the stub to the diff comparisons #1101

Merged
merged 4 commits into from
Oct 22, 2023
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
3 changes: 3 additions & 0 deletions src/Console/Command/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
*/
final class Extract implements Command
{
public const STUB_PATH = '.phar/stub.php';
public const PHAR_META_PATH = '.phar_meta.json';

private const PHAR_ARG = 'phar';
Expand Down Expand Up @@ -151,6 +152,7 @@ private static function dumpPhar(string $file, string $tmpDir): string
$pubKey = $file.'.pubkey';
$pubKeyContent = null;
$tmpPubKey = $tmpFile.'.pubkey';
$stub = $tmpDir.DIRECTORY_SEPARATOR.self::STUB_PATH;

try {
FS::copy($file, $tmpFile, true);
Expand All @@ -164,6 +166,7 @@ private static function dumpPhar(string $file, string $tmpDir): string
$pharMeta = PharMeta::fromPhar($phar, $pubKeyContent);

$phar->extractTo($tmpDir);
FS::dumpFile($stub, $phar->getStub());
} catch (Throwable $throwable) {
FS::remove([$tmpFile, $tmpPubKey]);

Expand Down
6 changes: 6 additions & 0 deletions src/Phar/Differ/ChecksumDiffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use KevinGH\Box\Phar\PharInfo;
use UnexpectedValueException;
use ValueError;
use function hash;
use function implode;

final class ChecksumDiffer implements Differ
Expand Down Expand Up @@ -110,6 +111,11 @@ private static function getFileHashesByRelativePathname(
$hashFiles = [];

try {
$hashFiles[$pharInfo->getStubPath()] = hash(
$algorithm,
$pharInfo->getStubContent(),
);

foreach ($pharInfo->getFiles() as $file) {
$hashFiles[$file->getRelativePathname()] = hash_file(
$algorithm,
Expand Down
6 changes: 6 additions & 0 deletions src/Phar/PharInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ public function getSignature(): ?array
return $this->meta->signature;
}

public function getStubPath(): string
{
return Extract::STUB_PATH;
}

public function getStubContent(): ?string
{
return $this->meta->stub;
Expand Down Expand Up @@ -311,6 +316,7 @@ private static function loadDumpedPharFiles(string $tmp): array
Finder::create()
->files()
->ignoreDotFiles(false)
->exclude('.phar')
->in($tmp),
),
);
Expand Down
58 changes: 57 additions & 1 deletion tests/Console/Command/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,60 @@ public static function gitDiffPharsProvider(): iterable

public static function GNUDiffPharsProvider(): iterable
{
yield from self::commonDiffPharsProvider(DiffMode::GNU);
foreach (self::commonDiffPharsProvider(DiffMode::GNU) as $label => $set) {
yield $label => 'different data; same content' === $label
? [
self::FIXTURES_DIR.'/simple-phar-bar.phar',
self::FIXTURES_DIR.'/simple-phar-bar-compressed.phar',
sprintf(
<<<'OUTPUT'

// Comparing the two archives...

Archive: simple-phar-bar.phar
Archive Compression: None
Files Compression: None
Signature: SHA-1
Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
Metadata: None
Timestamp: 1552839827 (2019-03-17T16:23:47+00:00)
Contents: 1 file (6.64KB)

Archive: simple-phar-bar-compressed.phar
Archive Compression: None
Files Compression: GZ
Signature: SHA-1
Signature Hash: 3A388D86C91C36659A043D52C2DEB64E8848DD1A
Metadata: None
Timestamp: 1552856416 (2019-03-17T21:00:16+00:00)
Contents: 1 file (6.65KB)

<diff-expected>--- PHAR A</diff-expected>
<diff-actual>+++ PHAR B</diff-actual>
@@ @@
Archive Compression: None
<diff-expected>-Files Compression: None</diff-expected>
<diff-actual>+Files Compression: GZ</diff-actual>
Signature: SHA-1
<diff-expected>-Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51</diff-expected>
<diff-actual>+Signature Hash: 3A388D86C91C36659A043D52C2DEB64E8848DD1A</diff-actual>
Metadata: None
<diff-expected>-Timestamp: 1552839827 (2019-03-17T16:23:47+00:00)</diff-expected>
<diff-expected>-Contents: 1 file (6.64KB)</diff-expected>
<diff-actual>+Timestamp: 1552856416 (2019-03-17T21:00:16+00:00)</diff-actual>
<diff-actual>+Contents: 1 file (6.65KB)</diff-actual>

// Comparing the two archives contents (%s diff)...

Common subdirectories: simple-phar-bar.phar/.phar and simple-phar-bar-compressed.phar/.phar

OUTPUT,
DiffMode::GNU->value,
),
ExitCode::FAILURE,
]
: $set;
}

yield 'different files' => [
self::FIXTURES_DIR.'/simple-phar-foo.phar',
Expand Down Expand Up @@ -620,6 +673,7 @@ public static function GNUDiffPharsProvider(): iterable

// Comparing the two archives contents (gnu diff)...

Common subdirectories: simple-phar-foo.phar/.phar and simple-phar-bar.phar/.phar
Only in simple-phar-bar.phar: bar.php
Only in simple-phar-foo.phar: foo.php

Expand Down Expand Up @@ -669,6 +723,7 @@ public static function GNUDiffPharsProvider(): iterable

// Comparing the two archives contents (gnu diff)...

Common subdirectories: simple-phar-bar.phar/.phar and simple-phar-baz.phar/.phar
diff --exclude=.phar_meta.json simple-phar-bar.phar/bar.php simple-phar-baz.phar/bar.php
3c3
< echo "Hello world!";
Expand Down Expand Up @@ -714,6 +769,7 @@ public static function GNUDiffPharsProvider(): iterable

// Comparing the two archives contents (gnu diff)...

Common subdirectories: simple-phar-bar.phar/.phar and simple-phar-baz.phar/.phar
diff '--exclude=.phar_meta.json' simple-phar-bar.phar/bar.php simple-phar-baz.phar/bar.php
3c3
< echo "Hello world!";
Expand Down
4 changes: 4 additions & 0 deletions tests/Console/Command/ExtractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private static function pharProvider(): iterable
);

$expectedSimplePharFiles = [
'.phar/stub.php' => $oldDefaultPharStub,
'.hidden' => 'baz',
'foo' => 'bar',
'.phar_meta.json' => $pharMeta->toJson(),
Expand All @@ -118,6 +119,7 @@ private static function pharProvider(): iterable
yield 'GZ compressed simple PHAR' => [
self::FIXTURES_DIR.'/gz-compressed-phar.phar',
[
'.phar/stub.php' => $oldDefaultPharStub,
'.hidden' => 'baz',
'foo' => 'bar',
'.phar_meta.json' => (new PharMeta(
Expand Down Expand Up @@ -149,6 +151,7 @@ private static function pharProvider(): iterable
yield 'sha512 signed PHAR' => [
self::FIXTURES_DIR.'/sha512.phar',
[
'.phar/stub.php' => $sha512Stub,
'index.php' => <<<'PHP'
<?php echo "Hello, world!\n";

Expand Down Expand Up @@ -177,6 +180,7 @@ private static function pharProvider(): iterable
yield 'OpenSSL signed PHAR' => [
self::FIXTURES_DIR.'/openssl.phar',
[
'.phar/stub.php' => $sha512Stub,
'index.php' => <<<'PHP'
<?php echo "Hello, world!\n";

Expand Down