Adds hidden elements option to file system browser directories and paths

This commit is contained in:
BinaryAlley committed 2024-10-11 20:37:43 +03:00
1 parent 15a1881495
commit b3db46ed5f
18 files changed
+217 -59

No files matched your search

@@ -196,10 +196,10 @@ public class GetDirectoryTreeQueryHandlerTests
IEnumerable<PathSegment> 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<string>(), Arg.Any<string>()).Returns(error);
// Act
ErrorOr<IEnumerable<FileSystemTreeNodeResponse>> 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<PathSegment>)pathSegments));
_mockPathService.Exists(Arg.Any<string>(), Arg.Any<bool>()).Returns(true);
_mockPathService.CombinePath(Arg.Any<string>(), Arg.Any<string>())
.Returns(callInfo => $"{callInfo.ArgAt<string>(0)}\\{callInfo.ArgAt<string>(1)}");
@@ -350,6 +351,7 @@ public class GetDirectoryTreeQueryHandlerTests
];
_mockDriveService.GetDrives().Returns(ErrorOrFactory.From(drives));
_mockPathService.Exists(Arg.Any<string>(), Arg.Any<bool>()).Returns(true);
_mockPathService.ParsePath(query.Path).Returns(ErrorOrFactory.From((IEnumerable<PathSegment>)pathSegments));
_mockPathService.CombinePath(Arg.Any<string>(), Arg.Any<string>())
.Returns(callInfo => $"{callInfo.ArgAt<string>(0)}\\{callInfo.ArgAt<string>(1)}");
@@ -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<string>()).Returns(false);
CheckPathExistsQuery query = new(null!, false);
_mockPathService.Exists(Arg.Any<string>(), Arg.Any<bool>()).Returns(false);
// Act
PathExistsResponse result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.Exists.Should().BeFalse();
_mockPathService.Received(1).Exists(Arg.Any<string>());
_mockPathService.Received(1).Exists(Arg.Any<string>(), Arg.Any<bool>());
}
[Fact]
@@ -16,14 +16,17 @@ public class CheckPathExistsQueryFixture
/// <summary>
/// Creates a random valid query to check if paths exist.
/// </summary>
/// <param name="includeHiddenElements">Whether to include hidden file system elements or not.</param>
/// <returns>The created query.</returns>
public static CheckPathExistsQuery CreateCheckPathExistsQuery()
public static CheckPathExistsQuery CreateCheckPathExistsQuery(bool includeHiddenElements = true)
{
return new Faker<CheckPathExistsQuery>()
.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
}
@@ -124,6 +124,38 @@ public class PathServiceTests
_mockPathStrategy.Received(1).Exists(Arg.Is<FileSystemPathId>(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<FileSystemPathId>(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<FileSystemPathId>(id => id.Path == existingPath), false);
}
[Fact]
public void Exists_WithValidNonExistingPath_ShouldReturnFalse()
{
@@ -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<IFileInfo>();
mockFileInfo.Attributes.Returns(System.IO.FileAttributes.Hidden);
_mockFileSystem.FileInfo.New(Arg.Any<string>()).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();
@@ -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<IFileInfo>();
mockFileInfo.Attributes.Returns(System.IO.FileAttributes.Hidden);
_mockFileSystem.FileInfo.New(Arg.Any<string>()).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();
@@ -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<OkObjectResult>().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<CheckPathExistsQuery>(q => q.Path == path), Arg.Is(cancellationToken));
@@ -674,7 +674,7 @@ public class PathControllerTests
// Act & Assert
cts.CancelAfter(50);
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => _sut.CheckPathExists(path, cts.Token));
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => _sut.CheckPathExists(path, true, cts.Token));
}
#endregion
}