Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix windows support #196

Merged
merged 13 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.* export-ignore
patches export-ignore
tests export-ignore
infection.json export-ignore
Makefile export-ignore
Expand Down
60 changes: 60 additions & 0 deletions .github/workflows/test-os.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Test OS

on:
push:
branches:
- main
pull_request:

jobs:
test-windows:
name: Windows
runs-on: windows-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install PHP with extensions
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
tools: composer:v2
ini-file: development

- name: Apply patch
run: composer config extra.patches.symfony/console --json --merge '["patches/Table.patch"]'
shell: bash

- name: Install Composer dependencies
uses: ramsey/composer-install@v2
with:
dependency-versions: highest
composer-options: --prefer-dist --prefer-stable

- name: Run tests with PHPUnit
run: vendor/bin/phpunit --exclude-group=skip-windows

test-macos:
name: Mac OS
runs-on: macos-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install PHP with extensions
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
tools: composer:v2
ini-file: development

- name: Install Composer dependencies
uses: ramsey/composer-install@v2
with:
dependency-versions: highest
composer-options: --prefer-dist --prefer-stable

- name: Run tests with PHPUnit
run: vendor/bin/phpunit
3 changes: 2 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ jobs:
- 8.2
- 8.3
dependencies: [highest]
runner: [ubuntu-latest]
symfony-require: ['']
include:
- php-version: 8.0
dependencies: lowest
- php-version: '8.2'
- php-version: 8.2
dependencies: highest
symfony-require: 7.0.*

Expand Down
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'no_trailing_whitespace_in_string' => false, // For string comparison in tests
'operator_linebreak' => true, // Instead of ['only_booleans' => true]
'phpdoc_to_comment' => [
'ignored_tags' => ['psalm-suppress'],
'ignored_tags' => ['phpstan-var', 'psalm-suppress'],
],
'single_line_throw' => false,

Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"webmozart/assert": "^1.10"
},
"require-dev": {
"cweagans/composer-patches": "^1.7",
"dereuromark/composer-prefer-lowest": "^0.1.10",
"ergebnis/composer-normalize": "^2.29",
"friendsofphp/php-cs-fixer": "^3.13.0",
Expand Down Expand Up @@ -59,6 +60,7 @@
],
"config": {
"allow-plugins": {
"cweagans/composer-patches": true,
"ergebnis/composer-normalize": true,
"infection/extension-installer": true
},
Expand Down
37 changes: 37 additions & 0 deletions patches/Table.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--- a/vendor/symfony/console/Helper/Table.php 2024-01-23 16:02:46
+++ b/vendor/symfony/console/Helper/Table.php 2024-02-19 21:36:41
@@ -361,7 +361,8 @@
for ($i = 0; $i < $maxRows; ++$i) {
$cell = (string) ($row[$i] ?? '');

- $parts = explode("\n", $cell);
+ $eol = str_contains($cell, "\r\n") ? "\r\n" : "\n";
+ $parts = explode($eol, $cell);
foreach ($parts as $idx => $part) {
if ($headers && !$containsColspan) {
if (0 === $idx) {
@@ -632,9 +633,10 @@
if (!str_contains($cell ?? '', "\n")) {
continue;
}
- $escaped = implode("\n", array_map(OutputFormatter::escapeTrailingBackslash(...), explode("\n", $cell)));
+ $eol = str_contains($cell, "\r\n") ? "\r\n" : "\n";
+ $escaped = implode($eol, array_map(OutputFormatter::escapeTrailingBackslash(...), explode($eol, $cell)));
$cell = $cell instanceof TableCell ? new TableCell($escaped, ['colspan' => $cell->getColspan()]) : $escaped;
- $lines = explode("\n", str_replace("\n", "<fg=default;bg=default></>\n", $cell));
+ $lines = explode($eol, str_replace($eol, "<fg=default;bg=default></>".$eol, $cell));
foreach ($lines as $lineKey => $line) {
if ($colspan > 1) {
$line = new TableCell($line, ['colspan' => $colspan]);
@@ -696,8 +698,9 @@
$nbLines = $cell->getRowspan() - 1;
$lines = [$cell];
if (str_contains($cell, "\n")) {
- $lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
- $nbLines = \count($lines) > $nbLines ? substr_count($cell, "\n") : $nbLines;
+ $eol = str_contains($cell, "\r\n") ? "\r\n" : "\n";
+ $lines = explode($eol, str_replace($eol, "<fg=default;bg=default>".$eol."</>", $cell));
+ $nbLines = \count($lines) > $nbLines ? substr_count($cell, $eol) : $nbLines;

$rows[$line][$column] = new TableCell($lines[0], ['colspan' => $cell->getColspan(), 'style' => $cell->getStyle()]);
unset($lines[0]);
17 changes: 14 additions & 3 deletions src/File/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@

final class FileHelper
{
/**
* @return non-empty-string
*/
public static function detectEOL(string $content): string
{
preg_match("/\r\n?|\n/", $content, $matches);
/** @phpstan-var array<non-empty-string> $matches */

return $matches[0] ?? \PHP_EOL;
}

public static function getAbsolutePath(string $path, ?string $workingDir = null): string
{
if (Path::isAbsolute($path)) {
Expand Down Expand Up @@ -78,17 +89,17 @@ private static function splitPath(
$baseDir = Path::canonicalize(self::getAbsolutePath($baseDir ?? '', $workingDir));
$path = Path::canonicalize(self::getAbsolutePath($path, $workingDir));

if (!str_starts_with($path, $baseDir.\DIRECTORY_SEPARATOR)) {
if (!str_starts_with($path, $baseDir.'/')) {
return [];
}

foreach ($ignoredDir as $ignoredDirectory) {
$ignoredDirectory = Path::canonicalize(self::getAbsolutePath($ignoredDirectory, $baseDir));
if (str_starts_with($path, $ignoredDirectory.\DIRECTORY_SEPARATOR)) {
if (str_starts_with($path, $ignoredDirectory.'/')) {
return [];
}
}

return explode(\DIRECTORY_SEPARATOR, substr($path, \strlen($baseDir) + 1));
return explode('/', substr($path, \strlen($baseDir) + 1));
}
}
10 changes: 5 additions & 5 deletions src/Report/Reporter/CheckstyleReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ public function display(
?string $level,
bool $debug
): void {
$text = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
$text = '<?xml version="1.0" encoding="UTF-8"?>'.\PHP_EOL;

$text .= '<checkstyle>'."\n";
$text .= '<checkstyle>'.\PHP_EOL;

foreach ($report->getFiles() as $file) {
$fileViolations = $report->getFileViolations($file, $level);
if (0 === \count($fileViolations)) {
continue;
}

$text .= sprintf(' <file name="%s">', $this->xmlEncode($file))."\n";
$text .= sprintf(' <file name="%s">', $this->xmlEncode($file)).\PHP_EOL;
foreach ($fileViolations as $violation) {
$line = (string) $violation->getLine();
$linePosition = (string) $violation->getLinePosition();
Expand All @@ -46,9 +46,9 @@ public function display(
if (null !== $ruleName) {
$text .= ' source="'.$ruleName.'"';
}
$text .= '/>'."\n";
$text .= '/>'.\PHP_EOL;
}
$text .= ' </file>'."\n";
$text .= ' </file>'.\PHP_EOL;
}

$text .= '</checkstyle>';
Expand Down
14 changes: 7 additions & 7 deletions src/Report/Reporter/JUnitReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public function display(
$violations = $report->getViolations($level);
$count = \count($violations);

$text = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
$text .= '<testsuites>'."\n";
$text = '<?xml version="1.0" encoding="UTF-8"?>'.\PHP_EOL;
$text .= '<testsuites>'.\PHP_EOL;
$text .= ' '.sprintf(
'<testsuite name="Twig CS Fixer" tests="%d" failures="%d">',
max($count, 1),
$count
)."\n";
).\PHP_EOL;

if ($count > 0) {
foreach ($violations as $violation) {
Expand All @@ -41,23 +41,23 @@ public function display(
$text .= $this->createTestCase('All OK');
}

$text .= ' </testsuite>'."\n";
$text .= ' </testsuite>'.\PHP_EOL;
$text .= '</testsuites>';

$output->writeln($text);
}

private function createTestCase(string $name, string $type = '', ?string $message = null): string
{
$result = ' '.sprintf('<testcase name="%s">', $this->xmlEncode($name))."\n";
$result = ' '.sprintf('<testcase name="%s">', $this->xmlEncode($name)).\PHP_EOL;

if (null !== $message) {
$result .= ' '
.sprintf('<failure type="%s" message="%s" />', $this->xmlEncode($type), $this->xmlEncode($message))
."\n";
.\PHP_EOL;
}

$result .= ' </testcase>'."\n";
$result .= ' </testcase>'.\PHP_EOL;

return $result;
}
Expand Down
10 changes: 6 additions & 4 deletions src/Report/Reporter/TextReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TwigCsFixer\File\FileHelper;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\Violation;

Expand Down Expand Up @@ -59,7 +60,7 @@ public function display(
$formattedText[] = sprintf(
self::ERROR_LINE_FORMAT,
$no,
wordwrap($code, self::ERROR_LINE_WIDTH)
wordwrap($code, self::ERROR_LINE_WIDTH, \PHP_EOL)
);

if ($no === $violation->getLine()) {
Expand All @@ -75,7 +76,7 @@ public function display(
$messageLevel = Violation::getLevelAsString($violation->getLevel());
$rows[] = [
new TableCell(sprintf('<comment>%s</comment>', $messageLevel)),
implode("\n", $formattedText),
implode(\PHP_EOL, $formattedText),
];
}

Expand Down Expand Up @@ -106,7 +107,8 @@ public function display(
*/
private function getContext(string $template, int $line): array
{
$lines = explode("\n", $template);
$eol = FileHelper::detectEOL($template);
$lines = explode($eol, $template);
$position = max(0, $line - 2);
$max = min(\count($lines), $line + 1);

Expand All @@ -128,7 +130,7 @@ private function formatErrorMessage(Violation $message, bool $debug): string
return sprintf(
sprintf('<fg=red>%s</fg=red>', self::ERROR_LINE_FORMAT),
self::ERROR_CURSOR_CHAR,
wordwrap($message->getDebugMessage($debug), self::ERROR_LINE_WIDTH)
wordwrap($message->getDebugMessage($debug), self::ERROR_LINE_WIDTH, \PHP_EOL)
);
}
}
6 changes: 3 additions & 3 deletions src/Runner/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Twig\Source;
use TwigCsFixer\Exception\CannotFixFileException;
use TwigCsFixer\File\FileHelper;
use TwigCsFixer\Rules\FixableRuleInterface;
use TwigCsFixer\Ruleset\Ruleset;
use TwigCsFixer\Token\Token;
Expand All @@ -21,7 +22,7 @@ final class Fixer implements FixerInterface

private int $loops = 0;

private string $eolChar = "\n";
private string $eolChar = \PHP_EOL;

/**
* The list of tokens that make up the file contents.
Expand Down Expand Up @@ -224,8 +225,7 @@ private function startFile(array $tokens): void

$this->tokens = array_map(static fn (Token $token): string => $token->getValue(), $tokens);

preg_match("/\r\n?|\n/", $this->getContent(), $matches);
$this->eolChar = $matches[0] ?? "\n";
$this->eolChar = FileHelper::detectEOL($this->getContent());
}

private function getContent(): string
Expand Down
Loading
Loading