Skip to content

Commit

Permalink
Merge pull request #10 from supportpal/windows-whitespace-escape
Browse files Browse the repository at this point in the history
Fix windows unrecognized command on directory with empty space
  • Loading branch information
sebastianfeldmann authored Jun 2, 2021
2 parents 490a6ba + 920fc5a commit 29e8bbd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Command/Executable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace SebastianFeldmann\Cli\Command;

use SebastianFeldmann\Cli\Command;
use SebastianFeldmann\Cli\Util;

/**
* Class Executable
Expand Down Expand Up @@ -59,7 +60,7 @@ class Executable implements Command
*/
public function __construct(string $cmd, array $exitCodes = [0])
{
$this->cmd = $cmd;
$this->cmd = ! defined('PHP_WINDOWS_VERSION_BUILD') ? $cmd : Util::escapeSpacesOnWindows($cmd);
$this->acceptableExitCodes = $exitCodes;
}

Expand Down
11 changes: 11 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,15 @@ public static function removeDir(string $dir)
}
rmdir($dir);
}

/**
* Escapes 'unescaped' space sequences on Windows.
* i.e: 'E:/Program Files' escaped to 'E:/Program^ Files'
* @param string $cmd
* @return string
*/
public static function escapeSpacesOnWindows(string $cmd): string
{
return preg_replace('/(?<!\^)( )/', '^ ', $cmd);
}
}
22 changes: 22 additions & 0 deletions tests/cli/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,28 @@ public function testRemoveDir()
$this->assertFileDoesNotExist($dirToDelete);
}

/**
* Tests Util::escapeSpacesOnWindows
* @dataProvider providerWindowsPathsCases
*/
public function testEscapeSpacesOnWindows(string $expected, string $cmd): void
{
self::assertSame($expected, Util::escapeSpacesOnWindows($cmd));
}

/**
* @return string[][]
*/
public function providerWindowsPathsCases(): array
{
$escapedCommand = 'E:/Program^ Files/';
$unEscapedCommand = 'E:/Program Files/';
return [
'test already escaped sequence' => [$escapedCommand, $escapedCommand],
'test unescaped sequence' => [$escapedCommand, $unEscapedCommand],
];
}

/**
* Create some temp command
*
Expand Down

0 comments on commit 29e8bbd

Please sign in to comment.