Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed Jan 28, 2025
1 parent 0dca2d4 commit ed666f1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/test-macos.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).

Expand All @@ -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
Expand Down
28 changes: 23 additions & 5 deletions src/Difftastic.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ final class Difftastic
{
private string $difftasticBinaryCommand;

private string $background = 'dark';

private string $color = 'auto';

/**
* @throws RuntimeException
*/
Expand Down Expand Up @@ -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 .
Expand Down Expand Up @@ -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);
}
Expand Down
22 changes: 21 additions & 1 deletion tests/DifftasticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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 = <<<DIFF_MAC_OS
/var/folders/mz/34zz0rcx2tq3m7jcqbd4wrkr0000gn/T/a --- Text
1 [1, 2, 3] 1 [3, 2, 1]
DIFF_MAC_OS;
}

$this->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);
}
}

0 comments on commit ed666f1

Please sign in to comment.