From ed666f1fa06bb9daaa82d6536d712c7dc1807e78 Mon Sep 17 00:00:00 2001 From: Raphael Stolt Date: Fri, 24 Jan 2025 18:52:23 +0100 Subject: [PATCH] Wip --- .github/workflows/test-macos.yml | 31 +++++++++++++++++++++++++++++++ README.md | 14 ++++++++++++++ src/Difftastic.php | 28 +++++++++++++++++++++++----- tests/DifftasticTest.php | 22 +++++++++++++++++++++- 4 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/test-macos.yml diff --git a/.github/workflows/test-macos.yml b/.github/workflows/test-macos.yml new file mode 100644 index 0000000..34990c8 --- /dev/null +++ b/.github/workflows/test-macos.yml @@ -0,0 +1,31 @@ +name: test-macos + +on: push + +jobs: + build: + name: "PHPUnit (PHP ${{ matrix.php }}) macOS" + runs-on: macos-latest + + strategy: + matrix: + php: + - "8.4" + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install difftastic + run: brew install difftastic + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php }}" + + - name: Install Composer dependencies + run: composer install --no-progress --prefer-dist --optimize-autoloader + + - name: Run tests + run: composer run-script test diff --git a/README.md b/README.md index 26d755b..6f4d016 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # Difftastic PHP +![Test Status](https://github.com/raphaelstolt/difftastic-php/workflows/test/badge.svg) +[![Version](http://img.shields.io/packagist/v/stolt/difftastic-php.svg?style=flat)](https://packagist.org/packages/stolt/difftastic-php) +![PHP Version](https://img.shields.io/badge/php-8.1+-ff69b4.svg) +[![PDS Skeleton](https://img.shields.io/badge/pds-skeleton-blue.svg?style=flat)](https://github.com/php-pds/skeleton) + This Composer package provides a wrapper around [difftastic](https://github.com/Wilfred/difftastic) for usage in PHP based projects; therefor it requires `difftastic` to be [installed](https://difftastic.wilfred.me.uk/installation.html). @@ -20,6 +25,15 @@ $difftastic = new Difftastic(); $diff = $difftastic->diff('[1, 2, 3]', '[3, 2, 1]'); ``` +With options differing from the default. + +```php +use Stolt\Difftastic; + +$difftastic = new Difftastic(background: 'light', color: 'never'); +$diff = $difftastic->diff('[1, 2, 3]', '[3, 2, 1]'); +``` + ### Running tests ``` bash diff --git a/src/Difftastic.php b/src/Difftastic.php index c53aeae..8ee70f9 100644 --- a/src/Difftastic.php +++ b/src/Difftastic.php @@ -10,6 +10,10 @@ final class Difftastic { private string $difftasticBinaryCommand; + private string $background = 'dark'; + + private string $color = 'auto'; + /** * @throws RuntimeException */ @@ -54,6 +58,22 @@ private function getDifftasticBinaryByOs(): string ) }; } + + private function getDifftasticOptions(): string + { + $options = ''; + + if ($this->background !== 'dark') { + $options .= ' --background ' . $this->background; + } + + if ($this->color !== 'auto') { + $options .= ' --color ' . $this->color; + } + + return $options; + } + private function createTemporaryFile(string $name, string $content): string { $file = DIRECTORY_SEPARATOR . @@ -84,11 +104,9 @@ public function diff(string $a, string $b): string $aFile = $this->createTemporaryFile('a', $a); $bFile = $this->createTemporaryFile('b', $b); - \exec( - $this->difftasticBinaryCommand . ' ' . $aFile . ' ' . $bFile . ' 2>&1', - $output, - $returnValue - ); + $command = $this->difftasticBinaryCommand . $this->getDifftasticOptions() . ' ' . $aFile . ' ' . $bFile . ' 2>&1'; + + \exec($command, $output, $returnValue); return \implode(PHP_EOL, $output); } diff --git a/tests/DifftasticTest.php b/tests/DifftasticTest.php index e5f79ec..e83c4a1 100644 --- a/tests/DifftasticTest.php +++ b/tests/DifftasticTest.php @@ -4,6 +4,8 @@ namespace Stolt\Tests; +use PHPUnit\Framework\Attributes\PreserveGlobalState; +use PHPUnit\Framework\Attributes\RunInSeparateProcess; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\Ticket; use PHPUnit\Framework\TestCase; @@ -42,6 +44,7 @@ public function throwsExpectedExceptionOnMisconfiguration(): void #[Test] #[Ticket('https://github.com/Wilfred/difftastic/issues/809')] + #[RunInSeparateProcess] public function returnsExpectedDiff(): void { $difftastic = new Difftastic(); @@ -52,6 +55,23 @@ public function returnsExpectedDiff(): void No such file: /tmp/b DIFF; - $this->assertEquals($diff, $expectedDifftasticOutput); + if (\strtolower(PHP_OS_FAMILY) === 'darwin') { + $expectedDifftasticOutput = <<assertEquals($this->removeFirstOutputLine($diff), $this->removeFirstOutputLine($expectedDifftasticOutput)); + } + + private function removeFirstOutputLine(string $output): string + { + $parts = \explode(PHP_EOL, $output); + + \array_shift($parts); + + return \implode(PHP_EOL, $parts); } }