8000 improved errors for invalid dump targets · symfony/symfony@c6ccc3f · GitHub
[go: up one dir, main page]

Skip to content

Commit c6ccc3f

Browse files
committed
improved errors for invalid dump targets
1 parent 7793b79 commit c6ccc3f

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,21 @@ public function isAbsolutePath($file)
525525
* @param null|int $mode The file mode (octal). If null, file permissions are not modified
526526
* Deprecated since version 2.3.12, to be removed in 3.0.
527527
*
528-
* @throws IOException If the file cannot be written to.
528+
* @throws IOException if the file cannot be written to
529529
*/
530530
public function dumpFile($filename, $content, $mode = 0666)
531531
{
532532
$dir = dirname($filename);
533533

534534
if (!is_dir($dir)) {
535+
if (is_file($dir)) {
536+
throw new IOException(sprintf('The parent path of "%s" is a file.', $dir));
537+
}
538+
539+
if (is_link($dir)) {
540+
throw new IOException(sprintf('The parent path of "%s" is a symlink to a nonexistent directory.', $dir));
541+
}
542+
535543
$this->mkdir($dir);
536544
}
537545

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,35 @@ public function testDumpKeepsExistingPermissionsWhenOverwritingAnExistingFile()
11021102
$this->assertFilePermissions(745, $filename);
11031103
}
11041104

1105+
/**
1106+
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
1107+
* @expectedExceptionMessageRegExp /^The parent path of ".*" is a file\.$/
1108+
*/
1109+
public function testDumpFailsIfParentDirectoryIsAnExistingFile()
1110+
{
1111+
$file = $this->workspace.DIRECTORY_SEPARATOR.'foo';
1112+
$target = $file.DIRECTORY_SEPARATOR.'bar';
1113+
touch($file);
1114+
1115+
$this->filesystem->dumpFile($target, 'baz');
1116+
}
1117+
1118+
/**
1119+
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
1120+
* @expectedExceptionMessageRegExp /^The parent path of ".*" is a symlink to a nonexistent directory\.$/
1121+
*/
1122+
public function testDumpFailsIfTargetIsSymlinkAndSymlinkTargetDoesNotExist()
1123+
{
1124+
$dir = $this->workspace.DIRECTORY_SEPARATOR.'foo';
1125+
mkdir($dir);
1126+
$link = $this->workspace.DIRECTORY_SEPARATOR.'bar';
1127+
symlink($dir, $link);
1128+
rmdir($dir);
1129+
$target = $link.DIRECTORY_SEPARATOR.'baz';
1130+
1131+
$this->filesystem->dumpFile($target, 'baz');
1132+
}
1133+
11051134
public function testCopyShouldKeepExecutionPermission()
11061135
{
11071136
$this->markAsSkippedIfChmodIsMissing();

0 commit comments

Comments
 (0)
0