Skip to content

fix: #736 - MoveTo implementation for MockDirectoryInfo under TestingHelpers #864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace System.IO.Abstractions.TestingHelpers
public class MockDirectoryInfo : DirectoryInfoBase
{
private readonly IMockFileDataAccessor mockFileDataAccessor;
private readonly string directoryPath;
private string directoryPath;
private readonly string originalPath;
private MockFileData cachedMockFileData;
private bool refreshOnNextRead;
Expand All @@ -27,14 +27,7 @@ public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string dire
this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));

originalPath = directoryPath;
directoryPath = mockFileDataAccessor.Path.GetFullPath(directoryPath);

directoryPath = directoryPath.TrimSlashes();
if (XFS.IsWindowsPlatform())
{
directoryPath = directoryPath.TrimEnd(' ');
}
this.directoryPath = directoryPath;
this.directoryPath = GetCleanDirectoryPath(directoryPath);
Refresh();
}

Expand Down Expand Up @@ -371,6 +364,7 @@ public override IFileSystemInfo[] GetFileSystemInfos(string searchPattern, Enume
public override void MoveTo(string destDirName)
{
mockFileDataAccessor.Directory.Move(directoryPath, destDirName);
directoryPath = GetCleanDirectoryPath(destDirName);
}

/// <inheritdoc />
Expand Down Expand Up @@ -413,6 +407,19 @@ private MockFileData GetMockFileDataForWrite()
return mockFileDataAccessor.GetFile(directoryPath)
?? throw CommonExceptions.CouldNotFindPartOfPath(directoryPath);
}

private string GetCleanDirectoryPath(string path)
{
string cleanPath = mockFileDataAccessor.Path.GetFullPath(path);

cleanPath = cleanPath.TrimSlashes();
if (XFS.IsWindowsPlatform())
{
cleanPath = cleanPath.TrimEnd(' ');
}

return cleanPath;
}

/// <inheritdoc />
public override string ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void MockDirectoryInfo_Exists(string path, bool expected)
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{XFS.Path(@"c:\temp\folder\file.txt"), new MockFileData("Hello World")}
{ XFS.Path(@"c:\temp\folder\file.txt"), new MockFileData("Hello World") }
});
var directoryInfo = new MockDirectoryInfo(fileSystem, path);

Expand All @@ -61,7 +61,7 @@ public void MockDirectoryInfo_Attributes_ShouldReturnMinusOneForNonExistingFile(
{
var fileSystem = new MockFileSystem();
var directoryInfo = new MockDirectoryInfo(fileSystem, XFS.Path(@"c:\non\existing\file.txt"));
FileAttributes expected = (FileAttributes)(-1);
FileAttributes expected = (FileAttributes) (-1);

Assert.That(directoryInfo.Attributes, Is.EqualTo(expected));
}
Expand All @@ -84,7 +84,7 @@ public void MockDirectoryInfo_GetFiles_ShouldWorkWithUNCPath()
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{fileName, ""}
{ fileName, "" }
});

var directoryInfo = new MockDirectoryInfo(fileSystem, directoryName);
Expand All @@ -97,15 +97,14 @@ public void MockDirectoryInfo_GetFiles_ShouldWorkWithUNCPath()
}



[Test]
public void MockDirectoryInfo_FullName_ShouldReturnFullNameWithoutIncludingTrailingPathDelimiter()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{
XFS.Path(@"c:\temp\folder\file.txt"),
new MockFileData("Hello World")
new MockFileData("Hello World")
}
});
var directoryInfo = new MockDirectoryInfo(fileSystem, XFS.Path(@"c:\temp\folder"));
Expand Down Expand Up @@ -600,6 +599,23 @@ public void MockDirectoryInfo_LastWriteTimeUtc_ShouldReturnDefaultTimeForNonExis
Assert.That(result, Is.EqualTo(MockFileData.DefaultDateTimeOffset.UtcDateTime));
}

[Test]
public void MockDirectoryInfo_MoveTo_ShouldUpdateFullName()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\source", new MockDirectoryData() },
});
var directoryInfo = new MockDirectoryInfo(fileSystem, XFS.Path(@"c:\source"));

// Act
directoryInfo.MoveTo(@"c:\destination");

// Assert
Assert.AreEqual(@"c:\destination", directoryInfo.FullName);
}

public void MockDirectoryInfo_CreationTime_SetterShouldThrowDirectoryNotFoundExceptionForNonExistingDirectory()
{
var newTime = new DateTime(2022, 04, 06);
Expand Down Expand Up @@ -653,6 +669,5 @@ public void MockDirectoryInfo_LastWriteTimeUtc_SetterShouldThrowDirectoryNotFoun

Assert.That(() => directoryInfo.LastWriteTime = newTime, Throws.TypeOf<DirectoryNotFoundException>());
}

}
}