From 00d572785e85c6cd14f103a48dedcddf0d7a392e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 11 Oct 2023 20:24:53 +0200 Subject: [PATCH 1/3] fix --- src/Phar/Differ/DifferFactory.php | 2 +- src/Phar/Differ/GitDiffer.php | 65 +++++++++++++++++++ src/Phar/Differ/ProcessCommandBasedDiffer.php | 2 +- tests/Console/Command/DiffTest.php | 33 ++++++++-- 4 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 src/Phar/Differ/GitDiffer.php diff --git a/src/Phar/Differ/DifferFactory.php b/src/Phar/Differ/DifferFactory.php index 29a433035..aeeeffe45 100644 --- a/src/Phar/Differ/DifferFactory.php +++ b/src/Phar/Differ/DifferFactory.php @@ -25,7 +25,7 @@ public function create( ): Differ { return match ($mode) { DiffMode::FILE_NAME => new FilenameDiffer(), - DiffMode::GIT => new ProcessCommandBasedDiffer('git diff --no-index'), + DiffMode::GIT => new GitDiffer(), DiffMode::GNU => new ProcessCommandBasedDiffer('diff --exclude='.Extract::PHAR_META_PATH), DiffMode::CHECKSUM => new ChecksumDiffer($checksumAlgorithm), }; diff --git a/src/Phar/Differ/GitDiffer.php b/src/Phar/Differ/GitDiffer.php new file mode 100644 index 000000000..8120674ac --- /dev/null +++ b/src/Phar/Differ/GitDiffer.php @@ -0,0 +1,65 @@ + + * Théo Fidry + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace KevinGH\Box\Phar\Differ; + +use Fidry\Console\Input\IO; +use KevinGH\Box\Console\Command\Extract; +use KevinGH\Box\Phar\PharInfo; +use function array_filter; +use function explode; +use function implode; +use function sprintf; +use function str_starts_with; + +final class GitDiffer implements Differ +{ + public function diff(PharInfo $pharInfoA, PharInfo $pharInfoB, IO $io): void + { + $gitDiff = ProcessCommandBasedDiffer::getDiff( + $pharInfoA, + $pharInfoB, + 'git diff --no-index', + ); + + if (null === $gitDiff) { + $io->writeln(Differ::NO_DIFF_MESSAGE); + + return; + } + + $separator = 'diff --git '; + + $diffLines = explode( + $separator, + $gitDiff, + ); + + $pharMetaLine = sprintf( + 'a%2$s/%1$s b%3$s/%1$s', + Extract::PHAR_META_PATH, + $pharInfoA->getFileName(), + $pharInfoB->getFileName(), + ); + + $filteredLines = array_filter( + $diffLines, + static fn (string $line) => !str_starts_with($line, $pharMetaLine) + ); + + $filteredDiff = implode($separator, $filteredLines); + + $io->writeln('' === $filteredDiff ? Differ::NO_DIFF_MESSAGE : $filteredDiff); + } +} diff --git a/src/Phar/Differ/ProcessCommandBasedDiffer.php b/src/Phar/Differ/ProcessCommandBasedDiffer.php index f573f4745..ec77e6c37 100644 --- a/src/Phar/Differ/ProcessCommandBasedDiffer.php +++ b/src/Phar/Differ/ProcessCommandBasedDiffer.php @@ -35,7 +35,7 @@ public function diff(PharInfo $pharInfoA, PharInfo $pharInfoB, IO $io): void $io->writeln($result ?? Differ::NO_DIFF_MESSAGE); } - private static function getDiff(PharInfo $pharInfoA, PharInfo $pharInfoB, string $command): ?string + public static function getDiff(PharInfo $pharInfoA, PharInfo $pharInfoB, string $command): ?string { $pharInfoATmp = $pharInfoA->getTmp(); $pharInfoBTmp = $pharInfoB->getTmp(); diff --git a/tests/Console/Command/DiffTest.php b/tests/Console/Command/DiffTest.php index 6c72b572c..3c8792d2e 100644 --- a/tests/Console/Command/DiffTest.php +++ b/tests/Console/Command/DiffTest.php @@ -63,10 +63,6 @@ public function test_it_can_display_the_diff_of_two_phar_files( ?string $expectedOutput, int $expectedStatusCode, ): void { - if (DiffMode::GIT === $diffMode) { - self::markTestSkipped('TODO'); - } - $command = [ 'command' => 'diff', 'pharA' => realpath($pharAPath), @@ -538,7 +534,18 @@ public static function gitDiffPharsProvider(): iterable Metadata: None Contents: 1 file (6.64KB) - // Comparing the two archives contents... + --- PHAR A + +++ PHAR B + @@ @@ + Archive Compression: None + Files Compression: None + Signature: SHA-1 + -Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57 + +Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51 + Metadata: None + Contents: 1 file (6.64KB) + + // Comparing the two archives contents (git diff)... diff --git asimple-phar-foo.phar/foo.php bsimple-phar-bar.phar/bar.php similarity index 100% @@ -546,7 +553,7 @@ public static function gitDiffPharsProvider(): iterable rename to simple-phar-bar.phar/bar.php OUTPUT, - 3, + ExitCode::FAILURE, ]; yield 'same files different content' => [ @@ -572,7 +579,19 @@ public static function gitDiffPharsProvider(): iterable Metadata: None Contents: 1 file (6.61KB) - // Comparing the two archives contents... + --- PHAR A + +++ PHAR B + @@ @@ + Archive Compression: None + Files Compression: None + Signature: SHA-1 + -Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51 + +Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5 + Metadata: None + -Contents: 1 file (6.64KB) + +Contents: 1 file (6.61KB) + + // Comparing the two archives contents (git diff)... diff --git asimple-phar-bar.phar/bar.php bsimple-phar-baz.phar/bar.php index 290849f..8aac305 100644 From 366624364a37c50343e66d8d44d2c1524f1e62f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 11 Oct 2023 20:26:09 +0200 Subject: [PATCH 2/3] fix --- tests/Console/Command/DiffTest.php | 51 +----------------------------- 1 file changed, 1 insertion(+), 50 deletions(-) diff --git a/tests/Console/Command/DiffTest.php b/tests/Console/Command/DiffTest.php index 3c8792d2e..27cd323c5 100644 --- a/tests/Console/Command/DiffTest.php +++ b/tests/Console/Command/DiffTest.php @@ -121,7 +121,6 @@ public function test_it_can_display_the_list_diff_of_two_phar_files(): void */ public function test_it_can_display_the_git_diff_of_two_phar_files(): void { - self::markTestSkipped('TODO'); $pharPath = realpath(self::FIXTURES_DIR.'/simple-phar-foo.phar'); $this->commandTester->execute( @@ -134,7 +133,7 @@ public function test_it_can_display_the_git_diff_of_two_phar_files(): void ); $expectedOutput = <<<'OUTPUT' - ⚠️ Using the option "list-diff" is deprecated. Use "--diff=file-name" instead. + ⚠️ Using the option "git-diff" is deprecated. Use "--diff=git" instead. // Comparing the two archives... @@ -178,54 +177,6 @@ public function test_it_can_display_the_gnu_diff_of_two_phar_files(): void ); } - public function test_it_can_check_the_sum_of_two_phar_files(): void - { - self::markTestSkipped('TODO'); - (function (): void { - $pharPath = realpath(self::FIXTURES_DIR.'/simple-phar-foo.phar'); - - ob_start(); - $this->commandTester->execute( - [ - 'command' => 'diff', - 'pharA' => $pharPath, - 'pharB' => $pharPath, - '--check' => null, - ], - ); - $actual = DisplayNormalizer::removeTrailingSpaces(ob_get_clean()); - - $expected = <<<'OUTPUT' - No differences encountered. - - OUTPUT; - - $this->assertSame($expected, $actual); - $this->assertSame(0, $this->commandTester->getStatusCode()); - })(); - - (function (): void { - ob_start(); - $this->commandTester->execute( - [ - 'command' => 'diff', - 'pharA' => realpath(self::FIXTURES_DIR.'/simple-phar-foo.phar'), - 'pharB' => realpath(self::FIXTURES_DIR.'/simple-phar-bar.phar'), - '--check' => null, - ], - ); - $actual = DisplayNormalizer::removeTrailingSpaces(ob_get_clean()); - - $expected = <<<'OUTPUT' - No differences encountered. - - OUTPUT; - - $this->assertSame($expected, $actual); - $this->assertSame(0, $this->commandTester->getStatusCode()); - })(); - } - public function test_it_cannot_compare_non_existent_files(): void { try { From e343fc67ae06078596a374788417f941931e47ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 11 Oct 2023 20:28:26 +0200 Subject: [PATCH 3/3] fix --- tests/Console/Command/DiffTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Console/Command/DiffTest.php b/tests/Console/Command/DiffTest.php index 27cd323c5..ce795cf15 100644 --- a/tests/Console/Command/DiffTest.php +++ b/tests/Console/Command/DiffTest.php @@ -15,7 +15,6 @@ namespace KevinGH\Box\Console\Command; use Fidry\Console\Command\Command; -use Fidry\Console\DisplayNormalizer; use Fidry\Console\ExitCode; use InvalidArgumentException; use KevinGH\Box\Phar\DiffMode; @@ -25,8 +24,6 @@ use KevinGH\Box\Test\RequiresPharReadonlyOff; use Symfony\Component\Console\Output\OutputInterface; use function array_splice; -use function ob_get_clean; -use function Safe\ob_start; use function Safe\realpath; /**