Skip to content

Commit

Permalink
Fix 2
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Feb 19, 2024
1 parent 96128ad commit f95bc12
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
test:
name: PHP ${{ matrix.runner }} + ${{ matrix.php-version }} + ${{ matrix.dependencies }}
name: PHP ${{ matrix.php-version }} + ${{ matrix.dependencies }}
runs-on: ubuntu-latest

env:
Expand Down
8 changes: 4 additions & 4 deletions src/Report/Reporter/TextReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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 +75,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 +106,7 @@ public function display(
*/
private function getContext(string $template, int $line): array
{
$lines = explode("\n", $template);
$lines = explode(\PHP_EOL, $template);
$position = max(0, $line - 2);
$max = min(\count($lines), $line + 1);

Expand All @@ -128,7 +128,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: 5 additions & 1 deletion tests/Binary/TwigCsFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

use Symfony\Component\Process\Process;
use TwigCsFixer\Tests\FileTestCase;
use TwigCsFixer\Tests\TestHelper;

final class TwigCsFixerTest extends FileTestCase
{
public function testBinary(): void
{
$process = Process::fromShellCommandline(sprintf('%s lint Fixtures', __DIR__.'/../../bin/twig-cs-fixer'));
$process = Process::fromShellCommandline(sprintf(
'%s lint Fixtures',
TestHelper::getOsPath(__DIR__.'/../../bin/twig-cs-fixer')
));

static::assertSame(0, $process->run());
}
Expand Down
17 changes: 9 additions & 8 deletions tests/Command/TwigCsFixerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use TwigCsFixer\Command\TwigCsFixerCommand;
use TwigCsFixer\Config\Config;
use TwigCsFixer\Tests\FileTestCase;
use TwigCsFixer\Tests\TestHelper;

final class TwigCsFixerCommandTest extends FileTestCase
{
Expand Down Expand Up @@ -59,8 +60,8 @@ public function testExecuteWithReportErrors(): void
]);

$display = $commandTester->getDisplay();
static::assertStringContainsString('directory/subdirectory/file.twig', $display);
static::assertStringContainsString('directory/file.twig', $display);
static::assertStringContainsString(TestHelper::getOsPath('directory/subdirectory/file.twig'), $display);
static::assertStringContainsString(TestHelper::getOsPath('directory/file.twig'), $display);
static::assertStringNotContainsString('DelimiterSpacing.After', $display);
static::assertStringContainsString(
'[ERROR] Files linted: 3, notices: 0, warnings: 0, errors: 3',
Expand All @@ -80,8 +81,8 @@ public function testExecuteWithReportErrorsAndDebug(): void
]);

$display = $commandTester->getDisplay();
static::assertStringContainsString('directory/subdirectory/file.twig', $display);
static::assertStringContainsString('directory/file.twig', $display);
static::assertStringContainsString(TestHelper::getOsPath('directory/subdirectory/file.twig'), $display);
static::assertStringContainsString(TestHelper::getOsPath('directory/file.twig'), $display);
static::assertStringContainsString('DelimiterSpacing.After', $display);
static::assertStringContainsString(
'[ERROR] Files linted: 3, notices: 0, warnings: 0, errors: 3',
Expand All @@ -102,8 +103,8 @@ public function testExecuteWithReportErrorsFixed(): void

$display = $commandTester->getDisplay();
static::assertStringNotContainsString('Changed', $display);
static::assertStringNotContainsString('directory/subdirectory/file.twig', $display);
static::assertStringContainsString('directory/file.twig', $display);
static::assertStringNotContainsString(TestHelper::getOsPath('directory/subdirectory/file.twig'), $display);
static::assertStringContainsString(TestHelper::getOsPath('directory/file.twig'), $display);
static::assertStringContainsString(
'[ERROR] Files linted: 3, notices: 0, warnings: 0, errors: 1',
$display
Expand All @@ -123,8 +124,8 @@ public function testExecuteWithReportErrorsFixedVerbose(): void

$display = $commandTester->getDisplay();
static::assertStringContainsString('Changed', $display);
static::assertStringContainsString('directory/subdirectory/file.twig', $display);
static::assertStringContainsString('directory/file.twig', $display);
static::assertStringContainsString(TestHelper::getOsPath('directory/subdirectory/file.twig'), $display);
static::assertStringContainsString(TestHelper::getOsPath('directory/file.twig'), $display);
static::assertStringContainsString(
'[ERROR] Files linted: 3, notices: 0, warnings: 0, errors: 1',
$display
Expand Down
26 changes: 1 addition & 25 deletions tests/Config/ConfigResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function resolveConfigDataProvider(): iterable
yield [__DIR__.'/Fixtures/directoryWithCustomRuleset', null, 'Custom'];
yield [__DIR__.'/Fixtures/directoryWithCustomRuleset2', null, 'CustomDist'];
yield [__DIR__, 'Fixtures/directoryWithCustomRuleset/.twig-cs-fixer.php', 'Custom'];
yield ['/tmp', __DIR__.'/Fixtures/directoryWithCustomRuleset/.twig-cs-fixer.php', 'Custom'];
yield [__DIR__.'/..', __DIR__.'/Fixtures/directoryWithCustomRuleset/.twig-cs-fixer.php', 'Custom'];
}

/**
Expand Down Expand Up @@ -114,30 +114,6 @@ public static function resolveFinderDataProvider(): iterable
];
}

/**
* @dataProvider configPathIsCorrectlyGeneratedDataProvider
*/
public function testConfigPathIsCorrectlyGenerated(string $configPath, string $path): void
{
$configResolver = new ConfigResolver('/tmp/path/not/found');

$this->expectExceptionMessage(sprintf('Cannot find the config file "%s".', $configPath));
$configResolver->resolveConfig([], $path);
}

/**
* @return iterable<array-key, array{string, string}>
*/
public static function configPathIsCorrectlyGeneratedDataProvider(): iterable
{
yield ['/tmp/path/not/found/', ''];
yield ['/tmp/path/not/found/a', 'a'];
yield ['/tmp/path/not/found/../a', '../a'];
yield ['/a', '/a'];
yield ['\\a', '\\a'];
yield ['C:\WINDOWS', 'C:\WINDOWS'];
}

/**
* @param class-string<CacheManagerInterface>|null $expectedCacheManager
*
Expand Down
5 changes: 3 additions & 2 deletions tests/FileTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function setUp(): void
{
parent::setUp();

$fixtureDir = $this->getDir().'/Fixtures';
$fixtureDir = $this->getDir().\DIRECTORY_SEPARATOR.'Fixtures';
$tmpFixtures = $this->getTmpPath($fixtureDir);

if ($tmpFixtures !== $fixtureDir) {
Expand Down Expand Up @@ -58,6 +58,7 @@ protected function getFilesystem(): Filesystem

protected function getTmpPath(string $path): string
{
$path = TestHelper::getOsPath($path);
if (!str_starts_with($path, $this->getDir())) {
return $path;
}
Expand Down Expand Up @@ -87,7 +88,7 @@ private function getTmp(): string
if (false === $tmp) {
$this->tmp = $this->getDir();
} else {
$this->tmp = $tmp.'/twig-cs-fixer';
$this->tmp = $tmp.\DIRECTORY_SEPARATOR.'twig-cs-fixer';
}
}

Expand Down
27 changes: 16 additions & 11 deletions tests/Report/Reporter/CheckstyleReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use TwigCsFixer\Report\Reporter\CheckstyleReporter;
use TwigCsFixer\Report\Violation;
use TwigCsFixer\Report\ViolationId;
use TwigCsFixer\Tests\TestHelper;

final class CheckstyleReporterTest extends TestCase
{
Expand Down Expand Up @@ -81,7 +82,7 @@ public function testDisplayErrors(string $expected, ?string $level, bool $debug)
$textFormatter->display($output, $report, $level, $debug);

$text = $output->fetch();
static::assertStringContainsString($expected, $text);
static::assertSame($expected, rtrim($text));
}

/**
Expand All @@ -94,21 +95,23 @@ public static function displayDataProvider(): iterable
<<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="%1\$s/Fixtures/file.twig">
<file name="%1\$s">
<error line="1" severity="notice" message="Notice" source="NoticeRule"/>
<error line="2" column="22" severity="warning" message="Warning" source="WarningRule"/>
<error line="3" column="33" severity="error" message="Error" source="ErrorRule"/>
<error severity="fatal" message="Fatal"/>
</file>
<file name="%1\$s/Fixtures/file2.twig">
<file name="%2\$s">
<error line="1" severity="notice" message="Notice2" source="Notice2Rule"/>
</file>
<file name="%1\$s/Fixtures/file3.twig">
<file name="%3\$s">
<error severity="fatal" message="&apos;&quot;&lt;&amp;&gt;&quot;&apos;"/>
</file>
</checkstyle>
EOD,
__DIR__
TestHelper::getOsPath(__DIR__.'/Fixtures/file.twig'),
TestHelper::getOsPath(__DIR__.'/Fixtures/file2.twig'),
TestHelper::getOsPath(__DIR__.'/Fixtures/file3.twig'),
),
null,
false,
Expand All @@ -119,16 +122,17 @@ public static function displayDataProvider(): iterable
<<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="%1\$s/Fixtures/file.twig">
<file name="%1\$s">
<error line="3" column="33" severity="error" message="Error" source="ErrorRule"/>
<error severity="fatal" message="Fatal"/>
</file>
<file name="%1\$s/Fixtures/file3.twig">
<file name="%2\$s">
<error severity="fatal" message="&apos;&quot;&lt;&amp;&gt;&quot;&apos;"/>
</file>
</checkstyle>
EOD,
__DIR__
TestHelper::getOsPath(__DIR__.'/Fixtures/file.twig'),
TestHelper::getOsPath(__DIR__.'/Fixtures/file3.twig'),
),
Report::MESSAGE_TYPE_ERROR,
false,
Expand All @@ -139,16 +143,17 @@ public static function displayDataProvider(): iterable
<<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="%1\$s/Fixtures/file.twig">
<file name="%1\$s">
<error line="3" column="33" severity="error" message="ErrorId:3:33" source="ErrorRule"/>
<error severity="fatal" message="FatalId"/>
</file>
<file name="%1\$s/Fixtures/file3.twig">
<file name="%2\$s">
<error severity="fatal" message="FatalId"/>
</file>
</checkstyle>
EOD,
__DIR__
TestHelper::getOsPath(__DIR__.'/Fixtures/file.twig'),
TestHelper::getOsPath(__DIR__.'/Fixtures/file3.twig'),
),
Report::MESSAGE_TYPE_ERROR,
true,
Expand Down
25 changes: 13 additions & 12 deletions tests/Report/Reporter/GithubReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use TwigCsFixer\Report\Reporter\GithubReporter;
use TwigCsFixer\Report\Violation;
use TwigCsFixer\Report\ViolationId;
use TwigCsFixer\Tests\TestHelper;

final class GithubReporterTest extends TestCase
{
Expand All @@ -21,7 +22,7 @@ public function testDisplayErrors(string $expected, ?string $level, bool $debug)
{
$textFormatter = new GithubReporter();

$file = __DIR__.'/Fixtures/file.twig';
$file = TestHelper::getOsPath(__DIR__.'/Fixtures/file.twig');
$report = new Report([new \SplFileInfo($file)]);

$violation0 = new Violation(
Expand Down Expand Up @@ -61,7 +62,7 @@ public function testDisplayErrors(string $expected, ?string $level, bool $debug)
$textFormatter->display($output, $report, $level, $debug);

$text = $output->fetch();
static::assertStringContainsString($expected, $text);
static::assertSame($expected, rtrim($text));
}

/**
Expand All @@ -72,25 +73,25 @@ public static function displayDataProvider(): iterable
yield [
sprintf(
<<<EOD
::notice file=%1\$s/Fixtures/file.twig,line=1::Notice
::warning file=%1\$s/Fixtures/file.twig,line=2,col=22::Warning
::error file=%1\$s/Fixtures/file.twig,line=3,col=33::Error
::error file=%1\$s/Fixtures/file.twig::Fatal%%0Awith new line
::notice file=%1\$s,line=1::Notice
::warning file=%1\$s,line=2,col=22::Warning
::error file=%1\$s,line=3,col=33::Error
::error file=%1\$s::Fatal%%0Awith new line
EOD,
__DIR__
TestHelper::getOsPath(__DIR__.'/Fixtures/file.twig'),
),
null,
false,
];
yield [
sprintf(
<<<EOD
::notice file=%1\$s/Fixtures/file.twig,line=1::NoticeId:1
::warning file=%1\$s/Fixtures/file.twig,line=2,col=22::WarningId:2:22
::error file=%1\$s/Fixtures/file.twig,line=3,col=33::ErrorId:3:33
::error file=%1\$s/Fixtures/file.twig::FatalId
::notice file=%1\$s,line=1::NoticeId:1
::warning file=%1\$s,line=2,col=22::WarningId:2:22
::error file=%1\$s,line=3,col=33::ErrorId:3:33
::error file=%1\$s::FatalId
EOD,
__DIR__
TestHelper::getOsPath(__DIR__.'/Fixtures/file.twig'),
),
null,
true,
Expand Down
Loading

0 comments on commit f95bc12

Please sign in to comment.