Skip to content

Commit b1acca3

Browse files
Cikidg
authored andcommitted
FileSystem::rename() fix renaming file/directory if only case changes (#155)
1 parent 32fd477 commit b1acca3

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/Utils/FileSystem.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ public static function rename(string $name, string $newName, bool $overwrite = t
103103

104104
} else {
105105
static::createDir(dirname($newName));
106-
static::delete($newName);
106+
if (realpath($name) !== realpath($newName)) {
107+
static::delete($newName);
108+
}
107109
if (!@rename($name, $newName)) { // @ is escalated to exception
108110
throw new Nette\IOException("Unable to rename file or directory '$name' to '$newName'.");
109111
}

tests/Utils/FileSystem.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,18 @@ test(function () { // rename
133133
FileSystem::rename(TEMP_DIR . '/10', TEMP_DIR . '/9');
134134
Assert::false(file_exists(TEMP_DIR . '/9/x/file'));
135135
Assert::false(file_exists(TEMP_DIR . '/10'));
136+
137+
FileSystem::createDir(TEMP_DIR . '/11/');
138+
FileSystem::rename(TEMP_DIR . '/11', TEMP_DIR . '/11');
139+
Assert::true(file_exists(TEMP_DIR . '/11'));
140+
FileSystem::rename(TEMP_DIR . '/11', TEMP_DIR . '/11/');
141+
Assert::true(file_exists(TEMP_DIR . '/11'));
136142
});
137143

138144
Assert::exception(function () {
139145
FileSystem::rename(TEMP_DIR . '/10', TEMP_DIR . '/9');
140146
}, Nette\IOException::class, "File or directory '%S%' not found.");
141147

142-
Assert::exception(function () {
143-
FileSystem::rename(TEMP_DIR . '/9', TEMP_DIR . '/9');
144-
}, Nette\IOException::class, "Unable to rename file or directory '%a%' to '%a%'.");
145-
146148

147149
test(function () { // isAbsolute
148150
Assert::false(FileSystem::isAbsolute(''));

0 commit comments

Comments
 (0)