Skip to content

Commit

Permalink
Merge pull request #1030 from spiral/bugfix/normalize-path
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Dec 15, 2023
1 parent 28bf102 commit a7df4c0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
8 changes: 7 additions & 1 deletion src/Files.php
Expand Up @@ -313,10 +313,16 @@ public function tempFilename(string $extension = '', string $location = null): s

public function normalizePath(string $path, bool $asDirectory = false): string
{
$isUnc = \str_starts_with($path, '\\\\') || \str_starts_with($path, '//');
if ($isUnc) {
$leadingSlashes = \substr($path, 0, 2);
$path = \substr($path, 2);
}

$path = \str_replace(['//', '\\'], '/', $path);

//Potentially open links and ../ type directories?
return \rtrim($path, '/') . ($asDirectory ? '/' : '');
return ($isUnc ? $leadingSlashes : '') . \rtrim($path, '/') . ($asDirectory ? '/' : '');
}

/**
Expand Down
39 changes: 24 additions & 15 deletions tests/ConversionTest.php
Expand Up @@ -6,51 +6,60 @@

use Spiral\Files\Files;

class ConversionTest extends TestCase
final class ConversionTest extends TestCase
{
public function testNormalizeFilePath(): void
private Files $files;

protected function setUp(): void
{
$files = new Files();
$this->files = new Files();
}

$this->assertSame('/abc/file.name', $files->normalizePath('/abc\\file.name'));
$this->assertSame('/abc/file.name', $files->normalizePath('\\abc//file.name'));
public function testNormalizeFilePath(): void
{
$this->assertSame('/abc/file.name', $this->files->normalizePath('/abc\\file.name'));
$this->assertSame('/abc/file.name', $this->files->normalizePath('\\abc//file.name'));
}

public function testNormalizeDirectoryPath(): void
{
$files = new Files();
$this->assertSame('/abc/dir/', $this->files->normalizePath('\\abc/dir', true));
$this->assertSame('/abc/dir/', $this->files->normalizePath('\\abc//dir', true));
}

$this->assertSame('/abc/dir/', $files->normalizePath('\\abc/dir', true));
$this->assertSame('/abc/dir/', $files->normalizePath('\\abc//dir', true));
public function testNormalizeUniversalNamingConventionPath(): void
{
$this->assertSame('//host/path/resource', $this->files->normalizePath('//host/path/resource'));
$this->assertSame('//host/path/resource', $this->files->normalizePath('//host/path//resource'));
$this->assertSame('\\\\host/path/resource', $this->files->normalizePath('\\\\host/path/resource'));
$this->assertSame('\\\\host/path/resource', $this->files->normalizePath('\\\\host/path//resource'));
}

public function testRelativePath(): void
{
$files = new Files();

$this->assertSame(
'some-filename.txt',
$files->relativePath('/abc/some-filename.txt', '/abc')
$this->files->relativePath('/abc/some-filename.txt', '/abc')
);

$this->assertSame(
'../some-filename.txt',
$files->relativePath('/abc/../some-filename.txt', '/abc')
$this->files->relativePath('/abc/../some-filename.txt', '/abc')
);

$this->assertSame(
'../../some-filename.txt',
$files->relativePath('/abc/../../some-filename.txt', '/abc')
$this->files->relativePath('/abc/../../some-filename.txt', '/abc')
);

$this->assertSame(
'./some-filename.txt',
$files->relativePath('/abc/some-filename.txt', '/abc/..')
$this->files->relativePath('/abc/some-filename.txt', '/abc/..')
);

$this->assertSame(
'../some-filename.txt',
$files->relativePath('/abc/some-filename.txt', '/abc/../..')
$this->files->relativePath('/abc/some-filename.txt', '/abc/../..')
);
}
}

0 comments on commit a7df4c0

Please sign in to comment.