Skip to content

Commit e4b9b53

Browse files
committed
createFile() should create directories.
To improve compatibility with `Shell::createFile()` `ConsoleIo::createFile()` should also create the required directories. Because `Cake\Filesystem` is soft-deprecated I've rebuilt the directory creation logic here as Spl does not handle it.
1 parent 540c353 commit e4b9b53

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Console/ConsoleIo.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,12 @@ public function createFile($path, $contents, $forceOverwrite = false)
566566
}
567567

568568
try {
569+
// Create the directory using the current user permissions.
570+
$directory = dirname($path);
571+
if (!file_exists($directory)) {
572+
mkdir($directory, 0777 ^ umask(), true);
573+
}
574+
569575
$file = new SplFileObject($path, 'w');
570576
} catch (RuntimeException $e) {
571577
$this->error("Could not write to `{$path}`. Permission denied.", 2);

tests/TestCase/Console/ConsoleIoTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ public function testErrHelpers($method)
580580
*/
581581
public function testCreateFileSuccess()
582582
{
583+
$this->err->expects($this->never())
584+
->method('write');
583585
$path = TMP . 'shell_test';
584586
mkdir($path);
585587

@@ -592,6 +594,23 @@ public function testCreateFileSuccess()
592594
$this->assertStringEqualsFile($file, $contents);
593595
}
594596

597+
public function testCreateFileDirectoryCreation()
598+
{
599+
$this->err->expects($this->never())
600+
->method('write');
601+
602+
$directory = TMP . 'shell_test';
603+
$this->assertFileNotExists($directory, 'Directory should not exist before createFile');
604+
605+
$path = $directory . DS . 'create.txt';
606+
$contents = 'some content';
607+
$result = $this->io->createFile($path, $contents);
608+
609+
$this->assertTrue($result, 'File should create');
610+
$this->assertFileExists($path);
611+
$this->assertStringEqualsFile($path, $contents);
612+
}
613+
595614
/**
596615
* Test that createFile with permissions error.
597616
*

0 commit comments

Comments
 (0)