Skip to content

Commit

Permalink
#11272 Throw an exception early when invalid task is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
burzum committed Oct 6, 2017
1 parent b3dd206 commit 65757db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
15 changes: 14 additions & 1 deletion src/Console/Shell.php
Expand Up @@ -16,7 +16,7 @@

use Cake\Console\Exception\ConsoleException;
use Cake\Console\Exception\StopException;
use Cake\Core\Configure;
use Cake\Core\App;
use Cake\Core\Plugin;
use Cake\Datasource\ModelAwareTrait;
use Cake\Filesystem\File;
Expand All @@ -27,6 +27,7 @@
use Cake\Utility\Text;
use ReflectionException;
use ReflectionMethod;
use RuntimeException;

/**
* Base class for command-line utilities for automating programmer chores.
Expand Down Expand Up @@ -197,6 +198,8 @@ public function __construct(ConsoleIo $io = null)
if (isset($this->modelClass)) {
$this->loadModel();
}

$this->loadTasks();
}

/**
Expand Down Expand Up @@ -301,6 +304,16 @@ public function loadTasks()
$this->_taskMap = $this->Tasks->normalizeArray((array)$this->tasks);
$this->taskNames = array_merge($this->taskNames, array_keys($this->_taskMap));

foreach ($this->_taskMap as $taskName => $task) {
$class = App::className($task['class'], 'Shell/Task', 'Task');
if (!class_exists($class)) {
throw new RuntimeException(sprintf(
'Task `%s` not found. Maybe you made a typo or a plugin is missing or not loaded?',
$taskName
));
}
}

return true;
}

Expand Down
23 changes: 18 additions & 5 deletions tests/TestCase/Console/ShellTest.php
Expand Up @@ -28,7 +28,7 @@
class MergeShell extends Shell
{

public $tasks = ['DbConfig', 'Fixture'];
public $tasks = ['Command', 'Extract'];

public $modelClass = 'Articles';
}
Expand Down Expand Up @@ -170,7 +170,7 @@ public function testInitialize()
static::setAppNamespace();

Plugin::load('TestPlugin');
$this->Shell->tasks = ['DbConfig' => ['one', 'two']];
$this->Shell->tasks = ['Extract' => ['one', 'two']];
$this->Shell->plugin = 'TestPlugin';
$this->Shell->modelClass = 'TestPlugin.TestPluginComments';
$this->Shell->initialize();
Expand Down Expand Up @@ -704,15 +704,28 @@ public function testCreateFileNoPermissions()
*/
public function testHasTask()
{
$this->Shell->tasks = ['Extract', 'DbConfig'];
$this->Shell->tasks = ['Extract', 'Assets'];
$this->Shell->loadTasks();

$this->assertTrue($this->Shell->hasTask('extract'));
$this->assertTrue($this->Shell->hasTask('Extract'));
$this->assertFalse($this->Shell->hasTask('random'));

$this->assertTrue($this->Shell->hasTask('db_config'));
$this->assertTrue($this->Shell->hasTask('DbConfig'));
$this->assertTrue($this->Shell->hasTask('assets'));
$this->assertTrue($this->Shell->hasTask('Assets'));
}

/**
* test task loading exception
*
* @expectedException \RuntimeException
* @expectedExceptionMessage Task `DoesNotExist` not found. Maybe you made a typo or a plugin is missing or not loaded?
* @return void
*/
public function testMissingTaskException()
{
$this->Shell->tasks = ['DoesNotExist'];
$this->Shell->loadTasks();
}

/**
Expand Down

0 comments on commit 65757db

Please sign in to comment.