Skip to content

Commit

Permalink
Added FileSystemInfoComparer and tests (#11)
Browse files Browse the repository at this point in the history
* Added FileSystemInfoComparer and tests

* Rename MockFileSystemTests

* DeleteDirectory should call GetDirectories to get children

---------

Co-authored-by: Brad White <bradselw@pm.me>
  • Loading branch information
bradselw and Brad White committed May 23, 2023
1 parent 049b34b commit 77e82e1
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 103 deletions.
42 changes: 42 additions & 0 deletions src/FileSystem/Comparers/FileSystemInfoComparer.cs
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.");
}
}
}
}
16 changes: 14 additions & 2 deletions src/FileSystem/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace DevOptimal.SystemUtilities.FileSystem
Expand All @@ -21,7 +22,18 @@ public class MockFileSystem : IFileSystem

public MockFileSystem()
{
data = new ConcurrentDictionary<string, byte[]>(StringComparer.OrdinalIgnoreCase);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
data = new ConcurrentDictionary<string, byte[]>(StringComparer.OrdinalIgnoreCase);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
data = new ConcurrentDictionary<string, byte[]>(StringComparer.Ordinal);
}
else
{
throw new NotSupportedException($"The operating system '{RuntimeInformation.OSDescription}' is not supported.");
}
}

public void CopyFile(string sourcePath, string destinationPath, bool overwrite)
Expand Down Expand Up @@ -81,7 +93,7 @@ public void DeleteDirectory(string path, bool recursive)
throw new IOException("A file with the same name and location specified by path exists.");
}

var children = data.Keys.Where(p => Path.GetDirectoryName(p).Equals(path, StringComparison.OrdinalIgnoreCase));
var children = GetDirectories(path, "*", SearchOption.TopDirectoryOnly);

if (children.Any())
{
Expand Down
60 changes: 60 additions & 0 deletions test/FileSystem.Tests/FileSystemInfoComparerTests.cs
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);
}
}
}
92 changes: 0 additions & 92 deletions test/FileSystem.Tests/FileTests.cs

This file was deleted.

0 comments on commit 77e82e1

Please sign in to comment.