Skip to content

Commit

Permalink
fix: Fix the Diff command (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Oct 9, 2023
1 parent 5b6a195 commit 14cf228
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
57 changes: 29 additions & 28 deletions src/Console/Command/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@
use Fidry\Console\ExitCode;
use Fidry\Console\Input\IO;
use KevinGH\Box\Console\PharInfoRenderer;
use KevinGH\Box\Phar\CompressionAlgorithm;
use KevinGH\Box\Phar\PharDiff;
use KevinGH\Box\Phar\PharInfo;
use PharFileInfo;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Filesystem\Path;
use Throwable;
use Webmozart\Assert\Assert;
use function array_map;
use function count;
// TODO: migrate to Safe API
use function explode;
use function is_string;
use function KevinGH\Box\check_php_settings;
use function KevinGH\Box\format_size;
use function sprintf;
use const PHP_EOL;

/**
* @private
Expand Down Expand Up @@ -153,6 +153,12 @@ private function compareArchives(PharDiff $diff, IO $io): int
$pharInfoA = $diff->getPharInfoA();
$pharInfoB = $diff->getPharInfoB();

if ($pharInfoA->equals($pharInfoB)) {
$io->success('The two archives are identical');

return ExitCode::SUCCESS;
}

self::renderArchive(
$diff->getPharInfoA()->getFileName(),
$pharInfoA,
Expand Down Expand Up @@ -230,33 +236,28 @@ private function compareContents(PharDiff $diff, IO $io): int
*/
private static function renderPaths(string $symbol, PharInfo $pharInfo, array $paths, IO $io): void
{
foreach ($paths as $path) {
/** @var PharFileInfo $file */
$file = $pharInfo->getPhar()[str_replace($pharInfo->getRoot(), '', $path)];

$compression = '<fg=red>[NONE]</fg=red>';

foreach (CompressionAlgorithm::cases() as $compressionAlgorithm) {
if (CompressionAlgorithm::NONE !== $compressionAlgorithm
&& $file->isCompressed($compressionAlgorithm->value)
) {
$compression = "<fg=cyan>[{$compressionAlgorithm->name}]</fg=cyan>";
break;
}
}
$bufferedOutput = new BufferedOutput(
$io->getVerbosity(),
$io->isDecorated(),
$io->getOutput()->getFormatter(),
);

$fileSize = format_size($file->getCompressedSize());
PharInfoRenderer::renderContent(
$bufferedOutput,
$pharInfo,
false,
false,
);

$io->writeln(
sprintf(
'%s %s %s - %s',
$symbol,
$path,
$compression,
$fileSize,
),
);
}
$lines = array_map(
static fn (string $line) => '' === $line ? '' : $symbol.' '.$line,
explode(
PHP_EOL,
$bufferedOutput->fetch(),
),
);

$io->writeln($lines);
}

private static function renderArchive(string $fileName, PharInfo $pharInfo, IO $io): void
Expand Down
4 changes: 1 addition & 3 deletions src/Phar/IncompariblePhars.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\PharInfo;

use KevinGH\Box\Pharaoh\PharError;
namespace KevinGH\Box\Phar;

final class IncompariblePhars extends PharError
{
Expand Down
36 changes: 29 additions & 7 deletions tests/Console/Command/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Fidry\Console\ExitCode;
use InvalidArgumentException;
use KevinGH\Box\Phar\InvalidPhar;
use KevinGH\Box\Platform;
use KevinGH\Box\Test\CommandTestCase;
use KevinGH\Box\Test\RequiresPharReadonlyOff;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -40,7 +41,6 @@ class DiffTest extends CommandTestCase

protected function setUp(): void
{
self::markTestSkipped('TODO');
$this->markAsSkippedIfPharReadonlyIsOn();

parent::setUp();
Expand Down Expand Up @@ -88,6 +88,7 @@ public function test_it_displays_the_list_diff_of_two_phar_files_by_default(): v
+++ Files present in "simple-phar-bar.phar" but not in "simple-phar-foo.phar"
- foo.php [NONE] - 29.00B
+ bar.php [NONE] - 29.00B
[ERROR] 2 file(s) difference
Expand All @@ -106,6 +107,7 @@ public function test_it_can_display_the_git_diff_of_two_phar_files(
?string $expectedOutput,
int $expectedStatusCode,
): void {
self::markTestSkipped('TODO');
$actualOutput = $executeCommand($this->commandTester);

if (null !== $expectedOutput) {
Expand Down Expand Up @@ -134,6 +136,7 @@ public function test_it_can_display_the_gnu_diff_of_two_phar_files(

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');

Expand Down Expand Up @@ -315,6 +318,7 @@ static function (CommandTester $commandTester): string {
+++ Files present in "simple-phar-bar.phar" but not in "simple-phar-foo.phar"
- foo.php [NONE] - 29.00B
+ bar.php [NONE] - 29.00B
[ERROR] 2 file(s) difference
Expand Down Expand Up @@ -342,12 +346,14 @@ static function (CommandTester $commandTester): string {
// Comparing the two archives... (do not check the signatures)
Archive: simple-phar-bar.phar
Compression: None
Archive Compression: None
Files Compression: None
Metadata: None
Contents: 1 file (6.64KB)
Archive: simple-phar-bar-compressed.phar
Compression: GZ
Archive Compression: None
Files Compression: GZ
Metadata: None
Contents: 1 file (6.65KB)
Expand Down Expand Up @@ -448,7 +454,7 @@ static function (CommandTester $commandTester): string {
rename to simple-phar-bar.phar/bar.php

OUTPUT,
1,
3,
])();

yield (static fn (): array => [
Expand All @@ -465,7 +471,7 @@ static function (CommandTester $commandTester): string {
return DisplayNormalizer::removeTrailingSpaces($commandTester->getDisplay(true));
},
null,
1,
2,
])();

yield (static fn (): array => [
Expand Down Expand Up @@ -594,15 +600,31 @@ static function (CommandTester $commandTester): string {

return DisplayNormalizer::removeTrailingSpaces($commandTester->getDisplay(true));
},
<<<'OUTPUT'
Platform::isOSX()
? <<<'OUTPUT'
// Comparing the two archives... (do not check the signatures)
[OK] The two archives are identical
// Comparing the two archives contents...
diff --exclude=.phar_meta.json simple-phar-bar.phar/bar.php simple-phar-baz.phar/bar.php
3c3
< echo "Hello world!";
---
> echo 'Hello world!';

OUTPUT
: <<<'OUTPUT'
// Comparing the two archives... (do not check the signatures)
[OK] The two archives are identical
// Comparing the two archives contents...
diff simple-phar-bar.phar/bar.php simple-phar-baz.phar/bar.php
diff '--exclude=.phar_meta.json' simple-phar-bar.phar/bar.php simple-phar-baz.phar/bar.php
3c3
< echo "Hello world!";
---
Expand Down
3 changes: 1 addition & 2 deletions tests/Console/Command/InfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use function implode;
use function preg_replace;
use function realpath;
use const PHP_OS_FAMILY;

/**
* @covers \KevinGH\Box\Console\Command\Info
Expand All @@ -46,7 +45,7 @@ class InfoTest extends CommandTestCase

protected function setUp(): void
{
if (PHP_OS_FAMILY !== 'Darwin') {
if (!Platform::isOSX()) {
self::markTestSkipped('This test requires more work to be working fine cross-platform.');
}

Expand Down

0 comments on commit 14cf228

Please sign in to comment.