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

use consistent directory-separator in baseline #700

Merged
merged 18 commits into from
Oct 17, 2021
8 changes: 6 additions & 2 deletions src/AstRunner/AstMap/FileName.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

namespace Qossmic\Deptrac\AstRunner\AstMap;

use Qossmic\Deptrac\File\FileHelper;

final class FileName implements TokenName
{
private string $path;

public function __construct(string $path)
{
$this->path = $path;
$this->path = FileHelper::normalizePath($path);
}

public function toString(): string
{
$wd = getcwd();

staabm marked this conversation as resolved.
Show resolved Hide resolved
return false !== $wd && 0 === strpos($this->path, $wd) ? substr($this->path, strlen($wd)) : $this->path;
$path = false !== $wd && 0 === strpos($this->path, $wd) ? substr($this->path, strlen($wd)) : $this->path;

return FileHelper::normalizePath($path);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I see, the path is already normalized here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't really make a difference, but to compensate your comments I got rid of this ugly ternary and reduced the normalization to the very minimal

}

public function getFilepath(): string
Expand Down
5 changes: 2 additions & 3 deletions src/Collector/DirectoryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use LogicException;
use Qossmic\Deptrac\AstRunner\AstMap;
use Qossmic\Deptrac\File\FileHelper;

class DirectoryCollector extends RegexCollector implements CollectorInterface
{
Expand All @@ -28,9 +29,7 @@ public function satisfy(

$filePath = $fileReference->getFilepath();
$validatedPattern = $this->getValidatedPattern($configuration);

// make paths/patterns cross-OS compatible
$normalizedPath = str_replace('\\', '/', $filePath);
$normalizedPath = FileHelper::normalizePath($filePath);

return 1 === preg_match($validatedPattern, $normalizedPath);
}
Expand Down
8 changes: 8 additions & 0 deletions src/File/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ public function toAbsolutePath(string $path): string

return $this->workingDirectory.'/'.$path;
}

/**
* make paths cross-OS compatible.
*/
public static function normalizePath(string $path): string
{
return str_replace('\\', '/', $path);
}
}
3 changes: 2 additions & 1 deletion src/PathNameFilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Qossmic\Deptrac;

use Qossmic\Deptrac\File\FileHelper;
use SplFileInfo;
use Symfony\Component\Finder\Iterator\PathFilterIterator;

Expand All @@ -16,7 +17,7 @@ public function accept(): bool
$filename = $fileInfo->getPathname();

if ('\\' === \DIRECTORY_SEPARATOR) {
$filename = str_replace('\\', '/', $filename);
$filename = FileHelper::normalizePath($filename);
}

return $this->isAccepted($filename);
Expand Down
22 changes: 22 additions & 0 deletions tests/AstRunner/AstMap/FileNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Tests\Qossmic\Deptrac\AstRunner\AstMap;

use PHPUnit\Framework\TestCase;
use Qossmic\Deptrac\AstRunner\AstMap;

final class FileNameTest extends TestCase
{
public function testPathNormalization(): void
{
$fileName = new AstMap\FileName('/path/to/file.php');
$this->assertSame('/path/to/file.php', $fileName->getFilepath());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if we could mock getcwd method call here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? its not necessary for the current test.

it would only be necessary, in case you want to test the already existing functionality which was not changed by this PR

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it would help with test coverage. That is what I meant by "nice". Also, it makes the test fragile. (not really on GitHub but locally for sure). If I were to run the test in the path directory, it would fail mysteriously. It is a tiny thing, but it is IMHO good practice to mock out system dependencies, like the current working directory.

$this->assertSame('/path/to/file.php', $fileName->toString());

$fileName = new AstMap\FileName('\\path\\to\\file.php');
$this->assertSame('/path/to/file.php', $fileName->getFilepath());
$this->assertSame('/path/to/file.php', $fileName->toString());
}
}