Skip to content

Commit 4746326

Browse files
committed
corrected logic
1 parent 84843b8 commit 4746326

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ public override void Move(string sourceDirName, string destDirName)
503503
var fullSourcePath = mockFileDataAccessor.Path.GetFullPath(sourceDirName).TrimSlashes();
504504
var fullDestPath = mockFileDataAccessor.Path.GetFullPath(destDirName).TrimSlashes();
505505

506-
if (mockFileDataAccessor.StringOperations.Equals(fullSourcePath, fullDestPath))
506+
if (XFS.IsUnixPlatform() ? mockFileDataAccessor.StringOperations.Equals(fullSourcePath, fullDestPath)
507+
: mockFileDataAccessor.StringOperations.Equals(fullSourcePath, fullDestPath, StringComparison.Ordinal))
507508
{
508509
throw new IOException("Source and destination path must be different.");
509510
}
@@ -534,12 +535,21 @@ public override void Move(string sourceDirName, string destDirName)
534535
{
535536
throw CommonExceptions.CouldNotFindPartOfPath(destDirName);
536537
}
537-
538-
if (mockFileDataAccessor.Directory.Exists(fullDestPath) || mockFileDataAccessor.File.Exists(fullDestPath))
538+
if (XFS.IsUnixPlatform())
539539
{
540-
throw CommonExceptions.CannotCreateBecauseSameNameAlreadyExists(fullDestPath);
540+
if (mockFileDataAccessor.Directory.Exists(fullDestPath) || mockFileDataAccessor.File.Exists(fullDestPath))
541+
{
542+
throw CommonExceptions.CannotCreateBecauseSameNameAlreadyExists(fullDestPath);
543+
}
544+
}
545+
else if (!mockFileDataAccessor.StringOperations.Equals(fullSourcePath, fullDestPath, StringComparison.OrdinalIgnoreCase))
546+
{
547+
// In Windows, file/dir names are case case sensetive, src and SRC and treated different
548+
if (mockFileDataAccessor.Directory.Exists(fullDestPath) || mockFileDataAccessor.File.Exists(fullDestPath))
549+
{
550+
throw CommonExceptions.CannotCreateBecauseSameNameAlreadyExists(fullDestPath);
551+
}
541552
}
542-
543553
mockFileDataAccessor.MoveDirectory(fullSourcePath, fullDestPath);
544554
}
545555

@@ -653,15 +663,15 @@ public override IEnumerable<string> EnumerateDirectories(string path, string sea
653663
.Where(p => !mockFileDataAccessor.StringOperations.Equals(p, path))
654664
.Select(p => FixPrefix(p, originalPath));
655665
}
656-
666+
657667
private string FixPrefix(string path, string originalPath)
658668
{
659669
var normalizedOriginalPath = mockFileDataAccessor.Path.GetFullPath(originalPath);
660670
var pathWithoutOriginalPath = path.Substring(normalizedOriginalPath.Length)
661671
.TrimStart(mockFileDataAccessor.Path.DirectorySeparatorChar);
662672
return mockFileDataAccessor.Path.Combine(originalPath, pathWithoutOriginalPath);
663673
}
664-
674+
665675
#if FEATURE_ENUMERATION_OPTIONS
666676
/// <inheritdoc />
667677
public override IEnumerable<string> EnumerateDirectories(string path, string searchPattern, EnumerationOptions enumerationOptions)

tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,4 +2204,21 @@ public async Task MockDirectory_Exists_ShouldReturnTrue_IfArgIsFrontSlashAndRoot
22042204

22052205
await That(fileSystem.Directory.Exists("/")).IsEqualTo(true);
22062206
}
2207+
2208+
[Test]
2209+
public static void MockDirectory_Move_ShouldNotThrowException_InWindows_When_SourceAndDestinationDifferOnlyInCasing()
2210+
2211+
{
2212+
// Arrange
2213+
MockFileSystem mockFs = new MockFileSystem();
2214+
string tempDir = mockFs.Path.GetTempPath();
2215+
string src = mockFs.Path.Combine(tempDir, "src");
2216+
string dest = mockFs.Path.Combine(tempDir, "SRC"); // different case
2217+
IDirectoryInfo srcDir = mockFs.DirectoryInfo.New(src);
2218+
srcDir.Create();
2219+
2220+
// Act & Assert
2221+
Assert.DoesNotThrow(() => mockFs.Directory.Move(src, dest));
2222+
}
2223+
22072224
}

0 commit comments

Comments
 (0)