Skip to content

Commit 3293094

Browse files
committed
Wip
1 parent 0dca2d4 commit 3293094

File tree

7 files changed

+145
-18
lines changed

7 files changed

+145
-18
lines changed

.github/workflows/lint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ jobs:
3030

3131
- name: Check coding styles
3232
run: composer run-script cs-lint
33+
34+
- name: Check leanness of package
35+
run: composer run-script validate-gitattributes

.github/workflows/test-macos.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: test-macos
2+
3+
on: push
4+
5+
jobs:
6+
build:
7+
name: "PHPUnit (PHP ${{ matrix.php }}) macOS"
8+
runs-on: macos-latest
9+
10+
strategy:
11+
matrix:
12+
php:
13+
- "8.4"
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
19+
- name: Install difftastic
20+
run: brew install difftastic
21+
22+
- name: Install PHP
23+
uses: shivammathur/setup-php@v2
24+
with:
25+
php-version: "${{ matrix.php }}"
26+
27+
- name: Install Composer dependencies
28+
run: composer install --no-progress --prefer-dist --optimize-autoloader
29+
30+
- name: Run tests
31+
run: composer run-script test

.github/workflows/test-windows.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: test-windows
2+
3+
on: push
4+
5+
jobs:
6+
build:
7+
name: "PHPUnit (PHP ${{ matrix.php }})"
8+
runs-on: "windows-latest"
9+
10+
strategy:
11+
matrix:
12+
php:
13+
- "8.1"
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
19+
- name: Install difftastic
20+
run: choco install difftastic
21+
22+
- name: Install PHP
23+
uses: shivammathur/setup-php@v2
24+
with:
25+
php-version: "${{ matrix.php }}"
26+
27+
- name: Install Composer dependencies
28+
run: composer install --no-progress --prefer-dist --optimize-autoloader
29+
30+
- name: Run tests
31+
run: composer run-script test

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# Difftastic PHP
22

3+
![Test Status](https://github.com/raphaelstolt/difftastic-php/workflows/test/badge.svg)
4+
[![Version](http://img.shields.io/packagist/v/stolt/difftastic-php.svg?style=flat)](https://packagist.org/packages/stolt/difftastic-php)
5+
![PHP Version](https://img.shields.io/badge/php-8.1+-ff69b4.svg)
6+
[![PDS Skeleton](https://img.shields.io/badge/pds-skeleton-blue.svg?style=flat)](https://github.com/php-pds/skeleton)
7+
38
This Composer package provides a wrapper around [difftastic](https://github.com/Wilfred/difftastic)
4-
for usage in PHP based projects; therefor it requires `difftastic` to be [installed](https://difftastic.wilfred.me.uk/installation.html).
9+
for usage in PHP based projects; therefor it requires `difftastic` to be [installed](https://difftastic.wilfred.me.uk/installation.html).
10+
Which can be done on macOS via a simple `brew install difftastic`.
511

612
## Installation
713

@@ -20,6 +26,15 @@ $difftastic = new Difftastic();
2026
$diff = $difftastic->diff('[1, 2, 3]', '[3, 2, 1]');
2127
```
2228

29+
With options differing from the default:
30+
31+
```php
32+
use Stolt\Difftastic;
33+
34+
$difftastic = new Difftastic(background: 'light', color: 'never');
35+
$diff = $difftastic->diff('[1, 2, 3]', '[3, 2, 1]');
36+
```
37+
2338
### Running tests
2439

2540
``` bash

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
"scripts": {
2727
"test": "phpunit",
2828
"test-with-coverage": "export XDEBUG_MODE=coverage && phpunit --coverage-html coverage-reports",
29+
"validate-gitattributes": "lean-package-validator validate",
2930
"cs-fix": "php-cs-fixer --allow-risky=yes fix . -vv || true",
3031
"cs-lint": "php-cs-fixer fix --diff --stop-on-violation --verbose --dry-run --allow-risky=yes",
3132
"static-analyse": "phpstan analyse --configuration phpstan.neon.dist",
3233
"pre-commit-check": [
3334
"@test",
3435
"@cs-lint",
35-
"@static-analyse"
36+
"@static-analyse",
37+
"@validate-gitattributes"
3638
]
3739
},
3840
"config": {
@@ -43,6 +45,7 @@
4345
"require-dev": {
4446
"friendsofphp/php-cs-fixer": "^3.0",
4547
"phpstan/phpstan": "^2.0",
46-
"phpunit/phpunit": "^11.4.4||^10.5.25"
48+
"phpunit/phpunit": "^11.4.4||^10.5.25",
49+
"stolt/lean-package-validator": "^4.1"
4750
}
4851
}

src/Difftastic.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ final class Difftastic
1010
{
1111
private string $difftasticBinaryCommand;
1212

13+
private string $background = 'dark';
14+
15+
private string $color = 'auto';
16+
1317
/**
1418
* @throws RuntimeException
1519
*/
@@ -26,9 +30,6 @@ public function __construct(string $background = 'dark', string $color = 'auto')
2630
if ($this->isDifftasticCommandAvailable() === false) {
2731
throw new RuntimeException('Difftastic CLI not available');
2832
}
29-
// is difftastic binary installed [x]
30-
// set config/option values [x] (see https://github.com/joeldrapper/difftastic-ruby/blob/main/lib/difftastic/differ.rb)
31-
// determine which difftastic binary to use, difftastic or difft [x]
3233
}
3334

3435
private function isDifftasticCommandAvailable(): bool
@@ -48,18 +49,37 @@ private function getDifftasticBinaryByOs(): string
4849
return match (\strtolower(PHP_OS_FAMILY)) {
4950
'darwin' => 'difft',
5051
'linux' => 'difftastic',
51-
'windows' => 'difftastic',
52+
'windows' => 'difft',
5253
default => throw new RuntimeException(
5354
'Unsupported operating system ' . PHP_OS_FAMILY
5455
)
5556
};
5657
}
58+
59+
private function getDifftasticOptions(): string
60+
{
61+
$options = '';
62+
63+
if ($this->background !== 'dark') {
64+
$options .= ' --background ' . $this->background;
65+
}
66+
67+
if ($this->color !== 'auto') {
68+
$options .= ' --color ' . $this->color;
69+
}
70+
71+
return $options;
72+
}
73+
5774
private function createTemporaryFile(string $name, string $content): string
5875
{
59-
$file = DIRECTORY_SEPARATOR .
60-
\trim(\sys_get_temp_dir(), DIRECTORY_SEPARATOR) .
61-
DIRECTORY_SEPARATOR .
62-
\ltrim($name, DIRECTORY_SEPARATOR);
76+
$file = DIRECTORY_SEPARATOR . \trim(\sys_get_temp_dir(), DIRECTORY_SEPARATOR)
77+
. DIRECTORY_SEPARATOR . \ltrim($name, DIRECTORY_SEPARATOR);
78+
79+
if ($this->isWindows()) {
80+
$file = \trim(\sys_get_temp_dir(), DIRECTORY_SEPARATOR)
81+
. DIRECTORY_SEPARATOR . \ltrim($name, DIRECTORY_SEPARATOR);
82+
}
6383

6484
file_put_contents($file, $content);
6585

@@ -84,11 +104,9 @@ public function diff(string $a, string $b): string
84104
$aFile = $this->createTemporaryFile('a', $a);
85105
$bFile = $this->createTemporaryFile('b', $b);
86106

87-
\exec(
88-
$this->difftasticBinaryCommand . ' ' . $aFile . ' ' . $bFile . ' 2>&1',
89-
$output,
90-
$returnValue
91-
);
107+
$command = $this->difftasticBinaryCommand . $this->getDifftasticOptions() . ' ' . $aFile . ' ' . $bFile . ' 2>&1';
108+
109+
\exec($command, $output, $returnValue);
92110

93111
return \implode(PHP_EOL, $output);
94112
}

tests/DifftasticTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Stolt\Tests;
66

7+
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
78
use PHPUnit\Framework\Attributes\Test;
89
use PHPUnit\Framework\Attributes\Ticket;
910
use PHPUnit\Framework\TestCase;
@@ -23,7 +24,7 @@ public function getsExpectedDifftasticBinary(): void
2324

2425
$expectedBinary = 'difftastic';
2526

26-
if (\strtolower(PHP_OS_FAMILY) === 'darwin') {
27+
if (\strtolower(PHP_OS_FAMILY) === 'darwin' || \strtolower(PHP_OS_FAMILY) === 'windows') {
2728
$expectedBinary = 'difft';
2829
}
2930

@@ -42,6 +43,7 @@ public function throwsExpectedExceptionOnMisconfiguration(): void
4243

4344
#[Test]
4445
#[Ticket('https://github.com/Wilfred/difftastic/issues/809')]
46+
#[RunInSeparateProcess]
4547
public function returnsExpectedDiff(): void
4648
{
4749
$difftastic = new Difftastic();
@@ -52,6 +54,30 @@ public function returnsExpectedDiff(): void
5254
No such file: /tmp/b
5355
DIFF;
5456

55-
$this->assertEquals($diff, $expectedDifftasticOutput);
57+
if (\strtolower(PHP_OS_FAMILY) === 'darwin') {
58+
$expectedDifftasticOutput = <<<DIFF_MAC_OS
59+
/var/folders/mz/34zz0rcx2tq3m7jcqbd4wrkr0000gn/T/a --- Text
60+
1 [1, 2, 3] 1 [3, 2, 1]
61+
62+
DIFF_MAC_OS;
63+
}
64+
65+
if (\strtolower(PHP_OS_FAMILY) === 'windows') {
66+
$expectedDifftasticOutput = <<<DIFF_WINDOWS_OS
67+
/var/folders/mz/34zz0rcx2tq3m7jcqbd4wrkr0000gn/T/a --- Text
68+
1 [1, 2, 3] 1 [3, 2, 1]
69+
DIFF_WINDOWS_OS;
70+
}
71+
72+
$this->assertEquals($this->removeFirstOutputLine($diff), $this->removeFirstOutputLine($expectedDifftasticOutput));
73+
}
74+
75+
private function removeFirstOutputLine(string $output): string
76+
{
77+
$parts = \explode(PHP_EOL, $output);
78+
79+
\array_shift($parts);
80+
81+
return \implode(PHP_EOL, $parts);
5682
}
5783
}

0 commit comments

Comments
 (0)