Skip to content
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

fix: avoid using methods from System.IO in Testing #562

Merged
merged 2 commits into from
Apr 15, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -131,7 +131,7 @@ public static IFileSystemInitializer<TFileSystem> InitializeIn<TFileSystem>(
options?.Invoke(optionsValue);
if (optionsValue.InitializeTempDirectory)
{
fileSystem.Directory.CreateDirectory(Path.GetTempPath());
fileSystem.Directory.CreateDirectory(fileSystem.ExecuteOrDefault().Path.GetTempPath());
}

return new FileSystemInitializer<TFileSystem>(fileSystem, ".");
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using Testably.Abstractions.RandomSystem;
using Testably.Abstractions.Testing.Statistics;
using Testably.Abstractions.Testing.Storage;
@@ -67,7 +66,7 @@ internal static string GetSubdirectoryPath(this MockFileSystem fileSystem,
{
fullFilePath = fullFilePath.Substring(currentDirectory.Length);
}
else if (fullFilePath.StartsWith(currentDirectory + Path.DirectorySeparatorChar,
else if (fullFilePath.StartsWith(currentDirectory + fileSystem.Execute.Path.DirectorySeparatorChar,
fileSystem.Execute.StringComparisonMode))
{
fullFilePath = fullFilePath.Substring(currentDirectory.Length + 1);
@@ -76,11 +75,11 @@ internal static string GetSubdirectoryPath(this MockFileSystem fileSystem,
{
string? parentName = currentDirectory;
while (parentName != null &&
!fullFilePath.StartsWith(parentName + Path.DirectorySeparatorChar,
!fullFilePath.StartsWith(parentName + fileSystem.Execute.Path.DirectorySeparatorChar,
fileSystem.Execute.StringComparisonMode))
{
parentName = Path.GetDirectoryName(parentName);
int lastIndex = givenPath.LastIndexOf(Path.DirectorySeparatorChar);
parentName = fileSystem.Execute.Path.GetDirectoryName(parentName);
int lastIndex = givenPath.LastIndexOf(fileSystem.Execute.Path.DirectorySeparatorChar);
if (lastIndex >= 0)
{
givenPath = givenPath.Substring(0, lastIndex);
2 changes: 1 addition & 1 deletion Source/Testably.Abstractions.Testing/Helpers/PathHelper.cs
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ internal static string GetFullPathOrWhiteSpace(this string? path, IFileSystem fi
return path ?? string.Empty;
}

return fileSystem.Path.GetFullPath(path);
return fileSystem.ExecuteOrDefault().Path.GetFullPath(path);
}

/// <summary>
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Testably.Abstractions.Testing.Helpers;
@@ -28,13 +27,13 @@ public static AdjustedLocation AdjustLocationFromSearchPattern(
StringBuilder givenPathPrefix = new();

while (searchPattern.StartsWith(
".." + Path.DirectorySeparatorChar, StringComparison.Ordinal) ||
".." + fileSystem.Execute.Path.DirectorySeparatorChar, StringComparison.Ordinal) ||
searchPattern.StartsWith(
".." + Path.AltDirectorySeparatorChar, StringComparison.Ordinal))
".." + fileSystem.Execute.Path.AltDirectorySeparatorChar, StringComparison.Ordinal))
{
fileSystem.Execute.OnNetFramework(
() => throw ExceptionFactory.SearchPatternCannotContainTwoDots());
parentDirectories.Push(Path.GetFileName(location.FullPath));
parentDirectories.Push(fileSystem.Execute.Path.GetFileName(location.FullPath));
location = location.GetParent() ??
throw new UnauthorizedAccessException(
$"The searchPattern '{searchPattern}' has too many '../' for path '{path}'");
@@ -47,10 +46,10 @@ public static AdjustedLocation AdjustLocationFromSearchPattern(
if (parentDirectories.Any())
{
givenPathPrefix.Length--;
givenPath = Path.Combine(
givenPath = fileSystem.Execute.Path.Combine(
givenPath,
givenPathPrefix.ToString(),
Path.Combine(parentDirectories.ToArray()));
fileSystem.Execute.Path.Combine(parentDirectories.ToArray()));
}
}

Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ public void
{
directoryName = directoryName.ToLowerInvariant();
FileSystem.Directory.CreateDirectory(directoryName.ToUpperInvariant());
string expectedPath = System.IO.Path.Combine(BasePath, directoryName);
string expectedPath = FileSystem.Path.Combine(BasePath, directoryName);
Exception? exception = Record.Exception(() =>
{
FileSystem.Directory.Delete(directoryName);
@@ -51,7 +51,7 @@ public void Delete_FullPath_ShouldDeleteDirectory(string directoryName)
public void Delete_MissingDirectory_ShouldThrowDirectoryNotFoundException(
string directoryName)
{
string expectedPath = System.IO.Path.Combine(BasePath, directoryName);
string expectedPath = FileSystem.Path.Combine(BasePath, directoryName);
Exception? exception = Record.Exception(() =>
{
FileSystem.Directory.Delete(directoryName);
@@ -66,7 +66,7 @@ public void Delete_MissingDirectory_ShouldThrowDirectoryNotFoundException(
public void Delete_Recursive_MissingDirectory_ShouldThrowDirectoryNotFoundException(
string directoryName)
{
string expectedPath = System.IO.Path.Combine(BasePath, directoryName);
string expectedPath = FileSystem.Path.Combine(BasePath, directoryName);
Exception? exception = Record.Exception(() =>
{
FileSystem.Directory.Delete(directoryName, true);
@@ -248,7 +248,7 @@ public void Delete_WithSubdirectory_ShouldThrowIOException_AndNotDeleteDirectory
// Path information only included in exception message on Windows and not in .NET Framework
messageContains: !Test.RunsOnWindows || Test.IsNetFramework
? null
: $"'{System.IO.Path.Combine(BasePath, path)}'");
: $"'{FileSystem.Path.Combine(BasePath, path)}'");
FileSystem.Should().HaveDirectory(path);
}
}
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ public void
EnumerateDirectories_MissingDirectory_ShouldThrowDirectoryNotFoundException(
string path)
{
string expectedPath = System.IO.Path.Combine(BasePath, path);
string expectedPath = FileSystem.Path.Combine(BasePath, path);
Exception? exception =
Record.Exception(()
=> FileSystem.Directory.EnumerateDirectories(path).ToList());
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ public void
EnumerateFileSystemEntries_MissingDirectory_ShouldThrowDirectoryNotFoundException(
string path)
{
string expectedPath = System.IO.Path.Combine(BasePath, path);
string expectedPath = FileSystem.Path.Combine(BasePath, path);
Exception? exception =
Record.Exception(()
=> FileSystem.Directory.EnumerateFileSystemEntries(path).ToList());
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ public void
EnumerateFiles_MissingDirectory_ShouldThrowDirectoryNotFoundException(
string path)
{
string expectedPath = System.IO.Path.Combine(BasePath, path);
string expectedPath = FileSystem.Path.Combine(BasePath, path);
Exception? exception =
Record.Exception(()
=> FileSystem.Directory.EnumerateFiles(path).ToList());
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ public void
GetDirectories_MissingDirectory_ShouldThrowDirectoryNotFoundException(
string path)
{
string expectedPath = System.IO.Path.Combine(BasePath, path);
string expectedPath = FileSystem.Path.Combine(BasePath, path);
Exception? exception =
Record.Exception(()
=> FileSystem.Directory.GetDirectories(path).ToList());
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ public void
GetFileSystemEntries_MissingDirectory_ShouldThrowDirectoryNotFoundException(
string path)
{
string expectedPath = System.IO.Path.Combine(BasePath, path);
string expectedPath = FileSystem.Path.Combine(BasePath, path);
Exception? exception =
Record.Exception(()
=> FileSystem.Directory.GetFileSystemEntries(path).ToList());
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ public void
GetFiles_MissingDirectory_ShouldThrowDirectoryNotFoundException(
string path)
{
string expectedPath = System.IO.Path.Combine(BasePath, path);
string expectedPath = FileSystem.Path.Combine(BasePath, path);
Exception? exception =
Record.Exception(()
=> FileSystem.Directory.GetFiles(path).ToList());
@@ -266,17 +266,17 @@ public void
public void GetFiles_WithRelativePathAndSubfolders_ShouldReturnRelativeFilePath(
string subfolder1, string subfolder2, string[] files)
{
string workingDirectory = System.IO.Path.Combine(BasePath, subfolder1, subfolder2);
string workingDirectory = FileSystem.Path.Combine(BasePath, subfolder1, subfolder2);
FileSystem.Directory.CreateDirectory(workingDirectory);
foreach (string file in files)
{
FileSystem.File.Create(System.IO.Path.Combine(workingDirectory, file));
FileSystem.File.Create(FileSystem.Path.Combine(workingDirectory, file));
}

FileSystem.Directory.SetCurrentDirectory(subfolder1);
IEnumerable<string> expectation =
files.Select(f => System.IO.Path.Combine("..", subfolder1, subfolder2, f));
string path = System.IO.Path.Combine("..", subfolder1, subfolder2);
files.Select(f => FileSystem.Path.Combine("..", subfolder1, subfolder2, f));
string path = FileSystem.Path.Combine("..", subfolder1, subfolder2);

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

Original file line number Diff line number Diff line change
@@ -78,7 +78,7 @@ public void ResolveLinkTarget_FinalTarget_ShouldFollowSymbolicLinkToFinalTarget(
{
string newPath = $"{path}-{i}";
FileSystem.Directory.CreateSymbolicLink(newPath,
System.IO.Path.Combine(BasePath, previousPath));
FileSystem.Path.Combine(BasePath, previousPath));
previousPath = newPath;
}

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

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

FileSystem.Directory.CreateDirectory(pathToFinalTarget);
FileSystem.Directory.CreateSymbolicLink(pathToMissingDirectory,
System.IO.Path.Combine(BasePath, pathToFinalTarget));
FileSystem.Path.Combine(BasePath, pathToFinalTarget));
FileSystem.Directory.CreateSymbolicLink(path,
System.IO.Path.Combine(BasePath, pathToMissingDirectory));
FileSystem.Path.Combine(BasePath, pathToMissingDirectory));
FileSystem.Directory.Delete(pathToMissingDirectory);

IFileSystemInfo? target =
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ public void
.GetFileSystemEntries(".", searchPattern, SearchOption.AllDirectories);

result.Length.Should().Be(expectedMatchingFiles);
result.Should().Contain(System.IO.Path.Combine(".", "..", "xyz", "a.test"));
result.Should().Contain(FileSystem.Path.Combine(".", "..", "xyz", "a.test"));
}

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

result.Should()
.Contain(System.IO.Path.Combine(".", "../..", "bar", "xyz", "a.test"));
.Contain(FileSystem.Path.Combine(".", "../..", "bar", "xyz", "a.test"));
}

[SkippableTheory]
@@ -84,16 +84,16 @@ public void
result.Length.Should().Be(expectedMatchingFiles);
if (!searchPattern.EndsWith("a*", StringComparison.Ordinal))
{
result.Should().Contain(System.IO.Path.Combine(".", "../../..", "foo"));
result.Should().Contain(FileSystem.Path.Combine(".", "../../..", "foo"));
result.Should()
.Contain(System.IO.Path.Combine(".", "../../..", "foo", "bar"));
.Contain(FileSystem.Path.Combine(".", "../../..", "foo", "bar"));
result.Should()
.Contain(System.IO.Path.Combine(".", "../../..", "foo", "bar", "xyz"));
.Contain(FileSystem.Path.Combine(".", "../../..", "foo", "bar", "xyz"));
}

result.Should()
.Contain(
System.IO.Path.Combine(".", "../../..", "foo", "bar", "xyz", "a.test"));
FileSystem.Path.Combine(".", "../../..", "foo", "bar", "xyz", "a.test"));
}

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

result.Length.Should().Be(2);
result.Should().Contain(System.IO.Path.Combine(".", "a.test"));
result.Should().Contain(System.IO.Path.Combine(".", "another.test"));
result.Should().Contain(FileSystem.Path.Combine(".", "a.test"));
result.Should().Contain(FileSystem.Path.Combine(".", "another.test"));
}

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

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

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

result.Length.Should().Be(expectedMatchingFiles);
result.Should().Contain(System.IO.Path.Combine(".", "..", path, "a.test"));
result.Should().Contain(FileSystem.Path.Combine(".", "..", path, "a.test"));
}

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

@@ -291,12 +291,12 @@ public void SearchPattern_StarDot_ShouldReturnFilesWithoutExtension()
if (Test.RunsOnWindows)
{
result.Length.Should().Be(1);
result.Should().Contain(System.IO.Path.Combine(".", "test"));
result.Should().Contain(FileSystem.Path.Combine(".", "test"));
}
else
{
result.Length.Should().Be(3);
result.Should().Contain(System.IO.Path.Combine(".", "test."));
result.Should().Contain(FileSystem.Path.Combine(".", "test."));
}
}

Original file line number Diff line number Diff line change
@@ -131,7 +131,7 @@ public void Create_TrailingDirectorySeparator_ShouldNotBeTrimmed(
result.Name.Should().Be(expectedName.TrimEnd(
FileSystem.Path.DirectorySeparatorChar,
FileSystem.Path.AltDirectorySeparatorChar));
result.FullName.Should().Be(System.IO.Path.Combine(BasePath, expectedName
result.FullName.Should().Be(FileSystem.Path.Combine(BasePath, expectedName
.Replace(FileSystem.Path.AltDirectorySeparatorChar,
FileSystem.Path.DirectorySeparatorChar)));
FileSystem.Should().HaveDirectory(nameWithSuffix);
Original file line number Diff line number Diff line change
@@ -15,10 +15,15 @@ public void New_NullCharacter_ShouldThrowArgumentException(string path)
#else
string expectedMessage = "Illegal characters in path.";
#endif
Exception? exception =
Record.Exception(() => FileSystem.DirectoryInfo.New(path));
Exception? exception = Record.Exception(() =>
{
_ = FileSystem.DirectoryInfo.New(path);
});

exception.Should().BeException<ArgumentException>(expectedMessage,
#if !NETFRAMEWORK
paramName: "path",
#endif
hResult: -2147024809);
}

Original file line number Diff line number Diff line change
@@ -100,7 +100,7 @@ public void ResolveLinkTarget_FinalTarget_ShouldFollowSymbolicLinkToFinalTarget(
{
string newPath = $"{path}-{i}";
FileSystem.File.CreateSymbolicLink(newPath,
System.IO.Path.Combine(BasePath, previousPath));
FileSystem.Path.Combine(BasePath, previousPath));
previousPath = newPath;
}

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

@@ -142,9 +142,9 @@ public void ResolveLinkTarget_MissingFileAtBeginningOfLinkChain_ShouldReturnPath
{
FileSystem.File.WriteAllText(pathToFinalTarget, null);
FileSystem.File.CreateSymbolicLink(pathToMissingFile,
System.IO.Path.Combine(BasePath, pathToFinalTarget));
FileSystem.Path.Combine(BasePath, pathToFinalTarget));
FileSystem.File.CreateSymbolicLink(path,
System.IO.Path.Combine(BasePath, pathToMissingFile));
FileSystem.Path.Combine(BasePath, pathToMissingFile));
FileSystem.File.Delete(pathToMissingFile);

IFileSystemInfo? target =
@@ -163,11 +163,11 @@ public void ResolveLinkTarget_MissingFileInLinkChain_ShouldReturnPathToMissingFi
{
FileSystem.File.WriteAllText(pathToFinalTarget, null);
FileSystem.File.CreateSymbolicLink(pathToMissingFile,
System.IO.Path.Combine(BasePath, pathToFinalTarget));
FileSystem.Path.Combine(BasePath, pathToFinalTarget));
FileSystem.File.CreateSymbolicLink(pathToIntermediateTarget,
System.IO.Path.Combine(BasePath, pathToMissingFile));
FileSystem.Path.Combine(BasePath, pathToMissingFile));
FileSystem.File.CreateSymbolicLink(path,
System.IO.Path.Combine(BasePath, pathToIntermediateTarget));
FileSystem.Path.Combine(BasePath, pathToIntermediateTarget));
FileSystem.File.Delete(pathToMissingFile);

IFileSystemInfo? target =
Loading
Loading