Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Feb 19, 2024
1 parent 96128ad commit f2eaeec
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 116 deletions.
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 @@ -11,7 +11,11 @@ final class TwigCsFixerTest extends FileTestCase
{
public function testBinary(): void
{
$process = Process::fromShellCommandline(sprintf('%s lint Fixtures', __DIR__.'/../../bin/twig-cs-fixer'));
$slash = \DIRECTORY_SEPARATOR;
$process = Process::fromShellCommandline(sprintf(
'%s lint Fixtures',
__DIR__.$slash.'..'.$slash.'..'.$slash.'bin'.$slash.'twig-cs-fixer'
));

static::assertSame(0, $process->run());
}
Expand Down
88 changes: 56 additions & 32 deletions tests/Command/TwigCsFixerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testExecuteWithSuccess(): void

$commandTester = new CommandTester($command);
$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures/file.twig')],
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'file.twig')],
]);

static::assertStringContainsString(
Expand All @@ -38,7 +38,7 @@ public function testExecuteWithOptionFix(): void

$commandTester = new CommandTester($command);
$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures/file.twig')],
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'file.twig')],
'--fix' => true,
]);

Expand All @@ -55,12 +55,18 @@ public function testExecuteWithReportErrors(): void

$commandTester = new CommandTester($command);
$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures')],
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')],
]);

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

$commandTester = new CommandTester($command);
$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures')],
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')],
'--debug' => true,
]);

$display = $commandTester->getDisplay();
static::assertStringContainsString('directory/subdirectory/file.twig', $display);
static::assertStringContainsString('directory/file.twig', $display);
static::assertStringContainsString(
'directory'.\DIRECTORY_SEPARATOR.'subdirectory'.\DIRECTORY_SEPARATOR.'file.twig',
$display
);
static::assertStringContainsString(
'directory'.\DIRECTORY_SEPARATOR.'file.twig',
$display
);
static::assertStringContainsString('DelimiterSpacing.After', $display);
static::assertStringContainsString(
'[ERROR] Files linted: 3, notices: 0, warnings: 0, errors: 3',
Expand All @@ -96,14 +108,20 @@ public function testExecuteWithReportErrorsFixed(): void

$commandTester = new CommandTester($command);
$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures')],
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')],
'--fix' => true,
]);

$display = $commandTester->getDisplay();
static::assertStringNotContainsString('Changed', $display);
static::assertStringNotContainsString('directory/subdirectory/file.twig', $display);
static::assertStringContainsString('directory/file.twig', $display);
static::assertStringNotContainsString(
'directory'.\DIRECTORY_SEPARATOR.'subdirectory'.\DIRECTORY_SEPARATOR.'file.twig',
$display
);
static::assertStringContainsString(
'directory'.\DIRECTORY_SEPARATOR.'file.twig',
$display
);
static::assertStringContainsString(
'[ERROR] Files linted: 3, notices: 0, warnings: 0, errors: 1',
$display
Expand All @@ -117,14 +135,20 @@ public function testExecuteWithReportErrorsFixedVerbose(): void

$commandTester = new CommandTester($command);
$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures')],
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')],
'--fix' => true,
], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);

$display = $commandTester->getDisplay();
static::assertStringContainsString('Changed', $display);
static::assertStringContainsString('directory/subdirectory/file.twig', $display);
static::assertStringContainsString('directory/file.twig', $display);
static::assertStringContainsString(
'directory'.\DIRECTORY_SEPARATOR.'subdirectory'.\DIRECTORY_SEPARATOR.'file.twig',
$display
);
static::assertStringContainsString(
'directory'.\DIRECTORY_SEPARATOR.'file.twig',
$display
);
static::assertStringContainsString(
'[ERROR] Files linted: 3, notices: 0, warnings: 0, errors: 1',
$display
Expand All @@ -138,7 +162,7 @@ public function testExecuteWithReportOption(): void

$commandTester = new CommandTester($command);
$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures')],
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')],
'--no-cache' => true, // To avoid cache output
'--report' => 'null',
]);
Expand All @@ -153,8 +177,8 @@ public function testExecuteWithConfig(): void

$commandTester = new CommandTester($command);
$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures')],
'--config' => $this->getTmpPath(__DIR__.'/Fixtures/.twig-cs-fixer.php'),
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')],
'--config' => $this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'.twig-cs-fixer.php'),
]);

static::assertStringContainsString(
Expand All @@ -170,8 +194,8 @@ public function testExecuteWithError(): void

$commandTester = new CommandTester($command);
$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures')],
'--config' => $this->getTmpPath(__DIR__.'/Fixtures/.config-not-found.php'),
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')],
'--config' => $this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'.config-not-found.php'),
]);

static::assertStringStartsWith('Error: Cannot find the config file', $commandTester->getDisplay());
Expand All @@ -184,7 +208,7 @@ public function testExecuteWithCacheByDefault(): void

$commandTester = new CommandTester($command);

$path = $this->getTmpPath(__DIR__.'/Fixtures/file.twig');
$path = $this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'file.twig');

// Run two times to be sure to generate the cache.
$commandTester->execute([
Expand All @@ -207,7 +231,7 @@ public function testExecuteWithCacheDisabled(): void

$commandTester = new CommandTester($command);

$path = $this->getTmpPath(__DIR__.'/Fixtures/file.twig');
$path = $this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'file.twig');

// Run two times to be sure to generate the cache if we were using one.
$commandTester->execute([
Expand All @@ -232,8 +256,8 @@ public function testExecuteWithCacheFile(): void

$commandTester = new CommandTester($command);
$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures')],
'--config' => $this->getTmpPath(__DIR__.'/Fixtures/.twig-cs-fixer-with-cache.php'),
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')],
'--config' => $this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'.twig-cs-fixer-with-cache.php'),
]);

// Result with no ruleset
Expand All @@ -242,16 +266,16 @@ public function testExecuteWithCacheFile(): void
$commandTester->getDisplay()
);

$cachePath = $this->getTmpPath(__DIR__.'/Fixtures/.twig-cs-fixer.cache');
$cachePath = $this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'.twig-cs-fixer.cache');
$cacheContent = file_get_contents($cachePath);
static::assertNotFalse($cacheContent);

// Save the hashes in order to rewrite manually the cache later
$hashes = CacheEncoder::fromJson($cacheContent)->getHashes();

$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures')],
'--config' => $this->getTmpPath(__DIR__.'/Fixtures/.twig-cs-fixer-with-cache2.php'),
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')],
'--config' => $this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'.twig-cs-fixer-with-cache2.php'),
]);

// Result with standard ruleset
Expand All @@ -276,8 +300,8 @@ public function testExecuteWithCacheFile(): void
file_put_contents($cachePath, CacheEncoder::toJson($cache));

$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures')],
'--config' => $this->getTmpPath(__DIR__.'/Fixtures/.twig-cs-fixer-with-cache2.php'),
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')],
'--config' => $this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'.twig-cs-fixer-with-cache2.php'),
]);

// We get the same result as with no ruleset because of the cache
Expand All @@ -299,8 +323,8 @@ public function testExecuteWithCacheFile(): void
file_put_contents($cachePath, CacheEncoder::toJson($newCache));

$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures')],
'--config' => $this->getTmpPath(__DIR__.'/Fixtures/.twig-cs-fixer-with-cache2.php'),
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')],
'--config' => $this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'.twig-cs-fixer-with-cache2.php'),
]);

// We get back the real result because of the different php version
Expand All @@ -322,8 +346,8 @@ public function testExecuteWithCacheFile(): void
file_put_contents($cachePath, CacheEncoder::toJson($newCache));

$commandTester->execute([
'paths' => [$this->getTmpPath(__DIR__.'/Fixtures')],
'--config' => $this->getTmpPath(__DIR__.'/Fixtures/.twig-cs-fixer-with-cache2.php'),
'paths' => [$this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures')],
'--config' => $this->getTmpPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'.twig-cs-fixer-with-cache2.php'),
]);

// We get back the real result because of the different fixer version
Expand Down
21 changes: 13 additions & 8 deletions tests/Config/ConfigResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public static function resolveFinderDataProvider(): iterable
*/
public function testConfigPathIsCorrectlyGenerated(string $configPath, string $path): void
{
$configResolver = new ConfigResolver('/tmp/path/not/found');
$slash = \DIRECTORY_SEPARATOR;
$configResolver = new ConfigResolver($slash.'tmp'.$slash.'path'.$slash.'not'.$slash.'found');

$this->expectExceptionMessage(sprintf('Cannot find the config file "%s".', $configPath));
$configResolver->resolveConfig([], $path);
Expand All @@ -130,9 +131,11 @@ public function testConfigPathIsCorrectlyGenerated(string $configPath, string $p
*/
public static function configPathIsCorrectlyGeneratedDataProvider(): iterable
{
yield ['/tmp/path/not/found/', ''];
yield ['/tmp/path/not/found/a', 'a'];
yield ['/tmp/path/not/found/../a', '../a'];
$slash = \DIRECTORY_SEPARATOR;

yield [$slash.'tmp'.$slash.'path'.$slash.'not'.$slash.'found'.$slash, ''];
yield [$slash.'tmp'.$slash.'path'.$slash.'not'.$slash.'found'.$slash.'a', 'a'];
yield [$slash.'tmp'.$slash.'path'.$slash.'not'.$slash.'found'.$slash.'..'.$slash.'a', '..'.$slash.'a'];
yield ['/a', '/a'];
yield ['\\a', '\\a'];
yield ['C:\WINDOWS', 'C:\WINDOWS'];
Expand Down Expand Up @@ -173,19 +176,21 @@ public function testResolveCacheManager(
*/
public static function resolveCacheManagerDataProvider(): iterable
{
$slash = \DIRECTORY_SEPARATOR;

yield [null, false, Config::DEFAULT_CACHE_PATH, FileCacheManager::class];
yield [null, true, null, null];

$path = __DIR__.'/Fixtures/directoryWithCustomCacheManager/.twig-cs-fixer.php';
$path = __DIR__.$slash.'Fixtures'.$slash.'directoryWithCustomCacheManager'.$slash.'.twig-cs-fixer.php';
yield [$path, false, Config::DEFAULT_CACHE_PATH, NullCacheManager::class];
yield [$path, true, null, null];

$path = __DIR__.'/Fixtures/directoryWithCustomCacheFile/.twig-cs-fixer.php';
$cachePath = __DIR__.'/Fixtures/directoryWithCustomCacheFile/.twig-cs-fixer.cache';
$path = __DIR__.$slash.'Fixtures'.$slash.'directoryWithCustomCacheFile'.$slash.'.twig-cs-fixer.php';
$cachePath = __DIR__.$slash.'Fixtures'.$slash.'directoryWithCustomCacheFile'.$slash.'.twig-cs-fixer.cache';
yield [$path, false, $cachePath, FileCacheManager::class];
yield [$path, true, null, null];

$path = __DIR__.'/Fixtures/directoryWithNoCacheFile/.twig-cs-fixer.php';
$path = __DIR__.$slash.'Fixtures'.$slash.'directoryWithNoCacheFile'.$slash.'.twig-cs-fixer.php';
yield [$path, false, null, null];
yield [$path, true, null, null];
}
Expand Down
4 changes: 2 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 @@ -87,7 +87,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
Loading

0 comments on commit f2eaeec

Please sign in to comment.