Skip to content

Commit c360e56

Browse files
authored
fix: avoid using methods from System.IO in Testing (#562)
* Use methods from `Execute` instead of System.IO * Adjust tests to avoid using System.IO calls
1 parent 842ab4f commit c360e56

23 files changed

+364
-139
lines changed

Source/Testably.Abstractions.Testing/FileSystemInitializerExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public static IFileSystemInitializer<TFileSystem> InitializeIn<TFileSystem>(
131131
options?.Invoke(optionsValue);
132132
if (optionsValue.InitializeTempDirectory)
133133
{
134-
fileSystem.Directory.CreateDirectory(Path.GetTempPath());
134+
fileSystem.Directory.CreateDirectory(fileSystem.ExecuteOrDefault().Path.GetTempPath());
135135
}
136136

137137
return new FileSystemInitializer<TFileSystem>(fileSystem, ".");

Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.IO;
32
using Testably.Abstractions.RandomSystem;
43
using Testably.Abstractions.Testing.Statistics;
54
using Testably.Abstractions.Testing.Storage;
@@ -67,7 +66,7 @@ internal static string GetSubdirectoryPath(this MockFileSystem fileSystem,
6766
{
6867
fullFilePath = fullFilePath.Substring(currentDirectory.Length);
6968
}
70-
else if (fullFilePath.StartsWith(currentDirectory + Path.DirectorySeparatorChar,
69+
else if (fullFilePath.StartsWith(currentDirectory + fileSystem.Execute.Path.DirectorySeparatorChar,
7170
fileSystem.Execute.StringComparisonMode))
7271
{
7372
fullFilePath = fullFilePath.Substring(currentDirectory.Length + 1);
@@ -76,11 +75,11 @@ internal static string GetSubdirectoryPath(this MockFileSystem fileSystem,
7675
{
7776
string? parentName = currentDirectory;
7877
while (parentName != null &&
79-
!fullFilePath.StartsWith(parentName + Path.DirectorySeparatorChar,
78+
!fullFilePath.StartsWith(parentName + fileSystem.Execute.Path.DirectorySeparatorChar,
8079
fileSystem.Execute.StringComparisonMode))
8180
{
82-
parentName = Path.GetDirectoryName(parentName);
83-
int lastIndex = givenPath.LastIndexOf(Path.DirectorySeparatorChar);
81+
parentName = fileSystem.Execute.Path.GetDirectoryName(parentName);
82+
int lastIndex = givenPath.LastIndexOf(fileSystem.Execute.Path.DirectorySeparatorChar);
8483
if (lastIndex >= 0)
8584
{
8685
givenPath = givenPath.Substring(0, lastIndex);

Source/Testably.Abstractions.Testing/Helpers/PathHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static string GetFullPathOrWhiteSpace(this string? path, IFileSystem fi
4141
return path ?? string.Empty;
4242
}
4343

44-
return fileSystem.Path.GetFullPath(path);
44+
return fileSystem.ExecuteOrDefault().Path.GetFullPath(path);
4545
}
4646

4747
/// <summary>

Source/Testably.Abstractions.Testing/Storage/StorageExtensions.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using System.Linq;
54
using System.Text;
65
using Testably.Abstractions.Testing.Helpers;
@@ -28,13 +27,13 @@ public static AdjustedLocation AdjustLocationFromSearchPattern(
2827
StringBuilder givenPathPrefix = new();
2928

3029
while (searchPattern.StartsWith(
31-
".." + Path.DirectorySeparatorChar, StringComparison.Ordinal) ||
30+
".." + fileSystem.Execute.Path.DirectorySeparatorChar, StringComparison.Ordinal) ||
3231
searchPattern.StartsWith(
33-
".." + Path.AltDirectorySeparatorChar, StringComparison.Ordinal))
32+
".." + fileSystem.Execute.Path.AltDirectorySeparatorChar, StringComparison.Ordinal))
3433
{
3534
fileSystem.Execute.OnNetFramework(
3635
() => throw ExceptionFactory.SearchPatternCannotContainTwoDots());
37-
parentDirectories.Push(Path.GetFileName(location.FullPath));
36+
parentDirectories.Push(fileSystem.Execute.Path.GetFileName(location.FullPath));
3837
location = location.GetParent() ??
3938
throw new UnauthorizedAccessException(
4039
$"The searchPattern '{searchPattern}' has too many '../' for path '{path}'");
@@ -47,10 +46,10 @@ public static AdjustedLocation AdjustLocationFromSearchPattern(
4746
if (parentDirectories.Any())
4847
{
4948
givenPathPrefix.Length--;
50-
givenPath = Path.Combine(
49+
givenPath = fileSystem.Execute.Path.Combine(
5150
givenPath,
5251
givenPathPrefix.ToString(),
53-
Path.Combine(parentDirectories.ToArray()));
52+
fileSystem.Execute.Path.Combine(parentDirectories.ToArray()));
5453
}
5554
}
5655

Tests/Testably.Abstractions.Tests/FileSystem/Directory/DeleteTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void
1515
{
1616
directoryName = directoryName.ToLowerInvariant();
1717
FileSystem.Directory.CreateDirectory(directoryName.ToUpperInvariant());
18-
string expectedPath = System.IO.Path.Combine(BasePath, directoryName);
18+
string expectedPath = FileSystem.Path.Combine(BasePath, directoryName);
1919
Exception? exception = Record.Exception(() =>
2020
{
2121
FileSystem.Directory.Delete(directoryName);
@@ -51,7 +51,7 @@ public void Delete_FullPath_ShouldDeleteDirectory(string directoryName)
5151
public void Delete_MissingDirectory_ShouldThrowDirectoryNotFoundException(
5252
string directoryName)
5353
{
54-
string expectedPath = System.IO.Path.Combine(BasePath, directoryName);
54+
string expectedPath = FileSystem.Path.Combine(BasePath, directoryName);
5555
Exception? exception = Record.Exception(() =>
5656
{
5757
FileSystem.Directory.Delete(directoryName);
@@ -66,7 +66,7 @@ public void Delete_MissingDirectory_ShouldThrowDirectoryNotFoundException(
6666
public void Delete_Recursive_MissingDirectory_ShouldThrowDirectoryNotFoundException(
6767
string directoryName)
6868
{
69-
string expectedPath = System.IO.Path.Combine(BasePath, directoryName);
69+
string expectedPath = FileSystem.Path.Combine(BasePath, directoryName);
7070
Exception? exception = Record.Exception(() =>
7171
{
7272
FileSystem.Directory.Delete(directoryName, true);
@@ -248,7 +248,7 @@ public void Delete_WithSubdirectory_ShouldThrowIOException_AndNotDeleteDirectory
248248
// Path information only included in exception message on Windows and not in .NET Framework
249249
messageContains: !Test.RunsOnWindows || Test.IsNetFramework
250250
? null
251-
: $"'{System.IO.Path.Combine(BasePath, path)}'");
251+
: $"'{FileSystem.Path.Combine(BasePath, path)}'");
252252
FileSystem.Should().HaveDirectory(path);
253253
}
254254
}

Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void
2929
EnumerateDirectories_MissingDirectory_ShouldThrowDirectoryNotFoundException(
3030
string path)
3131
{
32-
string expectedPath = System.IO.Path.Combine(BasePath, path);
32+
string expectedPath = FileSystem.Path.Combine(BasePath, path);
3333
Exception? exception =
3434
Record.Exception(()
3535
=> FileSystem.Directory.EnumerateDirectories(path).ToList());

Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateFileSystemInfosTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void
1919
EnumerateFileSystemEntries_MissingDirectory_ShouldThrowDirectoryNotFoundException(
2020
string path)
2121
{
22-
string expectedPath = System.IO.Path.Combine(BasePath, path);
22+
string expectedPath = FileSystem.Path.Combine(BasePath, path);
2323
Exception? exception =
2424
Record.Exception(()
2525
=> FileSystem.Directory.EnumerateFileSystemEntries(path).ToList());

Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateFilesTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void
1919
EnumerateFiles_MissingDirectory_ShouldThrowDirectoryNotFoundException(
2020
string path)
2121
{
22-
string expectedPath = System.IO.Path.Combine(BasePath, path);
22+
string expectedPath = FileSystem.Path.Combine(BasePath, path);
2323
Exception? exception =
2424
Record.Exception(()
2525
=> FileSystem.Directory.EnumerateFiles(path).ToList());

Tests/Testably.Abstractions.Tests/FileSystem/Directory/GetDirectoriesTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void
1515
GetDirectories_MissingDirectory_ShouldThrowDirectoryNotFoundException(
1616
string path)
1717
{
18-
string expectedPath = System.IO.Path.Combine(BasePath, path);
18+
string expectedPath = FileSystem.Path.Combine(BasePath, path);
1919
Exception? exception =
2020
Record.Exception(()
2121
=> FileSystem.Directory.GetDirectories(path).ToList());

Tests/Testably.Abstractions.Tests/FileSystem/Directory/GetFileSystemInfosTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void
1919
GetFileSystemEntries_MissingDirectory_ShouldThrowDirectoryNotFoundException(
2020
string path)
2121
{
22-
string expectedPath = System.IO.Path.Combine(BasePath, path);
22+
string expectedPath = FileSystem.Path.Combine(BasePath, path);
2323
Exception? exception =
2424
Record.Exception(()
2525
=> FileSystem.Directory.GetFileSystemEntries(path).ToList());

Tests/Testably.Abstractions.Tests/FileSystem/Directory/GetFilesTests.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void
1919
GetFiles_MissingDirectory_ShouldThrowDirectoryNotFoundException(
2020
string path)
2121
{
22-
string expectedPath = System.IO.Path.Combine(BasePath, path);
22+
string expectedPath = FileSystem.Path.Combine(BasePath, path);
2323
Exception? exception =
2424
Record.Exception(()
2525
=> FileSystem.Directory.GetFiles(path).ToList());
@@ -266,17 +266,17 @@ public void
266266
public void GetFiles_WithRelativePathAndSubfolders_ShouldReturnRelativeFilePath(
267267
string subfolder1, string subfolder2, string[] files)
268268
{
269-
string workingDirectory = System.IO.Path.Combine(BasePath, subfolder1, subfolder2);
269+
string workingDirectory = FileSystem.Path.Combine(BasePath, subfolder1, subfolder2);
270270
FileSystem.Directory.CreateDirectory(workingDirectory);
271271
foreach (string file in files)
272272
{
273-
FileSystem.File.Create(System.IO.Path.Combine(workingDirectory, file));
273+
FileSystem.File.Create(FileSystem.Path.Combine(workingDirectory, file));
274274
}
275275

276276
FileSystem.Directory.SetCurrentDirectory(subfolder1);
277277
IEnumerable<string> expectation =
278-
files.Select(f => System.IO.Path.Combine("..", subfolder1, subfolder2, f));
279-
string path = System.IO.Path.Combine("..", subfolder1, subfolder2);
278+
files.Select(f => FileSystem.Path.Combine("..", subfolder1, subfolder2, f));
279+
string path = FileSystem.Path.Combine("..", subfolder1, subfolder2);
280280

281281
List<string> result = FileSystem.Directory.GetFiles(path).ToList();
282282

Tests/Testably.Abstractions.Tests/FileSystem/Directory/ResolveLinkTargetTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void ResolveLinkTarget_FinalTarget_ShouldFollowSymbolicLinkToFinalTarget(
7878
{
7979
string newPath = $"{path}-{i}";
8080
FileSystem.Directory.CreateSymbolicLink(newPath,
81-
System.IO.Path.Combine(BasePath, previousPath));
81+
FileSystem.Path.Combine(BasePath, previousPath));
8282
previousPath = newPath;
8383
}
8484

@@ -102,7 +102,7 @@ public void ResolveLinkTarget_FinalTargetWithTooManyLevels_ShouldThrowIOExceptio
102102
{
103103
string newPath = $"{path}-{i}";
104104
FileSystem.Directory.CreateSymbolicLink(newPath,
105-
System.IO.Path.Combine(BasePath, previousPath));
105+
FileSystem.Path.Combine(BasePath, previousPath));
106106
previousPath = newPath;
107107
}
108108

@@ -125,9 +125,9 @@ public void
125125

126126
FileSystem.Directory.CreateDirectory(pathToFinalTarget);
127127
FileSystem.Directory.CreateSymbolicLink(pathToMissingDirectory,
128-
System.IO.Path.Combine(BasePath, pathToFinalTarget));
128+
FileSystem.Path.Combine(BasePath, pathToFinalTarget));
129129
FileSystem.Directory.CreateSymbolicLink(path,
130-
System.IO.Path.Combine(BasePath, pathToMissingDirectory));
130+
FileSystem.Path.Combine(BasePath, pathToMissingDirectory));
131131
FileSystem.Directory.Delete(pathToMissingDirectory);
132132

133133
IFileSystemInfo? target =

Tests/Testably.Abstractions.Tests/FileSystem/Directory/SearchFilterTests.cs

+15-15
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void
2929
.GetFileSystemEntries(".", searchPattern, SearchOption.AllDirectories);
3030

3131
result.Length.Should().Be(expectedMatchingFiles);
32-
result.Should().Contain(System.IO.Path.Combine(".", "..", "xyz", "a.test"));
32+
result.Should().Contain(FileSystem.Path.Combine(".", "..", "xyz", "a.test"));
3333
}
3434

3535
[SkippableTheory]
@@ -54,12 +54,12 @@ public void
5454
result.Length.Should().Be(expectedMatchingFiles);
5555
if (!searchPattern.EndsWith("a*", StringComparison.Ordinal))
5656
{
57-
result.Should().Contain(System.IO.Path.Combine(".", "../..", "bar"));
58-
result.Should().Contain(System.IO.Path.Combine(".", "../..", "bar", "xyz"));
57+
result.Should().Contain(FileSystem.Path.Combine(".", "../..", "bar"));
58+
result.Should().Contain(FileSystem.Path.Combine(".", "../..", "bar", "xyz"));
5959
}
6060

6161
result.Should()
62-
.Contain(System.IO.Path.Combine(".", "../..", "bar", "xyz", "a.test"));
62+
.Contain(FileSystem.Path.Combine(".", "../..", "bar", "xyz", "a.test"));
6363
}
6464

6565
[SkippableTheory]
@@ -84,16 +84,16 @@ public void
8484
result.Length.Should().Be(expectedMatchingFiles);
8585
if (!searchPattern.EndsWith("a*", StringComparison.Ordinal))
8686
{
87-
result.Should().Contain(System.IO.Path.Combine(".", "../../..", "foo"));
87+
result.Should().Contain(FileSystem.Path.Combine(".", "../../..", "foo"));
8888
result.Should()
89-
.Contain(System.IO.Path.Combine(".", "../../..", "foo", "bar"));
89+
.Contain(FileSystem.Path.Combine(".", "../../..", "foo", "bar"));
9090
result.Should()
91-
.Contain(System.IO.Path.Combine(".", "../../..", "foo", "bar", "xyz"));
91+
.Contain(FileSystem.Path.Combine(".", "../../..", "foo", "bar", "xyz"));
9292
}
9393

9494
result.Should()
9595
.Contain(
96-
System.IO.Path.Combine(".", "../../..", "foo", "bar", "xyz", "a.test"));
96+
FileSystem.Path.Combine(".", "../../..", "foo", "bar", "xyz", "a.test"));
9797
}
9898

9999
[SkippableFact]
@@ -109,8 +109,8 @@ public void SearchPattern_ContainingAsterisk_ShouldReturnMatchingFiles()
109109
.GetFileSystemEntries(".", "a*.t*.", SearchOption.AllDirectories);
110110

111111
result.Length.Should().Be(2);
112-
result.Should().Contain(System.IO.Path.Combine(".", "a.test"));
113-
result.Should().Contain(System.IO.Path.Combine(".", "another.test"));
112+
result.Should().Contain(FileSystem.Path.Combine(".", "a.test"));
113+
result.Should().Contain(FileSystem.Path.Combine(".", "another.test"));
114114
}
115115

116116
[SkippableFact]
@@ -126,7 +126,7 @@ public void SearchPattern_ContainingQuestionMark_ShouldReturnMatchingFiles()
126126
.GetFileSystemEntries(".", "a-??s*", SearchOption.AllDirectories);
127127

128128
result.Length.Should().Be(1);
129-
result[0].Should().Be(System.IO.Path.Combine(".", "a-test"));
129+
result[0].Should().Be(FileSystem.Path.Combine(".", "a-test"));
130130
}
131131

132132
[SkippableFact]
@@ -182,7 +182,7 @@ public void
182182
.GetFileSystemEntries(".", searchPattern, SearchOption.AllDirectories);
183183

184184
result.Length.Should().Be(expectedMatchingFiles);
185-
result.Should().Contain(System.IO.Path.Combine(".", "..", path, "a.test"));
185+
result.Should().Contain(FileSystem.Path.Combine(".", "..", path, "a.test"));
186186
}
187187

188188
[SkippableTheory]
@@ -241,7 +241,7 @@ public void SearchPattern_EndingWithTwoDots_ShouldNotMatchAnyFile()
241241
else
242242
{
243243
result.Length.Should().Be(1);
244-
result.Should().Contain(System.IO.Path.Combine(".", "test.."));
244+
result.Should().Contain(FileSystem.Path.Combine(".", "test.."));
245245
}
246246
}
247247

@@ -291,12 +291,12 @@ public void SearchPattern_StarDot_ShouldReturnFilesWithoutExtension()
291291
if (Test.RunsOnWindows)
292292
{
293293
result.Length.Should().Be(1);
294-
result.Should().Contain(System.IO.Path.Combine(".", "test"));
294+
result.Should().Contain(FileSystem.Path.Combine(".", "test"));
295295
}
296296
else
297297
{
298298
result.Length.Should().Be(3);
299-
result.Should().Contain(System.IO.Path.Combine(".", "test."));
299+
result.Should().Contain(FileSystem.Path.Combine(".", "test."));
300300
}
301301
}
302302

Tests/Testably.Abstractions.Tests/FileSystem/DirectoryInfo/CreateTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void Create_TrailingDirectorySeparator_ShouldNotBeTrimmed(
131131
result.Name.Should().Be(expectedName.TrimEnd(
132132
FileSystem.Path.DirectorySeparatorChar,
133133
FileSystem.Path.AltDirectorySeparatorChar));
134-
result.FullName.Should().Be(System.IO.Path.Combine(BasePath, expectedName
134+
result.FullName.Should().Be(FileSystem.Path.Combine(BasePath, expectedName
135135
.Replace(FileSystem.Path.AltDirectorySeparatorChar,
136136
FileSystem.Path.DirectorySeparatorChar)));
137137
FileSystem.Should().HaveDirectory(nameWithSuffix);

Tests/Testably.Abstractions.Tests/FileSystem/DirectoryInfoFactory/Tests.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ public void New_NullCharacter_ShouldThrowArgumentException(string path)
1515
#else
1616
string expectedMessage = "Illegal characters in path.";
1717
#endif
18-
Exception? exception =
19-
Record.Exception(() => FileSystem.DirectoryInfo.New(path));
18+
Exception? exception = Record.Exception(() =>
19+
{
20+
_ = FileSystem.DirectoryInfo.New(path);
21+
});
2022

2123
exception.Should().BeException<ArgumentException>(expectedMessage,
24+
#if !NETFRAMEWORK
25+
paramName: "path",
26+
#endif
2227
hResult: -2147024809);
2328
}
2429

Tests/Testably.Abstractions.Tests/FileSystem/File/ResolveLinkTargetTests.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void ResolveLinkTarget_FinalTarget_ShouldFollowSymbolicLinkToFinalTarget(
100100
{
101101
string newPath = $"{path}-{i}";
102102
FileSystem.File.CreateSymbolicLink(newPath,
103-
System.IO.Path.Combine(BasePath, previousPath));
103+
FileSystem.Path.Combine(BasePath, previousPath));
104104
previousPath = newPath;
105105
}
106106

@@ -122,7 +122,7 @@ public void ResolveLinkTarget_FinalTargetWithTooManyLevels_ShouldThrowIOExceptio
122122
{
123123
string newPath = $"{path}-{i}";
124124
FileSystem.File.CreateSymbolicLink(newPath,
125-
System.IO.Path.Combine(BasePath, previousPath));
125+
FileSystem.Path.Combine(BasePath, previousPath));
126126
previousPath = newPath;
127127
}
128128

@@ -142,9 +142,9 @@ public void ResolveLinkTarget_MissingFileAtBeginningOfLinkChain_ShouldReturnPath
142142
{
143143
FileSystem.File.WriteAllText(pathToFinalTarget, null);
144144
FileSystem.File.CreateSymbolicLink(pathToMissingFile,
145-
System.IO.Path.Combine(BasePath, pathToFinalTarget));
145+
FileSystem.Path.Combine(BasePath, pathToFinalTarget));
146146
FileSystem.File.CreateSymbolicLink(path,
147-
System.IO.Path.Combine(BasePath, pathToMissingFile));
147+
FileSystem.Path.Combine(BasePath, pathToMissingFile));
148148
FileSystem.File.Delete(pathToMissingFile);
149149

150150
IFileSystemInfo? target =
@@ -163,11 +163,11 @@ public void ResolveLinkTarget_MissingFileInLinkChain_ShouldReturnPathToMissingFi
163163
{
164164
FileSystem.File.WriteAllText(pathToFinalTarget, null);
165165
FileSystem.File.CreateSymbolicLink(pathToMissingFile,
166-
System.IO.Path.Combine(BasePath, pathToFinalTarget));
166+
FileSystem.Path.Combine(BasePath, pathToFinalTarget));
167167
FileSystem.File.CreateSymbolicLink(pathToIntermediateTarget,
168-
System.IO.Path.Combine(BasePath, pathToMissingFile));
168+
FileSystem.Path.Combine(BasePath, pathToMissingFile));
169169
FileSystem.File.CreateSymbolicLink(path,
170-
System.IO.Path.Combine(BasePath, pathToIntermediateTarget));
170+
FileSystem.Path.Combine(BasePath, pathToIntermediateTarget));
171171
FileSystem.File.Delete(pathToMissingFile);
172172

173173
IFileSystemInfo? target =

0 commit comments

Comments
 (0)