Skip to content

Commit

Permalink
Normalize paths to run on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
srsbiz committed Jan 31, 2024
1 parent ca7f5f3 commit e20a0c1
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 25 deletions.
3 changes: 3 additions & 0 deletions lib/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\RST\Meta\Metas;
use InvalidArgumentException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Finder\Finder;

use function file_exists;
Expand Down Expand Up @@ -132,6 +133,8 @@ public function build(
string $directory,
string $targetDirectory = 'output'
): void {
$directory = Path::normalize($directory);
$targetDirectory = Path::normalize($targetDirectory);
// Creating output directory if doesn't exists
if (! is_dir($targetDirectory)) {
$this->filesystem->mkdir($targetDirectory, 0755);
Expand Down
5 changes: 3 additions & 2 deletions lib/Builder/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\RST\Meta\Metas;
use InvalidArgumentException;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

Expand Down Expand Up @@ -54,7 +55,7 @@ public function scan(): ParseQueue
// completely populate the splFileInfos property
$this->fileInfos = [];
foreach ($this->finder as $fileInfo) {
$relativeFilename = $fileInfo->getRelativePathname();
$relativeFilename = Path::normalize($fileInfo->getRelativePathname());
// strip off the extension
$documentPath = substr($relativeFilename, 0, -(strlen($this->fileExtension) + 1));

Expand Down Expand Up @@ -142,6 +143,6 @@ private function hasFileBeenUpdated(string $filename): bool
*/
private function getFilenameFromFile(SplFileInfo $file): string
{
return substr($file->getRelativePathname(), 0, -(strlen($this->fileExtension) + 1));
return substr(Path::normalize($file->getRelativePathname()), 0, -(strlen($this->fileExtension) + 1));
}
}
8 changes: 5 additions & 3 deletions lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
use Doctrine\RST\Templates\TwigAdapter;
use Doctrine\RST\Templates\TwigTemplateRenderer;
use RuntimeException;
use Symfony\Component\Filesystem\Path;
use Twig\Environment as TwigEnvironment;

use function array_map;
use function sprintf;
use function sys_get_temp_dir;

Expand Down Expand Up @@ -93,7 +95,7 @@ class Configuration

public function __construct()
{
$this->cacheDir = sys_get_temp_dir() . '/doctrine-rst-parser';
$this->cacheDir = Path::normalize(sys_get_temp_dir()) . '/doctrine-rst-parser';

$this->eventManager = new EventManager();

Expand All @@ -113,7 +115,7 @@ public function getCacheDir(): string

public function setCacheDir(string $cacheDir): void
{
$this->cacheDir = $cacheDir;
$this->cacheDir = Path::normalize($cacheDir);
}

public function getTemplateRenderer(): TemplateRenderer
Expand Down Expand Up @@ -141,7 +143,7 @@ public function getCustomTemplateDirs(): array
/** @param string[] $customTemplateDirs */
public function setCustomTemplateDirs(array $customTemplateDirs): void
{
$this->customTemplateDirs = $customTemplateDirs;
$this->customTemplateDirs = array_map([Path::class, 'normalize'], $customTemplateDirs);
}

public function addCustomTemplateDir(string $customTemplateDir): void
Expand Down
7 changes: 4 additions & 3 deletions lib/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\RST\References\ResolvedReference;
use Doctrine\RST\Templates\TemplateRenderer;
use InvalidArgumentException;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\String\Slugger\AsciiSlugger;

use function array_shift;
Expand Down Expand Up @@ -372,7 +373,7 @@ public function getDirName(): string

public function setCurrentFileName(string $filename): void
{
$this->currentFileName = $filename;
$this->currentFileName = Path::normalize($filename);
}

/**
Expand All @@ -388,7 +389,7 @@ public function getCurrentFileName(): string

public function setCurrentDirectory(string $directory): void
{
$this->currentDirectory = $directory;
$this->currentDirectory = Path::normalize($directory);
}

public function getCurrentDirectory(): string
Expand All @@ -403,7 +404,7 @@ public function absoluteRelativePath(string $url): string

public function setTargetDirectory(string $directory): void
{
$this->targetDirectory = $directory;
$this->targetDirectory = Path::normalize($directory);
}

public function getTargetDirectory(): string
Expand Down
6 changes: 5 additions & 1 deletion lib/FileIncluder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\RST;

use RuntimeException;
use Symfony\Component\Filesystem\Path;

use function explode;
use function file_exists;
Expand All @@ -26,6 +27,7 @@ final class FileIncluder
/** @var string */
private $includeRoot;

/** @param string $includeRoot Many roots can be provided by separating them with | */
public function __construct(
Environment $environment,
bool $includeAllowed,
Expand Down Expand Up @@ -83,7 +85,9 @@ private function isFileIncludeAllowed(string $path): bool
return false;
}

foreach (explode(':', $this->includeRoot) as $root) {
$real = Path::normalize($real);

foreach (explode('|', $this->includeRoot) as $root) {
if (strpos($real, $root) === 0) {
return true;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/Nodes/TocNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace Doctrine\RST\Nodes;

use Doctrine\RST\Environment;
use Symfony\Component\Filesystem\Path;

use function array_map;

class TocNode extends Node
{
Expand All @@ -27,7 +30,7 @@ public function __construct(Environment $environment, array $files, array $optio
{
parent::__construct();

$this->files = $files;
$this->files = array_map([Path::class, 'normalize'], $files);
$this->environment = $environment;
$this->options = $options;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Templates/TwigEnvironmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\RST\Templates;

use Doctrine\RST\Configuration;
use Symfony\Component\Filesystem\Path;
use Twig\Environment as TwigEnvironment;
use Twig\Loader\FilesystemLoader;

Expand Down Expand Up @@ -32,7 +33,7 @@ private static function getTemplateDirs(Configuration $configuration): array

$templateDirectories = $configuration->getCustomTemplateDirs();
// add the fallback directory
$templateDirectories[] = __DIR__;
$templateDirectories[] = Path::normalize(__DIR__);

foreach ($templateDirectories as $templateDir) {
$themePath = $templateDir . '/' . $configuration->getTheme() . '/' . $fileExtension;
Expand Down
7 changes: 4 additions & 3 deletions lib/Toc/GlobSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\RST\Toc;

use Doctrine\RST\Environment;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Finder\Finder;

use function array_merge;
Expand All @@ -27,7 +28,7 @@ class GlobSearcher
*/
public function globSearch(Environment $environment, string $globPattern): array
{
$currentFilePath = (string) realpath(rtrim($environment->absoluteRelativePath(''), '/'));
$currentFilePath = Path::normalize((string) realpath(rtrim($environment->absoluteRelativePath(''), '/')));

$rootDocPath = rtrim(str_replace($environment->getDirName(), '', $currentFilePath), '/');

Expand All @@ -51,7 +52,7 @@ public function globSearch(Environment $environment, string $globPattern): array
foreach ($finder as $file) {
if ($file->isDir()) {
// remove the root directory so it is a relative path from the root
$relativePath = str_replace($rootDocPath, '', (string) $file->getRealPath());
$relativePath = str_replace($rootDocPath, '', Path::normalize((string) $file->getRealPath()));

// recursively search in this directory
$dirFiles = $this->globSearch($environment, $relativePath . '/*');
Expand All @@ -60,7 +61,7 @@ public function globSearch(Environment $environment, string $globPattern): array
} else {
// Trim the root path and the .rst extension. This is what the
// RST parser requires to add a dependency.
$file = str_replace([$rootDocPath, '.rst'], '', (string) $file->getRealPath());
$file = str_replace([$rootDocPath, '.rst'], '', Path::normalize((string) $file->getRealPath()));

$allFiles[] = $file;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/BaseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\RST\Builder;
use Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Path;

use function file_get_contents;
use function shell_exec;
Expand Down Expand Up @@ -34,12 +35,12 @@ protected function configureBuilder(Builder $builder): void

protected function sourceFile(string $file = ''): string
{
return __DIR__ . '/' . $this->getFixturesDirectory() . '/input/' . $file;
return Path::normalize(__DIR__) . '/' . $this->getFixturesDirectory() . '/input/' . $file;
}

protected function targetFile(string $file = ''): string
{
return __DIR__ . '/' . $this->getFixturesDirectory() . '/output/' . $file;
return Path::normalize(__DIR__) . '/' . $this->getFixturesDirectory() . '/output/' . $file;
}

/** @throws Exception */
Expand Down
7 changes: 4 additions & 3 deletions tests/FileIncluderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\Filesystem\Path;

use function trim;

Expand All @@ -22,9 +23,9 @@ public function testInclude(): void
$this->environment->expects(self::once())
->method('absoluteRelativePath')
->with('include.rst')
->willReturn(__DIR__ . '/Parser/files/include.rst');
->willReturn(Path::normalize(__DIR__) . '/Parser/files/include.rst');

$fileIncluder = new FileIncluder($this->environment, true, __DIR__ . '/Parser/files');
$fileIncluder = new FileIncluder($this->environment, true, Path::normalize(__DIR__) . '/Parser/files');

$contents = $fileIncluder->includeFiles('.. include:: include.rst');

Expand All @@ -36,7 +37,7 @@ public function testIncludeWithEmptyIncludeRoot(): void
$this->environment->expects(self::once())
->method('absoluteRelativePath')
->with('include.rst')
->willReturn(__DIR__ . '/Parser/files/include.rst');
->willReturn(Path::normalize(__DIR__) . '/Parser/files/include.rst');

$fileIncluder = new FileIncluder($this->environment, true, '');

Expand Down
3 changes: 2 additions & 1 deletion tests/GlobSearcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\RST\Environment;
use Doctrine\RST\Toc\GlobSearcher;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Path;

use function sort;

Expand All @@ -17,7 +18,7 @@ class GlobSearcherTest extends TestCase

public function testGlobSearch(): void
{
$dir = __DIR__ . '/BuilderToctree/input';
$dir = Path::normalize(__DIR__) . '/BuilderToctree/input';

$environment = $this->createMock(Environment::class);

Expand Down
5 changes: 3 additions & 2 deletions tests/Parser/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\Filesystem\Path;

use function assert;
use function count;
Expand Down Expand Up @@ -383,7 +384,7 @@ public function testIncludesKeepScope(): void

public function testIncludesPolicy(): void
{
$directory = __DIR__ . '/files/';
$directory = Path::normalize(__DIR__) . '/files/';
$parser = new Parser();
$environment = $parser->getEnvironment();
$environment->setCurrentDirectory($directory);
Expand All @@ -397,7 +398,7 @@ public function testIncludesPolicy(): void
self::assertStringContainsString('SUBDIRECTORY OK', $document);
self::assertStringContainsString('EXTERNAL FILE INCLUDED!', $document);

// Disbaled policy:
// Disabled policy:
$parser->setIncludePolicy(false);
$nodes = $parser->parseFile($directory . 'inclusion-policy.rst')->getNodes();
self::assertCount(1, $nodes);
Expand Down
7 changes: 4 additions & 3 deletions tests/Templates/TwigEnvironmentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

Expand All @@ -29,7 +30,7 @@ public function testTemplateDirectoriesNothingCustom(): void

// no theme, no custom dirs
self::assertLoaderPaths(
[(string) realpath(__DIR__ . '/../../lib/Templates/default/html')],
[Path::normalize((string) realpath(__DIR__ . '/../../lib/Templates/default/html'))],
TwigEnvironmentFactory::createTwigEnvironment($configuration)
);
}
Expand Down Expand Up @@ -61,7 +62,7 @@ public function testTemplateDirectoriesThemeAndDirectories(): void
$dir1 . '/cool_theme/html',
$dir2 . '/cool_theme/html',
$dir1 . '/default/html',
(string) realpath(__DIR__ . '/../../lib/Templates/default/html'),
Path::normalize((string) realpath(__DIR__ . '/../../lib/Templates/default/html')),
],
TwigEnvironmentFactory::createTwigEnvironment($configuration)
);
Expand All @@ -80,7 +81,7 @@ private static function assertLoaderPaths(array $expectedPaths, Environment $twi

protected function setUp(): void
{
$this->tmpPath = sys_get_temp_dir() . '/_rst_twig_tests';
$this->tmpPath = Path::normalize(sys_get_temp_dir()) . '/_rst_twig_tests';
$this->filesystem = new Filesystem();
}

Expand Down

0 comments on commit e20a0c1

Please sign in to comment.