Skip to content

Commit

Permalink
feat(core): Path::isNormalized().
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed May 17, 2024
1 parent ba5b3d7 commit d6ded7f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/Utils/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public static function isAbsolute(string $path): bool {
return SymfonyPath::isAbsolute(static::normalize($path));
}

public static function isNormalized(string $path): bool {
return static::normalize($path) === $path;
}

public static function normalize(string $path): string {
return SymfonyPath::canonicalize($path);
}
Expand Down
25 changes: 25 additions & 0 deletions packages/core/src/Utils/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,29 @@ public function testGetRelativePath(): void {
self::assertEquals('../to/file', Path::getRelativePath('/any/path', '/any/path/../to/file'));
self::assertEquals('to/file', Path::getRelativePath('/absolute/path', 'to/./file'));
}

public function testJoin(): void {
self::assertEquals('/any/path', Path::join('/any/path'));
self::assertEquals('/any/path', Path::join('/any', 'path'));
self::assertEquals('/path', Path::join('/any', '..', 'path'));
self::assertEquals('any/path', Path::join('.', 'any', '.', 'path'));
self::assertEquals('../any/path', Path::join('..', 'any', '.', 'path'));
}

public function testNormalize(): void {
self::assertEquals('/any/path', Path::normalize('/any/path'));
self::assertEquals('any/path', Path::normalize('any/path'));
self::assertEquals('any/path', Path::normalize('./any/path'));
self::assertEquals('any/path', Path::normalize('././any/path'));
self::assertEquals('../any/path', Path::normalize('./../any/path'));
self::assertEquals('path', Path::normalize('./any/../path'));
}

public function testIsNormalized(): void {
self::assertTrue(Path::isNormalized('/any/path'));
self::assertTrue(Path::isNormalized('any/path'));
self::assertFalse(Path::isNormalized('./any/path'));
self::assertFalse(Path::isNormalized('././any/path'));
self::assertFalse(Path::isNormalized('./../any/path'));
}
}

0 comments on commit d6ded7f

Please sign in to comment.