Fixes file system domain unit tests failing on UNIX systems

This commit is contained in:
BinaryAlley committed 2024-10-06 17:20:26 +03:00
1 parent 45e9911363
commit 90e3237ec4
5 files changed
+482 -323

No files matched your search

@@ -3,6 +3,8 @@ using Lumina.Presentation.Web.Common.Enums.FileSystem;
using Lumina.Presentation.Web.Common.Models.Configuration; using Lumina.Presentation.Web.Common.Models.Configuration;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Runtime.InteropServices;
#endregion #endregion
namespace Lumina.Presentation.Web.ViewComponents; namespace Lumina.Presentation.Web.ViewComponents;
@@ -38,7 +40,7 @@ public class FileSystemBrowserViewComponent : ViewComponent
{ {
return View(new return View(new
{ {
Path = @"C:\", Path = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? @"/" : @"C:\",
ServerBasePath = $"{_serverConfigurationModel.BaseAddress}:{_serverConfigurationModel.Port}/api/v{_serverConfigurationModel.ApiVersion}/", ServerBasePath = $"{_serverConfigurationModel.BaseAddress}:{_serverConfigurationModel.Port}/api/v{_serverConfigurationModel.ApiVersion}/",
ClientBasePath = "http://localhost:5012/", // TODO: take from appsettings or environment ClientBasePath = "http://localhost:5012/", // TODO: take from appsettings or environment
ViewMode = FileSystemViewMode.List, ViewMode = FileSystemViewMode.List,
@@ -17,6 +17,9 @@ using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.IO.Abstractions; using System.IO.Abstractions;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
#endregion #endregion
namespace Lumina.Domain.UnitTests.Core.Aggregates.FileManagementAggregate.Services; namespace Lumina.Domain.UnitTests.Core.Aggregates.FileManagementAggregate.Services;
@@ -33,6 +36,29 @@ public class DirectoryProviderServiceTests
private readonly IFileSystemPermissionsService _mockFileSystemPermissionsService; private readonly IFileSystemPermissionsService _mockFileSystemPermissionsService;
private readonly DirectoryProviderService _sut; private readonly DirectoryProviderService _sut;
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture; private readonly FileSystemPathIdFixture _fileSystemPathIdFixture;
private static readonly bool s_isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
private readonly string _pathSource = s_isLinux ? "/Source" : @"C:\Source";
private readonly string _pathSourceSubDir = s_isLinux ? "/Source/SubDir" : @"C:\Source\SubDir";
private readonly string _pathSourceSubDir1 = s_isLinux ? "/Source/SubDir1" : @"C:\Source\SubDir1";
private readonly string _pathSourceSubDir2 = s_isLinux ? "/Source/SubDir2" : @"C:\Source\SubDir2";
private readonly string _pathSourceFile1 = s_isLinux ? "/Source/file1.txt" : @"C:\Source\file1.txt";
private readonly string _pathSourceFile2 = s_isLinux ? "/Source/file2.txt" : @"C:\Source\file2.txt";
private readonly string _pathDestinationFile1 = s_isLinux ? "/Destination/file1.txt" : @"C:\Destination\file1.txt";
private readonly string _pathDestinationFile2 = s_isLinux ? "/Destination/file2.txt" : @"C:\Destination\file2.txt";
private readonly string _pathDestination = s_isLinux ? "/Destination" : @"C:\Destination";
private readonly string _pathDestinationSubDir = s_isLinux ? "/Destination/SubDir" : @"C:\Destination\SubDir";
private readonly string _pathDestinationSubDir1 = s_isLinux ? "/Destination/SubDir1" : @"C:\Destination\SubDir1";
private readonly string _pathDestinationSubDir2 = s_isLinux ? "/Destination/SubDir2" : @"C:\Destination\SubDir2";
private readonly string _pathDestinationFile = s_isLinux ? "/Destination/file.txt" : @"C:\Destination\file.txt";
private readonly string _pathVisible1 = s_isLinux ? "/Visible" : @"C:\Visible";
private readonly string _pathHidden1 = s_isLinux ? "/Hidden" : @"C:\Hidden";
private readonly string _pathVisible2 = s_isLinux ? "/Visible/" : @"C:\Visible\";
private readonly string _pathHidden2 = s_isLinux ? "/Hidden/" : @"C:\Hidden\";
private readonly string _pathValid1 = s_isLinux ? "/Valid" : @"C:\Valid";
private readonly string _pathInvalid1 = s_isLinux ? "/Invalid" : @"C:\Invalid";
private readonly string _pathValid2 = s_isLinux ? "/Valid/" : @"C:\Valid\";
private readonly string _pathInvalid2 = s_isLinux ? "/Invalid/" : @"C:\Invalid\";
private readonly char _dirSeparator = s_isLinux ? '/' : '\\';
#endregion #endregion
#region ====================================================================== CTOR ===================================================================================== #region ====================================================================== CTOR =====================================================================================
@@ -73,7 +99,7 @@ public class DirectoryProviderServiceTests
string[] subdirectories = _fixture.CreateMany<string>(3).ToArray(); string[] subdirectories = _fixture.CreateMany<string>(3).ToArray();
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory, false).Returns(true); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory, false).Returns(true);
_mockFileSystem.Directory.GetDirectories(path.Path).Returns(subdirectories); _mockFileSystem.Directory.GetDirectories(path.Path).Returns(subdirectories);
_mockFileSystem.Path.DirectorySeparatorChar.Returns('\\'); _mockFileSystem.Path.DirectorySeparatorChar.Returns(_dirSeparator);
// Act // Act
ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetSubdirectoryPaths(path, false); ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetSubdirectoryPaths(path, false);
@@ -81,7 +107,7 @@ public class DirectoryProviderServiceTests
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Should().HaveCount(3); result.Value.Should().HaveCount(3);
result.Value.Select(x => x.Path).Should().BeEquivalentTo(subdirectories.Select(x => x + '\\')); result.Value.Select(x => x.Path).Should().BeEquivalentTo(subdirectories.Select(x => x + _dirSeparator));
} }
[Fact] [Fact]
@@ -89,12 +115,12 @@ public class DirectoryProviderServiceTests
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId();
string[] subdirectories = [@"C:\Visible", @"C:\Hidden"]; string[] subdirectories = [_pathVisible1, _pathHidden1];
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory, false).Returns(true); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory, false).Returns(true);
_mockFileSystem.Directory.GetDirectories(path.Path).Returns(subdirectories); _mockFileSystem.Directory.GetDirectories(path.Path).Returns(subdirectories);
_mockFileSystem.Path.DirectorySeparatorChar.Returns('\\'); _mockFileSystem.Path.DirectorySeparatorChar.Returns(_dirSeparator);
_mockFileSystem.File.GetAttributes(@"C:\Visible").Returns(FileAttributes.Directory); _mockFileSystem.File.GetAttributes(_pathVisible1).Returns(FileAttributes.Directory);
_mockFileSystem.File.GetAttributes(@"C:\Hidden").Returns(FileAttributes.Directory | FileAttributes.Hidden); _mockFileSystem.File.GetAttributes(_pathHidden1).Returns(FileAttributes.Directory | FileAttributes.Hidden);
// Act // Act
ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetSubdirectoryPaths(path, false); ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetSubdirectoryPaths(path, false);
@@ -102,7 +128,7 @@ public class DirectoryProviderServiceTests
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Should().HaveCount(1); result.Value.Should().HaveCount(1);
result.Value.First().Path.Should().Be(@"C:\Visible\"); result.Value.First().Path.Should().Be(_pathVisible2);
} }
[Fact] [Fact]
@@ -110,12 +136,12 @@ public class DirectoryProviderServiceTests
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId();
string[] subdirectories = [@"C:\Visible", @"C:\Hidden"]; string[] subdirectories = [_pathVisible1, _pathHidden1];
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory, false).Returns(true); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory, false).Returns(true);
_mockFileSystem.Directory.GetDirectories(path.Path).Returns(subdirectories); _mockFileSystem.Directory.GetDirectories(path.Path).Returns(subdirectories);
_mockFileSystem.Path.DirectorySeparatorChar.Returns('\\'); _mockFileSystem.Path.DirectorySeparatorChar.Returns(_dirSeparator);
_mockFileSystem.File.GetAttributes(@"C:\Visible").Returns(FileAttributes.Directory); _mockFileSystem.File.GetAttributes(_pathVisible1).Returns(FileAttributes.Directory);
_mockFileSystem.File.GetAttributes(@"C:\Hidden").Returns(FileAttributes.Directory | FileAttributes.Hidden); _mockFileSystem.File.GetAttributes(_pathHidden1).Returns(FileAttributes.Directory | FileAttributes.Hidden);
// Act // Act
ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetSubdirectoryPaths(path, true); ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetSubdirectoryPaths(path, true);
@@ -123,7 +149,7 @@ public class DirectoryProviderServiceTests
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Should().HaveCount(2); result.Value.Should().HaveCount(2);
result.Value.Select(x => x.Path).Should().BeEquivalentTo([@"C:\Visible\", @"C:\Hidden\"]); result.Value.Select(x => x.Path).Should().BeEquivalentTo([_pathVisible2, _pathHidden2]);
} }
[Fact] [Fact]
@@ -131,12 +157,12 @@ public class DirectoryProviderServiceTests
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId();
string[] subdirectories = [@"C:\Valid", @"C:\Invalid"]; string[] subdirectories = [_pathValid1, _pathInvalid1];
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory, false).Returns(true); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory, false).Returns(true);
_mockFileSystem.Directory.GetDirectories(path.Path).Returns(subdirectories); _mockFileSystem.Directory.GetDirectories(path.Path).Returns(subdirectories);
_mockFileSystem.Path.DirectorySeparatorChar.Returns('\\'); _mockFileSystem.Path.DirectorySeparatorChar.Returns(_dirSeparator);
_mockFileSystem.File.GetAttributes(@"C:\Valid").Returns(FileAttributes.Directory); _mockFileSystem.File.GetAttributes(_pathValid1).Returns(FileAttributes.Directory);
_mockFileSystem.File.GetAttributes(@"C:\Invalid").Throws(new Exception("Access denied")); _mockFileSystem.File.GetAttributes(_pathInvalid1).Throws(new Exception("Access denied"));
// Act // Act
ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetSubdirectoryPaths(path, false); ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetSubdirectoryPaths(path, false);
@@ -144,8 +170,8 @@ public class DirectoryProviderServiceTests
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Should().HaveCount(2); result.Value.Should().HaveCount(2);
result.Value.First().Path.Should().Be(@"C:\Invalid\"); result.Value.First().Path.Should().Be(_pathInvalid2);
result.Value.Last().Path.Should().Be(@"C:\Valid\"); result.Value.Last().Path.Should().Be(_pathValid2);
} }
[Fact] [Fact]
@@ -153,18 +179,23 @@ public class DirectoryProviderServiceTests
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId();
string[] subdirectories = [@"C:\C", @"C:\A", @"C:\B"]; string[] subdirectories = s_isLinux
? new[] { "/C", "/A", "/B" }
: new[] { @"C:\C", @"C:\A", @"C:\B" };
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory, false).Returns(true); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory, false).Returns(true);
_mockFileSystem.Directory.GetDirectories(path.Path).Returns(subdirectories); _mockFileSystem.Directory.GetDirectories(path.Path).Returns(subdirectories);
_mockFileSystem.Path.DirectorySeparatorChar.Returns('\\'); _mockFileSystem.Path.DirectorySeparatorChar.Returns(_dirSeparator);
_mockFileSystem.File.GetAttributes(Arg.Any<string>()).Returns(FileAttributes.Directory); _mockFileSystem.File.GetAttributes(Arg.Any<string>()).Returns(FileAttributes.Directory);
string[] expectedPaths = s_isLinux
? new[] { "/A/", "/B/", "/C/" }
: new[] { @"C:\A\", @"C:\B\", @"C:\C\" };
// Act // Act
ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetSubdirectoryPaths(path, false); ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetSubdirectoryPaths(path, false);
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Select(x => x.Path).Should().BeEquivalentTo([@"C:\A\", @"C:\B\", @"C:\C\"], options => options.WithStrictOrdering()); result.Value.Select(x => x.Path).Should().BeEquivalentTo(expectedPaths, options => options.WithStrictOrdering());
} }
[Fact] [Fact]
@@ -217,7 +248,9 @@ public class DirectoryProviderServiceTests
public void GetFileName_WhenPathIsValid_ShouldReturnFileName() public void GetFileName_WhenPathIsValid_ShouldReturnFileName()
{ {
// Arrange // Arrange
string fullPath = @"C:\folder\subfolder\file.txt"; string fullPath = s_isLinux
? "/folder/subfolder/file.txt"
: @"C:\folder\subfolder\file.txt";
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(fullPath); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(fullPath);
_mockFileSystem.Path.GetFileName(fullPath).Returns("file.txt"); _mockFileSystem.Path.GetFileName(fullPath).Returns("file.txt");
@@ -233,10 +266,12 @@ public class DirectoryProviderServiceTests
public void GetFileName_WhenPathEndsWithDirectorySeparator_ShouldReturnLastSegment() public void GetFileName_WhenPathEndsWithDirectorySeparator_ShouldReturnLastSegment()
{ {
// Arrange // Arrange
string fullPath = @"C:\folder\subfolder\"; string fullPath = s_isLinux
? "/folder/subfolder/"
: @"C:\folder\subfolder\";
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(fullPath); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(fullPath);
_mockFileSystem.Path.GetFileName(fullPath[..^1]).Returns("subfolder"); _mockFileSystem.Path.GetFileName(fullPath[..^1]).Returns("subfolder");
_mockFileSystem.Path.DirectorySeparatorChar.Returns('\\'); _mockFileSystem.Path.DirectorySeparatorChar.Returns(_dirSeparator);
// Act // Act
ErrorOr<string> result = _sut.GetFileName(path); ErrorOr<string> result = _sut.GetFileName(path);
@@ -250,10 +285,12 @@ public class DirectoryProviderServiceTests
public void GetFileName_WhenPathIsRoot_ShouldReturnEmptyString() public void GetFileName_WhenPathIsRoot_ShouldReturnEmptyString()
{ {
// Arrange // Arrange
string fullPath = @"C:\"; string fullPath = s_isLinux
? "/C/"
: @"/";
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(fullPath); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(fullPath);
_mockFileSystem.Path.GetFileName(fullPath[..^1]).Returns(string.Empty); _mockFileSystem.Path.GetFileName(fullPath[..^1]).Returns(string.Empty);
_mockFileSystem.Path.DirectorySeparatorChar.Returns('\\'); _mockFileSystem.Path.DirectorySeparatorChar.Returns(_dirSeparator);
// Act // Act
ErrorOr<string> result = _sut.GetFileName(path); ErrorOr<string> result = _sut.GetFileName(path);
@@ -335,7 +372,9 @@ public class DirectoryProviderServiceTests
// Arrange // Arrange
FileSystemPathId parentPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Parent"); FileSystemPathId parentPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Parent");
string directoryName = "NewDirectory"; string directoryName = "NewDirectory";
string fullPath = @"C:\Parent\NewDirectory"; string fullPath = s_isLinux
? "/Parent/NewDirectory"
: @"C:\Parent\NewDirectory";
_mockFileSystemPermissionsService.CanAccessPath(parentPath, FileAccessMode.Write, false).Returns(true); _mockFileSystemPermissionsService.CanAccessPath(parentPath, FileAccessMode.Write, false).Returns(true);
_mockFileSystem.Path.Combine(parentPath.Path, directoryName).Returns(fullPath); _mockFileSystem.Path.Combine(parentPath.Path, directoryName).Returns(fullPath);
@@ -410,8 +449,8 @@ public class DirectoryProviderServiceTests
public void CopyDirectory_WhenSourceDirectoryExistsAndUserHasAccess_ShouldCopyDirectoryAndReturnPath() public void CopyDirectory_WhenSourceDirectoryExistsAndUserHasAccess_ShouldCopyDirectoryAndReturnPath()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSource);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
bool overrideExisting = false; bool overrideExisting = false;
_mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true); _mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true);
@@ -457,14 +496,16 @@ public class DirectoryProviderServiceTests
public void CopyDirectory_WhenDestinationDirectoryExists_ShouldCreateUniqueDirectoryName() public void CopyDirectory_WhenDestinationDirectoryExists_ShouldCreateUniqueDirectoryName()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSource);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
string expectedNewDirectoryPath = @"C:\Destination - Copy (1)"; string expectedNewDirectoryPath = s_isLinux
? "/Destination - Copy (1)"
: @"C:\Destination - Copy (1)";
bool overrideExisting = false; bool overrideExisting = false;
_mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true); _mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true);
_mockFileSystem.Directory.Exists(destinationPath.Path).Returns(true); _mockFileSystem.Directory.Exists(destinationPath.Path).Returns(true);
_mockFileSystem.Directory.Exists(@"C:\Destination").Returns(true); _mockFileSystem.Directory.Exists(_pathDestination).Returns(true);
_mockFileSystem.Directory.Exists(expectedNewDirectoryPath).Returns(false); _mockFileSystem.Directory.Exists(expectedNewDirectoryPath).Returns(false);
_mockFileSystem.Directory.CreateDirectory(expectedNewDirectoryPath).Returns(Substitute.For<IDirectoryInfo>()); _mockFileSystem.Directory.CreateDirectory(expectedNewDirectoryPath).Returns(Substitute.For<IDirectoryInfo>());
@@ -489,14 +530,15 @@ public class DirectoryProviderServiceTests
public void CopyDirectory_WhenCopyingFilesAndSubdirectories_ShouldCopyAllContents() public void CopyDirectory_WhenCopyingFilesAndSubdirectories_ShouldCopyAllContents()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSource);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
bool overrideExisting = true; bool overrideExisting = true;
// ensure all relevant directories exist // ensure all relevant directories exist
_mockFileSystem.Directory.Exists(@"C:\Source").Returns(true); _mockFileSystem.Directory.Exists(_pathSource).Returns(true);
_mockFileSystem.Directory.Exists(@"C:\Source\SubDir").Returns(true); _mockFileSystem.Directory.Exists(_pathSourceSubDir).Returns(true);
_mockFileSystem.Directory.Exists(@"C:\Destination").Returns(false); _mockFileSystem.Directory.Exists(_pathDestination).Returns(false);
_mockFileSystem.Directory.CreateDirectory(Arg.Any<string>()).Returns(callInfo => { _mockFileSystem.Directory.CreateDirectory(Arg.Any<string>()).Returns(callInfo => {
IDirectoryInfo mockDirInfo = Substitute.For<IDirectoryInfo>(); IDirectoryInfo mockDirInfo = Substitute.For<IDirectoryInfo>();
mockDirInfo.FullName.Returns(callInfo.Arg<string>()); mockDirInfo.FullName.Returns(callInfo.Arg<string>());
@@ -509,18 +551,18 @@ public class DirectoryProviderServiceTests
IDirectoryInfo mockSubDir = Substitute.For<IDirectoryInfo>(); IDirectoryInfo mockSubDir = Substitute.For<IDirectoryInfo>();
mockSubDir.Name.Returns("SubDir"); mockSubDir.Name.Returns("SubDir");
mockSubDir.FullName.Returns(@"C:\Source\SubDir"); mockSubDir.FullName.Returns(_pathSourceSubDir);
IDirectoryInfo sourceDirectoryInfo = Substitute.For<IDirectoryInfo>(); IDirectoryInfo sourceDirectoryInfo = Substitute.For<IDirectoryInfo>();
sourceDirectoryInfo.GetFiles().Returns([mockFile]); sourceDirectoryInfo.GetFiles().Returns([mockFile]);
sourceDirectoryInfo.GetDirectories().Returns([mockSubDir]); sourceDirectoryInfo.GetDirectories().Returns([mockSubDir]);
_mockFileSystem.DirectoryInfo.New(@"C:\Source").Returns(sourceDirectoryInfo); _mockFileSystem.DirectoryInfo.New(_pathSource).Returns(sourceDirectoryInfo);
// mock subdirectory info // mock subdirectory info
IDirectoryInfo subDirInfo = Substitute.For<IDirectoryInfo>(); IDirectoryInfo subDirInfo = Substitute.For<IDirectoryInfo>();
subDirInfo.GetFiles().Returns([]); subDirInfo.GetFiles().Returns([]);
subDirInfo.GetDirectories().Returns([]); subDirInfo.GetDirectories().Returns([]);
_mockFileSystem.DirectoryInfo.New(@"C:\Source\SubDir").Returns(subDirInfo); _mockFileSystem.DirectoryInfo.New(_pathSourceSubDir).Returns(subDirInfo);
// mock the Path.Combine method for all necessary cases // mock the Path.Combine method for all necessary cases
_mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>()) _mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>())
@@ -531,11 +573,11 @@ public class DirectoryProviderServiceTests
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Path.Should().Be(@"C:\Destination"); result.Value.Path.Should().Be(_pathDestination);
_mockFileSystem.Directory.Received(1).CreateDirectory(@"C:\Destination"); _mockFileSystem.Directory.Received(1).CreateDirectory(_pathDestination);
_mockFileSystem.Directory.Received(1).CreateDirectory(@"C:\Destination\SubDir"); _mockFileSystem.Directory.Received(1).CreateDirectory(_pathDestinationSubDir);
mockFile.Received(1).CopyTo(@"C:\Destination\file.txt", overrideExisting); mockFile.Received(1).CopyTo(_pathDestinationFile, overrideExisting);
_mockFileSystem.File.Received(1).SetAttributes(@"C:\Destination\file.txt", FileAttributes.Normal); _mockFileSystem.File.Received(1).SetAttributes(_pathDestinationFile, FileAttributes.Normal);
} }
[Fact] [Fact]
@@ -561,8 +603,10 @@ public class DirectoryProviderServiceTests
public void MoveDirectory_WhenSourceDirectoryDoesNotExist_ShouldReturnDirectoryNotFoundError() public void MoveDirectory_WhenSourceDirectoryDoesNotExist_ShouldReturnDirectoryNotFoundError()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\NonExistentSource"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); s_isLinux ? "/NonExistentSource" : @"C:\NonExistentSource"
);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
bool overrideExisting = false; bool overrideExisting = false;
_mockFileSystem.Directory.Exists(sourcePath.Path).Returns(false); _mockFileSystem.Directory.Exists(sourcePath.Path).Returns(false);
@@ -579,8 +623,8 @@ public class DirectoryProviderServiceTests
public void MoveDirectory_WhenDestinationDirectoryDoesNotExist_ShouldPerformSimpleMove() public void MoveDirectory_WhenDestinationDirectoryDoesNotExist_ShouldPerformSimpleMove()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSource);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
bool overrideExisting = false; bool overrideExisting = false;
_mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true); _mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true);
@@ -599,21 +643,21 @@ public class DirectoryProviderServiceTests
public void MoveDirectory_WhenDestinationDirectoryExists_ShouldMergeDirectories() public void MoveDirectory_WhenDestinationDirectoryExists_ShouldMergeDirectories()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSource);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
bool overrideExisting = true; bool overrideExisting = true;
_mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true); _mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true);
_mockFileSystem.Directory.Exists(destinationPath.Path).Returns(true); _mockFileSystem.Directory.Exists(destinationPath.Path).Returns(true);
_mockFileSystem.Directory.GetFiles(sourcePath.Path).Returns([@"C:\Source\file1.txt"]); _mockFileSystem.Directory.GetFiles(sourcePath.Path).Returns([_pathSourceFile1]);
_mockFileSystem.Directory.GetDirectories(sourcePath.Path).Returns([@"C:\Source\SubDir"]); _mockFileSystem.Directory.GetDirectories(sourcePath.Path).Returns([_pathSourceSubDir]);
_mockFileSystem.Directory.GetFiles(@"C:\Source\SubDir").Returns([]); _mockFileSystem.Directory.GetFiles(_pathSourceSubDir).Returns([]);
_mockFileSystem.Directory.GetDirectories(@"C:\Source\SubDir").Returns([]); _mockFileSystem.Directory.GetDirectories(_pathSourceSubDir).Returns([]);
_mockFileSystem.Path.GetFileName(@"C:\Source\file1.txt").Returns("file1.txt"); _mockFileSystem.Path.GetFileName(_pathSourceFile1).Returns("file1.txt");
_mockFileSystem.Path.GetFileName(@"C:\Source\SubDir").Returns("SubDir"); _mockFileSystem.Path.GetFileName(_pathSourceSubDir).Returns("SubDir");
_mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>()) _mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>())
.Returns(callInfo => Path.Combine(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1))); .Returns(callInfo => Path.Combine(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1)));
@@ -626,8 +670,11 @@ public class DirectoryProviderServiceTests
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Path.Should().Be(destinationPath.Path); result.Value.Path.Should().Be(destinationPath.Path);
_mockFileSystem.File.Received(1).Move(@"C:\Source\file1.txt", @"C:\Destination\file1.txt"); _mockFileSystem.File.Received(1).Move(_pathSourceFile1, _pathDestinationFile1);
_mockFileSystem.Directory.Received(1).Move(@"C:\Source\SubDir", @"C:\Destination\SubDir"); _mockFileSystem.Directory.Received(1).Move(
_pathSourceSubDir,
s_isLinux ? "/Destination/SubDir" : @"C:\Destination\SubDir"
);
_mockFileSystem.Directory.Received(1).Delete(sourcePath.Path); _mockFileSystem.Directory.Received(1).Delete(sourcePath.Path);
} }
@@ -635,8 +682,8 @@ public class DirectoryProviderServiceTests
public void MoveDirectory_WhenExceptionOccurs_ShouldReturnDirectoryMoveError() public void MoveDirectory_WhenExceptionOccurs_ShouldReturnDirectoryMoveError()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSource);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
bool overrideExisting = false; bool overrideExisting = false;
_mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true); _mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true);
@@ -656,26 +703,26 @@ public class DirectoryProviderServiceTests
public void MergeDirectories_WhenSourceContainsFilesAndSubdirectories_ShouldMergeCorrectly() public void MergeDirectories_WhenSourceContainsFilesAndSubdirectories_ShouldMergeCorrectly()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSource);
FileSystemPathId destPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
bool overwrite = true; bool overwrite = true;
_mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true); _mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true);
_mockFileSystem.Directory.Exists(destPath.Path).Returns(true); _mockFileSystem.Directory.Exists(destPath.Path).Returns(true);
_mockFileSystem.Directory.GetFiles(sourcePath.Path).Returns([@"C:\Source\file1.txt", @"C:\Source\file2.txt"]); _mockFileSystem.Directory.GetFiles(sourcePath.Path).Returns([_pathSourceFile1, _pathSourceFile2]);
_mockFileSystem.Directory.GetDirectories(sourcePath.Path).Returns([@"C:\Source\SubDir1", @"C:\Source\SubDir2"]); _mockFileSystem.Directory.GetDirectories(sourcePath.Path).Returns([_pathSourceSubDir1, _pathSourceSubDir2]);
_mockFileSystem.Path.GetFileName(@"C:\Source\file1.txt").Returns("file1.txt"); _mockFileSystem.Path.GetFileName(_pathSourceFile1).Returns("file1.txt");
_mockFileSystem.Path.GetFileName(@"C:\Source\file2.txt").Returns("file2.txt"); _mockFileSystem.Path.GetFileName(_pathSourceFile2).Returns("file2.txt");
_mockFileSystem.Path.GetFileName(@"C:\Source\SubDir1").Returns("SubDir1"); _mockFileSystem.Path.GetFileName(_pathSourceSubDir1).Returns("SubDir1");
_mockFileSystem.Path.GetFileName(@"C:\Source\SubDir2").Returns("SubDir2"); _mockFileSystem.Path.GetFileName(_pathSourceSubDir2).Returns("SubDir2");
_mockFileSystem.File.Exists(@"C:\Destination\file1.txt").Returns(true); _mockFileSystem.File.Exists(_pathDestinationFile1).Returns(true);
_mockFileSystem.File.Exists(@"C:\Destination\file2.txt").Returns(false); _mockFileSystem.File.Exists(_pathDestinationFile2).Returns(false);
_mockFileSystem.Directory.Exists(@"C:\Destination\SubDir1").Returns(true); _mockFileSystem.Directory.Exists(_pathDestinationSubDir1).Returns(true);
_mockFileSystem.Directory.Exists(@"C:\Destination\SubDir2").Returns(false); _mockFileSystem.Directory.Exists(_pathDestinationSubDir2).Returns(false);
_mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>()) _mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>())
.Returns(callInfo => Path.Combine(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1))); .Returns(callInfo => Path.Combine(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1)));
@@ -688,10 +735,10 @@ public class DirectoryProviderServiceTests
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Should().Be(destPath); result.Value.Should().Be(destPath);
_mockFileSystem.File.Received(1).Delete(@"C:\Destination\file1.txt"); _mockFileSystem.File.Received(1).Delete(_pathDestinationFile1);
_mockFileSystem.File.Received(1).Move(@"C:\Source\file1.txt", @"C:\Destination\file1.txt"); _mockFileSystem.File.Received(1).Move(_pathSourceFile1, _pathDestinationFile1);
_mockFileSystem.File.Received(1).Move(@"C:\Source\file2.txt", @"C:\Destination\file2.txt"); _mockFileSystem.File.Received(1).Move(_pathSourceFile2, _pathDestinationFile2);
_mockFileSystem.Directory.Received(1).Move(@"C:\Source\SubDir2", @"C:\Destination\SubDir2"); _mockFileSystem.Directory.Received(1).Move(_pathSourceSubDir2, _pathDestinationSubDir2);
_mockFileSystem.Directory.Received(1).Delete(sourcePath.Path); _mockFileSystem.Directory.Received(1).Delete(sourcePath.Path);
} }
@@ -699,26 +746,26 @@ public class DirectoryProviderServiceTests
public void MoveDirectory_WhenOverwriteIsFalse_ShouldMoveOnlyNonExistingFiles() public void MoveDirectory_WhenOverwriteIsFalse_ShouldMoveOnlyNonExistingFiles()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSource);
FileSystemPathId destPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
bool overwrite = false; bool overwrite = false;
_mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true); _mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true);
_mockFileSystem.Directory.Exists(destPath.Path).Returns(true); _mockFileSystem.Directory.Exists(destPath.Path).Returns(true);
_mockFileSystem.Directory.GetFiles(sourcePath.Path).Returns([@"C:\Source\file1.txt", @"C:\Source\file2.txt"]); _mockFileSystem.Directory.GetFiles(sourcePath.Path).Returns([_pathSourceFile1, _pathSourceFile2]);
_mockFileSystem.Directory.GetDirectories(sourcePath.Path).Returns([]); _mockFileSystem.Directory.GetDirectories(sourcePath.Path).Returns([]);
_mockFileSystem.Path.GetFileName(@"C:\Source\file1.txt").Returns("file1.txt"); _mockFileSystem.Path.GetFileName(_pathSourceFile1).Returns("file1.txt");
_mockFileSystem.Path.GetFileName(@"C:\Source\file2.txt").Returns("file2.txt"); _mockFileSystem.Path.GetFileName(_pathSourceFile2).Returns("file2.txt");
_mockFileSystem.File.Exists(@"C:\Destination\file1.txt").Returns(true); _mockFileSystem.File.Exists(_pathDestinationFile1).Returns(true);
_mockFileSystem.File.Exists(@"C:\Destination\file2.txt").Returns(false); _mockFileSystem.File.Exists(_pathDestinationFile2).Returns(false);
_mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>()) _mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>())
.Returns(callInfo => Path.Combine(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1))); .Returns(callInfo => Path.Combine(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1)));
_mockFileSystem.Directory.EnumerateFileSystemEntries(sourcePath.Path).Returns([@"C:\Source\file2.txt"]); _mockFileSystem.Directory.EnumerateFileSystemEntries(sourcePath.Path).Returns([_pathSourceFile2]);
// Act // Act
ErrorOr<FileSystemPathId> result = _sut.MoveDirectory(sourcePath, destPath, overwrite); ErrorOr<FileSystemPathId> result = _sut.MoveDirectory(sourcePath, destPath, overwrite);
@@ -727,8 +774,8 @@ public class DirectoryProviderServiceTests
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Should().Be(destPath); result.Value.Should().Be(destPath);
_mockFileSystem.File.DidNotReceive().Delete(Arg.Any<string>()); _mockFileSystem.File.DidNotReceive().Delete(Arg.Any<string>());
_mockFileSystem.File.DidNotReceive().Move(@"C:\Source\file1.txt", @"C:\Destination\file1.txt"); _mockFileSystem.File.DidNotReceive().Move(_pathSourceFile1, _pathDestinationFile1);
_mockFileSystem.File.Received(1).Move(@"C:\Source\file2.txt", @"C:\Destination\file2.txt"); _mockFileSystem.File.Received(1).Move(_pathSourceFile2, _pathDestinationFile2);
_mockFileSystem.Directory.DidNotReceive().Delete(sourcePath.Path); _mockFileSystem.Directory.DidNotReceive().Delete(sourcePath.Path);
} }
@@ -736,8 +783,8 @@ public class DirectoryProviderServiceTests
public void MergeDirectories_WhenSourceIsEmpty_ShouldDeleteSourceDirectory() public void MergeDirectories_WhenSourceIsEmpty_ShouldDeleteSourceDirectory()
{ {
// Arrange // Arrange
string sourceDir = @"C:\Source"; string sourceDir = _pathSource;
string destDir = @"C:\Destination"; string destDir = _pathDestination;
bool overwrite = true; bool overwrite = true;
_mockFileSystem.Directory.GetFiles(sourceDir).Returns([]); _mockFileSystem.Directory.GetFiles(sourceDir).Returns([]);
@@ -757,32 +804,34 @@ public class DirectoryProviderServiceTests
public void MergeDirectories_WhenSourceContainsNestedDirectories_ShouldMergeRecursively() public void MergeDirectories_WhenSourceContainsNestedDirectories_ShouldMergeRecursively()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSource);
FileSystemPathId destPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
bool overwrite = true; bool overwrite = true;
_mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true); _mockFileSystem.Directory.Exists(sourcePath.Path).Returns(true);
_mockFileSystem.Directory.Exists(destPath.Path).Returns(true); _mockFileSystem.Directory.Exists(destPath.Path).Returns(true);
_mockFileSystem.Directory.Exists(@"C:\Source\SubDir").Returns(true); _mockFileSystem.Directory.Exists(_pathSourceSubDir).Returns(true);
_mockFileSystem.Directory.Exists(@"C:\Destination\SubDir").Returns(true); _mockFileSystem.Directory.Exists(s_isLinux ? "/Destination/SubDir" : @"C:\Destination\SubDir").Returns(true);
_mockFileSystem.Directory.GetFiles(sourcePath.Path).Returns([]); _mockFileSystem.Directory.GetFiles(sourcePath.Path).Returns([]);
_mockFileSystem.Directory.GetDirectories(sourcePath.Path).Returns([@"C:\Source\SubDir"]); _mockFileSystem.Directory.GetDirectories(sourcePath.Path).Returns([_pathSourceSubDir]);
_mockFileSystem.Directory.GetFiles(@"C:\Source\SubDir").Returns([@"C:\Source\SubDir\file.txt"]); _mockFileSystem.Directory.GetFiles(_pathSourceSubDir).Returns(
_mockFileSystem.Directory.GetDirectories(@"C:\Source\SubDir").Returns([]); s_isLinux ? ["/Source/SubDir/file.txt"] : [@"C:\Source\SubDir\file.txt"]
);
_mockFileSystem.Directory.GetDirectories(_pathSourceSubDir).Returns([]);
_mockFileSystem.Path.GetFileName(@"C:\Source\SubDir").Returns("SubDir"); _mockFileSystem.Path.GetFileName(_pathSourceSubDir).Returns("SubDir");
_mockFileSystem.Path.GetFileName(@"C:\Source\SubDir\file.txt").Returns("file.txt"); _mockFileSystem.Path.GetFileName(s_isLinux ? "/Source/SubDir/file.txt" : @"C:\Source\SubDir\file.txt").Returns("file.txt");
_mockFileSystem.File.Exists(@"C:\Destination\SubDir\file.txt").Returns(false); _mockFileSystem.File.Exists(s_isLinux ? "/Destination/SubDir/file.txt" : @"C:\Destination\SubDir\file.txt").Returns(false);
_mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>()) _mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>())
.Returns(callInfo => Path.Combine(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1))); .Returns(callInfo => Path.Combine(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1)));
_mockFileSystem.Directory.EnumerateFileSystemEntries(sourcePath.Path).Returns([@"C:\Source\SubDir"]); _mockFileSystem.Directory.EnumerateFileSystemEntries(sourcePath.Path).Returns([_pathSourceSubDir]);
_mockFileSystem.Directory.EnumerateFileSystemEntries(@"C:\Source\SubDir").Returns([]); _mockFileSystem.Directory.EnumerateFileSystemEntries(_pathSourceSubDir).Returns([]);
// Act // Act
ErrorOr<FileSystemPathId> result = _sut.MoveDirectory(sourcePath, destPath, overwrite); ErrorOr<FileSystemPathId> result = _sut.MoveDirectory(sourcePath, destPath, overwrite);
@@ -790,8 +839,11 @@ public class DirectoryProviderServiceTests
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Should().Be(destPath); result.Value.Should().Be(destPath);
_mockFileSystem.File.Received(1).Move(@"C:\Source\SubDir\file.txt", @"C:\Destination\SubDir\file.txt"); _mockFileSystem.File.Received(1).Move(
_mockFileSystem.Directory.Received(1).Delete(@"C:\Source\SubDir"); s_isLinux ? "/Source/SubDir/file.txt" : @"C:\Source\SubDir\file.txt",
s_isLinux ? "/Destination/SubDir/file.txt" : @"C:\Destination\SubDir\file.txt"
);
_mockFileSystem.Directory.Received(1).Delete(_pathSourceSubDir);
_mockFileSystem.Directory.DidNotReceive().Delete(sourcePath.Path); _mockFileSystem.Directory.DidNotReceive().Delete(sourcePath.Path);
} }
@@ -799,10 +851,12 @@ public class DirectoryProviderServiceTests
public void RenameDirectory_WhenParentDirectoryIsWritable_ShouldRenameSuccessfully() public void RenameDirectory_WhenParentDirectoryIsWritable_ShouldRenameSuccessfully()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\OldName"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/OldName" : @"C:\OldName"
);
string newName = "NewName"; string newName = "NewName";
string parentPath = @"C:\"; string parentPath = s_isLinux ? "/" : @"C:\";
string newPath = @"C:\NewName"; string newPath = s_isLinux ? "/NewName" : @"C:\NewName";
IDirectoryInfo parentDirectoryInfo = Substitute.For<IDirectoryInfo>(); IDirectoryInfo parentDirectoryInfo = Substitute.For<IDirectoryInfo>();
parentDirectoryInfo.FullName.Returns(parentPath); parentDirectoryInfo.FullName.Returns(parentPath);
@@ -824,10 +878,12 @@ public class DirectoryProviderServiceTests
public void RenameDirectory_WhenParentDirectoryIsNotWritable_ShouldReturnUnauthorizedAccessError() public void RenameDirectory_WhenParentDirectoryIsNotWritable_ShouldReturnUnauthorizedAccessError()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\OldName"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/OldName" : @"C:\OldName"
);
string newName = "NewName"; string newName = "NewName";
string parentPath = @"C:\"; string parentPath = s_isLinux ? "/" : @"C:\";
string newPath = @"C:\NewName"; string newPath = s_isLinux ? "/NewName" : @"C:\NewName";
IDirectoryInfo parentDirectoryInfo = Substitute.For<IDirectoryInfo>(); IDirectoryInfo parentDirectoryInfo = Substitute.For<IDirectoryInfo>();
parentDirectoryInfo.FullName.Returns(parentPath); parentDirectoryInfo.FullName.Returns(parentPath);
@@ -848,10 +904,12 @@ public class DirectoryProviderServiceTests
public void RenameDirectory_WhenDirectoryIsNotExecutable_ShouldReturnUnauthorizedAccessError() public void RenameDirectory_WhenDirectoryIsNotExecutable_ShouldReturnUnauthorizedAccessError()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\OldName"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/OldName" : @"C:\OldName"
);
string newName = "NewName"; string newName = "NewName";
string parentPath = @"C:\"; string parentPath = s_isLinux ? "/" : @"C:\";
string newPath = @"C:\NewName"; string newPath = s_isLinux ? "/NewName" : @"C:\NewName";
IDirectoryInfo parentDirectoryInfo = Substitute.For<IDirectoryInfo>(); IDirectoryInfo parentDirectoryInfo = Substitute.For<IDirectoryInfo>();
parentDirectoryInfo.FullName.Returns(parentPath); parentDirectoryInfo.FullName.Returns(parentPath);
@@ -873,7 +931,9 @@ public class DirectoryProviderServiceTests
public void RenameDirectory_WhenParentDirectoryIsNull_ShouldReturnInvalidPathError() public void RenameDirectory_WhenParentDirectoryIsNull_ShouldReturnInvalidPathError()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\OldName"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/OldName" : @"C:\OldName"
);
string newName = "NewName"; string newName = "NewName";
_mockFileSystem.Directory.GetParent(path.Path).Returns((IDirectoryInfo)null!); _mockFileSystem.Directory.GetParent(path.Path).Returns((IDirectoryInfo)null!);
@@ -891,9 +951,11 @@ public class DirectoryProviderServiceTests
public void RenameDirectory_WhenNewPathIsInvalid_ShouldReturnInvalidPathError() public void RenameDirectory_WhenNewPathIsInvalid_ShouldReturnInvalidPathError()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\OldName"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/OldName" : @"C:\OldName"
);
string newName = "NewName"; string newName = "NewName";
string parentPath = @"C:\"; string parentPath = s_isLinux ? "/" : @"C:\";
IDirectoryInfo parentDirectoryInfo = Substitute.For<IDirectoryInfo>(); IDirectoryInfo parentDirectoryInfo = Substitute.For<IDirectoryInfo>();
parentDirectoryInfo.FullName.Returns(parentPath); parentDirectoryInfo.FullName.Returns(parentPath);
@@ -913,7 +975,9 @@ public class DirectoryProviderServiceTests
public void DeleteDirectory_WhenDirectoryExistsAndHasDeletePermission_ShouldDeleteSuccessfully() public void DeleteDirectory_WhenDirectoryExistsAndHasDeletePermission_ShouldDeleteSuccessfully()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\DirectoryToDelete"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/DirectoryToDelete" : @"C:\DirectoryToDelete"
);
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Delete, false).Returns(true); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Delete, false).Returns(true);
IDirectoryInfo mockDirectoryInfo = Substitute.For<IDirectoryInfo>(); IDirectoryInfo mockDirectoryInfo = Substitute.For<IDirectoryInfo>();
_mockFileSystem.DirectoryInfo.New(path.Path).Returns(mockDirectoryInfo); _mockFileSystem.DirectoryInfo.New(path.Path).Returns(mockDirectoryInfo);
@@ -931,7 +995,9 @@ public class DirectoryProviderServiceTests
public void DeleteDirectory_WhenNoDeletePermission_ShouldReturnUnauthorizedAccessError() public void DeleteDirectory_WhenNoDeletePermission_ShouldReturnUnauthorizedAccessError()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\DirectoryToDelete"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/DirectoryToDelete" : @"C:\DirectoryToDelete"
);
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Delete, false).Returns(false); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Delete, false).Returns(false);
// Act // Act
@@ -16,6 +16,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
#endregion #endregion
namespace Lumina.Domain.UnitTests.Core.Aggregates.FileManagementAggregate.Services; namespace Lumina.Domain.UnitTests.Core.Aggregates.FileManagementAggregate.Services;
@@ -33,6 +35,11 @@ public class DirectoryServiceTests
private readonly DirectoryService _sut; private readonly DirectoryService _sut;
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture; private readonly FileSystemPathIdFixture _fileSystemPathIdFixture;
private readonly DirectoryFixture _directoryFixture; private readonly DirectoryFixture _directoryFixture;
private static readonly bool s_isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
private readonly string _pathDirTest = s_isLinux ? "/TestDir" : @"C:\TestDir";
private readonly string _pathDirTestSubDir1 = s_isLinux ? "/TestDir/Sub1" : @"C:\TestDir\Sub1";
private readonly string _pathDirTestSubDir2 = s_isLinux ? "/TestDir/Sub2" : @"C:\TestDir\Sub2";
private readonly char _dirSeparator = s_isLinux ? '/' : '\\';
#endregion #endregion
#region ====================================================================== CTOR ===================================================================================== #region ====================================================================== CTOR =====================================================================================
@@ -56,13 +63,13 @@ public class DirectoryServiceTests
public void GetSubdirectories_WithValidPath_ShouldReturnListOfDirectories() public void GetSubdirectories_WithValidPath_ShouldReturnListOfDirectories()
{ {
// Arrange // Arrange
string path = @"C:\TestDir"; string path = _pathDirTest;
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path);
FileSystemPathId[] subPaths = FileSystemPathId[] subPaths =
[ [
_fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\Sub1"), _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTestSubDir1),
_fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\Sub2") _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTestSubDir2)
]; ];
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements) _mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements)
@@ -106,7 +113,7 @@ public class DirectoryServiceTests
public void GetSubdirectories_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError() public void GetSubdirectories_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
string path = @"C:\TestDir"; string path = _pathDirTest;
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path);
@@ -125,10 +132,12 @@ public class DirectoryServiceTests
public void GetSubdirectories_WhenSubdirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory() public void GetSubdirectories_WhenSubdirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory()
{ {
// Arrange // Arrange
string path = @"C:\TestDir"; string path = _pathDirTest;
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path);
FileSystemPathId subPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\InaccessibleSub"); FileSystemPathId subPath = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/InaccessibleSub" : @"C:\TestDir\InaccessibleSub"
);
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements) _mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements)
.Returns(ErrorOrFactory.From(new[] { subPath }.AsEnumerable())); .Returns(ErrorOrFactory.From(new[] { subPath }.AsEnumerable()));
@@ -157,8 +166,8 @@ public class DirectoryServiceTests
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId[] subPaths = FileSystemPathId[] subPaths =
[ [
_fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\Sub1"), _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTestSubDir1),
_fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\Sub2") _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTestSubDir2)
]; ];
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(parentDirectory.Id, includeHiddenElements) _mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(parentDirectory.Id, includeHiddenElements)
@@ -207,7 +216,9 @@ public class DirectoryServiceTests
// Arrange // Arrange
Directory parentDirectory = _directoryFixture.CreateDirectory(); Directory parentDirectory = _directoryFixture.CreateDirectory();
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId subPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\InaccessibleSub"); FileSystemPathId subPath = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/InaccessibleSub" : @"C:\TestDir\InaccessibleSub"
);
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(parentDirectory.Id, includeHiddenElements) _mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(parentDirectory.Id, includeHiddenElements)
.Returns(ErrorOrFactory.From(new[] { subPath }.AsEnumerable())); .Returns(ErrorOrFactory.From(new[] { subPath }.AsEnumerable()));
@@ -232,12 +243,12 @@ public class DirectoryServiceTests
public void GetSubdirectories_WithValidFileSystemPathId_ShouldReturnListOfDirectories() public void GetSubdirectories_WithValidFileSystemPathId_ShouldReturnListOfDirectories()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTest);
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId[] subPaths = FileSystemPathId[] subPaths =
[ [
_fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\Sub1"), _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTestSubDir1),
_fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\Sub2") _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTestSubDir2)
]; ];
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements) _mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements)
@@ -266,7 +277,7 @@ public class DirectoryServiceTests
public void GetSubdirectoriesWithFileSystemPathId_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError() public void GetSubdirectoriesWithFileSystemPathId_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTest);
bool includeHiddenElements = false; bool includeHiddenElements = false;
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements) _mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements)
@@ -284,9 +295,11 @@ public class DirectoryServiceTests
public void GetSubdirectoriesWithFileSystemPathId_WhenSubdirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory() public void GetSubdirectoriesWithFileSystemPathId_WhenSubdirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTest);
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId subPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\InaccessibleSub"); FileSystemPathId subPath = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/InaccessibleSub" : @"C:\TestDir\InaccessibleSub"
);
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements) _mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements)
.Returns(ErrorOrFactory.From(new[] { subPath }.AsEnumerable())); .Returns(ErrorOrFactory.From(new[] { subPath }.AsEnumerable()));
@@ -311,7 +324,7 @@ public class DirectoryServiceTests
public void CreateDirectory_WithValidStringPath_ShouldReturnNewDirectory() public void CreateDirectory_WithValidStringPath_ShouldReturnNewDirectory()
{ {
// Arrange // Arrange
string parentPath = @"C:\TestDir"; string parentPath = _pathDirTest;
string newDirName = "NewDirectory"; string newDirName = "NewDirectory";
FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(parentPath); FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(parentPath);
FileSystemPathId newDirPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(System.IO.Path.Combine(parentPath, newDirName)); FileSystemPathId newDirPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(System.IO.Path.Combine(parentPath, newDirName));
@@ -352,7 +365,7 @@ public class DirectoryServiceTests
public void CreateDirectory_WithFileSystemPathId_ShouldReturnNewDirectory() public void CreateDirectory_WithFileSystemPathId_ShouldReturnNewDirectory()
{ {
// Arrange // Arrange
FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir"); FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTest);
string newDirName = "NewDirectory"; string newDirName = "NewDirectory";
FileSystemPathId newDirPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(System.IO.Path.Combine(parentPathId.Path, newDirName)); FileSystemPathId newDirPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(System.IO.Path.Combine(parentPathId.Path, newDirName));
@@ -377,7 +390,7 @@ public class DirectoryServiceTests
public void CreateDirectory_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError() public void CreateDirectory_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir"); FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTest);
string newDirName = "NewDirectory"; string newDirName = "NewDirectory";
_mockEnvironmentContext.DirectoryProviderService.CreateDirectory(parentPathId, newDirName) _mockEnvironmentContext.DirectoryProviderService.CreateDirectory(parentPathId, newDirName)
@@ -395,7 +408,7 @@ public class DirectoryServiceTests
public void CreateDirectory_WhenDirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory() public void CreateDirectory_WhenDirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory()
{ {
// Arrange // Arrange
FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir"); FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTest);
string newDirName = "NewDirectory"; string newDirName = "NewDirectory";
FileSystemPathId newDirPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(System.IO.Path.Combine(parentPathId.Path, newDirName)); FileSystemPathId newDirPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(System.IO.Path.Combine(parentPathId.Path, newDirName));
@@ -424,7 +437,7 @@ public class DirectoryServiceTests
public void CreateDirectory_WhenDirectoryExistsReturnsError_ShouldPropagateError() public void CreateDirectory_WhenDirectoryExistsReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir"); FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTest);
string newDirName = "NewDirectory"; string newDirName = "NewDirectory";
FileSystemPathId newDirPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(System.IO.Path.Combine(parentPathId.Path, newDirName)); FileSystemPathId newDirPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(System.IO.Path.Combine(parentPathId.Path, newDirName));
@@ -444,7 +457,7 @@ public class DirectoryServiceTests
public void CreateDirectory_WhenDirectoryAlreadyExists_ShouldReturnDirectoryAlreadyExistsError() public void CreateDirectory_WhenDirectoryAlreadyExists_ShouldReturnDirectoryAlreadyExistsError()
{ {
// Arrange // Arrange
FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir"); FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTest);
string newDirName = "ExistingDirectory"; string newDirName = "ExistingDirectory";
FileSystemPathId newDirPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(System.IO.Path.Combine(parentPathId.Path, newDirName)); FileSystemPathId newDirPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(System.IO.Path.Combine(parentPathId.Path, newDirName));
_mockPlatformContext.PathStrategy.CombinePath(parentPathId, newDirName).Returns(ErrorOrFactory.From(newDirPathId)); _mockPlatformContext.PathStrategy.CombinePath(parentPathId, newDirName).Returns(ErrorOrFactory.From(newDirPathId));
@@ -463,7 +476,7 @@ public class DirectoryServiceTests
public void CreateDirectory_WhenCombinePathReturnsError_ShouldReturnError() public void CreateDirectory_WhenCombinePathReturnsError_ShouldReturnError()
{ {
// Arrange // Arrange
FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir"); FileSystemPathId parentPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDirTest);
string newDirName = "NewDirectory"; string newDirName = "NewDirectory";
_mockPlatformContext.PathStrategy.CombinePath(parentPathId, newDirName) _mockPlatformContext.PathStrategy.CombinePath(parentPathId, newDirName)
@@ -485,10 +498,12 @@ public class DirectoryServiceTests
public void RenameDirectory_WithValidStringPath_ShouldReturnRenamedDirectory() public void RenameDirectory_WithValidStringPath_ShouldReturnRenamedDirectory()
{ {
// Arrange // Arrange
string oldPath = @"C:\TestDir\OldName"; string oldPath = s_isLinux ? "/TestDir/OldName" : @"C:\TestDir\OldName";
string newName = "NewName"; string newName = "NewName";
FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(oldPath); FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(oldPath);
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\NewName"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/NewName" : @"C:\TestDir\NewName"
);
_mockEnvironmentContext.DirectoryProviderService.RenameDirectory(oldPathId, newName) _mockEnvironmentContext.DirectoryProviderService.RenameDirectory(oldPathId, newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
@@ -527,9 +542,13 @@ public class DirectoryServiceTests
public void RenameDirectory_WithFileSystemPathId_ShouldReturnRenamedDirectory() public void RenameDirectory_WithFileSystemPathId_ShouldReturnRenamedDirectory()
{ {
// Arrange // Arrange
FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\OldName"); FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/OldName" : @"C:\TestDir\OldName"
);
string newName = "NewName"; string newName = "NewName";
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\NewName"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/NewName" : @"C:\TestDir\NewName"
);
_mockEnvironmentContext.DirectoryProviderService.RenameDirectory(oldPathId, newName) _mockEnvironmentContext.DirectoryProviderService.RenameDirectory(oldPathId, newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
@@ -553,7 +572,9 @@ public class DirectoryServiceTests
public void RenameDirectory_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError() public void RenameDirectory_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\OldName"); FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/OldName" : @"C:\TestDir\OldName"
);
string newName = "NewName"; string newName = "NewName";
_mockEnvironmentContext.DirectoryProviderService.RenameDirectory(oldPathId, newName) _mockEnvironmentContext.DirectoryProviderService.RenameDirectory(oldPathId, newName)
@@ -571,9 +592,13 @@ public class DirectoryServiceTests
public void RenameDirectory_WhenDirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory() public void RenameDirectory_WhenDirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory()
{ {
// Arrange // Arrange
FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\OldName"); FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/OldName" : @"C:\TestDir\OldName"
);
string newName = "NewName"; string newName = "NewName";
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\NewName"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/NewName" : @"C:\TestDir\NewName"
);
_mockEnvironmentContext.DirectoryProviderService.RenameDirectory(oldPathId, newName) _mockEnvironmentContext.DirectoryProviderService.RenameDirectory(oldPathId, newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
@@ -600,9 +625,13 @@ public class DirectoryServiceTests
public void RenameDirectory_WhenDirectoryAlreadyExists_ShouldReturnDirectoryAlreadyExistsError() public void RenameDirectory_WhenDirectoryAlreadyExists_ShouldReturnDirectoryAlreadyExistsError()
{ {
// Arrange // Arrange
FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\OldName"); FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/OldName" : @"C:\TestDir\OldName"
);
string newName = "ExistingDirectory"; string newName = "ExistingDirectory";
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\ExistingDirectory"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/ExistingDirectory" : @"C:\TestDir\ExistingDirectory"
);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
@@ -621,7 +650,9 @@ public class DirectoryServiceTests
public void RenameDirectory_WhenCombinePathReturnsError_ShouldReturnError() public void RenameDirectory_WhenCombinePathReturnsError_ShouldReturnError()
{ {
// Arrange // Arrange
FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\OldName"); FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/OldName" : @"C:\TestDir\OldName"
);
string newName = "NewName"; string newName = "NewName";
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
@@ -643,10 +674,13 @@ public class DirectoryServiceTests
public void RenameDirectory_WhenDirectoryExistsReturnsError_ShouldPropagateError() public void RenameDirectory_WhenDirectoryExistsReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\OldName"); FileSystemPathId oldPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/OldName" : @"C:\TestDir\OldName"
);
string newName = "NewName"; string newName = "NewName";
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\NewName"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/NewName" : @"C:\TestDir\NewName"
);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
@@ -668,7 +702,7 @@ public class DirectoryServiceTests
public void DeleteDirectory_WithValidStringPath_ShouldReturnDeleted() public void DeleteDirectory_WithValidStringPath_ShouldReturnDeleted()
{ {
// Arrange // Arrange
string path = @"C:\TestDir\ToDelete"; string path = s_isLinux ? "/TestDir/ToDelete" : @"C:\TestDir\ToDelete";
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path);
_mockEnvironmentContext.DirectoryProviderService.DeleteDirectory(pathId) _mockEnvironmentContext.DirectoryProviderService.DeleteDirectory(pathId)
@@ -700,7 +734,9 @@ public class DirectoryServiceTests
public void DeleteDirectory_WithFileSystemPathId_ShouldReturnDeleted() public void DeleteDirectory_WithFileSystemPathId_ShouldReturnDeleted()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\ToDelete"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/ToDelete" : @"C:\TestDir\ToDelete"
);
_mockEnvironmentContext.DirectoryProviderService.DeleteDirectory(pathId) _mockEnvironmentContext.DirectoryProviderService.DeleteDirectory(pathId)
.Returns(Result.Deleted); .Returns(Result.Deleted);
@@ -717,7 +753,9 @@ public class DirectoryServiceTests
public void DeleteDirectory_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError() public void DeleteDirectory_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\ToDelete"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/ToDelete" : @"C:\TestDir\ToDelete"
);
_mockEnvironmentContext.DirectoryProviderService.DeleteDirectory(pathId) _mockEnvironmentContext.DirectoryProviderService.DeleteDirectory(pathId)
.Returns(Errors.Permission.UnauthorizedAccess); .Returns(Errors.Permission.UnauthorizedAccess);
@@ -734,7 +772,9 @@ public class DirectoryServiceTests
public void DeleteDirectory_WhenDirectoryDoesNotExist_ShouldReturnDirectoryNotFoundError() public void DeleteDirectory_WhenDirectoryDoesNotExist_ShouldReturnDirectoryNotFoundError()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\NonExistent"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/TestDir/NonExistent" : @"C:\TestDir\NonExistent"
);
_mockEnvironmentContext.DirectoryProviderService.DeleteDirectory(pathId) _mockEnvironmentContext.DirectoryProviderService.DeleteDirectory(pathId)
.Returns(Errors.FileManagement.DirectoryNotFound); .Returns(Errors.FileManagement.DirectoryNotFound);
@@ -17,6 +17,8 @@ using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.IO.Abstractions; using System.IO.Abstractions;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
#endregion #endregion
namespace Lumina.Domain.UnitTests.Core.Aggregates.FileManagementAggregate.Services; namespace Lumina.Domain.UnitTests.Core.Aggregates.FileManagementAggregate.Services;
@@ -33,6 +35,17 @@ public class FileProviderServiceTests
private readonly IFileSystemPermissionsService _mockFileSystemPermissionsService; private readonly IFileSystemPermissionsService _mockFileSystemPermissionsService;
private readonly FileProviderService _sut; private readonly FileProviderService _sut;
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture; private readonly FileSystemPathIdFixture _fileSystemPathIdFixture;
private static readonly bool s_isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
private readonly string _pathDirTest = s_isLinux ? "/TestDir" : @"C:\TestDir";
private readonly string _pathVisible1 = s_isLinux ? "/Visible.txt" : @"C:\Visible.txt";
private readonly string _pathHidden1 = s_isLinux ? "/Hidden.txt" : @"C:\Hidden.txt";
private readonly string _pathValid1 = s_isLinux ? "/Valid.txt" : @"C:\Valid.txt";
private readonly string _pathInvalid1 = s_isLinux ? "/Invalid.txt" : @"C:\Invalid.txt";
private readonly string _pathSourceFile = s_isLinux ? "/Source/file.txt" : @"C:\Source\file.txt";
private readonly string _pathDestinationFile = s_isLinux ? "/Destination/file.txt" : @"C:\Destination\file.txt";
private readonly string _pathSource = s_isLinux ? "/Source" : @"C:\Source";
private readonly string _pathDestination = s_isLinux ? "/Destination" : @"C:\Destination";
private readonly char _dirSeparator = s_isLinux ? '/' : '\\';
#endregion #endregion
#region ====================================================================== CTOR ===================================================================================== #region ====================================================================== CTOR =====================================================================================
@@ -88,11 +101,11 @@ public class FileProviderServiceTests
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId();
string[] files = [@"C:\Visible.txt", @"C:\Hidden.txt"]; string[] files = [_pathVisible1, _pathHidden1];
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true);
_mockFileSystem.Directory.GetFiles(path.Path).Returns(files); _mockFileSystem.Directory.GetFiles(path.Path).Returns(files);
_mockFileSystem.File.GetAttributes(@"C:\Visible.txt").Returns(FileAttributes.Normal); _mockFileSystem.File.GetAttributes(_pathVisible1).Returns(FileAttributes.Normal);
_mockFileSystem.File.GetAttributes(@"C:\Hidden.txt").Returns(FileAttributes.Hidden); _mockFileSystem.File.GetAttributes(_pathHidden1).Returns(FileAttributes.Hidden);
// Act // Act
ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, false); ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, false);
@@ -100,7 +113,7 @@ public class FileProviderServiceTests
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Should().HaveCount(1); result.Value.Should().HaveCount(1);
result.Value.First().Path.Should().Be(@"C:\Visible.txt"); result.Value.First().Path.Should().Be(_pathVisible1);
} }
[Fact] [Fact]
@@ -108,11 +121,11 @@ public class FileProviderServiceTests
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId();
string[] files = [@"C:\Visible.txt", @"C:\Hidden.txt"]; string[] files = [_pathVisible1, _pathHidden1];
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true);
_mockFileSystem.Directory.GetFiles(path.Path).Returns(files); _mockFileSystem.Directory.GetFiles(path.Path).Returns(files);
_mockFileSystem.File.GetAttributes(@"C:\Visible.txt").Returns(FileAttributes.Normal); _mockFileSystem.File.GetAttributes(_pathVisible1).Returns(FileAttributes.Normal);
_mockFileSystem.File.GetAttributes(@"C:\Hidden.txt").Returns(FileAttributes.Hidden); _mockFileSystem.File.GetAttributes(_pathHidden1).Returns(FileAttributes.Hidden);
// Act // Act
ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, true); ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, true);
@@ -120,7 +133,7 @@ public class FileProviderServiceTests
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Should().HaveCount(2); result.Value.Should().HaveCount(2);
result.Value.Select(x => x.Path).Should().BeEquivalentTo([@"C:\Visible.txt", @"C:\Hidden.txt"]); result.Value.Select(x => x.Path).Should().BeEquivalentTo([_pathVisible1, _pathHidden1]);
} }
[Fact] [Fact]
@@ -128,11 +141,11 @@ public class FileProviderServiceTests
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId();
string[] files = [@"C:\Valid.txt", @"C:\Invalid.txt"]; string[] files = [_pathValid1, _pathInvalid1];
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true);
_mockFileSystem.Directory.GetFiles(path.Path).Returns(files); _mockFileSystem.Directory.GetFiles(path.Path).Returns(files);
_mockFileSystem.File.GetAttributes(@"C:\Valid.txt").Returns(FileAttributes.Normal); _mockFileSystem.File.GetAttributes(_pathValid1).Returns(FileAttributes.Normal);
_mockFileSystem.File.GetAttributes(@"C:\Invalid.txt").Throws(new Exception("Access denied")); _mockFileSystem.File.GetAttributes(_pathInvalid1).Throws(new Exception("Access denied"));
// Act // Act
ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, false); ErrorOr<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, false);
@@ -140,7 +153,7 @@ public class FileProviderServiceTests
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Should().HaveCount(2); result.Value.Should().HaveCount(2);
result.Value.Select(x => x.Path).Should().BeEquivalentTo([@"C:\Invalid.txt", @"C:\Valid.txt"]); result.Value.Select(x => x.Path).Should().BeEquivalentTo([_pathInvalid1, _pathValid1]);
} }
[Fact] [Fact]
@@ -148,7 +161,9 @@ public class FileProviderServiceTests
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId();
string[] files = [@"C:\C.txt", @"C:\A.txt", @"C:\B.txt"]; string[] files = s_isLinux
? new[] { "/C.txt", "/A.txt", "/B.txt" }
: new[] { @"C:\C.txt", @"C:\A.txt", @"C:\B.txt" };
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true);
_mockFileSystem.Directory.GetFiles(path.Path).Returns(files); _mockFileSystem.Directory.GetFiles(path.Path).Returns(files);
_mockFileSystem.File.GetAttributes(Arg.Any<string>()).Returns(FileAttributes.Normal); _mockFileSystem.File.GetAttributes(Arg.Any<string>()).Returns(FileAttributes.Normal);
@@ -459,23 +474,23 @@ public class FileProviderServiceTests
public void CopyFile_WhenSourceFileExistsAndUserHasAccess_ShouldCopyFileAndReturnPath() public void CopyFile_WhenSourceFileExistsAndUserHasAccess_ShouldCopyFileAndReturnPath()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
bool overrideExisting = false; bool overrideExisting = false;
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true); _mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
_mockFileSystem.File.Exists(Path.Combine(destinationPath.Path, "file.txt")).Returns(false); _mockFileSystem.File.Exists(Path.Combine(destinationPath.Path, "file.txt")).Returns(false);
_mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt"); _mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt");
_mockFileSystem.Path.Combine(destinationPath.Path, "file.txt").Returns(@"C:\Destination\file.txt"); _mockFileSystem.Path.Combine(destinationPath.Path, "file.txt").Returns(_pathDestinationFile);
// Act // Act
ErrorOr<FileSystemPathId> result = _sut.CopyFile(sourcePath, destinationPath, overrideExisting); ErrorOr<FileSystemPathId> result = _sut.CopyFile(sourcePath, destinationPath, overrideExisting);
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Path.Should().Be(@"C:\Destination\file.txt"); result.Value.Path.Should().Be(_pathDestinationFile);
_mockFileSystem.File.Received(1).Copy(sourcePath.Path, @"C:\Destination\file.txt", overrideExisting); _mockFileSystem.File.Received(1).Copy(sourcePath.Path, _pathDestinationFile, overrideExisting);
} }
[Fact] [Fact]
@@ -500,16 +515,16 @@ public class FileProviderServiceTests
public void CopyFile_WhenDestinationFileExistsAndOverrideIsFalse_ShouldReturnFileAlreadyExistsError() public void CopyFile_WhenDestinationFileExistsAndOverrideIsFalse_ShouldReturnFileAlreadyExistsError()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
string expectedDestinationFilePath = @"C:\Destination\file.txt"; string expectedDestinationFilePath = _pathDestinationFile;
bool overrideExisting = false; bool overrideExisting = false;
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true); _mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
_mockFileSystem.File.Exists(expectedDestinationFilePath).Returns(true); _mockFileSystem.File.Exists(expectedDestinationFilePath).Returns(true);
_mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt"); _mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt");
_mockFileSystem.Path.GetDirectoryName(sourcePath.Path).Returns(@"C:\Source"); _mockFileSystem.Path.GetDirectoryName(sourcePath.Path).Returns(_pathSource);
_mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>()) _mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>())
.Returns(callInfo => Path.Combine(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1))); .Returns(callInfo => Path.Combine(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1)));
@@ -527,23 +542,23 @@ public class FileProviderServiceTests
public void CopyFile_WhenOverrideExistingIsTrue_ShouldOverwriteExistingFile() public void CopyFile_WhenOverrideExistingIsTrue_ShouldOverwriteExistingFile()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
bool overrideExisting = true; bool overrideExisting = true;
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true); _mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
_mockFileSystem.File.Exists(@"C:\Destination\file.txt").Returns(true); _mockFileSystem.File.Exists(_pathDestinationFile).Returns(true);
_mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt"); _mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt");
_mockFileSystem.Path.Combine(destinationPath.Path, "file.txt").Returns(@"C:\Destination\file.txt"); _mockFileSystem.Path.Combine(destinationPath.Path, "file.txt").Returns(_pathDestinationFile);
// Act // Act
ErrorOr<FileSystemPathId> result = _sut.CopyFile(sourcePath, destinationPath, overrideExisting); ErrorOr<FileSystemPathId> result = _sut.CopyFile(sourcePath, destinationPath, overrideExisting);
// Assert // Assert
result.IsError.Should().BeFalse(); result.IsError.Should().BeFalse();
result.Value.Path.Should().Be(@"C:\Destination\file.txt"); result.Value.Path.Should().Be(_pathDestinationFile);
_mockFileSystem.File.Received(1).Copy(sourcePath.Path, @"C:\Destination\file.txt", overrideExisting); _mockFileSystem.File.Received(1).Copy(sourcePath.Path, _pathDestinationFile, overrideExisting);
} }
[Fact] [Fact]
@@ -570,7 +585,7 @@ public class FileProviderServiceTests
public void CopyFile_WhenCopyingToSameDirectory_ShouldCreateUniqueFileName() public void CopyFile_WhenCopyingToSameDirectory_ShouldCreateUniqueFileName()
{ {
// Arrange // Arrange
string sourceDirectory = @"C:\Source"; string sourceDirectory = _pathSource;
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(Path.Combine(sourceDirectory, "file.txt")); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(Path.Combine(sourceDirectory, "file.txt"));
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(sourceDirectory); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(sourceDirectory);
string expectedNewFilePath = Path.Combine(sourceDirectory, "file - Copy (1).txt"); string expectedNewFilePath = Path.Combine(sourceDirectory, "file - Copy (1).txt");
@@ -612,8 +627,10 @@ public class FileProviderServiceTests
public void MoveFile_WhenSourceFileDoesNotExist_ShouldReturnFileNotFoundError() public void MoveFile_WhenSourceFileDoesNotExist_ShouldReturnFileNotFoundError()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\NonExistentSource.txt"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); s_isLinux ? "/NonExistentSource.txt" : @"C:\NonExistentSource.txt"
);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
bool overrideExisting = false; bool overrideExisting = false;
_mockFileSystem.File.Exists(sourcePath.Path).Returns(false); _mockFileSystem.File.Exists(sourcePath.Path).Returns(false);
@@ -630,9 +647,9 @@ public class FileProviderServiceTests
public void MoveFile_WhenDestinationFileDoesNotExist_ShouldPerformSimpleMove() public void MoveFile_WhenDestinationFileDoesNotExist_ShouldPerformSimpleMove()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
string destinationFilePath = @"C:\Destination\file.txt"; string destinationFilePath = _pathDestinationFile;
bool overrideExisting = false; bool overrideExisting = false;
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true); _mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
@@ -653,9 +670,9 @@ public class FileProviderServiceTests
public void MoveFile_WhenDestinationFileExistsAndOverrideIsTrue_ShouldOverwriteFile() public void MoveFile_WhenDestinationFileExistsAndOverrideIsTrue_ShouldOverwriteFile()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
string destinationFilePath = @"C:\Destination\file.txt"; string destinationFilePath = _pathDestinationFile;
bool overrideExisting = true; bool overrideExisting = true;
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true); _mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
@@ -676,9 +693,9 @@ public class FileProviderServiceTests
public void MoveFile_WhenDestinationFileExistsAndOverrideIsFalse_ShouldReturnFileAlreadyExistsError() public void MoveFile_WhenDestinationFileExistsAndOverrideIsFalse_ShouldReturnFileAlreadyExistsError()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
string destinationFilePath = @"C:\Destination\file.txt"; string destinationFilePath = _pathDestinationFile;
bool overrideExisting = false; bool overrideExisting = false;
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true); _mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
@@ -698,9 +715,9 @@ public class FileProviderServiceTests
public void MoveFile_WhenExceptionOccurs_ShouldReturnFileMoveError() public void MoveFile_WhenExceptionOccurs_ShouldReturnFileMoveError()
{ {
// Arrange // Arrange
FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination);
string destinationFilePath = @"C:\Destination\file.txt"; string destinationFilePath = _pathDestinationFile;
bool overrideExisting = false; bool overrideExisting = false;
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true); _mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
@@ -722,10 +739,12 @@ public class FileProviderServiceTests
public void RenameFile_WhenParentDirectoryIsWritable_ShouldRenameSuccessfully() public void RenameFile_WhenParentDirectoryIsWritable_ShouldRenameSuccessfully()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\OldName.txt"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/OldName.txt" : @"C:\OldName.txt"
);
string newName = "NewName.txt"; string newName = "NewName.txt";
string parentPath = @"C:\"; string parentPath = s_isLinux ? "/" : @"C:\";
string newPath = @"C:\NewName.txt"; string newPath = s_isLinux ? "/NewName.txt" : @"C:\NewName.txt";
IFileInfo fileInfo = Substitute.For<IFileInfo>(); IFileInfo fileInfo = Substitute.For<IFileInfo>();
fileInfo.DirectoryName.Returns(parentPath); fileInfo.DirectoryName.Returns(parentPath);
@@ -747,10 +766,12 @@ public class FileProviderServiceTests
public void RenameFile_WhenParentDirectoryIsNotWritable_ShouldReturnUnauthorizedAccessError() public void RenameFile_WhenParentDirectoryIsNotWritable_ShouldReturnUnauthorizedAccessError()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\OldName.txt"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/OldName.txt" : @"C:\OldName.txt"
);
string newName = "NewName.txt"; string newName = "NewName.txt";
string parentPath = @"C:\"; string parentPath = s_isLinux ? "/" : @"C:\";
string newPath = @"C:\NewName.txt"; string newPath = s_isLinux ? "/NewName.txt" : @"C:\NewName.txt";
IFileInfo fileInfo = Substitute.For<IFileInfo>(); IFileInfo fileInfo = Substitute.For<IFileInfo>();
fileInfo.DirectoryName.Returns(parentPath); fileInfo.DirectoryName.Returns(parentPath);
@@ -771,10 +792,12 @@ public class FileProviderServiceTests
public void RenameFile_WhenFileIsNotExecutable_ShouldReturnUnauthorizedAccessError() public void RenameFile_WhenFileIsNotExecutable_ShouldReturnUnauthorizedAccessError()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\OldName.txt"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/OldName.txt" : @"C:\OldName.txt"
);
string newName = "NewName.txt"; string newName = "NewName.txt";
string parentPath = @"C:\"; string parentPath = s_isLinux ? "/" : @"C:\";
string newPath = @"C:\NewName.txt"; string newPath = s_isLinux ? "/NewName.txt" : @"C:\NewName.txt";
IFileInfo fileInfo = Substitute.For<IFileInfo>(); IFileInfo fileInfo = Substitute.For<IFileInfo>();
fileInfo.DirectoryName.Returns(parentPath); fileInfo.DirectoryName.Returns(parentPath);
@@ -796,7 +819,9 @@ public class FileProviderServiceTests
public void RenameFile_WhenParentDirectoryIsNull_ShouldReturnInvalidPathError() public void RenameFile_WhenParentDirectoryIsNull_ShouldReturnInvalidPathError()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\OldName.txt"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/OldName.txt" : @"C:\OldName.txt"
);
string newName = "NewName.txt"; string newName = "NewName.txt";
_mockFileSystem.FileInfo.New(path.Path).Returns((IFileInfo)null!); _mockFileSystem.FileInfo.New(path.Path).Returns((IFileInfo)null!);
@@ -814,9 +839,11 @@ public class FileProviderServiceTests
public void RenameFile_WhenNewPathIsInvalid_ShouldReturnInvalidPathError() public void RenameFile_WhenNewPathIsInvalid_ShouldReturnInvalidPathError()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\OldName.txt"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/OldName.txt" : @"C:\OldName.txt"
);
string newName = "NewName.txt"; string newName = "NewName.txt";
string parentPath = @"C:\"; string parentPath = s_isLinux ? "/" : @"C:\";
IFileInfo fileInfo = Substitute.For<IFileInfo>(); IFileInfo fileInfo = Substitute.For<IFileInfo>();
fileInfo.DirectoryName.Returns(parentPath); fileInfo.DirectoryName.Returns(parentPath);
@@ -836,10 +863,12 @@ public class FileProviderServiceTests
public void RenameFile_WhenParentDirectoryExistsButNotWritable_ShouldReturnUnauthorizedAccessError() public void RenameFile_WhenParentDirectoryExistsButNotWritable_ShouldReturnUnauthorizedAccessError()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\OldName.txt"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/OldName.txt" : @"C:\OldName.txt"
);
string newName = "NewName.txt"; string newName = "NewName.txt";
string parentPath = @"C:\"; string parentPath = s_isLinux ? "/" : @"C:\";
string newPath = @"C:\NewName.txt"; string newPath = s_isLinux ? "/NewName.txt" : @"C:\NewName.txt";
IFileInfo fileInfo = Substitute.For<IFileInfo>(); IFileInfo fileInfo = Substitute.For<IFileInfo>();
fileInfo.DirectoryName.Returns(parentPath); fileInfo.DirectoryName.Returns(parentPath);
@@ -867,7 +896,9 @@ public class FileProviderServiceTests
public void DeleteFile_WhenFileExistsAndHasDeletePermission_ShouldDeleteSuccessfully() public void DeleteFile_WhenFileExistsAndHasDeletePermission_ShouldDeleteSuccessfully()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\FileToDelete.txt"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/FileToDelete.txt" : @"C:\FileToDelete.txt"
);
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Delete).Returns(true); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Delete).Returns(true);
_mockFileSystem.File.Exists(path.Path).Returns(true); _mockFileSystem.File.Exists(path.Path).Returns(true);
@@ -884,7 +915,9 @@ public class FileProviderServiceTests
public void DeleteFile_WhenNoDeletePermission_ShouldReturnUnauthorizedAccessError() public void DeleteFile_WhenNoDeletePermission_ShouldReturnUnauthorizedAccessError()
{ {
// Arrange // Arrange
FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\FileToDelete.txt"); FileSystemPathId path = _fileSystemPathIdFixture.CreateFileSystemPathId(
s_isLinux ? "/FileToDelete.txt" : @"C:\FileToDelete.txt"
);
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Delete).Returns(false); _mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Delete).Returns(false);
// Act // Act
@@ -16,6 +16,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
#endregion #endregion
namespace Lumina.Domain.UnitTests.Core.Aggregates.FileManagementAggregate.Services; namespace Lumina.Domain.UnitTests.Core.Aggregates.FileManagementAggregate.Services;
@@ -33,6 +35,20 @@ public class FileServiceTests
private readonly FileService _sut; private readonly FileService _sut;
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture; private readonly FileSystemPathIdFixture _fileSystemPathIdFixture;
private readonly FileFixture _fileFixture; private readonly FileFixture _fileFixture;
private static readonly bool s_isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
private readonly string _pathTestDir= s_isLinux ? "/TestDir" : @"C:\TestDir";
private readonly string _pathTestDirFile1 = s_isLinux ? "/TestDir/File1.txt" : @"C:\TestDir\File1.txt";
private readonly string _pathTestDirFile2 = s_isLinux ? "/TestDir/File2.txt" : @"C:\TestDir\File2.txt";
private readonly string _pathTestDirInaccessible = s_isLinux ? "/TestDir/InaccessibleFile.txt" : @"C:\TestDir\InaccessibleFile.txt";
private readonly string _pathDestination1 = s_isLinux ? "/Destination/" : @"C:\Destination\";
private readonly string _pathDestination2 = s_isLinux ? "/Destination" : @"C:\Destination";
private readonly string _pathSourceFile = s_isLinux ? "/Source/file.txt" : @"C:\Source\file.txt";
private readonly string _pathDestinationFile = s_isLinux ? "/Destination/file.txt" : @"C:\Destination\file.txt";
private readonly string _pathSourceOldFile = s_isLinux ? "/Source/oldfile.txt" : @"C:\Source\oldfile.txt";
private readonly string _pathSourceNewFile = s_isLinux ? "/Source/newfile.txt" : @"C:\Source\newfile.txt";
private readonly string _pathSourceExistingFile = s_isLinux ? "/Source/existingfile.txt" : @"C:\Source\existingfile.txt";
private readonly string _pathSourceNonExistingFile = s_isLinux ? "/Source/nonexistent.txt" : @"C:\Source\nonexistent.txt";
private readonly char _dirSeparator = s_isLinux ? '/' : '\\';
#endregion #endregion
#region ====================================================================== CTOR ===================================================================================== #region ====================================================================== CTOR =====================================================================================
@@ -56,13 +72,13 @@ public class FileServiceTests
public void GetFiles_WithValidPath_ShouldReturnListOfFiles() public void GetFiles_WithValidPath_ShouldReturnListOfFiles()
{ {
// Arrange // Arrange
string path = @"C:\TestDir"; string path = _pathTestDir;
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path);
FileSystemPathId[] filePaths = FileSystemPathId[] filePaths =
[ [
_fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\File1.txt"), _fileSystemPathIdFixture.CreateFileSystemPathId(_pathTestDirFile1),
_fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\File2.txt") _fileSystemPathIdFixture.CreateFileSystemPathId(_pathTestDirFile2)
]; ];
_mockEnvironmentContext.FileProviderService.GetFilePaths(pathId, includeHiddenElements) _mockEnvironmentContext.FileProviderService.GetFilePaths(pathId, includeHiddenElements)
@@ -108,7 +124,7 @@ public class FileServiceTests
public void GetFiles_WhenFileProviderServiceReturnsError_ShouldPropagateError() public void GetFiles_WhenFileProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
string path = @"C:\TestDir"; string path = _pathTestDir;
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path);
@@ -127,10 +143,10 @@ public class FileServiceTests
public void GetFiles_WhenFileDetailsAreInaccessible_ShouldReturnInaccessibleFile() public void GetFiles_WhenFileDetailsAreInaccessible_ShouldReturnInaccessibleFile()
{ {
// Arrange // Arrange
string path = @"C:\TestDir"; string path = _pathTestDir;
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path);
FileSystemPathId filePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\InaccessibleFile.txt"); FileSystemPathId filePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathTestDirInaccessible);
_mockEnvironmentContext.FileProviderService.GetFilePaths(pathId, includeHiddenElements) _mockEnvironmentContext.FileProviderService.GetFilePaths(pathId, includeHiddenElements)
.Returns(ErrorOrFactory.From(new[] { filePath }.AsEnumerable())); .Returns(ErrorOrFactory.From(new[] { filePath }.AsEnumerable()));
@@ -161,8 +177,8 @@ public class FileServiceTests
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId[] filePaths = FileSystemPathId[] filePaths =
[ [
_fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\File1.txt"), _fileSystemPathIdFixture.CreateFileSystemPathId(_pathTestDirFile1),
_fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\File2.txt") _fileSystemPathIdFixture.CreateFileSystemPathId(_pathTestDirFile2)
]; ];
_mockEnvironmentContext.FileProviderService.GetFilePaths(parentFile.Id, includeHiddenElements) _mockEnvironmentContext.FileProviderService.GetFilePaths(parentFile.Id, includeHiddenElements)
@@ -213,7 +229,7 @@ public class FileServiceTests
// Arrange // Arrange
File parentFile = _fileFixture.CreateFile(); File parentFile = _fileFixture.CreateFile();
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId filePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\InaccessibleFile.txt"); FileSystemPathId filePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathTestDirInaccessible);
_mockEnvironmentContext.FileProviderService.GetFilePaths(parentFile.Id, includeHiddenElements) _mockEnvironmentContext.FileProviderService.GetFilePaths(parentFile.Id, includeHiddenElements)
.Returns(ErrorOrFactory.From(new[] { filePath }.AsEnumerable())); .Returns(ErrorOrFactory.From(new[] { filePath }.AsEnumerable()));
@@ -240,12 +256,12 @@ public class FileServiceTests
public void GetFiles_WithValidFileSystemPathId_ShouldReturnListOfFiles() public void GetFiles_WithValidFileSystemPathId_ShouldReturnListOfFiles()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathTestDir);
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId[] filePaths = FileSystemPathId[] filePaths =
[ [
_fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\File1.txt"), _fileSystemPathIdFixture.CreateFileSystemPathId(_pathTestDirFile1),
_fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\File2.txt") _fileSystemPathIdFixture.CreateFileSystemPathId(_pathTestDirFile2)
]; ];
_mockEnvironmentContext.FileProviderService.GetFilePaths(pathId, includeHiddenElements) _mockEnvironmentContext.FileProviderService.GetFilePaths(pathId, includeHiddenElements)
@@ -276,7 +292,7 @@ public class FileServiceTests
public void GetFilesWithFileSystemPathId_WhenFileProviderServiceReturnsError_ShouldPropagateError() public void GetFilesWithFileSystemPathId_WhenFileProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathTestDir);
bool includeHiddenElements = false; bool includeHiddenElements = false;
_mockEnvironmentContext.FileProviderService.GetFilePaths(pathId, includeHiddenElements) _mockEnvironmentContext.FileProviderService.GetFilePaths(pathId, includeHiddenElements)
@@ -294,9 +310,9 @@ public class FileServiceTests
public void GetFilesWithFileSystemPathId_WhenFileDetailsAreInaccessible_ShouldReturnInaccessibleFile() public void GetFilesWithFileSystemPathId_WhenFileDetailsAreInaccessible_ShouldReturnInaccessibleFile()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathTestDir);
bool includeHiddenElements = false; bool includeHiddenElements = false;
FileSystemPathId filePath = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\TestDir\InaccessibleFile.txt"); FileSystemPathId filePath = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathTestDirInaccessible);
_mockEnvironmentContext.FileProviderService.GetFilePaths(pathId, includeHiddenElements) _mockEnvironmentContext.FileProviderService.GetFilePaths(pathId, includeHiddenElements)
.Returns(ErrorOrFactory.From(new[] { filePath }.AsEnumerable())); .Returns(ErrorOrFactory.From(new[] { filePath }.AsEnumerable()));
@@ -323,14 +339,14 @@ public class FileServiceTests
public void CopyFile_WithValidPaths_ShouldReturnCopiedFile() public void CopyFile_WithValidPaths_ShouldReturnCopiedFile()
{ {
// Arrange // Arrange
string sourcePath = @"C:\Source\file.txt"; string sourcePath = _pathSourceFile;
string destinationPath = @"C:\Destination\"; string destinationPath = _pathDestination1;
bool overrideExisting = true; bool overrideExisting = true;
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(destinationPath); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(destinationPath);
FileSystemPathId copiedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination\file.txt"); FileSystemPathId copiedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestinationFile);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), Arg.Any<string>()) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), Arg.Any<string>())
.Returns(copiedFilePathId); .Returns(copiedFilePathId);
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true);
@@ -357,8 +373,8 @@ public class FileServiceTests
public void CopyFile_WhenSourceDoesNotExist_ShouldReturnError() public void CopyFile_WhenSourceDoesNotExist_ShouldReturnError()
{ {
// Arrange // Arrange
string sourcePath = @"C:\Source\nonexistent.txt"; string sourcePath = _pathSourceNonExistingFile;
string destinationPath = @"C:\Destination\"; string destinationPath = _pathDestination1;
bool overrideExisting = true; bool overrideExisting = true;
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath);
@@ -376,13 +392,13 @@ public class FileServiceTests
public void CopyFile_WhenFileProviderServiceReturnsError_ShouldPropagateError() public void CopyFile_WhenFileProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
string sourcePath = @"C:\Source\file.txt"; string sourcePath = _pathSourceFile;
string destinationPath = @"C:\Destination\"; string destinationPath = _pathDestination1;
bool overrideExisting = true; bool overrideExisting = true;
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(destinationPath); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(destinationPath);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(ErrorOrFactory.From(true)); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(ErrorOrFactory.From(true));
_mockEnvironmentContext.FileProviderService.CopyFile(sourcePathId, destinationPathId, overrideExisting) _mockEnvironmentContext.FileProviderService.CopyFile(sourcePathId, destinationPathId, overrideExisting)
.Returns(Errors.Permission.UnauthorizedAccess); .Returns(Errors.Permission.UnauthorizedAccess);
@@ -399,14 +415,14 @@ public class FileServiceTests
public void CopyFile_WhenRetrievingFileInfoFails_ShouldReturnInaccessibleFile() public void CopyFile_WhenRetrievingFileInfoFails_ShouldReturnInaccessibleFile()
{ {
// Arrange // Arrange
string sourcePath = @"C:\Source\file.txt"; string sourcePath = _pathSourceFile;
string destinationPath = @"C:\Destination\"; string destinationPath = _pathDestination1;
bool overrideExisting = true; bool overrideExisting = true;
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(destinationPath); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(destinationPath);
FileSystemPathId copiedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination\file.txt"); FileSystemPathId copiedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestinationFile);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(ErrorOrFactory.From(true)); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(ErrorOrFactory.From(true));
_mockEnvironmentContext.FileProviderService.CopyFile(sourcePathId, destinationPathId, overrideExisting) _mockEnvironmentContext.FileProviderService.CopyFile(sourcePathId, destinationPathId, overrideExisting)
.Returns(ErrorOrFactory.From(copiedFilePathId)); .Returns(ErrorOrFactory.From(copiedFilePathId));
@@ -430,7 +446,7 @@ public class FileServiceTests
{ {
// Arrange // Arrange
string invalidSourcePath = string.Empty; string invalidSourcePath = string.Empty;
string destinationPath = @"C:\Destination\"; string destinationPath = _pathDestination1;
bool overrideExisting = true; bool overrideExisting = true;
// Act // Act
@@ -445,10 +461,10 @@ public class FileServiceTests
public void CopyFile_WithInvalidDestinationPath_ShouldReturnError() public void CopyFile_WithInvalidDestinationPath_ShouldReturnError()
{ {
// Arrange // Arrange
string sourcePath = @"C:\Source\file.txt"; string sourcePath = _pathSourceFile;
string invalidDestinationPath = string.Empty; string invalidDestinationPath = string.Empty;
bool overrideExisting = true; bool overrideExisting = true;
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
// Act // Act
ErrorOr<File> result = _sut.CopyFile(sourcePath, invalidDestinationPath, overrideExisting); ErrorOr<File> result = _sut.CopyFile(sourcePath, invalidDestinationPath, overrideExisting);
@@ -462,9 +478,9 @@ public class FileServiceTests
public void CopyFile_WithValidFileSystemPathIds_ShouldReturnCopiedFile() public void CopyFile_WithValidFileSystemPathIds_ShouldReturnCopiedFile()
{ {
// Arrange // Arrange
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination2);
FileSystemPathId copiedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination\file.txt"); FileSystemPathId copiedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestinationFile);
bool overrideExisting = true; bool overrideExisting = true;
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true);
@@ -491,8 +507,8 @@ public class FileServiceTests
public void CopyFileWithFileSystemPathIds_WhenSourceDoesNotExist_ShouldReturnError() public void CopyFileWithFileSystemPathIds_WhenSourceDoesNotExist_ShouldReturnError()
{ {
// Arrange // Arrange
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\nonexistent.txt"); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceNonExistingFile);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination2);
bool overrideExisting = true; bool overrideExisting = true;
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(false); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(false);
@@ -509,8 +525,8 @@ public class FileServiceTests
public void CopyFileWithFileSystemPathIds_WhenFileProviderServiceReturnsError_ShouldPropagateError() public void CopyFileWithFileSystemPathIds_WhenFileProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination2);
bool overrideExisting = true; bool overrideExisting = true;
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true);
@@ -529,9 +545,9 @@ public class FileServiceTests
public void CopyFileWithFileSystemPathIds_WhenRetrievingFileInfoFails_ShouldReturnInaccessibleFile() public void CopyFileWithFileSystemPathIds_WhenRetrievingFileInfoFails_ShouldReturnInaccessibleFile()
{ {
// Arrange // Arrange
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination2);
FileSystemPathId copiedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination\file.txt"); FileSystemPathId copiedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestinationFile);
bool overrideExisting = true; bool overrideExisting = true;
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true);
@@ -556,14 +572,14 @@ public class FileServiceTests
public void MoveFile_WithValidPaths_ShouldReturnMovedFile() public void MoveFile_WithValidPaths_ShouldReturnMovedFile()
{ {
// Arrange // Arrange
string sourcePath = @"C:\Source\file.txt"; string sourcePath = _pathSourceFile;
string destinationPath = @"C:\Destination\"; string destinationPath = _pathDestination1;
bool overrideExisting = true; bool overrideExisting = true;
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(destinationPath); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(destinationPath);
FileSystemPathId movedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination\file.txt"); FileSystemPathId movedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestinationFile);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), Arg.Any<string>()) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), Arg.Any<string>())
.Returns(movedFilePathId); .Returns(movedFilePathId);
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true);
@@ -590,12 +606,12 @@ public class FileServiceTests
public void MoveFile_WhenSourceDoesNotExist_ShouldReturnError() public void MoveFile_WhenSourceDoesNotExist_ShouldReturnError()
{ {
// Arrange // Arrange
string sourcePath = @"C:\Source\nonexistent.txt"; string sourcePath = _pathSourceNonExistingFile;
string destinationPath = @"C:\Destination"; string destinationPath = _pathDestination2;
bool overrideExisting = true; bool overrideExisting = true;
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(false); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(false);
// Act // Act
@@ -610,13 +626,13 @@ public class FileServiceTests
public void MoveFile_WhenFileProviderServiceReturnsError_ShouldPropagateError() public void MoveFile_WhenFileProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
string sourcePath = @"C:\Source\file.txt"; string sourcePath = _pathSourceFile;
string destinationPath = @"C:\Destination\"; string destinationPath = _pathDestination1;
bool overrideExisting = true; bool overrideExisting = true;
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(destinationPath); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(destinationPath);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true);
_mockEnvironmentContext.FileProviderService.MoveFile(sourcePathId, destinationPathId, overrideExisting) _mockEnvironmentContext.FileProviderService.MoveFile(sourcePathId, destinationPathId, overrideExisting)
.Returns(Errors.Permission.UnauthorizedAccess); .Returns(Errors.Permission.UnauthorizedAccess);
@@ -633,14 +649,14 @@ public class FileServiceTests
public void MoveFile_WhenRetrievingFileInfoFails_ShouldReturnInaccessibleFile() public void MoveFile_WhenRetrievingFileInfoFails_ShouldReturnInaccessibleFile()
{ {
// Arrange // Arrange
string sourcePath = @"C:\Source\file.txt"; string sourcePath = _pathSourceFile;
string destinationPath = @"C:\Destination\"; string destinationPath = _pathDestination1;
bool overrideExisting = true; bool overrideExisting = true;
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(destinationPath); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(destinationPath);
FileSystemPathId movedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination\file.txt"); FileSystemPathId movedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestinationFile);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), Arg.Any<string>()) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), Arg.Any<string>())
.Returns(movedFilePathId); .Returns(movedFilePathId);
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true);
@@ -666,10 +682,10 @@ public class FileServiceTests
{ {
// Arrange // Arrange
string invalidSourcePath = string.Empty; string invalidSourcePath = string.Empty;
string destinationPath = @"C:\Destination\"; string destinationPath = _pathDestination1;
bool overrideExisting = true; bool overrideExisting = true;
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
// Act // Act
ErrorOr<File> result = _sut.MoveFile(invalidSourcePath, destinationPath, overrideExisting); ErrorOr<File> result = _sut.MoveFile(invalidSourcePath, destinationPath, overrideExisting);
@@ -683,11 +699,11 @@ public class FileServiceTests
public void MoveFile_WithInvalidDestinationPath_ShouldReturnError() public void MoveFile_WithInvalidDestinationPath_ShouldReturnError()
{ {
// Arrange // Arrange
string sourcePath = @"C:\Source\file.txt"; string sourcePath = _pathSourceFile;
string invalidDestinationPath = string.Empty; string invalidDestinationPath = string.Empty;
bool overrideExisting = true; bool overrideExisting = true;
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
// Act // Act
ErrorOr<File> result = _sut.MoveFile(sourcePath, invalidDestinationPath, overrideExisting); ErrorOr<File> result = _sut.MoveFile(sourcePath, invalidDestinationPath, overrideExisting);
@@ -701,9 +717,9 @@ public class FileServiceTests
public void MoveFile_WithValidFileSystemPathIds_ShouldReturnMovedFile() public void MoveFile_WithValidFileSystemPathIds_ShouldReturnMovedFile()
{ {
// Arrange // Arrange
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination2);
FileSystemPathId movedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination\file.txt"); FileSystemPathId movedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestinationFile);
bool overrideExisting = true; bool overrideExisting = true;
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true);
@@ -730,8 +746,10 @@ public class FileServiceTests
public void MoveFileWithFileSystemPathIds_WhenSourceDoesNotExist_ShouldReturnError() public void MoveFileWithFileSystemPathIds_WhenSourceDoesNotExist_ShouldReturnError()
{ {
// Arrange // Arrange
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\nonexistent.txt"); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination"); s_isLinux ? "/Source/nonexistent.txt" : _pathSourceNonExistingFile
);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination2);
bool overrideExisting = true; bool overrideExisting = true;
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(false); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(false);
@@ -748,8 +766,8 @@ public class FileServiceTests
public void MoveFileWithFileSystemPathIds_WhenFileProviderServiceReturnsError_ShouldPropagateError() public void MoveFileWithFileSystemPathIds_WhenFileProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination\"); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination1);
bool overrideExisting = true; bool overrideExisting = true;
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true);
@@ -768,9 +786,9 @@ public class FileServiceTests
public void MoveFileWithFileSystemPathIds_WhenRetrievingFileInfoFails_ShouldReturnInaccessibleFile() public void MoveFileWithFileSystemPathIds_WhenRetrievingFileInfoFails_ShouldReturnInaccessibleFile()
{ {
// Arrange // Arrange
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination\"); FileSystemPathId destinationPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestination1);
FileSystemPathId movedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Destination\file.txt"); FileSystemPathId movedFilePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathDestinationFile);
bool overrideExisting = true; bool overrideExisting = true;
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true); _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId).Returns(true);
@@ -795,12 +813,12 @@ public class FileServiceTests
public void MoveFile_WhenFileExistsReturnsError_ShouldPropagateError() public void MoveFile_WhenFileExistsReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
string sourcePath = @"C:\Source\file.txt"; string sourcePath = _pathSourceFile;
string destinationPath = @"C:\Destination\"; string destinationPath = _pathDestination1;
bool overrideExisting = true; bool overrideExisting = true;
FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath); FileSystemPathId sourcePathId = _fileSystemPathIdFixture.CreateFileSystemPathId(sourcePath);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockEnvironmentContext.FileProviderService.FileExists(sourcePathId) _mockEnvironmentContext.FileProviderService.FileExists(sourcePathId)
.Returns(Errors.Permission.UnauthorizedAccess); .Returns(Errors.Permission.UnauthorizedAccess);
@@ -816,12 +834,12 @@ public class FileServiceTests
public void RenameFile_WithValidPathAndName_ShouldReturnRenamedFile() public void RenameFile_WithValidPathAndName_ShouldReturnRenamedFile()
{ {
// Arrange // Arrange
string path = @"C:\Source\oldfile.txt"; string path = _pathSourceOldFile;
string newName = "newfile.txt"; string newName = "newfile.txt";
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path);
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\newfile.txt"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceNewFile);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
_mockEnvironmentContext.FileProviderService.FileExists(newPathId).Returns(false); _mockEnvironmentContext.FileProviderService.FileExists(newPathId).Returns(false);
@@ -848,11 +866,11 @@ public class FileServiceTests
public void RenameFile_WhenNewNameAlreadyExists_ShouldReturnError() public void RenameFile_WhenNewNameAlreadyExists_ShouldReturnError()
{ {
// Arrange // Arrange
string path = @"C:\Source\oldfile.txt"; string path = _pathSourceOldFile;
string newName = "existingfile.txt"; string newName = "existingfile.txt";
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\existingfile.txt"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceExistingFile);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
_mockEnvironmentContext.FileProviderService.FileExists(newPathId).Returns(true); _mockEnvironmentContext.FileProviderService.FileExists(newPathId).Returns(true);
@@ -869,12 +887,12 @@ public class FileServiceTests
public void RenameFile_WhenFileProviderServiceReturnsError_ShouldPropagateError() public void RenameFile_WhenFileProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
string path = @"C:\Source\oldfile.txt"; string path = _pathSourceOldFile;
string newName = "newfile.txt"; string newName = "newfile.txt";
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path);
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\newfile.txt"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceNewFile);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
_mockEnvironmentContext.FileProviderService.FileExists(newPathId).Returns(false); _mockEnvironmentContext.FileProviderService.FileExists(newPathId).Returns(false);
@@ -893,12 +911,12 @@ public class FileServiceTests
public void RenameFile_WhenRetrievingFileInfoFails_ShouldReturnInaccessibleFile() public void RenameFile_WhenRetrievingFileInfoFails_ShouldReturnInaccessibleFile()
{ {
// Arrange // Arrange
string path = @"C:\Source\oldfile.txt"; string path = _pathSourceOldFile;
string newName = "newfile.txt"; string newName = "newfile.txt";
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path);
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\newfile.txt"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceNewFile);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
_mockEnvironmentContext.FileProviderService.FileExists(newPathId).Returns(false); _mockEnvironmentContext.FileProviderService.FileExists(newPathId).Returns(false);
@@ -926,7 +944,7 @@ public class FileServiceTests
string invalidPath = string.Empty; string invalidPath = string.Empty;
string newName = "newfile.txt"; string newName = "newfile.txt";
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
// Act // Act
ErrorOr<File> result = _sut.RenameFile(invalidPath, newName); ErrorOr<File> result = _sut.RenameFile(invalidPath, newName);
@@ -940,9 +958,9 @@ public class FileServiceTests
public void RenameFileWithValidFileSystemPathIdAndName_ShouldReturnRenamedFile() public void RenameFileWithValidFileSystemPathIdAndName_ShouldReturnRenamedFile()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\oldfile.txt"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceOldFile);
string newName = "newfile.txt"; string newName = "newfile.txt";
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\newfile.txt"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceNewFile);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
@@ -970,9 +988,9 @@ public class FileServiceTests
public void RenameFileWithFileSystemPathId_WhenNewNameAlreadyExists_ShouldReturnError() public void RenameFileWithFileSystemPathId_WhenNewNameAlreadyExists_ShouldReturnError()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\oldfile.txt"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceOldFile);
string newName = "existingfile.txt"; string newName = "existingfile.txt";
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\existingfile.txt"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceExistingFile);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
@@ -990,9 +1008,9 @@ public class FileServiceTests
public void RenameFileWithFileSystemPathId_WhenFileProviderServiceReturnsError_ShouldPropagateError() public void RenameFileWithFileSystemPathId_WhenFileProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\oldfile.txt"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceOldFile);
string newName = "newfile.txt"; string newName = "newfile.txt";
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\newfile.txt"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceNewFile);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
@@ -1012,9 +1030,9 @@ public class FileServiceTests
public void RenameFileWithFileSystemPathId_WhenRetrievingFileInfoFails_ShouldReturnInaccessibleFile() public void RenameFileWithFileSystemPathId_WhenRetrievingFileInfoFails_ShouldReturnInaccessibleFile()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\oldfile.txt"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceOldFile);
string newName = "newfile.txt"; string newName = "newfile.txt";
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\newfile.txt"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceNewFile);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
@@ -1040,10 +1058,10 @@ public class FileServiceTests
public void RenameFile_WhenCombinePathReturnsError_ShouldPropagateError() public void RenameFile_WhenCombinePathReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
string path = @"C:\Source\oldfile.txt"; string path = _pathSourceOldFile;
string newName = "newfile.txt"; string newName = "newfile.txt";
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(Errors.FileManagement.InvalidPath); .Returns(Errors.FileManagement.InvalidPath);
@@ -1059,11 +1077,11 @@ public class FileServiceTests
public void RenameFile_WhenFileExistsReturnsError_ShouldPropagateError() public void RenameFile_WhenFileExistsReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
string path = @"C:\Source\oldfile.txt"; string path = _pathSourceOldFile;
string newName = "newfile.txt"; string newName = "newfile.txt";
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\newfile.txt"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceNewFile);
_mockPlatformContext.PathStrategy.PathSeparator.Returns('\\'); _mockPlatformContext.PathStrategy.PathSeparator.Returns(_dirSeparator);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
_mockEnvironmentContext.FileProviderService.FileExists(newPathId) _mockEnvironmentContext.FileProviderService.FileExists(newPathId)
@@ -1081,7 +1099,7 @@ public class FileServiceTests
public void RenameFileWithFileSystemPathId_WhenCombinePathReturnsError_ShouldPropagateError() public void RenameFileWithFileSystemPathId_WhenCombinePathReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\oldfile.txt"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceOldFile);
string newName = "newfile.txt"; string newName = "newfile.txt";
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
@@ -1099,9 +1117,9 @@ public class FileServiceTests
public void RenameFileWithFileSystemPathId_WhenFileExistsReturnsError_ShouldPropagateError() public void RenameFileWithFileSystemPathId_WhenFileExistsReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\oldfile.txt"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceOldFile);
string newName = "newfile.txt"; string newName = "newfile.txt";
FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\newfile.txt"); FileSystemPathId newPathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceNewFile);
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName) _mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
.Returns(ErrorOrFactory.From(newPathId)); .Returns(ErrorOrFactory.From(newPathId));
@@ -1120,7 +1138,7 @@ public class FileServiceTests
public void DeleteFile_WithValidPath_ShouldReturnDeleted() public void DeleteFile_WithValidPath_ShouldReturnDeleted()
{ {
// Arrange // Arrange
string path = @"C:\Source\file.txt"; string path = _pathSourceFile;
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path);
_mockEnvironmentContext.FileProviderService.DeleteFile(pathId) _mockEnvironmentContext.FileProviderService.DeleteFile(pathId)
@@ -1152,7 +1170,7 @@ public class FileServiceTests
public void DeleteFile_WhenFileProviderServiceReturnsError_ShouldPropagateError() public void DeleteFile_WhenFileProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
string path = @"C:\Source\file.txt"; string path = _pathSourceFile;
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(path);
_mockEnvironmentContext.FileProviderService.DeleteFile(pathId) _mockEnvironmentContext.FileProviderService.DeleteFile(pathId)
@@ -1170,7 +1188,7 @@ public class FileServiceTests
public void DeleteFile_WithValidFileSystemPathId_ShouldReturnDeleted() public void DeleteFile_WithValidFileSystemPathId_ShouldReturnDeleted()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
_mockEnvironmentContext.FileProviderService.DeleteFile(pathId) _mockEnvironmentContext.FileProviderService.DeleteFile(pathId)
.Returns(Result.Deleted); .Returns(Result.Deleted);
@@ -1187,7 +1205,7 @@ public class FileServiceTests
public void DeleteFileWithFileSystemPathId_WhenFileProviderServiceReturnsError_ShouldPropagateError() public void DeleteFileWithFileSystemPathId_WhenFileProviderServiceReturnsError_ShouldPropagateError()
{ {
// Arrange // Arrange
FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(@"C:\Source\file.txt"); FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(_pathSourceFile);
_mockEnvironmentContext.FileProviderService.DeleteFile(pathId) _mockEnvironmentContext.FileProviderService.DeleteFile(pathId)
.Returns(Errors.Permission.UnauthorizedAccess); .Returns(Errors.Permission.UnauthorizedAccess);