Skip to content

Commit

Permalink
return file directory in Windows format if running under Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
deminy committed Dec 20, 2018
1 parent 6fce491 commit 9583841
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/CrowdStar/SVNAgent/Actions/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use CrowdStar\SVNAgent\SVNHelper;
use CrowdStar\SVNAgent\Traits\LoggerTrait;
use CrowdStar\SVNAgent\Traits\PathTrait;
use CrowdStar\SVNAgent\WindowsCompatibleInterface;
use MrRio\ShellWrap;
use MrRio\ShellWrapException;
use NinjaMutex\Lock\FlockLock;
Expand Down Expand Up @@ -253,7 +254,12 @@ public function getSvnUri(): string
*/
public function getSvnDir(): string
{
return $this->getConfig()->getSvnRootDir() . $this->getPath();
$svnDir = $this->getConfig()->getSvnRootDir() . $this->getPath();
if (($this instanceof WindowsCompatibleInterface) && $this->getConfig()->onWindows()) {
return PathHelper::toWindowsPath($svnDir);
}

return $svnDir;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/CrowdStar/SVNAgent/PathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ public static function normalizePath(string $path): string
return DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR;
}

/**
* Convert a Linux path from a Windows subsystem for Linux to the host path on Windows. Here we have an assumption
* that the path is under folder C:\Users on Windows.
*
* @param string $path
* @return string
* @throws Exception
*/
public static function toWindowsPath(string $path): string
{
if (stripos($path, '/Users/') === false) {
throw new Exception('only paths under folder C:\Users on Windows can be converted');
}

return str_replace('/', '\\', ucfirst(preg_replace('/^(.+)\/(\w)\/(Users)\/(.*)$/i', '$2:\\\$3\\\$4', $path)));
}

/**
* @param string $path
* @return string
Expand Down
31 changes: 31 additions & 0 deletions tests/CrowdStar/SVNAgent/PathHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@
*/
class PathHelperTest extends TestCase
{
/**
* @return array
*/
public function dataToWindowsPath(): array
{
return [
[
'C:\Users\james\path\to\a\folder',
'/mnt/c/Users/james/path/to/a/folder',
'convert a Linux path under WSL to the host path on Windows',
],
[
'C:\users\james\path\to\a\folder',
'/mnt/c/users/james/path/to/a/folder',
'convert a Linux path under WSL to the host path on Windows, where the Linux path is in lowercase',
],
];
}

/**
* @dataProvider dataToWindowsPath
* @covers PathHelper::toWindowsPath()
* @param string $expected
* @param string $path
* @param string $message
*/
public function testToWindowsPath(string $expected, string $path, string $message)
{
$this->assertSame($expected, PathHelper::toWindowsPath($path), $message);
}

/**
* @return array
*/
Expand Down

0 comments on commit 9583841

Please sign in to comment.