Skip to content

Commit

Permalink
Merge pull request #1029 from maxbeckers/patch-586
Browse files Browse the repository at this point in the history
Fix unittests for windows
  • Loading branch information
Denis Brumann committed Dec 2, 2022
2 parents a02eafc + 048d144 commit 9a2b42b
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 35 deletions.
8 changes: 6 additions & 2 deletions tests/Core/InputCollector/FileInputCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public function testCollectsPhpFilesUsingAbsolutePath(): void
{
$collector = new FileInputCollector([__DIR__.'/Fixtures'], [], sys_get_temp_dir());

$files = $collector->collect();
$files = array_map(static function ($filePath) {
return Path::normalize($filePath);
}, $collector->collect());

natcasesort($files);

Expand All @@ -32,7 +34,9 @@ public function testCollectsPhpFilesUsingRelativePath(): void
{
$collector = new FileInputCollector(['Fixtures'], [], __DIR__);

$files = $collector->collect();
$files = array_map(static function ($filePath) {
return Path::normalize($filePath);
}, $collector->collect());

natcasesort($files);

Expand Down
5 changes: 3 additions & 2 deletions tests/Core/InputCollector/PathNameFilterIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ public function testFilter(Iterator $inner, array $matchPatterns, array $noMatch

$values = array_map(
static function (SplFileInfo $fileInfo) {
return str_replace('/', DIRECTORY_SEPARATOR, $fileInfo->getPathname());
// replace the DIRECTORY_SEPARATOR with / to match with the expected result
return str_replace(DIRECTORY_SEPARATOR, '/', $fileInfo->getPathname());
},
iterator_to_array($iterator, false)
);

sort($values);
sort($resultArray);

self::assertSame($resultArray, array_values($values));
self::assertSame($resultArray, $values);
}

public function getTestFilterData(): array
Expand Down
18 changes: 10 additions & 8 deletions tests/Supportive/Console/ConsoleSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use const PHP_EOL;

final class ConsoleSubscriberTest extends TestCase
{
public function testSubscribedEvents(): void
Expand Down Expand Up @@ -48,7 +50,7 @@ public function testOnPreCreateAstMapEventWithVerboseVerbosity(): void
$subscriber = new ConsoleSubscriber($output);
$subscriber->onPreCreateAstMapEvent(new PreCreateAstMapEvent(9999999));

self::assertSame("Start to create an AstMap for 9999999 Files.\n", $symfonyOutput->fetch());
self::assertSame('Start to create an AstMap for 9999999 Files.'.PHP_EOL, $symfonyOutput->fetch());
}

public function testOnPostCreateAstMapEventWithVerboseVerbosity(): void
Expand All @@ -59,7 +61,7 @@ public function testOnPostCreateAstMapEventWithVerboseVerbosity(): void
$subscriber = new ConsoleSubscriber($output);
$subscriber->onPostCreateAstMapEvent(new PostCreateAstMapEvent());

self::assertSame("AstMap created.\n", $symfonyOutput->fetch());
self::assertSame('AstMap created.'.PHP_EOL, $symfonyOutput->fetch());
}

public function testOnAstFileAnalysedEventWithVerboseVerbosity(): void
Expand All @@ -70,7 +72,7 @@ public function testOnAstFileAnalysedEventWithVerboseVerbosity(): void
$subscriber = new ConsoleSubscriber($output);
$subscriber->onAstFileAnalysedEvent(new AstFileAnalysedEvent('foo.php'));

self::assertSame("Parsing File foo.php\n", $symfonyOutput->fetch());
self::assertSame('Parsing File foo.php'.PHP_EOL, $symfonyOutput->fetch());
}

public function testOnAstFileSyntaxErrorEvent(): void
Expand All @@ -83,7 +85,7 @@ public function testOnAstFileSyntaxErrorEvent(): void
new AstFileSyntaxErrorEvent('foo.php', 'Invalid')
);

self::assertSame("\nSyntax Error on File foo.php\nInvalid\n\n", $symfonyOutput->fetch());
self::assertSame("\nSyntax Error on File foo.php\nInvalid\n".PHP_EOL, $symfonyOutput->fetch());
}

public function testOnPreDependencyEmit(): void
Expand All @@ -94,7 +96,7 @@ public function testOnPreDependencyEmit(): void
$subscriber = new ConsoleSubscriber($output);
$subscriber->onPreDependencyEmit(new PreEmitEvent('emitter-name'));

self::assertSame("start emitting dependencies \"emitter-name\"\n", $symfonyOutput->fetch());
self::assertSame('start emitting dependencies "emitter-name"'.PHP_EOL, $symfonyOutput->fetch());
}

public function testOnPostDependencyEmit(): void
Expand All @@ -105,7 +107,7 @@ public function testOnPostDependencyEmit(): void
$subscriber = new ConsoleSubscriber($output);
$subscriber->onPostDependencyEmit(new PostEmitEvent());

self::assertSame("end emitting dependencies\n", $symfonyOutput->fetch());
self::assertSame('end emitting dependencies'.PHP_EOL, $symfonyOutput->fetch());
}

public function testOnPreDependencyFlatten(): void
Expand All @@ -116,7 +118,7 @@ public function testOnPreDependencyFlatten(): void
$subscriber = new ConsoleSubscriber($output);
$subscriber->onPreDependencyFlatten(new PreFlattenEvent());

self::assertSame("start flatten dependencies\n", $symfonyOutput->fetch());
self::assertSame('start flatten dependencies'.PHP_EOL, $symfonyOutput->fetch());
}

public function testOnPostDependencyFlatten(): void
Expand All @@ -127,6 +129,6 @@ public function testOnPostDependencyFlatten(): void
$subscriber = new ConsoleSubscriber($output);
$subscriber->onPostDependencyFlatten(new PostFlattenEvent());

self::assertSame("end flatten dependencies\n", $symfonyOutput->fetch());
self::assertSame('end flatten dependencies'.PHP_EOL, $symfonyOutput->fetch());
}
}
39 changes: 27 additions & 12 deletions tests/Supportive/Console/ProgressSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
use Qossmic\Deptrac\Supportive\Console\Subscriber\ProgressSubscriber;
use Qossmic\Deptrac\Supportive\Console\Symfony\Style;
use Qossmic\Deptrac\Supportive\Console\Symfony\SymfonyOutput;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use const PHP_EOL;

final class ProgressSubscriberTest extends TestCase
{
public function testSubscribedEvents(): void
Expand All @@ -38,12 +42,8 @@ public function testProgress(): void
$subscriber->onAstFileAnalysedEvent(new AstFileAnalysedEvent('foo.php'));
$subscriber->onPostCreateAstMapEvent(new PostCreateAstMapEvent());

$expectedOutput = <<<OUT
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0%
1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
OUT;
$expectedOutput = " 0/1 [{$this->getEmptyBarOutput()}] 0%".PHP_EOL.
" 1/1 [{$this->getBarOutput()}] 100%".PHP_EOL.PHP_EOL;

self::assertSame($expectedOutput, $bufferedOutput->fetch());
}
Expand All @@ -56,12 +56,8 @@ public function testOnPostCreateAstMapEvent(): void
$subscriber->onPreCreateAstMapEvent(new PreCreateAstMapEvent(1));
$subscriber->onPostCreateAstMapEvent(new PostCreateAstMapEvent());

$expectedOutput = <<<OUT
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0%
1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
OUT;
$expectedOutput = " 0/1 [{$this->getEmptyBarOutput()}] 0%".PHP_EOL.
" 1/1 [{$this->getBarOutput()}] 100%".PHP_EOL.PHP_EOL;

self::assertSame($expectedOutput, $formatter->fetch());
}
Expand All @@ -73,4 +69,23 @@ private function createSymfonyOutput(BufferedOutput $bufferedOutput): SymfonyOut
new Style(new SymfonyStyle($this->createMock(InputInterface::class), $bufferedOutput))
);
}

private function getEmptyBarOutput(): string
{
$progressChar = $this->getProgressBar()->getProgressCharacter();

return $progressChar.str_repeat($this->getProgressBar()->getEmptyBarCharacter(), '' === $progressChar ? 28 : 27);
}

private function getBarOutput(): string
{
return str_repeat($this->getProgressBar()->getBarCharacter(), 28);
}

private function getProgressBar(): ProgressBar
{
$style = new SymfonyStyle($this->createMock(InputInterface::class), $this->createMock(OutputInterface::class));

return $style->createProgressBar(28);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Style\SymfonyStyle;

use const PHP_EOL;

final class GithubActionsOutputFormatterTest extends TestCase
{
public function testGetName(): void
Expand Down Expand Up @@ -78,7 +80,7 @@ public function finishProvider(): iterable
],
'errors' => [],
'warnings' => [],
"::error file=/home/testuser/originalA.php,line=12::ACME\OriginalA must not depend on ACME\OriginalB (LayerA on LayerB)\n",
"::error file=/home/testuser/originalA.php,line=12::ACME\OriginalA must not depend on ACME\OriginalB (LayerA on LayerB)".PHP_EOL,
];

yield 'Skipped Violation' => [
Expand All @@ -91,7 +93,7 @@ public function finishProvider(): iterable
],
'errors' => [],
'warnings' => [],
"::warning file=/home/testuser/originalA.php,line=12::[SKIPPED] ACME\OriginalA must not depend on ACME\OriginalB (LayerA on LayerB)\n",
"::warning file=/home/testuser/originalA.php,line=12::[SKIPPED] ACME\OriginalA must not depend on ACME\OriginalB (LayerA on LayerB)".PHP_EOL,
];

yield 'Uncovered Dependency' => [
Expand All @@ -103,7 +105,7 @@ public function finishProvider(): iterable
],
'errors' => [],
'warnings' => [],
"::warning file=/home/testuser/originalA.php,line=12::ACME\OriginalA has uncovered dependency on ACME\OriginalB (LayerA)\n",
"::warning file=/home/testuser/originalA.php,line=12::ACME\OriginalA has uncovered dependency on ACME\OriginalB (LayerA)".PHP_EOL,
];

yield 'Inherit dependency' => [
Expand Down Expand Up @@ -141,14 +143,14 @@ public function finishProvider(): iterable
],
'errors' => [],
'warnings' => [],
"::error file=originalA.php,line=12::ClassA must not depend on ClassB (LayerA on LayerB)%0AClassInheritD::6 ->%0AClassInheritC::5 ->%0AClassInheritB::4 ->%0AClassInheritA::3 ->%0AACME\OriginalB::12\n",
"::error file=originalA.php,line=12::ClassA must not depend on ClassB (LayerA on LayerB)%0AClassInheritD::6 ->%0AClassInheritC::5 ->%0AClassInheritB::4 ->%0AClassInheritA::3 ->%0AACME\OriginalB::12".PHP_EOL,
];

yield 'an error occurred' => [
'violations' => [],
'errors' => [new Error('an error occurred')],
'warnings' => [],
"::error ::an error occurred\n",
'::error ::an error occurred'.PHP_EOL,
];

yield 'an warning occurred' => [
Expand All @@ -157,7 +159,7 @@ public function finishProvider(): iterable
'warnings' => [
Warning::tokenIsInMoreThanOneLayer(ClassLikeToken::fromFQCN('Foo\Bar')->toString(), ['Layer 1', 'Layer 2']),
],
"::warning ::Foo\Bar is in more than one layer [\"Layer 1\", \"Layer 2\"]. It is recommended that one token should only be in one layer.\n",
"::warning ::Foo\Bar is in more than one layer [\"Layer 1\", \"Layer 2\"]. It is recommended that one token should only be in one layer.".PHP_EOL,
];
}

Expand Down Expand Up @@ -220,7 +222,7 @@ public function testUncoveredWithFailOnUncoveredAreReportedAsError(): void
);

self::assertSame(
"::error file=/home/testuser/originalA.php,line=12::ACME\OriginalA has uncovered dependency on ACME\OriginalB (LayerA)\n",
"::error file=/home/testuser/originalA.php,line=12::ACME\OriginalA has uncovered dependency on ACME\OriginalB (LayerA)".PHP_EOL,
$bufferedOutput->fetch()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Path;

use const PHP_EOL;

final class GraphVizDotOutputFormatterTest extends TestCase
{
Expand Down Expand Up @@ -59,7 +62,10 @@ public function testFinish(): void
],
])))->finish($context, $this->createSymfonyOutput($bufferedOutput), $input);

self::assertSame(sprintf("Script dumped to %s\n", $dotFile), $bufferedOutput->fetch());
self::assertSame(sprintf('Script dumped to %s'.PHP_EOL, Path::normalize($dotFile)), Path::normalize($bufferedOutput->fetch()));

$this->replaceWindowsLineEndings($dotFile);

self::assertFileEquals(__DIR__.'/data/graphviz-expected.dot', $dotFile);

unlink($dotFile);
Expand Down Expand Up @@ -106,7 +112,10 @@ public function testGroups(): void
],
])))->finish($context, $this->createSymfonyOutput($bufferedOutput), $input);

self::assertSame(sprintf("Script dumped to %s\n", $dotFile), $bufferedOutput->fetch());
self::assertSame(sprintf('Script dumped to %s'.PHP_EOL, Path::normalize($dotFile)), Path::normalize($bufferedOutput->fetch()));

$this->replaceWindowsLineEndings($dotFile);

self::assertFileEquals(__DIR__.'/data/graphviz-groups.dot', $dotFile);

unlink($dotFile);
Expand Down Expand Up @@ -153,7 +162,10 @@ public function testPointToGroups(): void
],
])))->finish($context, $this->createSymfonyOutput($bufferedOutput), $input);

self::assertSame(sprintf("Script dumped to %s\n", $dotFile), $bufferedOutput->fetch());
self::assertSame(sprintf('Script dumped to %s'.PHP_EOL, Path::normalize($dotFile)), Path::normalize($bufferedOutput->fetch()));

$this->replaceWindowsLineEndings($dotFile);

self::assertFileEquals(__DIR__.'/data/graphviz-groups-point.dot', $dotFile);

unlink($dotFile);
Expand All @@ -166,4 +178,12 @@ private function createSymfonyOutput(BufferedOutput $bufferedOutput): SymfonyOut
new Style(new SymfonyStyle($this->createMock(InputInterface::class), $bufferedOutput))
);
}

private function replaceWindowsLineEndings($expectedFile): void
{
if ("\n" !== PHP_EOL) {
// fix line endings on windows to match with the files in git
file_put_contents($expectedFile, str_replace(PHP_EOL, "\n", file_get_contents($expectedFile)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Style\SymfonyStyle;

use const PHP_EOL;

class TableOutputFormatterTest extends TestCase
{
public function testGetName(): void
Expand Down Expand Up @@ -342,6 +344,6 @@ public function testBasic(array $rules, array $errors, array $warnings, string $
)
);

static::assertSame($expectedOutput, $bufferedOutput->fetch());
static::assertSame(str_replace("\n", PHP_EOL, $expectedOutput), $bufferedOutput->fetch());
}
}

0 comments on commit 9a2b42b

Please sign in to comment.