-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix wildcard matching for environment IDs that contain a / character
Affected commands: `environment:delete` and `user:add`
- Loading branch information
1 parent
c7a0a34
commit 230e37f
Showing
4 changed files
with
62 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Platformsh\Cli\Util; | ||
|
||
class Wildcard | ||
{ | ||
/** | ||
* Selects strings in a list matching a list of wildcards. | ||
* | ||
* @param string[] $subjects | ||
* @param string[] $wildcards | ||
* | ||
* @return string[] | ||
*/ | ||
public static function select(array $subjects, $wildcards) | ||
{ | ||
$found = []; | ||
foreach ($wildcards as $wildcard) { | ||
$pattern = '/^' . \str_replace('%', '.*', \preg_quote($wildcard, '/')) . '$/'; | ||
$found = \array_merge($found, \preg_grep($pattern, $subjects)); | ||
} | ||
return \array_unique($found); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Platformsh\Cli\Tests\Util; | ||
|
||
use Platformsh\Cli\Util\Wildcard; | ||
|
||
class WildcardTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testSelect() | ||
{ | ||
$cases = [ | ||
[['a', 'b', 'c'], ['a'], ['a']], | ||
[['foo', 'bar', 'baz'], ['foo'], ['foo']], | ||
[['foo', 'bar', 'baz', 'bazz', 'boz'], ['ba%'], ['bar', 'baz', 'bazz']], | ||
[ | ||
['feature/apple', 'feature/pear', 'feature/banana', 'feature/blueberry'], | ||
['feature/banana'], | ||
['feature/banana'], | ||
], | ||
[ | ||
['feature/apple', 'feature/pear', 'feature/banana', 'feature/blueberry'], | ||
['f%b%y', 'f%/pear', 'hotfix/%'], | ||
['feature/blueberry', 'feature/pear'], | ||
], | ||
]; | ||
foreach ($cases as $i => $case) { | ||
list($subjects, $wildcards, $result) = $case; | ||
$this->assertEquals($result, Wildcard::select($subjects, $wildcards), "Case $i"); | ||
} | ||
} | ||
} |