From b3db46ed5f169de2a883f2e1a620026c449bf441 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Fri, 11 Oct 2024 20:37:43 +0300 Subject: [PATCH] Adds hidden elements option to file system browser directories and paths --- .../GetDirectoryTreeQueryHandler.cs | 32 +++++++++------- .../CheckPathExists/CheckPathExistsQuery.cs | 5 ++- .../CheckPathExistsQueryHandler.cs | 6 +-- .../Services/IPathService.cs | 7 ++-- .../Services/PathService.cs | 5 ++- .../Strategies/Path/IPathStrategy.cs | 7 ++-- .../Strategies/Path/UnixPathStrategy.cs | 21 +++++++++- .../Strategies/Path/WindowsPathStrategy.cs | 21 +++++++++- .../FileManagement/PathController.cs | 5 ++- .../FileManagement/PathController.cs | 5 ++- .../wwwroot/js/file-system-browser-dialog.js | 2 +- .../GetDirectoryTreeQueryHandlerTests.cs | 6 ++- .../CheckPathExistsQueryHandlerTests.cs | 38 +++++++++++++++++-- .../Fixtures/CheckPathExistsQueryFixture.cs | 9 +++-- .../Services/PathServiceTests.cs | 32 ++++++++++++++++ .../Strategies/Path/UnixPathStrategyTests.cs | 36 +++++++++++++++--- .../Path/WindowsPathStrategyTests.cs | 33 +++++++++++++--- .../FileManagement/PathControllerTests.cs | 6 +-- 18 files changed, 217 insertions(+), 59 deletions(-) diff --git a/src/Lumina.Application/Core/FileManagement/Directories/Queries/GetDirectoryTree/GetDirectoryTreeQueryHandler.cs b/src/Lumina.Application/Core/FileManagement/Directories/Queries/GetDirectoryTree/GetDirectoryTreeQueryHandler.cs index 732c071a..b723221f 100644 --- a/src/Lumina.Application/Core/FileManagement/Directories/Queries/GetDirectoryTree/GetDirectoryTreeQueryHandler.cs +++ b/src/Lumina.Application/Core/FileManagement/Directories/Queries/GetDirectoryTree/GetDirectoryTreeQueryHandler.cs @@ -67,7 +67,7 @@ public class GetDirectoryTreeQueryHandler : IRequestHandler pathParts = pathPartsResults.Value.ToList(); - ErrorOr> buildDirectoryTreeResult = BuildDirectoryTree(result, pathParts, out FileSystemTreeNodeResponse? currentNode); + ErrorOr> buildDirectoryTreeResult = BuildDirectoryTree(result, pathParts, out FileSystemTreeNodeResponse? currentNode, request.IncludeHiddenElements); if (buildDirectoryTreeResult.IsError) return buildDirectoryTreeResult.Errors; @@ -97,10 +97,11 @@ public class GetDirectoryTreeQueryHandler : IRequestHandler /// The list of available drives. /// The list of parsed path segments (directories). + /// Whether to include hidden file system elements or not. /// /// An containing a list of representing the directory tree, or an error message. /// - private ErrorOr> BuildDirectoryTree(List drives, List pathParts, out FileSystemTreeNodeResponse currentNode) + private ErrorOr> BuildDirectoryTree(List drives, List pathParts, out FileSystemTreeNodeResponse currentNode, bool includeHiddenElements = true) { // find the root drive that matches the first part of the path FileSystemTreeNodeResponse drive = drives.First(drive => drive.Name.Contains(pathParts[0].Name)); @@ -114,18 +115,21 @@ public class GetDirectoryTreeQueryHandler : IRequestHandler combinedPathResult = _pathService.CombinePath(currentPath, pathParts[i].Name); if (combinedPathResult.IsError) return combinedPathResult.Errors; - - currentPath = combinedPathResult.Value; - // create a new node for the directory and add it as a child of the current node - FileSystemTreeNodeResponse newNode = new() + if (_pathService.Exists(combinedPathResult.Value, includeHiddenElements)) { - Path = currentPath, - Name = pathParts[i].Name, - ItemType = FileSystemItemType.Directory, - IsExpanded = true - }; - currentNode.Children.Add(newNode); - currentNode = newNode; // move to the new node + + currentPath = combinedPathResult.Value; + // create a new node for the directory and add it as a child of the current node + FileSystemTreeNodeResponse newNode = new() + { + Path = currentPath, + Name = pathParts[i].Name, + ItemType = FileSystemItemType.Directory, + IsExpanded = true + }; + currentNode.Children.Add(newNode); + currentNode = newNode; // move to the new node + } } return new List { drive }; } @@ -143,7 +147,7 @@ public class GetDirectoryTreeQueryHandler : IRequestHandler /// The path for which to check the existence. -public record CheckPathExistsQuery(string Path) : IRequest; \ No newline at end of file +/// Whether to include hidden file system elements or not. +public record CheckPathExistsQuery(string Path, bool IncludeHiddenElements) : IRequest; diff --git a/src/Lumina.Application/Core/FileManagement/Paths/Queries/CheckPathExists/CheckPathExistsQueryHandler.cs b/src/Lumina.Application/Core/FileManagement/Paths/Queries/CheckPathExists/CheckPathExistsQueryHandler.cs index 6c73b901..bd158ed2 100644 --- a/src/Lumina.Application/Core/FileManagement/Paths/Queries/CheckPathExists/CheckPathExistsQueryHandler.cs +++ b/src/Lumina.Application/Core/FileManagement/Paths/Queries/CheckPathExists/CheckPathExistsQueryHandler.cs @@ -1,4 +1,4 @@ -#region ========================================================================= USING ===================================================================================== +#region ========================================================================= USING ===================================================================================== using Lumina.Contracts.Responses.FileManagement; using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services; using Mediator; @@ -39,7 +39,7 @@ public class CheckPathExistsQueryHandler : IRequestHandler public ValueTask Handle(CheckPathExistsQuery request, CancellationToken cancellationToken) { - return ValueTask.FromResult(new PathExistsResponse(_pathService.Exists(request.Path))); + return ValueTask.FromResult(new PathExistsResponse(_pathService.Exists(request.Path, request.IncludeHiddenElements))); } #endregion -} \ No newline at end of file +} diff --git a/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Services/IPathService.cs b/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Services/IPathService.cs index 1e258251..b34f442a 100644 --- a/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Services/IPathService.cs +++ b/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Services/IPathService.cs @@ -1,4 +1,4 @@ -#region ========================================================================= USING ===================================================================================== +#region ========================================================================= USING ===================================================================================== using ErrorOr; using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects; using System.Collections.Generic; @@ -30,8 +30,9 @@ public interface IPathService /// Checks if exists. /// /// The path to be checked. + /// Whether to include hidden file system elements or not. /// if exists, otherwise. - bool Exists(string path); + bool Exists(string path, bool includeHiddenElements = true); /// /// Tries to combine with . @@ -68,4 +69,4 @@ public interface IPathService /// An containing the root of , or an error. ErrorOr GetPathRoot(string path); #endregion -} \ No newline at end of file +} diff --git a/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Services/PathService.cs b/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Services/PathService.cs index cb3cf783..a1bc87bc 100644 --- a/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Services/PathService.cs +++ b/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Services/PathService.cs @@ -53,13 +53,14 @@ public class PathService : IPathService /// Checks if exists. /// /// The path to be checked. + /// Whether to include hidden file system elements or not. /// if exists, otherwise. - public bool Exists(string path) + public bool Exists(string path, bool includeHiddenElements = true) { ErrorOr newPathResult = FileSystemPathId.Create(path); if (newPathResult.IsError) return false; - return _platformContext.PathStrategy.Exists(newPathResult.Value); + return _platformContext.PathStrategy.Exists(newPathResult.Value, includeHiddenElements); } /// diff --git a/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Strategies/Path/IPathStrategy.cs b/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Strategies/Path/IPathStrategy.cs index b099942e..ea4d79ac 100644 --- a/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Strategies/Path/IPathStrategy.cs +++ b/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Strategies/Path/IPathStrategy.cs @@ -1,4 +1,4 @@ -#region ========================================================================= USING ===================================================================================== +#region ========================================================================= USING ===================================================================================== using ErrorOr; using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects; using System.Collections.Generic; @@ -27,8 +27,9 @@ public interface IPathStrategy /// Checks if exists. /// /// The path to be checked. + /// Whether to include hidden file system elements or not. /// if exists, otherwise. - bool Exists(FileSystemPathId path); + bool Exists(FileSystemPathId path, bool includeHiddenElements = true); /// /// Tries to combine with . @@ -65,4 +66,4 @@ public interface IPathStrategy /// An containing the root of , or an error. ErrorOr GetPathRoot(FileSystemPathId path); #endregion -} \ No newline at end of file +} diff --git a/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Strategies/Path/UnixPathStrategy.cs b/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Strategies/Path/UnixPathStrategy.cs index 8ed60045..41fa6cc6 100644 --- a/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Strategies/Path/UnixPathStrategy.cs +++ b/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Strategies/Path/UnixPathStrategy.cs @@ -4,6 +4,7 @@ using Lumina.Domain.Common.Errors; using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects; using System; using System.Collections.Generic; +using System.IO; using System.IO.Abstractions; using System.Linq; using System.Text.RegularExpressions; @@ -58,10 +59,26 @@ public class UnixPathStrategy : IUnixPathStrategy /// Checks if exists. /// /// The path to be checked. + /// Whether to include hidden file system elements or not. /// if exists, otherwise. - public bool Exists(FileSystemPathId path) + public bool Exists(FileSystemPathId path, bool includeHiddenElements = true) { - return _fileSystem.Path.Exists(path.Path); + if (!_fileSystem.Path.Exists(path.Path)) + return false; + bool isHidden; + if (_fileSystem.Directory.Exists(path.Path)) + { + IDirectoryInfo dirInfo = _fileSystem.DirectoryInfo.New(path.Path); + isHidden = (dirInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden; + } + else if (_fileSystem.File.Exists(path.Path)) + { + IFileInfo fileInfo = _fileSystem.FileInfo.New(path.Path); + isHidden = (fileInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden; + } + else // path exists but is neither a file nor a directory (drive, etc) + return true; + return includeHiddenElements || !isHidden; } /// diff --git a/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Strategies/Path/WindowsPathStrategy.cs b/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Strategies/Path/WindowsPathStrategy.cs index eecd8085..9e1336fe 100644 --- a/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Strategies/Path/WindowsPathStrategy.cs +++ b/src/Lumina.Domain/Core/Aggregates/FileManagement/FileManagementAggregate/Strategies/Path/WindowsPathStrategy.cs @@ -4,6 +4,7 @@ using Lumina.Domain.Common.Errors; using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects; using System; using System.Collections.Generic; +using System.IO; using System.IO.Abstractions; using System.Linq; using System.Text.RegularExpressions; @@ -57,10 +58,26 @@ public class WindowsPathStrategy : IWindowsPathStrategy /// Checks if exists. /// /// The path to be checked. + /// Whether to include hidden file system elements or not. /// if exists, otherwise. - public bool Exists(FileSystemPathId path) + public bool Exists(FileSystemPathId path, bool includeHiddenElements = true) { - return _fileSystem.Path.Exists(path.Path); + if (!_fileSystem.Path.Exists(path.Path)) + return false; + bool isHidden; + if (_fileSystem.Directory.Exists(path.Path)) + { + IDirectoryInfo dirInfo = _fileSystem.DirectoryInfo.New(path.Path); + isHidden = (dirInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden; + } + else if (_fileSystem.File.Exists(path.Path)) + { + IFileInfo fileInfo = _fileSystem.FileInfo.New(path.Path); + isHidden = (fileInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden; + } + else // path exists but is neither a file nor a directory (drive, etc) + return true; + return includeHiddenElements || !isHidden; } /// diff --git a/src/Lumina.Presentation.Api/Core/Controllers/FileManagement/PathController.cs b/src/Lumina.Presentation.Api/Core/Controllers/FileManagement/PathController.cs index 5e61f9ca..888e8ddc 100644 --- a/src/Lumina.Presentation.Api/Core/Controllers/FileManagement/PathController.cs +++ b/src/Lumina.Presentation.Api/Core/Controllers/FileManagement/PathController.cs @@ -118,11 +118,12 @@ public class PathController : ApiController /// Checks whether exists or not. /// /// The path to be checked. + /// Whether to include hidden file system elements or not. /// Cancellation token that can be used to stop the execution. [HttpGet("check-path-exists")] - public async Task CheckPathExists([FromQuery, ModelBinder(typeof(UrlStringBinder))] string path, CancellationToken cancellationToken) + public async Task CheckPathExists([FromQuery, ModelBinder(typeof(UrlStringBinder))] string path, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken) { - PathExistsResponse result = await _mediator.Send(new CheckPathExistsQuery(path), cancellationToken).ConfigureAwait(false); + PathExistsResponse result = await _mediator.Send(new CheckPathExistsQuery(path, includeHiddenElements), cancellationToken).ConfigureAwait(false); return Ok(result); } #endregion diff --git a/src/Lumina.Presentation.Web/Controllers/FileManagement/PathController.cs b/src/Lumina.Presentation.Web/Controllers/FileManagement/PathController.cs index 9227db57..ab031803 100644 --- a/src/Lumina.Presentation.Web/Controllers/FileManagement/PathController.cs +++ b/src/Lumina.Presentation.Web/Controllers/FileManagement/PathController.cs @@ -94,11 +94,12 @@ public class PathController : Controller /// Checks whether exists or not. /// /// The path to be checked. + /// Whether to include hidden file system elements or not. /// Cancellation token that can be used to stop the execution. [HttpGet("check-path-exists")] - public async Task CheckPathExists([FromQuery] string path, CancellationToken cancellationToken) + public async Task CheckPathExists([FromQuery] string path, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken) { - PathExistsModel response = await _apiHttpClient.GetAsync($"path/check-path-exists?path={Uri.EscapeDataString(path)}", cancellationToken: cancellationToken); + PathExistsModel response = await _apiHttpClient.GetAsync($"path/check-path-exists?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken: cancellationToken); return Json(new { success = true, data = new { exists = response.Exists } }); } #endregion diff --git a/src/Lumina.Presentation.Web/wwwroot/js/file-system-browser-dialog.js b/src/Lumina.Presentation.Web/wwwroot/js/file-system-browser-dialog.js index 828fd094..1d48f684 100644 --- a/src/Lumina.Presentation.Web/wwwroot/js/file-system-browser-dialog.js +++ b/src/Lumina.Presentation.Web/wwwroot/js/file-system-browser-dialog.js @@ -238,7 +238,7 @@ async function checkPathAsync(path) { if (addressBarInput.value !== null && !addressBarInput.value.endsWith(pathSeparator)) addressBarInput.value += pathSeparator; } else { - const pathExistsResponse = await callApiGetAsync(`${clientBasePath}path/check-path-exists?path=${encodeURIComponent(path)}`); + const pathExistsResponse = await callApiGetAsync(`${clientBasePath}path/check-path-exists?path=${encodeURIComponent(path)}&includeHiddenElements=${showHiddenElements}`); if (pathExistsResponse !== undefined && !pathExistsResponse.exists) { notificationService.show("Specified path does not exist!", NotificationType.ERROR); // TODO: should ask error message from server when translation is implemented return false; diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Core/FileManagement/Directories/Queries/GetDirectoryTree/GetDirectoryTreeQueryHandlerTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Core/FileManagement/Directories/Queries/GetDirectoryTree/GetDirectoryTreeQueryHandlerTests.cs index a4e0accd..4d43f598 100644 --- a/tests/UnitTests/Lumina.Application.UnitTests/Core/FileManagement/Directories/Queries/GetDirectoryTree/GetDirectoryTreeQueryHandlerTests.cs +++ b/tests/UnitTests/Lumina.Application.UnitTests/Core/FileManagement/Directories/Queries/GetDirectoryTree/GetDirectoryTreeQueryHandlerTests.cs @@ -196,10 +196,10 @@ public class GetDirectoryTreeQueryHandlerTests IEnumerable pathSegments = new[] { firstPathSegment }.Concat(_pathSegmentFixture.CreateMany(2)); Error error = Error.Failure("PathService.Error", "An error occurred"); - _mockDriveService.GetDrives().Returns(ErrorOrFactory.From(drives)); + _mockDriveService.GetDrives().Returns(ErrorOrFactory.From(drives)); _mockPathService.ParsePath(query.Path).Returns(ErrorOrFactory.From(pathSegments)); _mockPathService.CombinePath(Arg.Any(), Arg.Any()).Returns(error); - + // Act ErrorOr> result = await _sut.Handle(query, CancellationToken.None); @@ -313,6 +313,7 @@ public class GetDirectoryTreeQueryHandlerTests _mockDriveService.GetDrives().Returns(ErrorOrFactory.From(drives)); _mockPathService.ParsePath(query.Path).Returns(ErrorOrFactory.From((IEnumerable)pathSegments)); + _mockPathService.Exists(Arg.Any(), Arg.Any()).Returns(true); _mockPathService.CombinePath(Arg.Any(), Arg.Any()) .Returns(callInfo => $"{callInfo.ArgAt(0)}\\{callInfo.ArgAt(1)}"); @@ -350,6 +351,7 @@ public class GetDirectoryTreeQueryHandlerTests ]; _mockDriveService.GetDrives().Returns(ErrorOrFactory.From(drives)); + _mockPathService.Exists(Arg.Any(), Arg.Any()).Returns(true); _mockPathService.ParsePath(query.Path).Returns(ErrorOrFactory.From((IEnumerable)pathSegments)); _mockPathService.CombinePath(Arg.Any(), Arg.Any()) .Returns(callInfo => $"{callInfo.ArgAt(0)}\\{callInfo.ArgAt(1)}"); diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Core/FileManagement/Pahs/Queries/CheckPathExists/CheckPathExistsQueryHandlerTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Core/FileManagement/Pahs/Queries/CheckPathExists/CheckPathExistsQueryHandlerTests.cs index 0fcf9e4b..87941d3b 100644 --- a/tests/UnitTests/Lumina.Application.UnitTests/Core/FileManagement/Pahs/Queries/CheckPathExists/CheckPathExistsQueryHandlerTests.cs +++ b/tests/UnitTests/Lumina.Application.UnitTests/Core/FileManagement/Pahs/Queries/CheckPathExists/CheckPathExistsQueryHandlerTests.cs @@ -36,7 +36,7 @@ public class CheckPathExistsQueryHandlerTests #region ===================================================================== METHODS =================================================================================== [Fact] - public async Task Handle_WhenPathExists_ShouldReturnTrueResponse() + public async Task Handle_WhenPathExistsAndIsNotHidden_ShouldReturnTrueResponse() { // Arrange CheckPathExistsQuery query = CheckPathExistsQueryFixture.CreateCheckPathExistsQuery(); @@ -50,6 +50,36 @@ public class CheckPathExistsQueryHandlerTests _mockPathService.Received(1).Exists(query.Path); } + [Fact] + public async Task Handle_WhenPathExistsAndIsHiddenAndIncludeHiddenElementsIsTrue_ShouldReturnTrueResponse() + { + // Arrange + CheckPathExistsQuery query = CheckPathExistsQueryFixture.CreateCheckPathExistsQuery(true); + _mockPathService.Exists(query.Path).Returns(true); + + // Act + PathExistsResponse result = await _sut.Handle(query, CancellationToken.None); + + // Assert + result.Exists.Should().BeTrue(); + _mockPathService.Received(1).Exists(query.Path); + } + + [Fact] + public async Task Handle_WhenPathExistsAndIsHiddenAndIncludeHiddenElementsIsFalse_ShouldReturnFalseResponse() + { + // Arrange + CheckPathExistsQuery query = CheckPathExistsQueryFixture.CreateCheckPathExistsQuery(false); + _mockPathService.Exists(query.Path, false).Returns(false); + + // Act + PathExistsResponse result = await _sut.Handle(query, CancellationToken.None); + + // Assert + result.Exists.Should().BeFalse(); + _mockPathService.Received(1).Exists(query.Path, false); + } + [Fact] public async Task Handle_WhenPathDoesNotExist_ShouldReturnFalseResponse() { @@ -69,15 +99,15 @@ public class CheckPathExistsQueryHandlerTests public async Task Handle_WhenCalledWithNullPath_ShouldStillCallPathService() { // Arrange - CheckPathExistsQuery query = new(null!); - _mockPathService.Exists(Arg.Any()).Returns(false); + CheckPathExistsQuery query = new(null!, false); + _mockPathService.Exists(Arg.Any(), Arg.Any()).Returns(false); // Act PathExistsResponse result = await _sut.Handle(query, CancellationToken.None); // Assert result.Exists.Should().BeFalse(); - _mockPathService.Received(1).Exists(Arg.Any()); + _mockPathService.Received(1).Exists(Arg.Any(), Arg.Any()); } [Fact] diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Core/FileManagement/Pahs/Queries/CheckPathExists/Fixtures/CheckPathExistsQueryFixture.cs b/tests/UnitTests/Lumina.Application.UnitTests/Core/FileManagement/Pahs/Queries/CheckPathExists/Fixtures/CheckPathExistsQueryFixture.cs index b8323566..c8b03454 100644 --- a/tests/UnitTests/Lumina.Application.UnitTests/Core/FileManagement/Pahs/Queries/CheckPathExists/Fixtures/CheckPathExistsQueryFixture.cs +++ b/tests/UnitTests/Lumina.Application.UnitTests/Core/FileManagement/Pahs/Queries/CheckPathExists/Fixtures/CheckPathExistsQueryFixture.cs @@ -16,14 +16,17 @@ public class CheckPathExistsQueryFixture /// /// Creates a random valid query to check if paths exist. /// + /// Whether to include hidden file system elements or not. /// The created query. - public static CheckPathExistsQuery CreateCheckPathExistsQuery() + public static CheckPathExistsQuery CreateCheckPathExistsQuery(bool includeHiddenElements = true) { return new Faker() .CustomInstantiator(f => new CheckPathExistsQuery( - default! + default!, + default )) - .RuleFor(x => x.Path, f => f.System.FilePath()); + .RuleFor(x => x.Path, f => f.System.FilePath()) + .RuleFor(x => x.IncludeHiddenElements, includeHiddenElements); } #endregion } diff --git a/tests/UnitTests/Lumina.Domain.UnitTests/Core/Aggregates/FileManagementAggregate/Services/PathServiceTests.cs b/tests/UnitTests/Lumina.Domain.UnitTests/Core/Aggregates/FileManagementAggregate/Services/PathServiceTests.cs index 2f19c721..6761f802 100644 --- a/tests/UnitTests/Lumina.Domain.UnitTests/Core/Aggregates/FileManagementAggregate/Services/PathServiceTests.cs +++ b/tests/UnitTests/Lumina.Domain.UnitTests/Core/Aggregates/FileManagementAggregate/Services/PathServiceTests.cs @@ -124,6 +124,38 @@ public class PathServiceTests _mockPathStrategy.Received(1).Exists(Arg.Is(id => id.Path == existingPath)); } + [Fact] + public void Exists_WhenPathIsHiddenAndIncludeHiddenElementsIsTrue_ShouldReturnTrue() + { + // Arrange + string existingPath = @"C:\ExistingPath"; + FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(existingPath); + _mockPathStrategy.Exists(pathId, true).Returns(true); + + // Act + bool result = _sut.Exists(existingPath, true); + + // Assert + result.Should().BeTrue(); + _mockPathStrategy.Received(1).Exists(Arg.Is(id => id.Path == existingPath)); + } + + [Fact] + public void Exists_WhenPathIsHiddenAndIncludeHiddenElementsIsFalse_ShouldReturnFalse() + { + // Arrange + string existingPath = @"C:\ExistingPath"; + FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(existingPath); + _mockPathStrategy.Exists(pathId, false).Returns(false); + + // Act + bool result = _sut.Exists(existingPath, false); + + // Assert + result.Should().BeFalse(); + _mockPathStrategy.Received(1).Exists(Arg.Is(id => id.Path == existingPath), false); + } + [Fact] public void Exists_WithValidNonExistingPath_ShouldReturnFalse() { diff --git a/tests/UnitTests/Lumina.Domain.UnitTests/Core/Aggregates/FileManagementAggregate/Strategies/Path/UnixPathStrategyTests.cs b/tests/UnitTests/Lumina.Domain.UnitTests/Core/Aggregates/FileManagementAggregate/Strategies/Path/UnixPathStrategyTests.cs index 7b4b1276..fa2dc152 100644 --- a/tests/UnitTests/Lumina.Domain.UnitTests/Core/Aggregates/FileManagementAggregate/Strategies/Path/UnixPathStrategyTests.cs +++ b/tests/UnitTests/Lumina.Domain.UnitTests/Core/Aggregates/FileManagementAggregate/Strategies/Path/UnixPathStrategyTests.cs @@ -128,18 +128,40 @@ public class UnixPathStrategyTests } [Fact] - public void Exists_WhenPathExists_ShouldReturnTrue() + public void Exists_WhenPathExistsAndIsHiddenAndIncludeHiddenElementsIsTrue_ShouldReturnTrue() + { + // Arrange + string existingPath = "/home/user/existing_file.txt"; + FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(existingPath); + + _mockFileSystem.Path.Exists(existingPath).Returns(true); + _mockFileSystem.File.Exists(existingPath).Returns(true); + + // Act + bool result = _sut.Exists(pathId, true); + + // Assert + result.Should().BeTrue(); + _mockFileSystem.Path.Received(1).Exists(existingPath); + } + + [Fact] + public void Exists_WhenPathExistsAndIsHiddenAndIncludeHiddenElementsIsFalse_ShouldReturnFalse() { // Arrange string existingPath = "/home/user/existing_file.txt"; FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(existingPath); _mockFileSystem.Path.Exists(existingPath).Returns(true); + _mockFileSystem.File.Exists(existingPath).Returns(true); + IFileInfo mockFileInfo = Substitute.For(); + mockFileInfo.Attributes.Returns(System.IO.FileAttributes.Hidden); + _mockFileSystem.FileInfo.New(Arg.Any()).Returns(mockFileInfo); // Act - bool result = _sut.Exists(pathId); + bool result = _sut.Exists(pathId, false); // Assert - result.Should().BeTrue(); + result.Should().BeFalse(); _mockFileSystem.Path.Received(1).Exists(existingPath); } @@ -168,7 +190,7 @@ public class UnixPathStrategyTests _mockFileSystem.Path.Exists(rootPath).Returns(true); // Act - bool result = _sut.Exists(pathId); + bool result = _sut.Exists(pathId, true); // Assert result.Should().BeTrue(); @@ -182,9 +204,10 @@ public class UnixPathStrategyTests string directoryPath = "/home/user/documents/"; FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(directoryPath); _mockFileSystem.Path.Exists(directoryPath).Returns(true); + _mockFileSystem.Directory.Exists(directoryPath).Returns(true); // Act - bool result = _sut.Exists(pathId); + bool result = _sut.Exists(pathId, true); // Assert result.Should().BeTrue(); @@ -198,9 +221,10 @@ public class UnixPathStrategyTests string filePath = "/home/user/documents/file.txt"; FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(filePath); _mockFileSystem.Path.Exists(filePath).Returns(true); + _mockFileSystem.File.Exists(filePath).Returns(true); // Act - bool result = _sut.Exists(pathId); + bool result = _sut.Exists(pathId, true); // Assert result.Should().BeTrue(); diff --git a/tests/UnitTests/Lumina.Domain.UnitTests/Core/Aggregates/FileManagementAggregate/Strategies/Path/WindowsPathStrategyTests.cs b/tests/UnitTests/Lumina.Domain.UnitTests/Core/Aggregates/FileManagementAggregate/Strategies/Path/WindowsPathStrategyTests.cs index b07b50df..ad1c2a3a 100644 --- a/tests/UnitTests/Lumina.Domain.UnitTests/Core/Aggregates/FileManagementAggregate/Strategies/Path/WindowsPathStrategyTests.cs +++ b/tests/UnitTests/Lumina.Domain.UnitTests/Core/Aggregates/FileManagementAggregate/Strategies/Path/WindowsPathStrategyTests.cs @@ -156,21 +156,42 @@ public class WindowsPathStrategyTests } [Fact] - public void Exists_WhenPathExists_ShouldReturnTrue() + public void Exists_WhenPathExistsAndIsHiddenAndIncludeHiddenElementsIsTrue_ShouldReturnTrue() { // Arrange string existingPath = @"C:\Users\User\existing_file.txt"; FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(existingPath); _mockFileSystem.Path.Exists(existingPath).Returns(true); + _mockFileSystem.File.Exists(existingPath).Returns(true); // Act - bool result = _sut.Exists(pathId); + bool result = _sut.Exists(pathId, true); // Assert result.Should().BeTrue(); _mockFileSystem.Path.Received(1).Exists(existingPath); } + [Fact] + public void Exists_WhenPathExistsAndIsHiddenAndIncludeHiddenElementsIsFalse_ShouldReturnFalse() + { + // Arrange + string existingPath = @"C:\Users\User\existing_file.txt"; + FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(existingPath); + _mockFileSystem.Path.Exists(existingPath).Returns(true); + _mockFileSystem.File.Exists(existingPath).Returns(true); + IFileInfo mockFileInfo = Substitute.For(); + mockFileInfo.Attributes.Returns(System.IO.FileAttributes.Hidden); + _mockFileSystem.FileInfo.New(Arg.Any()).Returns(mockFileInfo); + + // Act + bool result = _sut.Exists(pathId, false); + + // Assert + result.Should().BeFalse(); + _mockFileSystem.Path.Received(1).Exists(existingPath); + } + [Fact] public void Exists_WhenPathDoesNotExist_ShouldReturnFalse() { @@ -210,9 +231,10 @@ public class WindowsPathStrategyTests string directoryPath = @"C:\Users\User\Documents\"; FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(directoryPath); _mockFileSystem.Path.Exists(directoryPath).Returns(true); + _mockFileSystem.Directory.Exists(directoryPath).Returns(true); // Act - bool result = _sut.Exists(pathId); + bool result = _sut.Exists(pathId, true); // Assert result.Should().BeTrue(); @@ -226,9 +248,10 @@ public class WindowsPathStrategyTests string filePath = @"C:\Users\User\Documents\file.txt"; FileSystemPathId pathId = _fileSystemPathIdFixture.CreateFileSystemPathId(filePath); _mockFileSystem.Path.Exists(filePath).Returns(true); + _mockFileSystem.File.Exists(filePath).Returns(true); // Act - bool result = _sut.Exists(pathId); + bool result = _sut.Exists(pathId, true); // Assert result.Should().BeTrue(); @@ -244,7 +267,7 @@ public class WindowsPathStrategyTests _mockFileSystem.Path.Exists(uncPath).Returns(true); // Act - bool result = _sut.Exists(pathId); + bool result = _sut.Exists(pathId, true); // Assert result.Should().BeTrue(); diff --git a/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/PathControllerTests.cs b/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/PathControllerTests.cs index f6e30333..7c0ce137 100644 --- a/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/PathControllerTests.cs +++ b/tests/UnitTests/Lumina.Presentation.Api.UnitTests/Core/Controllers/FileManagement/PathControllerTests.cs @@ -634,7 +634,7 @@ public class PathControllerTests .Returns(expectedResponse); // Act - IActionResult result = await _sut.CheckPathExists(path, cancellationToken); + IActionResult result = await _sut.CheckPathExists(path, true, cancellationToken); // Assert OkObjectResult okResult = result.Should().BeOfType().Subject; @@ -652,7 +652,7 @@ public class PathControllerTests .Returns(new PathExistsResponse(true)); // Act - await _sut.CheckPathExists(path, cancellationToken); + await _sut.CheckPathExists(path, true, cancellationToken); // Assert await _mockMediator.Received(1).Send(Arg.Is(q => q.Path == path), Arg.Is(cancellationToken)); @@ -674,7 +674,7 @@ public class PathControllerTests // Act & Assert cts.CancelAfter(50); - await Assert.ThrowsAnyAsync(() => _sut.CheckPathExists(path, cts.Token)); + await Assert.ThrowsAnyAsync(() => _sut.CheckPathExists(path, true, cts.Token)); } #endregion }