Skip to content

Commit

Permalink
Merge 85382bb into 5808b2d
Browse files Browse the repository at this point in the history
  • Loading branch information
bsacharski committed Oct 3, 2020
2 parents 5808b2d + 85382bb commit 0a64e78
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/functions.php
Expand Up @@ -262,13 +262,14 @@ function cd($path)
*
* @param string $path
* @param callable $callback
* @return mixed|void Return value of the $callback function or void if callback doesn't return anything
*/
function within($path, $callback)
{
$lastWorkingPath = get('working_path', '');
try {
set('working_path', parse($path));
$callback();
return $callback();
} finally {
set('working_path', $lastWorkingPath);
}
Expand Down
47 changes: 47 additions & 0 deletions test/src/FunctionsTest.php
Expand Up @@ -135,6 +135,53 @@ public function testRunLocallyWithOptions()
self::assertEquals('overwritten', $output);
}

public function testWithinSetsWorkingPaths()
{
Context::get()->getConfig()->set('working_path', '/foo');

within('/bar', function () {
$withinWorkingPath = Context::get()->getConfig()->get('working_path');
self::assertEquals('/bar', $withinWorkingPath);
});

$originalWorkingPath = Context::get()->getConfig()->get('working_path');
self::assertEquals('/foo', $originalWorkingPath);
}

public function testWithinRestoresWorkingPathInCaseOfException()
{
Context::get()->getConfig()->set('working_path', '/foo');

try {
within('/bar', function () {
throw new \Exception('Dummy exception');
});
} catch (\Exception $exception) {
// noop
}

$originalWorkingPath = Context::get()->getConfig()->get('working_path');
self::assertEquals('/foo', $originalWorkingPath);
}

public function testWithinReturningValue()
{
$output = within('/foo', function () {
return 'bar';
});

self::assertEquals('bar', $output);
}

public function testWithinWithVoidFunction()
{
$output = within('/foo', function () {
// noop
});

self::assertNull($output);
}

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

0 comments on commit 0a64e78

Please sign in to comment.