Browse Source

Merge pull request #12826 from cakephp/consoleio-file

createFile() should create directories.
Mark Story 7 years ago
parent
commit
74af6770b0
2 changed files with 25 additions and 0 deletions
  1. 6 0
      src/Console/ConsoleIo.php
  2. 19 0
      tests/TestCase/Console/ConsoleIoTest.php

+ 6 - 0
src/Console/ConsoleIo.php

@@ -566,6 +566,12 @@ class ConsoleIo
         }
 
         try {
+            // Create the directory using the current user permissions.
+            $directory = dirname($path);
+            if (!file_exists($directory)) {
+                mkdir($directory, 0777 ^ umask(), true);
+            }
+
             $file = new SplFileObject($path, 'w');
         } catch (RuntimeException $e) {
             $this->error("Could not write to `{$path}`. Permission denied.", 2);

+ 19 - 0
tests/TestCase/Console/ConsoleIoTest.php

@@ -580,6 +580,8 @@ class ConsoleIoTest extends TestCase
      */
     public function testCreateFileSuccess()
     {
+        $this->err->expects($this->never())
+            ->method('write');
         $path = TMP . 'shell_test';
         mkdir($path);
 
@@ -592,6 +594,23 @@ class ConsoleIoTest extends TestCase
         $this->assertStringEqualsFile($file, $contents);
     }
 
+    public function testCreateFileDirectoryCreation()
+    {
+        $this->err->expects($this->never())
+            ->method('write');
+
+        $directory = TMP . 'shell_test';
+        $this->assertFileNotExists($directory, 'Directory should not exist before createFile');
+
+        $path = $directory . DS . 'create.txt';
+        $contents = 'some content';
+        $result = $this->io->createFile($path, $contents);
+
+        $this->assertTrue($result, 'File should create');
+        $this->assertFileExists($path);
+        $this->assertStringEqualsFile($path, $contents);
+    }
+
     /**
      * Test that createFile with permissions error.
      *