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
+484 -325

No files matched your search

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