Skip to content

Commit

Permalink
Merge pull request #120 from openeuropa/run-args
Browse files Browse the repository at this point in the history
Task 'run' should allow arguments and options.
  • Loading branch information
idimopoulos authored May 19, 2020
2 parents f2f10ad + 8b9c3ab commit 73be0c1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ At the moment the following tasks are supported (optional argument default value
| `process-php` | `taskAppendConfiguration()` | `type: append`, `config`, `source`, `destination`, `override` (false) |
| `process-php` | `taskPrependConfiguration()` | `type: prepend`, `config`, `source`, `destination`, `override` (false) |
| `process-php` | `taskWriteConfiguration()` | `type: write`, `config`, `source`, `destination`, `override` (false) |
| `run` | `taskExec()` | `command` (will run `./vendor/bin/run [command]`) |
| `run` | `taskExec()` | `command`, `arguments`, `options` (will run `./vendor/bin/run [command] [argument1] [argument2] ... --[option1]=[value1] --[option2]=[value2] ...`) |

Tasks provided as plain-text strings will be executed as is in the current working directory.

Expand Down
21 changes: 20 additions & 1 deletion src/Tasks/CollectionFactory/CollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,16 @@ protected function taskFactory($task)
]);

case "run":
return $this->taskExec($this->getConfig()->get('runner.bin_dir').'/run')->arg($task['command']);
$taskExec = $this->taskExec($this->getConfig()->get('runner.bin_dir').'/run')
->arg($task['command'])
->interactive($this->isTtySupported());
if (!empty($task['arguments'])) {
$taskExec->args($task['arguments']);
}
if (!empty($task['options'])) {
$taskExec->options($task['options'], '=');
}
return $taskExec;

case "process-php":
$this->secureOption($task, 'override', false);
Expand Down Expand Up @@ -197,4 +206,14 @@ protected function secureOption(array &$task, $name, $default)
{
$task[$name] = isset($task[$name]) ? $task[$name] : $default;
}

/**
* Checks if the TTY mode is supported
*
* @return bool
*/
protected function isTtySupported()
{
return PHP_OS !== 'WINNT' && (bool) @proc_open('echo 1 >/dev/null', [['file', '/dev/tty', 'r'], ['file', '/dev/tty', 'w'], ['file', '/dev/tty', 'w']], $pipes);
}
}
1 change: 1 addition & 0 deletions tests/AbstractTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function setup()
$this->output = new BufferedOutput();
$runner = new TaskRunner(new StringInput(''), $this->output, $this->getClassLoader());
$this->setContainer($runner->getContainer());
$this->getContainer()->get('config')->set('runner.bin_dir', realpath(__DIR__.'/../bin'));
}

/**
Expand Down
23 changes: 22 additions & 1 deletion tests/Tasks/CollectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use OpenEuropa\TaskRunner\Tasks\CollectionFactory\loadTasks;
use OpenEuropa\TaskRunner\Tests\AbstractTaskTest;
use Robo\Config\Config;

/**
* Class CollectionFactoryTest
Expand Down Expand Up @@ -74,6 +73,28 @@ public function testProcessPhpTask($type, $override, $destinationExists, $source
$this->assertEquals(trim($expected), trim(file_get_contents($destinationFile)));
}

/**
* Tests the 'run' task.
*/
public function testRunTask()
{
$filePath = $this->getSandboxFilepath('test-file.txt');

$tasks = [];
$tasks[] = [
'task' => 'run',
'command' => 'custom:test',
'arguments' => [
__METHOD__,
],
'options' => [
'filepath' => $filePath,
],
];
$this->taskCollectionFactory($tasks)->run();
$this->assertSame(__METHOD__, file_get_contents($filePath));
}

/**
* @return array
*/
Expand Down
29 changes: 29 additions & 0 deletions tests/custom/src/TaskRunner/Commands/TestCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace My\Custom\TaskRunner\Commands;

use OpenEuropa\TaskRunner\Commands\AbstractCommands;
use Symfony\Component\Console\Input\InputOption;

/**
* Commands for testing CollectionFactory tasks.
*/
class TestCommands extends AbstractCommands
{
/**
* @command custom:test
*
* @param string $content
* @param array $options
*
* @option filepath
*/
public function customTest(string $content, array $options = [
'filepath' => InputOption::VALUE_REQUIRED,
]): void
{
file_put_contents($options['filepath'], $content);
}
}
Empty file modified tests/sandbox/.gitkeep
100644 → 100755
Empty file.

0 comments on commit 73be0c1

Please sign in to comment.