Skip to content

Commit

Permalink
Added: Tests for the new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Apr 10, 2024
1 parent 7343974 commit 4d13715
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/NexusMods.Paths/Utilities/FileSystemMarshal.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Diagnostics.CodeAnalysis;

namespace NexusMods.Paths.Utilities;

/// <summary>
/// This is a suite of low level methods that provide 'escape hatches' for the
/// file system abstraction where lower level access is required.
/// This is a suite of low level methods that provide 'escape hatches' and
/// other utilities for the file system abstraction where lower level access
/// is required.
/// </summary>
/// <remarks>
/// This is named after classes like
Expand All @@ -31,7 +30,7 @@ public static class FileSystemMarshal
/// that works with the <see cref="IFileSystem"/> abstraction, or throw
/// an exception.
/// </remarks>
public static bool TryResolveRealFilesystemPath(AbsolutePath path, [MaybeNullWhen(false)] out string? fullFilePath)
public static bool TryResolveRealFilesystemPath(AbsolutePath path, out string? fullFilePath)
{
// Only paths from the Real 'FileSystem' can be resolved here.
if (path.FileSystem is not FileSystem realFs)
Expand Down
57 changes: 57 additions & 0 deletions tests/NexusMods.Paths.Tests/FileSystem/BaseFileSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Runtime.InteropServices;
using FluentAssertions;
using NexusMods.Paths.TestingHelpers;
using NexusMods.Paths.Utilities;

namespace NexusMods.Paths.Tests.FileSystem;

Expand Down Expand Up @@ -251,4 +252,60 @@ public void Test_KnownPath(string os, KnownPath knownPath, bool expected)
if (expected) act.Should().NotThrow();
else act.Should().Throw<Exception>();
}

[Fact]
public void TryResolveRealFilesystemPath_WithRealFileSystem_ReturnsTrue()
{
// Arrange
var fs = new Paths.FileSystem();
using var tempManager = new TemporaryFileManager(fs);
using var tempFile = tempManager.CreateFile();

// Act
var result = FileSystemMarshal.TryResolveRealFilesystemPath(tempFile, out var resolvedPath);

// Assert
result.Should().BeTrue();
resolvedPath.Should().Be(tempFile.Path.GetFullPath());
}

[Fact]
public void TryResolveRealFilesystemPath_WithInMemoryFileSystem_ReturnsFalse()
{
// Arrange
var fs = new InMemoryFileSystem();
using var tempManager = new TemporaryFileManager(fs);
using var tempFile = tempManager.CreateFile();

// Act
var result = FileSystemMarshal.TryResolveRealFilesystemPath(tempFile, out var resolvedPath);

// Assert
result.Should().BeFalse();
}

[Fact]
public void TryResolveRealFilesystemPath_WithMappedPath_ResolvesCorrectly()
{
// Arrange
var fs = new Paths.FileSystem();
using var tempManager = new TemporaryFileManager(fs);
using var tempFile1 = tempManager.CreateFile();
using var tempFile2 = tempManager.CreateFile();
var originalPath = tempFile1.Path;
var mappedPath = tempFile2.Path;

var overlayFileSystem = fs.CreateOverlayFileSystem(new Dictionary<AbsolutePath, AbsolutePath>
{
{ originalPath, mappedPath }
}, new Dictionary<KnownPath, AbsolutePath>());

// Act
var source = overlayFileSystem.FromUnsanitizedFullPath(originalPath.GetFullPath());
var result = FileSystemMarshal.TryResolveRealFilesystemPath(source, out var resolvedPath);

// Assert
result.Should().BeTrue();
resolvedPath.Should().Be(mappedPath.GetFullPath());
}
}
4 changes: 4 additions & 0 deletions tests/NexusMods.Paths.Tests/NexusMods.Paths.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Utilities\" />
</ItemGroup>
</Project>

0 comments on commit 4d13715

Please sign in to comment.