Skip to content

Commit

Permalink
Fix non interactive to only overwrite with designed force flag enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Dec 25, 2017
1 parent dff88e6 commit 807db8f
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 75 deletions.
9 changes: 8 additions & 1 deletion src/Console/Shell.php
Expand Up @@ -879,7 +879,14 @@ public function createFile($path, $contents)

$this->_io->out();

if (is_file($path) && empty($this->params['force']) && $this->interactive) {
$fileExists = is_file($path);
if ($fileExists && empty($this->params['force']) && !$this->interactive) {
$this->_io->out('<warning>File exists, skipping</warning>.');

return false;
}

if ($fileExists && $this->interactive && empty($this->params['force'])) {
$this->_io->out(sprintf('<warning>File `%s` exists</warning>', $path));
$key = $this->_io->askChoice('Do you want to overwrite?', ['y', 'n', 'a', 'q'], 'n');

Expand Down
113 changes: 42 additions & 71 deletions tests/TestCase/Console/ShellTest.php
Expand Up @@ -20,77 +20,10 @@
use Cake\Core\Plugin;
use Cake\Filesystem\Folder;
use Cake\TestSuite\TestCase;
use TestApp\Shell\MergeShell;
use TestApp\Shell\ShellTestShell;
use TestApp\Shell\TestingDispatchShell;

/**
* for testing merging vars
*/
class MergeShell extends Shell
{

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

public $modelClass = 'Articles';
}

/**
* ShellTestShell class
*/
class ShellTestShell extends Shell
{

/**
* name property
*
* @var string
*/
public $name = 'ShellTestShell';

/**
* stopped property
*
* @var int
*/
public $stopped;

/**
* testMessage property
*
* @var string
*/
public $testMessage = 'all your base are belong to us';

/**
* stop method
*
* @param int $status
* @return void
*/
protected function _stop($status = Shell::CODE_SUCCESS)
{
$this->stopped = $status;
}

protected function _secret()
{
}

//@codingStandardsIgnoreStart
public function doSomething()
{
}

protected function noAccess()
{
}

public function logSomething()
{
$this->log($this->testMessage);
}
//@codingStandardsIgnoreEnd
}

/**
* TestAppleTask class
*/
Expand Down Expand Up @@ -129,6 +62,14 @@ class ShellTest extends TestCase
'core.users'
];

/** @var \Cake\Console\Shell */
protected $Shell;

/**
* @var \Cake\Console\ConsoleIo|\PHPUnit\Framework\MockObject\MockObject
*/
protected $io;

/**
* setUp method
*
Expand Down Expand Up @@ -558,12 +499,38 @@ public function testCreateFileNonInteractive()
new Folder($path, true);

$contents = "<?php{$eol}echo 'test';${eol}\$te = 'st';{$eol}";

$this->Shell->interactive = false;
$result = $this->Shell->createFile($file, $contents);
$this->assertTrue($result);
$this->assertFileExists($file);
$this->assertEquals(file_get_contents($file), $contents);
}

/**
* Test that while in non interactive mode it will not overwrite files by default.
*
* @return void
*/
public function testCreateFileNonInteractiveFileExists()
{
$eol = PHP_EOL;
$path = TMP . 'shell_test';
$file = $path . DS . 'file1.php';
if (!is_dir($path)) {
mkdir($path, 0770, true);
}
touch($file);
$this->assertFileExists($file);

new Folder($path, true);

$contents = "<?php{$eol}echo 'test';${eol}\$te = 'st';{$eol}";
$this->Shell->interactive = false;
$result = $this->Shell->createFile($file, $contents);
$this->assertFalse($result);
}

/**
* Test that files are not changed with a 'n' reply.
*
Expand Down Expand Up @@ -619,7 +586,8 @@ public function testCreateFileOverwrite()
}

/**
* Test that there is no user prompt in non-interactive mode while file already exists.
* Test that there is no user prompt in non-interactive mode while file already exists
* and if force mode is explicitly enabled.
*
* @return void
*/
Expand All @@ -635,6 +603,7 @@ public function testCreateFileOverwriteNonInteractive()

$this->io->expects($this->never())->method('askChoice');

$this->Shell->params['force'] = true;
$this->Shell->interactive = false;
$result = $this->Shell->createFile($file, 'My content');
$this->assertTrue($result);
Expand Down Expand Up @@ -1017,13 +986,15 @@ public function testRunCommandWithMethodInSubcommands()
*/
public function testRunCommandWithMissingMethodInSubcommands()
{
/** @var \Cake\Console\ConsoleOptionParser|\PHPUnit\Framework\MockObject\MockObject $parser */
$parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
->setMethods(['help'])
->setConstructorArgs(['knife'])
->getMock();
$parser->addSubCommand('slice');

$io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
/** @var \Cake\Console\Shell|\PHPUnit\Framework\MockObject\MockObject $shell */
$shell = $this->getMockBuilder('Cake\Console\Shell')
->setMethods(['getOptionParser', 'startup'])
->setConstructorArgs([$io])
Expand Down Expand Up @@ -1383,7 +1354,7 @@ public function testQuietLog()
->method('setLoggers')
->with(ConsoleIo::QUIET);

$this->Shell = $this->getMockBuilder(__NAMESPACE__ . '\ShellTestShell')
$this->Shell = $this->getMockBuilder(ShellTestShell::class)
->setMethods(['welcome'])
->setConstructorArgs([$io])
->getMock();
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Shell/CommandListShellTest.php
Expand Up @@ -65,7 +65,7 @@ public function testMain()
$expected = "/\[.*CORE.*\] cache, help, i18n, orm_cache, plugin, routes, server/";
$this->assertOutputRegExp($expected);

$expected = "/\[.*app.*\] i18m, integration, sample/";
$expected = "/\[.*app.*\] i18m, integration, merge, sample/";
$this->assertOutputRegExp($expected);
$this->assertExitCode(Shell::CODE_SUCCESS);
$this->assertErrorEmpty();
Expand All @@ -86,7 +86,7 @@ public function testMainAppPriority()
$expected = "/\[.*CORE.*\] cache, help, orm_cache, plugin, routes, server/";
$this->assertOutputRegExp($expected);

$expected = "/\[.*app.*\] i18n, integration, sample/";
$expected = "/\[.*app.*\] i18n, integration, merge, sample/";
$this->assertOutputRegExp($expected);
$this->assertExitCode(Shell::CODE_SUCCESS);
$this->assertErrorEmpty();
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Shell/CompletionShellTest.php
Expand Up @@ -116,7 +116,7 @@ public function testCommands()
$output = $this->out->output;

$expected = 'TestPlugin.example TestPlugin.sample TestPluginTwo.example unique welcome ' .
"cache help i18n orm_cache plugin routes server version i18m integration sample testing_dispatch\n";
"cache help i18n orm_cache plugin routes server version i18m integration merge sample test testing_dispatch\n";
$this->assertTextEquals($expected, $output);
}

Expand Down
36 changes: 36 additions & 0 deletions tests/test_app/TestApp/Shell/MergeShell.php
@@ -0,0 +1,36 @@
<?php
/**
* MergeShell file
*
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.8
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace TestApp\Shell;

use Cake\Console\Shell;

/**
* for testing merging vars
*/
class MergeShell extends Shell
{
/**
* @var array
*/
public $tasks = ['DbConfig', 'Fixture'];

/**
* @var string
*/
public $modelClass = 'Articles';
}
77 changes: 77 additions & 0 deletions tests/test_app/TestApp/Shell/ShellTestShell.php
@@ -0,0 +1,77 @@
<?php
/**
* ShellTestShell file
*
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.8
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace TestApp\Shell;

use Cake\Console\Shell;

/**
* ShellTestShell class
*/
class ShellTestShell extends Shell
{
/**
* name property
*
* @var string
*/
public $name = 'ShellTestShell';

/**
* stopped property
*
* @var int
*/
public $stopped;

/**
* testMessage property
*
* @var string
*/
public $testMessage = 'all your base are belong to us';

/**
* stop method
*
* @param int $status
* @return void
*/
protected function _stop($status = Shell::CODE_SUCCESS)
{
$this->stopped = $status;
}

protected function _secret()
{
}

//@codingStandardsIgnoreStart
public function doSomething()
{
}

protected function noAccess()
{
}

public function logSomething()
{
$this->log($this->testMessage);
}
//@codingStandardsIgnoreEnd
}

0 comments on commit 807db8f

Please sign in to comment.