Browse Source

Merge pull request #6503 from dakota/overwrite-all

Allow for overwriting all files in shell
Mark Story 10 years ago
parent
commit
75d1173c30
2 changed files with 37 additions and 1 deletions
  1. 5 1
      src/Console/Shell.php
  2. 32 0
      tests/TestCase/Console/ShellTest.php

+ 5 - 1
src/Console/Shell.php

@@ -614,12 +614,16 @@ class Shell
 
         if (is_file($path) && empty($this->params['force']) && $this->interactive) {
             $this->_io->out(sprintf('<warning>File `%s` exists</warning>', $path));
-            $key = $this->_io->askChoice('Do you want to overwrite?', ['y', 'n', 'q'], 'n');
+            $key = $this->_io->askChoice('Do you want to overwrite?', ['y', 'n', 'a', 'q'], 'n');
 
             if (strtolower($key) === 'q') {
                 $this->_io->out('<error>Quitting</error>.', 2);
                 return $this->_stop();
             }
+            if (strtolower($key) === 'a') {
+                $this->params['force'] = true;
+                $key = 'y';
+            }
             if (strtolower($key) !== 'y') {
                 $this->_io->out(sprintf('Skip `%s`', $path), 2);
                 return false;

+ 32 - 0
tests/TestCase/Console/ShellTest.php

@@ -526,6 +526,38 @@ class ShellTest extends TestCase
     }
 
     /**
+     * Test that all files are changed with a 'a' reply.
+     *
+     * @return void
+     */
+    public function testCreateFileOverwriteAll()
+    {
+        $eol = PHP_EOL;
+        $path = TMP . 'shell_test';
+        $files = [
+            $path . DS . 'file1.php' => 'My first content',
+            $path . DS . 'file2.php' => 'My second content',
+            $path . DS . 'file3.php' => 'My third content'
+        ];
+
+        new Folder($path, true);
+
+        $this->io->expects($this->once())
+            ->method('askChoice')
+            ->will($this->returnValue('a'));
+
+        foreach ($files as $file => $contents) {
+            touch($file);
+            $this->assertTrue(file_exists($file));
+
+            $result = $this->Shell->createFile($file, $contents);
+            $this->assertTrue(file_exists($file));
+            $this->assertTextEquals($contents, file_get_contents($file));
+            $this->assertTrue($result, 'Did create file.');
+        }
+    }
+
+    /**
      * Test that you can't create files that aren't writable.
      *
      * @return void