1
0
Fork 0

Added tests for pathToUri and findFilesRecursive

pull/31/head
Stephan Unverwerth 2016-09-19 23:57:29 +02:00
parent 6b28cb6a93
commit ca0fcced20
6 changed files with 92 additions and 30 deletions

1
fixtures/recursive/a.txt Normal file
View File

@ -0,0 +1 @@
A

View File

@ -0,0 +1 @@
B

View File

@ -0,0 +1 @@
Peeakboo!

View File

@ -27,5 +27,7 @@ function findFilesRecursive(string $path, string $pattern): array {
* @return string
*/
function pathToUri(string $filepath): string {
return 'file://'.($filepath[0] == '/' || $filepath[0] == '\\' ? '' : '/').str_replace('\\', '/', $filepath);
$filepath = trim(str_replace('\\', '/', $filepath), '/');
$filepath = implode('/', array_map('urlencode', explode('/', $filepath)));
return 'file:///'.$filepath;
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types = 1);
namespace LanguageServer\Tests\Utils;
use PHPUnit\Framework\TestCase;
class FileUriTest extends TestCase
{
public function testSpecialCharsAreEscaped()
{
$uri = \LanguageServer\pathToUri('c:/path/to/file/dürüm döner.php');
$this->assertEquals('file:///c%3A/path/to/file/d%C3%BCr%C3%BCm+d%C3%B6ner.php', $uri);
}
public function testUriIsWellFormed()
{
$uri = \LanguageServer\pathToUri('var/log');
$this->assertEquals('file:///var/log', $uri);
$uri = \LanguageServer\pathToUri('/usr/local/bin');
$this->assertEquals('file:///usr/local/bin', $uri);
$uri = \LanguageServer\pathToUri('a/b/c/');
$this->assertEquals('file:///a/b/c', $uri);
$uri = \LanguageServer\pathToUri('/d/e/f');
$this->assertEquals('file:///d/e/f', $uri);
}
public function testBackslashesAreTransformed()
{
$uri = \LanguageServer\pathToUri('c:\\foo\\bar.baz');
$this->assertEquals('file:///c%3A/foo/bar.baz', $uri);
}
}

View File

@ -0,0 +1,21 @@
<?php
declare(strict_types = 1);
namespace LanguageServer\Tests\Utils;
use PHPUnit\Framework\TestCase;
class RecursiveFileSearchTest extends TestCase
{
public function testFilesAreFound()
{
$path = realpath(__DIR__ . '/../../fixtures/recursive');
$files = \LanguageServer\findFilesRecursive($path, '/.+\.txt/');
$this->assertEquals([
$path . DIRECTORY_SEPARATOR . 'a.txt',
$path . DIRECTORY_SEPARATOR . 'search' . DIRECTORY_SEPARATOR . 'b.txt',
$path . DIRECTORY_SEPARATOR . 'search' . DIRECTORY_SEPARATOR . 'here' . DIRECTORY_SEPARATOR . 'c.txt',
], $files);
}
}