Skip to content

Commit eabc64e

Browse files
refactor: de-duplicated normalize/fix path logic moving method to path verifier
1 parent f241cc2 commit eabc64e

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public MockFileStream(
5555
ThrowIfInvalidModeAccess(mode, access);
5656

5757
this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
58-
path = NormalizePath(path);
58+
path = mockFileDataAccessor.PathVerifier.FixPath(path);
5959
this.path = path;
6060
this.options = options;
6161

@@ -360,15 +360,4 @@ private TimeAdjustments GetTimeAdjustmentsForFileStreamWhenFileExists(FileMode m
360360
return TimeAdjustments.None;
361361
}
362362
}
363-
364-
private string NormalizePath(string path)
365-
{
366-
var normalizedPath = path
367-
.Replace(
368-
mockFileDataAccessor.Path.AltDirectorySeparatorChar,
369-
mockFileDataAccessor.Path.DirectorySeparatorChar)
370-
.TrimSlashes();
371-
normalizedPath = mockFileDataAccessor.Path.GetFullPath(normalizedPath);
372-
return normalizedPath;
373-
}
374363
}

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

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,6 @@ public MockFileSystem MockTime(Func<DateTime> dateTimeProvider)
128128
return this;
129129
}
130130

131-
private string FixPath(string path, bool checkCaps = false)
132-
{
133-
if (path == null)
134-
{
135-
throw new ArgumentNullException(nameof(path), StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL"));
136-
}
137-
138-
var pathSeparatorFixed = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
139-
var fullPath = Path.GetFullPath(pathSeparatorFixed);
140-
141-
return checkCaps ? GetPathWithCorrectDirectoryCapitalization(fullPath) : fullPath;
142-
}
143-
144131
//If C:\foo exists, ensures that trying to save a file to "C:\FOO\file.txt" instead saves it to "C:\foo\file.txt".
145132
private string GetPathWithCorrectDirectoryCapitalization(string fullPath)
146133
{
@@ -194,7 +181,7 @@ public MockFileData AdjustTimes(MockFileData fileData, TimeAdjustments timeAdjus
194181
/// <inheritdoc />
195182
public MockFileData GetFile(string path)
196183
{
197-
path = FixPath(path).TrimSlashes();
184+
path = pathVerifier.FixPath(path).TrimSlashes();
198185
return GetFileWithoutFixingPath(path);
199186
}
200187

@@ -210,7 +197,9 @@ public MockDriveData GetDrive(string name)
210197

211198
private void SetEntry(string path, MockFileData mockFile)
212199
{
213-
path = FixPath(path, true).TrimSlashes();
200+
path = GetPathWithCorrectDirectoryCapitalization(
201+
pathVerifier.FixPath(path)
202+
).TrimSlashes();
214203

215204
lock (files)
216205
{
@@ -232,7 +221,9 @@ private void SetEntry(string path, MockFileData mockFile)
232221
/// <inheritdoc />
233222
public void AddFile(string path, MockFileData mockFile, bool verifyAccess = true)
234223
{
235-
var fixedPath = FixPath(path, true);
224+
var fixedPath = GetPathWithCorrectDirectoryCapitalization(
225+
pathVerifier.FixPath(path)
226+
);
236227

237228
mockFile ??= new MockFileData(string.Empty);
238229
var file = GetFile(fixedPath);
@@ -319,7 +310,9 @@ public MockFileData GetFile(IFileInfo path)
319310
/// <inheritdoc />
320311
public void AddDirectory(string path)
321312
{
322-
var fixedPath = FixPath(path, true);
313+
var fixedPath = GetPathWithCorrectDirectoryCapitalization(
314+
pathVerifier.FixPath(path)
315+
);
323316
var separator = Path.DirectorySeparatorChar.ToString();
324317

325318
if (FileExists(fixedPath) && FileIsReadOnly(fixedPath))
@@ -408,8 +401,8 @@ public void AddDrive(string name, MockDriveData mockDrive)
408401
/// <inheritdoc />
409402
public void MoveDirectory(string sourcePath, string destPath)
410403
{
411-
sourcePath = FixPath(sourcePath);
412-
destPath = FixPath(destPath);
404+
sourcePath = pathVerifier.FixPath(sourcePath);
405+
destPath = pathVerifier.FixPath(destPath);
413406

414407
var sourcePathSequence = sourcePath.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
415408

@@ -452,7 +445,7 @@ bool PathStartsWith(string path, string[] minMatch)
452445
/// <inheritdoc />
453446
public void RemoveFile(string path, bool verifyAccess = true)
454447
{
455-
path = FixPath(path);
448+
path = pathVerifier.FixPath(path);
456449

457450
lock (files)
458451
{
@@ -473,7 +466,7 @@ public bool FileExists(string path)
473466
return false;
474467
}
475468

476-
path = FixPath(path).TrimSlashes();
469+
path = pathVerifier.FixPath(path).TrimSlashes();
477470

478471
lock (files)
479472
{

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,23 @@ public bool TryNormalizeDriveName(string name, out string result)
183183
result = name;
184184
return true;
185185
}
186+
187+
/// <summary>
188+
/// Resolves and normalizes a path.
189+
/// </summary>
190+
public string FixPath(string path)
191+
{
192+
if (path == null)
193+
{
194+
throw new ArgumentNullException(nameof(path), StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL"));
195+
}
196+
197+
var pathSeparatorFixed = path.Replace(
198+
_mockFileDataAccessor.Path.AltDirectorySeparatorChar,
199+
_mockFileDataAccessor.Path.DirectorySeparatorChar
200+
);
201+
var fullPath = _mockFileDataAccessor.Path.GetFullPath(pathSeparatorFixed);
202+
203+
return fullPath;
204+
}
186205
}

0 commit comments

Comments
 (0)