Browse Source

Fix non interactive to only overwrite with designed force flag enabled.

dereuromark 8 years ago
parent
commit
807db8f228

+ 8 - 1
src/Console/Shell.php

@@ -879,7 +879,14 @@ class Shell
 
         $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');
 

+ 42 - 71
tests/TestCase/Console/ShellTest.php

@@ -20,78 +20,11 @@ use Cake\Console\Shell;
 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
  */
 class TestAppleTask extends Shell
@@ -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
      *
@@ -558,6 +499,8 @@ class ShellTest extends TestCase
         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);
@@ -565,6 +508,30 @@ class ShellTest extends TestCase
     }
 
     /**
+     * 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.
      *
      * @return void
@@ -619,7 +586,8 @@ class ShellTest extends TestCase
     }
 
     /**
-     * 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
      */
@@ -635,6 +603,7 @@ class ShellTest extends TestCase
 
         $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);
@@ -1017,6 +986,7 @@ TEXT;
      */
     public function testRunCommandWithMissingMethodInSubcommands()
     {
+        /** @var \Cake\Console\ConsoleOptionParser|\PHPUnit\Framework\MockObject\MockObject $parser */
         $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
             ->setMethods(['help'])
             ->setConstructorArgs(['knife'])
@@ -1024,6 +994,7 @@ TEXT;
         $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])
@@ -1383,7 +1354,7 @@ TEXT;
             ->method('setLoggers')
             ->with(ConsoleIo::QUIET);
 
-        $this->Shell = $this->getMockBuilder(__NAMESPACE__ . '\ShellTestShell')
+        $this->Shell = $this->getMockBuilder(ShellTestShell::class)
             ->setMethods(['welcome'])
             ->setConstructorArgs([$io])
             ->getMock();

+ 2 - 2
tests/TestCase/Shell/CommandListShellTest.php

@@ -65,7 +65,7 @@ class CommandListShellTest extends ConsoleIntegrationTestCase
         $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();
@@ -86,7 +86,7 @@ class CommandListShellTest extends ConsoleIntegrationTestCase
         $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();

+ 1 - 1
tests/TestCase/Shell/CompletionShellTest.php

@@ -116,7 +116,7 @@ class CompletionShellTest extends TestCase
         $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);
     }
 

+ 36 - 0
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 - 0
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
+}