Skip to content

Commit

Permalink
Merge 6fbf4d6 into ebaff4e
Browse files Browse the repository at this point in the history
  • Loading branch information
bsacharski committed Oct 2, 2020
2 parents ebaff4e + 6fbf4d6 commit d5048c5
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@
- ISPManager recipe and docs.
- Symfony 5 recipe.
- Command for checking if a deploy is unlocked. [#2150] [#2150]
- Added `escape` and `unescape` method to ignore certain values from being parsing. [#2176]

### Fixed
- Normalize CRLF to LF new line endings. [#2111]
Expand Down Expand Up @@ -584,6 +585,7 @@
- Fixed remove of shared dir on first deploy.


[#2176]: https://github.com/deployphp/deployer/issues/2176
[#2170]: https://github.com/deployphp/deployer/issues/2170
[#2165]: https://github.com/deployphp/deployer/issues/2165
[#2150]: https://github.com/deployphp/deployer/issues/2150
Expand Down
4 changes: 3 additions & 1 deletion src/Configuration/Configuration.php
Expand Up @@ -13,6 +13,7 @@
use function Deployer\Support\array_merge_alternate;
use function Deployer\Support\is_closure;
use function Deployer\Support\normalize_line_endings;
use function Deployer\unescape;

class Configuration implements \ArrayAccess
{
Expand Down Expand Up @@ -107,7 +108,8 @@ public function parse($value)
{
if (is_string($value)) {
$normalizedValue = normalize_line_endings($value);
return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*\}\}/', [$this, 'parseCallback'], $normalizedValue);
$parsed = preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*\}\}/', [$this, 'parseCallback'], $normalizedValue);
return unescape($parsed);
}

return $value;
Expand Down
24 changes: 24 additions & 0 deletions src/functions.php
Expand Up @@ -549,6 +549,30 @@ function parse($value)
return Context::get()->getConfig()->parse($value);
}

/**
* Escapes value so it won't be parsed by @see parse()
*
* @param string $value value to be escaped
* @return string escaped value
*/
function escape($value)
{
$output = str_replace(['{{', '}}'], ['\\{\\{', '\\}\\}'], $value);
return $output;
}

/**
* Removes escape characters from the string that was processed with @see escape()
*
* @param string $value that has been previous escaped
* @return string value with escape characters removed
*/
function unescape($value)
{
$output = str_replace(['\\{\\{', '\\}\\}'], ['{{', '}}'], $value);
return $output;
}

/**
* Setup configuration option.
*
Expand Down
26 changes: 26 additions & 0 deletions test/src/Configuration/ConfigurationTest.php
Expand Up @@ -16,6 +16,32 @@ public function testParse()
self::assertEquals('a b', $config->parse('{{foo}} {{bar}}'));
}

public function parseWithRawDataProvider()
{
return [
'string with only raw value' => [ '\\{\\{foo\\}\\}', '{{foo}}', [] ],
'string with raw and parsed value' => ['\\{\\{foo\\}\\} {{foo}}', '{{foo}} bar', [ 'foo' => 'bar' ] ],
'string with multiple raw values' => ['\\{\\{foo\\}\\} {{foo}} \\{\\{baz\\}\\}', '{{foo}} bar {{baz}}', [ 'foo' => 'bar' ] ],
];
}

/**
* @dataProvider parseWithRawDataProvider
* @param string $input
* @param string $expected
* @param array $configValues
*/
public function testParseWithRaw($input, $expected, $configValues)
{
$config = new Configuration();
foreach ($configValues as $key => $value) {
$config->set($key, $value);
}

$output = $config->parse($input);
self::assertEquals($expected, $output);
}

public function testUnset()
{
$config = new Configuration();
Expand Down
44 changes: 44 additions & 0 deletions test/src/FunctionsTest.php
Expand Up @@ -135,6 +135,50 @@ public function testRunLocallyWithOptions()
self::assertEquals('overwritten', $output);
}

public function escapeValueDataProvider()
{
return [
'escape whole string' => [ '{{foo}}', '\\{\\{foo\\}\\}' ],
'escape part of string' => [ 'foo {{bar}}', 'foo \\{\\{bar\\}\\}' ],
'escape multiple times' => [ '{{foo}} {{bar}}', '\\{\\{foo\\}\\} \\{\\{bar\\}\\}' ],
'do nothing' => [ 'foo', 'foo' ],
];
}

public function unescapeValueDataProvider()
{
return [
'unescape whole string' => [ '\\{\\{foo\\}\\}', '{{foo}}' ],
'unescape part of string' => [ 'foo \\{\\{bar\\}\\}', 'foo {{bar}}' ],
'unescape multiple times' => [ '\\{\\{foo\\}\\} \\{\\{bar\\}\\}', '{{foo}} {{bar}}' ],
'do nothing' => [ 'foo', 'foo' ],
];
}

/**
* @dataProvider escapeValueDataProvider
*
* @param string $input
* @param string $expected
*/
public function testEscapeValue($input, $expected)
{
$output = escape($input);
self::assertEquals($expected, $output);
}

/**
* @dataProvider unescapeValueDataProvider
*
* @param string $input
* @param string $expected
*/
public function testUnescapeValue($input, $expected)
{
$output = unescape($input);
self::assertEquals($expected, $output);
}

private function taskToNames($tasks)
{
return array_map(function (Task $task) {
Expand Down

0 comments on commit d5048c5

Please sign in to comment.