-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added FileSystemInfoComparer and tests (#11)
* Added FileSystemInfoComparer and tests * Rename MockFileSystemTests * DeleteDirectory should call GetDirectories to get children --------- Co-authored-by: Brad White <bradselw@pm.me>
- Loading branch information
Showing
5 changed files
with
213 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace DevOptimal.SystemUtilities.FileSystem.Comparers | ||
{ | ||
public class FileSystemInfoComparer : IEqualityComparer<FileSystemInfo> | ||
{ | ||
public bool Equals(FileSystemInfo x, FileSystemInfo y) | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
return x.FullName.Equals(y.FullName, StringComparison.OrdinalIgnoreCase); | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
{ | ||
return x.FullName.Equals(y.FullName, StringComparison.Ordinal); | ||
} | ||
else | ||
{ | ||
throw new NotSupportedException($"The operating system '{RuntimeInformation.OSDescription}' is not supported."); | ||
} | ||
} | ||
|
||
public int GetHashCode(FileSystemInfo obj) | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
return obj.FullName.ToLower().GetHashCode(); | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
{ | ||
return obj.FullName.GetHashCode(); | ||
} | ||
else | ||
{ | ||
throw new NotSupportedException($"The operating system '{RuntimeInformation.OSDescription}' is not supported."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using DevOptimal.SystemUtilities.FileSystem.Comparers; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace DevOptimal.SystemUtilities.FileSystem.Tests | ||
{ | ||
[TestClass] | ||
public class FileSystemInfoComparerTests | ||
{ | ||
[TestMethod] | ||
public void CorrectlyComparesFileInfos() | ||
{ | ||
var files = new HashSet<FileInfo>(new FileSystemInfoComparer()) | ||
{ | ||
new FileInfo(@"C:\temp\foo.txt"), | ||
new FileInfo(@"C:\temp\FOO.txt") | ||
}; | ||
Assert.AreEqual(1, files.Count); | ||
|
||
files.Add(new FileInfo(@"C:\temp\bar.txt")); | ||
Assert.AreEqual(2, files.Count); | ||
} | ||
|
||
[TestMethod] | ||
public void CorrectlyComparesDirectoryInfos() | ||
{ | ||
var directories = new HashSet<DirectoryInfo>(new FileSystemInfoComparer()) | ||
{ | ||
new DirectoryInfo(@"C:\temp\foo"), | ||
new DirectoryInfo(@"C:\temp\FOO") | ||
}; | ||
Assert.AreEqual(1, directories.Count); | ||
|
||
directories.Add(new DirectoryInfo(@"C:\temp\bar")); | ||
Assert.AreEqual(2, directories.Count); | ||
} | ||
|
||
[TestMethod] | ||
public void CorrectlyHandlesParentDirectoryReferences() | ||
{ | ||
var files = new HashSet<FileInfo>(new FileSystemInfoComparer()) | ||
{ | ||
new FileInfo(@"C:\temp\bar\..\foo.txt"), | ||
new FileInfo(@"C:\temp\foo.txt") | ||
}; | ||
Assert.AreEqual(1, files.Count); | ||
} | ||
|
||
[TestMethod] | ||
public void CorrectlyHandlesCurrentDirectoryReferences() | ||
{ | ||
var files = new HashSet<FileInfo>(new FileSystemInfoComparer()) | ||
{ | ||
new FileInfo(@"C:\temp\\.\foo.txt"), | ||
new FileInfo(@"C:\temp\foo.txt") | ||
}; | ||
Assert.AreEqual(1, files.Count); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.