Skip to content

Commit cdeb078

Browse files
304NotModifiedfgreinacher
authored andcommitted
Fix MockFileInfo.FullName - normalize paths (#482)
Fixes #425
1 parent cb0443c commit cdeb078

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,35 @@ public void MockFileInfo_ToString_ShouldReturnOriginalFilePath(string path)
536536
Assert.AreEqual(filePath, mockFileInfo.ToString());
537537
}
538538

539+
540+
/// <summary>
541+
/// Normalize, tested with Path.GetFullPath and new FileInfo().FullName;
542+
/// </summary>
543+
[TestCaseSource(nameof(FromFileName_Paths_NormalizePaths_Cases))]
544+
public void FromFileName_Paths_NormalizePaths(string input, string expected)
545+
{
546+
// Arrange
547+
var mockFs = new MockFileSystem();
548+
549+
// Act
550+
var mockFileInfo = mockFs.FileInfo.FromFileName(input);
551+
var result = mockFileInfo.FullName;
552+
553+
// Assert
554+
Assert.AreEqual(expected, result);
555+
}
556+
557+
public static IEnumerable<string[]> FromFileName_Paths_NormalizePaths_Cases
558+
{
559+
get
560+
{
561+
yield return new[] { XFS.Path(@"c:\top\..\most\file"), XFS.Path(@"c:\most\file") };
562+
yield return new[] { XFS.Path(@"c:\top\..\most\..\dir\file"), XFS.Path(@"c:\dir\file") };
563+
yield return new[] { XFS.Path(@"\file"), XFS.Path(@"C:\file") };
564+
yield return new[] { XFS.Path(@"c:\top\../..\most\file"), XFS.Path(@"c:\most\file") };
565+
}
566+
}
567+
539568
#if NET40
540569
[Test]
541570
public void MockFileInfo_Replace_ShouldReplaceFileContents()

System.IO.Abstractions.TestingHelpers/MockFileInfo.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ public class MockFileInfo : FileInfoBase
77
{
88
private readonly IMockFileDataAccessor mockFileSystem;
99
private string path;
10+
private string originalPath;
1011

1112
public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path) : base(mockFileSystem?.FileSystem)
1213
{
1314
this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem));
14-
this.path = path ?? throw new ArgumentNullException(nameof(path));
15+
this.originalPath = path ?? throw new ArgumentNullException(nameof(path));
16+
this.path = mockFileSystem.Path.GetFullPath(path);
17+
1518
}
1619

1720
MockFileData MockFileData
@@ -314,7 +317,7 @@ public override long Length
314317

315318
public override string ToString()
316319
{
317-
return path;
320+
return originalPath;
318321
}
319322
}
320323
}

0 commit comments

Comments
 (0)