Skip to content

Commit f9cf4f9

Browse files
fgreinachersoi013
andauthored
fix: support changing only case in when moving mock files (#740)
Closes #737 Co-authored-by: soi013 <soi2flatline@gmail.com>
1 parent b06d921 commit f9cf4f9

3 files changed

Lines changed: 23 additions & 16 deletions

File tree

src/System.IO.Abstractions.TestingHelpers/MockFile.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public override void Move(string sourceFileName, string destFileName)
330330

331331
if (mockFileDataAccessor.GetFile(destFileName) != null)
332332
{
333-
if (destFileName.Equals(sourceFileName))
333+
if (mockFileDataAccessor.StringOperations.Equals(destFileName, sourceFileName))
334334
{
335335
return;
336336
}
@@ -340,7 +340,6 @@ public override void Move(string sourceFileName, string destFileName)
340340
}
341341
}
342342

343-
344343
var sourceFile = mockFileDataAccessor.GetFile(sourceFileName);
345344

346345
if (sourceFile == null)

src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Runtime.Versioning;
22
using System.Security.AccessControl;
3+
using System.Text;
34

45
namespace System.IO.Abstractions.TestingHelpers
56
{
@@ -230,25 +231,15 @@ public override FileSecurity GetAccessControl(AccessControlSections includeSecti
230231

231232
public override void MoveTo(string destFileName)
232233
{
233-
var movedFileInfo = CopyTo(destFileName);
234-
if (destFileName == FullName)
235-
{
236-
return;
237-
}
238-
Delete();
239-
path = movedFileInfo.FullName;
234+
mockFileSystem.File.Move(path, destFileName);
235+
path = mockFileSystem.Path.GetFullPath(destFileName);
240236
}
241237

242238
#if FEATURE_FILE_MOVE_WITH_OVERWRITE
243239
public override void MoveTo(string destFileName, bool overwrite)
244240
{
245-
var movedFileInfo = CopyTo(destFileName, overwrite);
246-
if (destFileName == FullName)
247-
{
248-
return;
249-
}
250-
Delete();
251-
path = movedFileInfo.FullName;
241+
mockFileSystem.File.Move(path, destFileName, overwrite);
242+
path = mockFileSystem.Path.GetFullPath(destFileName);
252243
}
253244
#endif
254245

tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,23 @@ public void MockFileInfo_MoveToWithOverwrite_ShouldSucceedWhenTargetAlreadyExist
548548
}
549549
#endif
550550

551+
[Test]
552+
public void MockFileInfo_MoveToOnlyCaseChanging_ShouldSucceed()
553+
{
554+
string sourceFilePath = XFS.Path(@"c:\temp\file.txt");
555+
string destFilePath = XFS.Path(@"c:\temp\FILE.txt");
556+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
557+
{
558+
{sourceFilePath, new MockFileData("1")},
559+
});
560+
561+
var fileInfo = fileSystem.FileInfo.FromFileName(sourceFilePath);
562+
fileInfo.MoveTo(destFilePath);
563+
564+
Assert.AreEqual(fileInfo.FullName, destFilePath);
565+
Assert.True(fileInfo.Exists);
566+
}
567+
551568
[Test]
552569
public void MockFileInfo_CopyTo_ThrowsExceptionIfSourceDoesntExist()
553570
{

0 commit comments

Comments
 (0)