Skip to content

Commit 3bac357

Browse files
phmatrayclaude
andcommitted
fix: add validation to prevent renaming to existing directory name (qa-requested)
Fixes: - Add duplicate directory check in VFS.Rename.cs before rename operation - Add test to verify exception is thrown when renaming to existing name Verified: - Validation follows same pattern as CreateDirectory duplicate check - Test matches pattern from existing duplicate validation tests - Fixes acceptance criteria #5: renaming to existing name throws exception QA Fix Session: 1 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 79c9123 commit 3bac357

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/Atypical.VirtualFileSystem.Core/SystemOperations/Commands/Rename/VFS.Rename.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public IVirtualFileSystem RenameDirectory(
3737
// update the directory node with the new path
3838
var oldName = directoryNode.Name;
3939
var newPath = new VFSDirectoryPath($"{directoryPath.Parent}/{newName}");
40+
41+
// Validate that the destination path doesn't already exist
42+
if (Index.ContainsKey(newPath))
43+
ThrowVirtualNodeAlreadyExists(newPath);
44+
4045
var updatedDirectoryNode = directoryNode.UpdatePath(newPath);
4146

4247
// Add the directory to its old parent directory with the new name

tests/Atypical.VirtualFileSystem.UnitTests/SystemOperations/Commands/VirtualFileSystem_MethodRenameDirectory_Tests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,23 @@ public void RenameDirectory_handles_deeply_nested_structure()
290290
_vfs.Index[newSubdirAtC].Path.Value.Should().Be("vfs://a/b/renamed_c/subdir_c");
291291
_vfs.Index[newSubdirAtE].Path.Value.Should().Be("vfs://a/b/renamed_c/d/e/subdir_e");
292292
}
293+
294+
[Fact]
295+
public void RenameDirectory_throws_exception_when_target_name_already_exists()
296+
{
297+
// Arrange
298+
_vfs.CreateDirectory(new VFSDirectoryPath("dir1/dir2/dir3"));
299+
_vfs.CreateDirectory(new VFSDirectoryPath("dir1/dir2/existing"));
300+
301+
// Act
302+
Action action = () => _vfs.RenameDirectory(
303+
new VFSDirectoryPath("dir1/dir2/dir3"),
304+
"existing"
305+
);
306+
307+
// Assert
308+
action.Should()
309+
.Throw<VirtualFileSystemException>()
310+
.WithMessage("The node 'vfs://dir1/dir2/existing' already exists in the index.");
311+
}
293312
}

0 commit comments

Comments
 (0)